rslt ← part (redn ##.pred) rarg ⍝ Partitioned reduction.
Suggested by Michael Baas, pred returns a partitioned reduction of its argument
array. [part] is a vector of partition sizes, such that (+/part)=¯1↑⍴rarg.
Ways to code this operator include:
pred←{⍺⍺/¨(∊⍺↑¨1)⊂⍵} ⍝ --3 SL
pred←{(⎕io ⎕ml)←1 3 ⋄ ⍺⍺/¨(⍺/⍳⍴⍺)⊂⍵} ⍝ 1-3 VMJ/MJ
pred←{(⎕io ⎕ml)←1 3 ⋄ ⍺⍺/⊃(⍺/⍳⍴⍺)⊂⍵} ⍝ 12- GQ
pred←{res←(⍴⍺)⍴⍺⍺/⍬ ⋄ res[⍺/⍳⊃⍴⍺]⍺⍺←⍵ ⋄ res} ⍝ -2- BT
pred←{⎕ml←3 ⋄ ⍺⍺ nz/⊃(⍺/⍳⍴⍺)⊂⍵} ⍝ 12- SM
pred←{⎕io←0 ⋄ (<⍀(+\⍺)∘.>⍳⍴⍵)⍺⍺ nz.×⍵} ⍝ -2- SM
pred←{⎕io←0 ⋄ ⍉(<⍀(+\⍺)∘.>⍳¯1↑⍴⍵)⍺⍺ nz.×⍉⍵} ⍝ 12- SM, where:
nz←{0≠⍺×⍵: ⍺ ⍺⍺ ⍵ ⋄ ⍺=0:⍵ ⋄ ⍺} ⍝ auxiliary op.
Note:
1. Works for matrix (and higher rank) rarg.
2. "Each-less" solution.
3. Unrestricted operand function (eg: ',').
Examples:
2 3 3 2 +pred ⍳10
3 12 21 19
1 2 1 ×pred 2 3 4⍴⍳24
1 6 4
5 42 8
9 110 12
13 210 16
17 342 20
21 506 24
1 2 1,pred 3 4⍴⍳12
┌─┬─────┬──┐
│1│2 3 │4 │
├─┼─────┼──┤
│5│6 7 │8 │
├─┼─────┼──┤
│9│10 11│12│
└─┴─────┴──┘
See also: acc foldl trav
Back to: contents
Back to: Workspaces