||Any valid integer(s), floating point number(s), or a sequence of such entities supported by this operation.||
||The result of the operation, usually in the same type as the input value.||
||The operand for this operation.||
Applies the binary operation on the input value and the operand and returns the result (or a sequence of results if the input and the operand are sequences).
Addition is a binary operation, i.e. it needs two arguments to give a result.
Math.Add shard takes in the Input and the parameter
:Operand to produce the Output.
Both Input and
:Operand can be an integer, a float, or a sequence of such entities (but both value types should match for a given operation). The Output is generally of the same type as the Input provided to the shard.
Binary operations on sequences¶
Math.Add operator as an example.
If sequences are passed as arguments, the operator takes pairs of correspondingly placed elements from these sequences and computes the result for each pair. This gives rise to different scenarios.
:Operandsequence sizes are equal
Since each element in Input sequence has a corresponding element in
:Operandsequence, the Output sequence also has the same number of resultant elements and hence the same size as the argument sequences.
Entity Sequence Seq. Size Input [a b c] 3
[1 2 3] 3 Output [(a+1) (b+2) (c+3)] 3
Input sequence size <
Here a few
:Operandsequence size - Input sequence size) will have no Input elements to pair off with. These
:Operandelements are ignored in the final Output. Hence the Output sequence size here will the same as the Input sequence size.
Entity Sequence Seq. Size Input [a b] 2
[1 2 3] 3 Output [(a+1) (b+2) (_+3)] => [(a+1) (b+2)] 2
Input sequence size >
Once all the
:Operandelements have been paired off and computed with the corresponding Input elements, the remaining Input elements (Input sequence size -
:Operandsequence size) will continue looping over the
:Operandsequence till all of the Input sequence elements have been used. As a result the Output sequence will again be the same size as the Input sequence.
Entity Sequence Seq. Size Input [a b c d e] 5
[1 2] 2 Output [(a+1) (b+2) (c+1) (d+2) (e+1)] 5
Such sequence operations are useful in transforming and translating 2D/3D grid values (a frequent requirement in graphics rendering). This is done by passing the transform inputs as an Input sequence (to be applied to every row/line for a 2D grid or to every 2D-matrix/plane for a 3D grid) of the 2D matrix and the
:Operand sequence as the set of 2D/3D coordinates (represented linearly) that is to be transformed.
1 2 3 4 5 6 7
[info] [shards/Math/Add/1.edn] 5 [info] [shards/Math/Add/1.edn] 7
1 2 3 4 5 6 7
[info] [shards/Math/Add/2.edn] 5.3 [info] [shards/Math/Add/2.edn] 7.4
1 2 3 4 5 6 7 8
[info] [shards/Math/Add/3.edn] [4 5.1 6.4] [info] [shards/Math/Add/3.edn] [7 9.1 8.6]
1 2 3 4 5 6 7 8 9
[info] [shards/Math/Add/4.edn]  [info] [shards/Math/Add/4.edn] 
1 2 3 4 5 6 7 8 9
[info] [shards/Math/Add/5.edn] [4 2 1 5 8] [info] [shards/Math/Add/5.edn] [10 6 7 9 14]