array operators
{}-operator (array construction)
constructs an array from elements:
da = { 2., 3., 5. } ! double[3] array
ia = { i1 : i2 } ! int array with all numbers between i1 and i2 (included!)
! if i1 > i2 => descending values: i1, i1-1 ... i2 (included)
oa = { t, j, 3., ia } ! Object[4] array
iaa = { { 11, 12, 13 }, { 21, 22, 23 } } ! int[2][] array
[]-operator (see JxnPortable/docs/)
- arr[i] array access
arr[ia] -> { arr[ ia[0] ], arr[ ia[1] ] ... arr[ ia[ length(ia) - 1 ] ] }
arr[{ i1, i2, i3 }] -> { arr[i1], arr[i2], arr[i3] }
arr[ i1 : i2 ] ! ~shortcut of arr[{ i1 : i2 }], reverse order if i1 > i2
arr[ i1 : ] -> { arr[i1], arr[i1+1] ... arr[ length(arr) - 1 ] }
negative array index i < 0: arr[i] -> arr[ length(arr) - (-i) ] e.g. { 2, 3, 5, 7 }[-2] returns 5
- obj[x] access
obj[x] maps to obj.get(x)
obj[arr]
-> obj.get( arrType[] ) if defined
-> else auto array: { obj.get( arr[0] ), obj.get( arr[1] ) ... obj.get( arr[ length(arr) - 1 ] ) }
obj[{ i1, i2, i3 }] -> { obj.get( i1 ), obj.get( i2 ), obj.get( obj.get( i3 ) ) }
obj[ i1 : i2 ] ! ~shortcut of obj[{ i1 : i2 }], reverse order if i1 > i2
if obj.get(int) and obj.size() defined:
obj[ i1 : ] -> { obj.get( i1 ), obj.get( i1 + 1 ) ... obj.get( obj.size() - 1 ) }
negative index i < 0: obj[i] -> obj( obj.size() - (-i) )
automatic array processing (auto array)
- Equal length arrays or an array and a single value can be operands of arithmetic operations ( + − * / % ^ ) resulting in automatic element-wise array arithmetic.
Example:
{ 2, 3, 5 } / { 4, 2, 1 }
= { 0.5, 1.5, 5.0 } (double[3])
- If a method with scalar parameter is called with an array argument jxn automatically unrolls the array.
Example:
sqrt( { 2., 3., 4. } ) ! or abreviation sqrt { 2., 3., 4. }
returns an array:
{ 1.4142135623730951, 1.7320508075688772, 2.0 } (double[3])
- In case of more than one parameter jxn tries to find a matching signature:
If unroll is necessary for more than one parameter combinations of equal length arrays are unrolled in parallel.
Auto array starts with the rightmost array argument and sucessively tries to unroll any combinations of
equal length arrays until a matching signature is found (first fit).
@JxnUnroll(ao)
forces unroll of ao:
- if a method signature with one or more java.lang.Object
parameter(s) exists, all objects including array objects match
as argument(s) for that parameter(s) and thus automatic unroll becomes disabled for an array argument on the object parameter
- using @JxnUnroll(ao)
wraps the array object to indicate that the argument must be unrolled
- the first argument using JxnUnroll
determines the length of all unrolled arguments
=> all array arguments using JxnUnroll
must be of identical length, auto array may unroll further array arguments of the same length
- If a method is called on an array of objects, the method is called successively on all array element objects:
- the result is returned in a new (single dimensional) array of the returnType of the method.
- the length of the array of objects determines the length of possible argument arrays to be unrolled in parallel as needed
(only argument arrays of the same length may be unrolled in parallel)
oa.fkt(arr) -> { oa[0].fkt(arr[0]), oa[1].fkt(arr[1]) ... }
- auto array implements one single loop (with one single control variable).
Use class JxnLoop
, if nested loops are required
- Special case: methods of java.lang.Object
e.g. getClass(), hashCode(), toString()
are directly called on the array object.
Use methods in class JxnObject
to call the methods on the array elements objects.
- In case of multidimensional arrays only one component can be unrolled automatically (one single loop)
- a[i][ja]
works as expected: gives array of the array elements of row i
with column indices given by array ja
- a[ia][j]
- does not work with auto array in 1st dimension and j
in 2nd dimension
- is interpreted as a[ia][i]
( i
th row of the sub array containing the rows defined by ia
)
- if required use class JxnLoop
or implement the nested loops in a user defined java class (source)
- example: JxnPortable/docs/
- See Also: section array operations in class
JxnUtilities