Operator overloading
ILArray<> objects profit from overloaded operators. This enables one to formulate algorithms much more intuitive in the following way:
// construct 4-dim. array: random numbers betw. 1..10
ILArray<double> A = ILMath.rand(3,2,5,4) * 9.0 + 1.0;
// subtract exp(A) from A, divide by 2:
A = (A - ILMath.exp(A)) / 2;
The principle gets clear. The size of all parameters in an operation must match. This means in an operation like: B + C, B and C must be of the same size.
Corresponding elements on B and C will get added and the result is stored into the corresponding element in A.
Alternatively either one of B or C may be scalar! The (same) scalar value will get added to all elements of the non-scalar array and the result is returned then.
The following operators are overloaded (if available in your language). Static functions are available, if you are using languages not supporting operator overloading.
| Operation | Operator overload | Static function | return type | T/A/B types |
|---|---|---|---|---|
| Addition | plus: '+' | ILMath.add(A,B) | ILArray<T> | all numeric |
| Subtraction | minus: '-' | ILMath.subtract(A,B) | ILArray<T> | all numeric |
| Negation | unary minus: '-' | ILMath.invert(A,RetVal) | N/A | all signed numeric |
| Multiplication | mult: '*' | ILMath.multiplyElem(A,B) | ILArray<T> | all numeric |
| Division | divide: '/' | ILMath.divide(A,B) | ILArray<T> | all numeric |
| Lower than | < | ILMath.lt(A,B) | ILLogicalArray | all numeric |
| Lower equal | <= | ILMath.le(A,B) | ILLogicalArray | all numeric |
| Greater than | > | ILMath.gt(A,B) | ILLogicalArray | all numeric |
| Greater equal | >= | ILMath.ge(A,B) | ILLogicalArray | all numeric |
| Equals | == | ILMath.eq(A,B) | ILLogicalArray | all numeric |
| Not equal to | != | ILMath.neq(A,B) | ILLogicalArray | all numeric |
Currently the generic type for both operands (for binary operators) must be the same. If you are using different types, one of the conversion functions (ILMath.convert, ILMath.to?????? ...) may be of interest for you. Those functions can be used to convert between types.
The generic type of the return values will always be the same as the type of the operands.
For all comparison operations the return value will be a ILLogicalArray - a special array which derives from ILArray<byte>, having 1 on corresponding elements where the comparison resulted to true, 0 else.