4.5 Assignment operations
Assignments of the kind described above also work with fixed sized arrays and matrices but, of course, then the sizes of the quantities on the left and the right side must fit exactly or an error will be flagged.
But fsc2
tries to be even more clever: when the dimensions of a
quantity the left side of an assignment is larger than the dimension of
the quantity on the right hand side it tries to assign the quantity on
the right hand side to as many elements or sub-arrays on the left hand
side as possible. You already have seen this in the discussion of the
initialization of (fixed size) arrays and matrices: when a number is
assigned to an array or matrix all elements of the array or matrix are
set to this number. To repeat:
B[ 3 ] = 9; b[ 2, 9 ] = sin( atan( 1.0 ) ); |
sets all elements of the array B
to 9 and all 18 elements of the
2-dimensional matrix b
to the sine of 45 degrees (atan(1)
evaluates to a quarter of pi).
The same assignment can also be done for variable sized arrays and matrixes, but only for the already existing elements can be set (if i.e. only a single sub-array of a 2-dimensional "matrix" has been created yet only the elements of this sub-array can be set this way).
But this does not only work for numbers on the right hand side but also
for arrays and even matrices. When you have for example a 2-dimensional
matrix on the left hand side and a one-dimensional array on the right
the array is automatically assigned to all of the sub-arrays of the
matrix. If you have in the VARIABLES
section:
z1[ 2 ] = { 3, 5 }; z2[ 2, * ] = z1; |
this will set up both the sub-arrays of z2
to have 2 elements each
and set both sub-arrays to the same values as the ones of z1
.
Within the other sections you have to write this a bit differently (but
even simpler) to indicate that you want to set all sub-arrays of z2
at once by just writing
z2 = z1; |
It's basically just an abbreviation for the longer form
z2[ 1 ] = z1; z2[ 2 ] = z1; |
or the even longer form of
z2[ 1, 1 ] = z1[ 1 ]; z2[ 1, 2 ] = z1[ 2 ]; z2[ 2, 1 ] = z1[ 1 ]; z2[ 2, 2 ] = z1[ 2 ]; |
Of course, the same also works with arrays of higher dimensions. If you have e.g. a 3-dimensional matrix, assigning it a number will set all of its elements (i.e. the elements of the sub-sub-arrays it's made up from), assigning it an 1-dimensional arrays will set all sub-sub-arrays to be identical to the 1-dimensional array on the right hand side, assigning it a 2-dimensional matrix will set all its sub-matrices and assigning it another 3-dimensional matrix will make the matrix on the left hand side identical to the one on the right hand side. Of course, if the matrix on the left hand side isn't variable sized, its sizes must fit the sizes of the quantity on the right hand side.
With dynamically sized arrays some care has to be taken when doing assignments. That's because the size of the array on the left hand side automatically gets adjusted to the one on the right hand side. This includes even cases where the size of the array on the right hand side is still unknown. In this case all information about the size of the left hand side array and its elements are discarded and it will now be treated as if its size never had been set.
This document was generated by Jens Thoms Toerring on September 6, 2017 using texi2html 1.82.