The Reduce Operator / and ⌿
Hybridity
/ and ⌿ are hybrids - they are both functions and operators. For use of / and ⌿ as functions please see the appropriate operator documentation.
When used as operators / and ⌿ apply functions across the arguments of a vector. If applied to a scalar they first convert it to a vector with a single element.
Monadic Use On Scalars
The ‘/’ operator applys a function to a scalar:
+/1
1
It returns a scalar.
⍴ +/1
Gives:
Monadic Use On Vectors
The ‘/’ operator successively applies a function to the elements of a vector:
+/ 1 2 3 4
10
and
×/1 2 3 4
giving:
24
’/’ always reduces the rank of the operand to the right (hence the name reduce):
A ← 2 2 ⍴ 1 22 333
+/ A
gives:
23 334
A of course was a 2 by 2 matrix:
  1 22
333  1
Each dimensions was reduced seperately.
Gotcha’s
The / operator reduces the rank and it will mondically reduce a vector of length 1 to a scalar:
A ← 1 ⍴ 4
⍴ +/ A
Gives a null result - its a scalar:
Dyadic Use On Scalars Are An Error
Attempting to use ‘/’ dyadically on a scalar produces an error:
1 +/ 1
Resulting in:
Error
1 +/ 1
-----^
RANK ERROR [RHS must be an array: It is a scalar ] on line 1 at character 6
It works on a vector of length 1, of course:
A ← 1 ⍴ 4
1 +/ A
Resulting in:
4
Moar Gotcha’s
See the points for improvement page for outstanding issues.
Dyadic Use On RHS Vectors With An LHS Scalar
The ‘/’ vector when used dyadically applies a sliding window:
2 +/ 1 2 3 4
Giving:
3 5 7
The LHS scalar can be replaced with a vector of length 1 with the same results:
A ← 1 ⍴ 2
A +/ 1 2 3 4
and as expected:
3 5 7
This is 1 + 2 then 2 + 3 then 3 + 4
This scales up to objects of higher shape
A ← 3 3 ⍴ 1 2 3 4
2 +/ A
Which returns:
3 5
5 3
7 5
We can derive that by looking at the intermediate shape:
3 3 ⍴ 1 2 3 4
which is of course:
1 2 3
4 1 2
3 4 1
The shape has been reduced from 3 3 to 3 2
Dyadic Use On RHS Vectors With An LHS Vector
This throws a LENGTH ERROR:
2 2 +/ 1 2 3 4
Giving
Error
2 2 +/ 1 2 3 4
^
LENGTH ERROR [The operator \"/\" can only take a scalar on the LHS: The shape of the LHS is [2] ] on line 1 at character 1
Using a lazy vector gives a different error - it is thrown at runtime during evaluation and not before it:
Error
2 2 +/ 1 2 3 4
^
LENGTH ERROR [The operator \"/\" can only take a scalar on the LHS: The shape of the LHS is unsized_vector ] on line 1 at character 1
Reducing With Ranks
By default / operates on the first (ie right hand axis) but you can specify the rank you wish to reduce on:
A ← ⍳ 24
B ← 2 3 4 ⍴ A
2 +/[2] B
Gives the sum over the second rank:
 6  8 10 12
14 16 18 20
30 32 34 36
38 40 42 44
The value of A is:
1  2  3  4
5  6  7  8
9 10 11 12
and a shape of 3 4. After reduction with rank its shape is 2 4
The Barred Reduce Operator ⌿
Like the function ⌿ the barred reduce operator operates on the last axis:
Whereas / without a rank:
A ← 2 2 ⍴ 1 22 333 444
+/ A
gives:
23 777
With ⌿:
A ← 2 2 ⍴ 1 22 333 444
+⌿ A
you get:
334 466
As in the function case if you explicity specify a rank then / and ⌿ behave identically.
