Essays/Distributed Apply

From J Wiki
Jump to: navigation, search

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

1. Robert Bernecky, Function arrays, Proceedings of the international conference on APL, p.53-56, June 11-15, 1984, Finland ACM 2. 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