Skip to content

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. IntInt2Int3Int4Int8Int16FloatFloat2Float3Float4Color[Any]
<output> The result of the operation, usually in the same type as the input value. IntInt2Int3Int4Int8Int16FloatFloat2Float3Float4Color[Any]
Operand The operand for this operation. 0 Int&IntInt2&Int2Int3&Int3Int4&Int4Int8&Int8Int16&Int16Float&FloatFloat2&Float2Float3&Float3Float4&Float4Color&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.

  1. Input and :Operand sequence sizes are equal

    Since 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
  2. Input sequence size < :Operand sequence size

    Here 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
  3. Input sequence size > :Operand sequence size

    Once 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
;; Add integers
(int 5) ;; :Input
(Log) ;; prints input => 5
(Math.Add
 2) ;; :Operand
(Log) ;; prints input + operand => 7
(Assert.Is 7 true) ;; expect: (5 + 2) => 7
[info] [shards/Math/Add/1.edn] 5
[info] [shards/Math/Add/1.edn] 7

 

1
2
3
4
5
6
7
;; Add floats
(float 5.3) ;; :Input
(Log) ;; prints input => 5.3
(Math.Add
 2.1) ;; :Operand
 (Log) ;; prints input + operand => 7.4
(Assert.Is 7.4 true) ;; expect: (5.3 + 2.1) => 7.4
[info] [shards/Math/Add/2.edn] 5.3
[info] [shards/Math/Add/2.edn] 7.4

 

1
2
3
4
5
6
7
8
;; Add equal-sized sequences
[4 5.1 6.4] ;; :Input
(Log) ;; prints input => [4 5.1 6.4]
(Math.Add
 [3 4.0 2.2]) ;; :Operand
;; input seq elements compute with corresponding operand seq elements
(Log) ;; prints input + operand => [7 9.1 8.6]
(Assert.IsAlmost [7 9.1 8.6] true) ;; expect: [(4 + 3) (5.1 + 4.0) (6.4 + 2.2)] => [7 9.1 8.6]
[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
;; Add unequal-sized sequences (input size < operand size)
[4.0] ;; :Input
(Log) ;; prints input => [4.0]
(Math.Add
 [3.0 4.0 2.2]) ;; :Operand
;; input seq elements compute with corresponding operand seq elements
;; since input size < operand size, remaining operand seq elements ignored
(Log) ;; prints input + operand => [7.0]
(Assert.Is [7.0] true) ;; expect: [(4.0 + 3.0) ... ... ] => [7.0]
[info] [shards/Math/Add/4.edn] [4]
[info] [shards/Math/Add/4.edn] [7]

 

1
2
3
4
5
6
7
8
9
;; Add unequal-sized sequences (input size > operand size)
[4 2 1 5 8] ;; :Input
(Log) ;; prints input => [4 2 1 5 8]
(Math.Add
 [6 4]) ;; :Operand
;; input seq elements compute with corresponding operand seq elements
;; for (input size > operand size): remaining input seq elements continually loop over operand seq elements
(Log) ;; prints input + operand => [10 6 7 9 14]
(Assert.Is [10 6 7 9 14] true) ;; expect: [(4 + 6) (2 + 4) (1 + 6) (5 + 4) (8 + 6)] => [10 6 7 9 14]
[info] [shards/Math/Add/5.edn] [4 2 1 5 8]
[info] [shards/Math/Add/5.edn] [10 6 7 9 14]