Exceptions differentiated by the length of an array

Hello everyone, I wanted to ask a question.
I am currently getting an array of doubles containing matrix = [magnitudes ; angles] where
I have different cases (separated by spaces and “;”):

case 1: [X1 X2; angle_x1 angle_x2] … [2x2]
case 2: [X1 Y1 X2 Y2; angle_x1 angle_y1 angle_x2 angle_y2] … [2x4]
case 3: [X1 Y1 Z1 X2 Y2 Z2; angle_x1 angle_y1 angle_z1 angle_x2 angle_y2 angle_z2] … [2x6]

I would like to be able to make exceptions according to the size of my array (or according to its number of columns) to retrieve information, for example if I have case 1 where the size of the matrix is M1, I only retrieve X1, if case 2 where my matrix is of size M2 get X1 and Y2, if case 3 where the size of the matrix is M3 get x1 y1 z1.

thx to all.

just use if-else, naively Matrix all have the same type information (unless you do some hackery to propagate this information but it’s probably not worth it)

Hello! Can you give me an example?

function magnitudes(matrix)
    size(matrix, 2) == 2 && return matrix[1, 1]
    size(matrix, 2) == 4 && return matrix[1, [1, 4]]
    size(matrix, 2) == 6 && return matrix[1, 1:3]
    error("Expected matrix of width 2, 4 or 6, got $(size(matrix, 2))")
1 Like

You might want to have these cases as different types, and then dispatch on the appropriate type.
For example in this case, explicit types for 1D 2D or 3D, or a single type parametrized by D, as in Point{D}.
This could pay for itself in future with efficiency when this D is known at compile type.