Impressum/Kontaktparent nodes: implemented functions

N-wise reduction

testNwiseReduction
| data result nums |
"http://www.microapl.co.uk/apl_help/ch_020_020_800.htm"

"N-Wise Reduction"
"data aplReduce:operation subsetsOf:subsetN"
"data aplReduce:operation subsetsOf:subsetN onAxis:axis"

"The definition of N-wise Reduction (aplReduce:subsetsOf:) is very similar to the definition of Reduction. The subsetN argument, an integer scalar or length one vector, is used to specify the length of successive subsets of the data argument on which the Reduction operation is performed. If the subsetN argument is negative, each subset is reversed before the reduction operation is carried out."

"For a subsetN argument of absolute value n and the selected axis of the data argument of length m, the number of subsets to which the reduction can be applied are: and thus the limiting case is where the sample size is 1 greater than the length of the selected axis, giving a empty result."

"Add up the numbers 2 at a time, starting at the beginning of the vector"

self assert: #(3 5 7 9 11 13 15 17 19) asApl = (10 aplInterval aplReduce:#+ subsetsOf:2).

"5 at a time"
self assert: #(15 20 25 30 35 40) asApl = (10 aplInterval aplReduce:#+ subsetsOf:5).

"10 at a time - nearly the same as ordinary Reduction"
self assert: #(55) asApl = (10 aplInterval aplReduce:#+ subsetsOf:10).
self assert: 55 asApl = (10 aplInterval aplReduce:#+).


"Sample size 1 greater than right argument empty result"
self assert: #() asApl = (10 aplInterval aplReduce:#+ subsetsOf:11).

data:= 12 aplInterval aplReshape:#(3 4).
result := #(3 5 7 11 13 15 19 21 23) aplReshape:#(3 3).

"Add up 2 at a time across the columns the second dimension"
self assert: result = ( data aplReduce:#+ subsetsOf:2 onAxis:2 ).

"Add up 2 at a time across the rows, the fist dimension"
result := #(6 8 10 12 14 16 18 20) aplReshape:#(2 4).
self assert: result = ( data aplReduce:#+ subsetsOf:2 onAxis:1 ).

"some 'random' data"
nums := #(2 8 5 6 3 1 7 10 4 9) asApl.
result := #(-6 3 -1 3 2 -6 -3 6 -5) asApl.

"Subtract sucessive pairs of elements"
self assert: result = (nums aplReduce:#- subsetsOf:2).

"Reverse the elements before subtracting"
result := #(6 -3 1 -3 -2 6 3 -6 5) asApl.
self assert: result = (nums aplReduce:#- subsetsOf: -2).

"Join elements, 2 at a time"
result := #('ABCD' 'CDEF' 'EFHI') asApl.
data := #('AB' 'CD' 'EF' 'HI') asApl.
self assert: result = (data aplReduce:#, subsetsOf:2).

result := #('ABCDEF' 'CDEFHI') asApl.
self assert: result = (data aplReduce:#, subsetsOf:3).