/ — Reduce
Monadic operator. Hanzi: 总 (總)
f/Y inserts f between the items along the last axis, then evaluates that expression as usual, from the right. -/1 2 3 is 1-2-3, which is 2. An empty axis reduces to f’s identity.
+/1 2 3 ⍝ 6
-/1 2 3 ⍝ 2
+/⍬ ⍝ 0Reduction returns the accumulator. An unseeded one-item reduction returns that item, at the type that a longer reduction gives: +/,$t counts one item as +/$t $f counts two. Unreduced axes supply the result frame.
A dfn that reduces several lanes, such as each row of a matrix, runs each stage on every lane at once, in a batched call. The result is the same as reducing each lane in turn.
,/⊂2 3 ⍝ 2 3
,/,⊂2 3 ⍝ 2 3
+/⊂⊂3 ⍝ ⊂3
+/,$t ⍝ 1ₓS f/ Y starts from seed S on the right. With Y holding [a b c], it is a f b f c f S. Empty reductions return the seed. Each lane uses the whole seed. The seed can share a run with the reduction, as in 10-/1 2 3. With a space, 10 -/1 2 3 is the strand 10 2.
10-/1 2 3 ⍝ ¯8
10+/2 3⍴⍳6ₓ ⍝ [13 22]
⊂10 +/ 1 2 3 ⍝ ⊂16
10 20 +/⍤0 1 [2 3]⍴⍳6ₓ ⍝ [13 32]Fixed-width integer reductions keep the promoted fixed type. A wider seed selects a wider accumulator without widening the stored input array:
v←[200 100]ₓ₁
+/v ⍝ 44ₓ₁
+/vₓ ⍝ 300ₓ
0ₓ₈+/v ⍝ 300ₓ₈
(+/)ₓ₈ v ⍝ 44ₓ₈For an ordinary exact total, use +/vₓ. The fixed-i64 seed makes each addition i64. The seed participates in promotion and cannot narrow wider input. Write 0ₓ₈+/v or 0ₓ₈ +/ v, not 0ₓ₈ +/v. A result suffix converts the completed total and cannot recover wrapped values.
Nested paths use seeded Pick reduction.
tree←[[10 20] [30 [40 50]]]
tree⊃/⌽1 1 0 ⍝ 40Use full windows for moving reductions.
+/↕1 2 3 4 ⍝ 3 5 7
-/⌽↕1 2 3 ⍝ 1 1
+/0↕1 2 ⍝ 0 0 0f/⍠K selects the axis. ⌿ defaults to the first.
Several axes form one reduction cell. f/⍠A Y ravels each selected cell before reducing, equivalent to (f/,)¨⊂⍠A Y. This is an exception to the general rule in Axis. A single axis retains ordinary reduction behavior.
n←2 2 3⍴⍳6ₓ
+/₁ ₂n ⍝ [15 15]ₓ
+/₀ ₂n ⍝ [6 24]ₓAxis order determines the ravel order. Unselected axes retain their order and keys. Selecting every axis gives the reduced value itself, as a dfn along the same axes does.
-/₀ ₁ 2 2⍴⍳4ₓ ⍝ ¯2ₓ
-/₁ ₀ 2 2⍴⍳4ₓ ⍝ ¯4ₓAn empty axis vector makes singleton cells. A seed is passed whole to each cell.
+/⍠⍬ [1 2 3] ⍝ 1 2 3
10+/₁ ₂ 2 2 3⍴⍳6ₓ ⍝ [25 25]Algebraic reductions such as +/ can reassociate their operands, as Accuracy describes. Other reductions keep operand order.
Errors
RANK: higher-rank axis specificationDOMAIN: axis vector with repeated or invalid axes, or a subject operand. Replicate with#