Math.Add¶
Name | - | Description | Default | Type |
---|---|---|---|---|
<input> |
Any valid integer(s), floating point number(s), or a sequence of such entities supported by this operation. | Int Int2 Int3 Int4 Int8 Int16 Float Float2 Float3 Float4 Color [Any] |
||
<output> |
The result of the operation, usually in the same type as the input value. | Int Int2 Int3 Int4 Int8 Int16 Float Float2 Float3 Float4 Color [Any] |
||
Operand |
The operand for this operation. | 0 |
Int &Int Int2 &Int2 Int3 &Int3 Int4 &Int4 Int8 &Int8 Int16 &Int16 Float &Float Float2 &Float2 Float3 &Float3 Float4 &Float4 Color &Color [Any] &[Any] |
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).
Details¶
Addition is a binary operation, i.e. it needs two arguments to give a result.
The 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¶
Taking 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.
-
Input and
:Operand
sequence sizes are equalSince each element in Input sequence has a corresponding element in
:Operand
sequence, 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 :Operand
[1 2 3] 3 Output [(a+1) (b+2) (c+3)] 3 -
Input sequence size <
:Operand
sequence sizeHere a few
:Operand
elements (:Operand
sequence size - Input sequence size) will have no Input elements to pair off with. These:Operand
elements 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 :Operand
[1 2 3] 3 Output [(a+1) (b+2) (_+3)] => [(a+1) (b+2)] 2 -
Input sequence size >
:Operand
sequence sizeOnce all the
:Operand
elements have been paired off and computed with the corresponding Input elements, the remaining Input elements (Input sequence size -:Operand
sequence size) will continue looping over the:Operand
sequence 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 :Operand
[1 2] 2 Output [(a+1) (b+2) (c+1) (d+2) (e+1)] 5
Note
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.
Examples¶
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] [4]
[info] [shards/Math/Add/4.edn] [7]
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]