[net.lang.apl] mixed rank scalar functions

ark@rabbit.UUCP (Andrew Koenig) (05/01/84)

I once thought through the problem of extending scalar functions to act
on arrays of different rank.  The basic problem is this: if you are
adding a matrix and a vector, should the vector be added to each row
of the matrix or each column?  I concluded that it should be possible
to do either, with the following syntax obviously suggesting itself:

	A+[1]B     or A+[2]B

Now, which should be which?  Well, suppose we consider higher ranks.
Then, in the cimplest non-trivial case, A and B will differ by one in
rank, and, if you like, the effect will be as if we had inserted a
1 in the appropriate place in the dimension vector of the lower-ranked
array, and then replicated it along that coordinate as appropriate.
Thus, if we are adding a 3 by 7 by 2 array to a 3 by 7 by 9 by 2 array,
we are inserting a new third element into the dimension vector of the
3 by 7 by 2 array, so this should be written as A+[3]B.

Back to the matrix+vector (or vector+matrix) case.  If we apply the
rule given above, we find that A+[1]B adds A to each row of B (or
B to each row of A), and A+[2]B adds A to each column of B.  Consistency
with other uses of [] requires that the highest possible subscript value
should be the default, so just adding a vector and a matrix should add
the vector to the columns of the matrix.