Impressum/Kontaktparent nodes: implemented functions

Replicate


testReplicate
| table result vec |
"http://www.microapl.co.uk/apl_help/ch_020_020_840.htm"

"data aplCompress:compress"

"This is used to generate multiple copies of elements of the data. In addition Replicate can be used either to replace a specified element with one or more instances of that element's prototype or to insert one or more instances of that dimension's prototype. Positive integers in the compress argument specify how many copies of each corresponding element in the right argument are wanted."

"Negative integers in the compress argument are used to insert or substitute prototypes. The two alternative mechanisms for this case are:"

"(a) Length of compress argument the same as the length of the selected dimension of the data argument. In this case, negative elements in the compress argument specify that the corresponding element in the right argument should be replaced by the appropriate quantity of its prototype."

"(b) If the number of non-negative elements in the compress argument is the same as the length of the selected dimension of the data argument, then negative elements in the compress argument indicate the position and quantity of prototype elements to insert the prototype being used being that of the first element of the axis."

"As usual, a scalar compress argument is extended to match the selected axis. If a replication is carried out along an axis of length 1, that axis will be extended."

"Replace second column of TABLE by 2 columns of 0s - the prototype"
table := 6 aplInterval aplReshape:#(2 3).
result := #(1 1 0 0 3 3 4 4 0 0 6 6) aplReshape:#(2 6).
self assert: result = (table aplCompress:#(2 -2 2) ).

"Insert two sets of two columns of 0s"
result := #(1 1 0 0 2 2 0 0 3 3 4 4 0 0 5 5 0 0 6 6) aplReshape:#(2 10).
self assert: result = (table aplCompress:#(2 -2 2 -2 2) ).

"Insert two copies of the prototype of the third element of VEC"
vec := (Array with:1 with:2 with:(4 aplInterval aplReshape:#(2 2)) with:3 with:4) asApl.
result := (Array with:1 with:2 with:(0 aplReshape:#(2 2)) with:(0 aplReshape:#(2 2)) with:3 with:4 ) asApl.
self assert: result = (vec aplCompress:#(1 1 -2 1 1)).

"Insert two copies of the prototype of VEC"
result := {1. 2. 0. 0.(4 aplInterval aplReshape:#(2 2)). 3. 4.} asApl.
self assert: result = (vec aplCompress:#(1 1 -2 1 1 1)).

self assert: 'AABBBCC' asApl = ('ABC' asApl aplCompress: #(2 3 2 )).

"With a scalar left argument, the 2 is is extended to each element on the right"
self assert: 'DDEEFF' asApl = ('DEF' asApl aplCompress: 2).

self assert: #(1 1 1 1 1 3 3 3 3 3) asApl = (#(1 2 3) asApl aplCompress:#(5 0 5)).

"TABLE as above. Replicate on last dimension"
result :=#(1 1 2 2 3 3 4 4 5 5 6 6) aplReshape:#(2 6).
self assert: result = (table aplCompress:2).

"Replicate on first dimension."
result :=#(1 2 3 1 2 3 4 5 6 4 5 6) aplReshape:#(4 3).
self assert: result = (table aplCompress:2 onAxis:1).

"Last axis, the columns, is extended to length 5 to satisfy compress argument"
result :='AAAAABBBBBCCCCC' aplReshape: #(3 5).
self assert: result = (('ABC' aplReshape:#(3 1)) aplCompress:#(2 3)).

"Last axis extended and blank column inserted"
result := 'AA AABB BBCC CC' aplReshape:#(3 5).
self assert: result = (('ABC' aplReshape:#(3 1)) aplCompress:#(2 -1 2) onAxis:2).