Existing APL systems effectively treat functions as scalars: A single function is applied to 1 or 2 array arguments. This represents a SIMD (Single- Instruction, Multiple-Data) [...] architecture. APL currently lacks facilities for explicitly specifying MIMD (Multiple-Instruction, Multiple-Data) processes; that is, concurrent application of a collection of functions to one or more arrays of data.
-- Robert Bernecky [1]
Distributed application occurs when item data are of different nature thus/or requires separate treatment. For example, aggregation summary of a mix of rate (average) and amount (total) type items.
The standard representation of function arrays in J is by means of gerund, items of which are applied to the arguments with Agenda @., Evoke `: conjunctions, etc. But it is not possible to apply items of gerund to items of the argument distributively, i.e.
U`V`W apply a,b,c <-> (U a),(V b),(W c)
Apply Foreign
The foreign verb Apply 128!:2 can be used to obtain distributed application with the following limitations:
- the function array is not in the standard gerund form
- it is impossible to provide the left argument, thus it is limited to monadic use only
(>'>:';'<:';'-') (128!:2"_1) 1 2 3 2 1 _3 (>'+/';'>./';'<./') (128!:2"_1) i.3 5 10 9 10 (>'+/';'|.';'|."1') <@(128!:2)"_1 i.3 5 +--+---------+--------------+ |10|9 8 7 6 5|14 13 12 11 10| +--+---------+--------------+
Definition
Distributed Apply that works with gerunds can be can be defined as follows
dapply=: 1 : 0
(i.#y) u@]@.["0 _1 y
:
r=. ,:x m@.0&{. y
for_i. i.&.<:#y do. r=. r,(i{x) m@.i i{y end.
)The monadic distributed apply uses distributed property of @.
Monadic Case
Note how compact and natural the expressions look now.
(<:`>:`-) dapply 1 2 3 0 3 _3 +/`(>./)`(<./) dapply i.3 5 10 9 10 <@(+/`|.`(|."1)) dapply i.3 5 NB. boxes gerund items with distributed @ +--+---------+--------------+ |10|9 8 7 6 5|14 13 12 11 10| +--+---------+--------------+
Dyadic Case
Examples
4 5 6 (<:`>:`-) dapply 1 2 3 0 1 3 4 5 6 (<:`>:`-) dapply i.3 5 0 0 0 0 1 1 0 0 0 0 _4 _5 _6 _7 _8 (i.3 2) <@(+/`(>./)`(<./)) dapply i.3 5 +---------+---------+---------+ |0 1 2 3 4|5 6 7 8 9|4 4 4 4 4| |1 2 3 4 5|5 6 7 8 9|5 5 5 5 5| +---------+---------+---------+
References
Robert Bernecky, Function arrays, Proceedings of the international conference on APL, p.53-56, June 11-15, 1984, Finland ACM
Robert Hodgkinson, Practical uses of operators in Sharp APL/HP, Proceedings of the international conference on APL, p.7-14, May 10 - 14, 1987, Dallas, Texas ACM
Comments
The phrase <@(+/`(>./)`(<./)) uses an undocumented construct. An alternative is:
gd=: ('@' <@; <)@,"0 (<'<') gd (+/`(>./)`(<./)) ┌───────────────┬────────────────┬────────────────┐ │┌─┬───────────┐│┌─┬────────────┐│┌─┬────────────┐│ ││@│┌─┬───────┐│││@│┌─┬────────┐│││@│┌─┬────────┐││ ││ ││<│┌─┬───┐││││ ││<│┌─┬────┐││││ ││<│┌─┬────┐│││ ││ ││ ││/│┌─┐│││││ ││ ││/│┌──┐│││││ ││ ││/│┌──┐││││ ││ ││ ││ ││+││││││ ││ ││ ││>.││││││ ││ ││ ││<.│││││ ││ ││ ││ │└─┘│││││ ││ ││ │└──┘│││││ ││ ││ │└──┘││││ ││ ││ │└─┴───┘││││ ││ │└─┴────┘││││ ││ │└─┴────┘│││ ││ │└─┴───────┘│││ │└─┴────────┘│││ │└─┴────────┘││ │└─┴───────────┘│└─┴────────────┘│└─┴────────────┘│ └───────────────┴────────────────┴────────────────┘ (i.3 2) ((<'<') gd (+/`(>./)`(<./))) dapply i.3 5 ┌─────────┬─────────┬─────────┐ │0 1 2 3 4│5 6 7 8 9│4 4 4 4 4│ │1 2 3 4 5│5 6 7 8 9│5 5 5 5 5│ └─────────┴─────────┴─────────┘This Essay is now out of date. The construct m@ and its relatives have been decomitted.
Roger's gd is a verb, and so cannot substitute for @ et. al., which are conjunctions. For a conjunction that emuates and extends the distributive power of pre-J6 @, see doog. That paper presents several utilities relevant to this essay, including several alternatives to dappl.
To bring this Essay up to date with J601c with the minimal changes, in the sentence <@(+/`|.`(|."1)) dapply i.3 5 the @ should be replaced with At from from the doog script.
However, if no one has any objections, I intend to rewrite this Essay (using doog). I note that there's no attribution for this Essay.
-- DanBron 2007-01-18 15:19:19
