Subject: re: collecting questions about J
From: richard.levine@canrem.com (Richard Levine)
Date: Fri, 26 May 95 03:18:00 -0500
Organization: CRS Online  (Toronto, Ontario)

Further to the discussion about functions applied to the rows and the
columns of a table, here is demonstration that the preferred way to
specify the application of a function TO EACH ROW OF A TABLE and the
application of the same function TO EACH COLUMN OF THE SAME TABLE is to
use the rank conjunction along with the transpose.  "Preferred way" in
this case is defined to mean "a general way", that is, "a way that
always gives a correct result, no matter what the function."

      transpose =. |:
      m =. i. 3 3
      m
0 1 2
3 4 5
6 7 8

   NB. f and g both sum the (non-zero) elements in y.
      f =. 3 : '+/ (-. 0=y.)#y.'
      g =. 3 : '+/y.'

   NB. apply f and g TO EACH ROW of table m
   NB. (that is, apply TO EACH RANK-1 CELL)
   NB. they are equivalent
      f"1 m
3 12 21
      g"1 m
3 12 21

   NB. apply f and g TO EACH COLUMN of table m
   NB. (that is, apply TO EACH RANK-1 CELL OF THE TRANSPOSE)
   NB. they are equivalent
      f"1 transpose m
9 12 15
      g"1 transpose m
9 12 15

   NB. ... BUT
   NB. f and g have different behaviours for RANK-2 cells
   NB. (f and g have different behaviours for a table)
      f m
 3  6  9
12 15 18
12 14 16
      g m
9 12 15

   NB. similarly for the "mean" function
      mean =. +/ % #

   NB. mean OF EACH ROW of table m
      mean"1 m
1 4 7

   NB. mean OF EACH COLUMN of table m
      mean"1 transpose m
3 4 5

This may of course be compared with the syntax in APL, where
the sum of each row is given by +/[2]m and the sum of each column is
given by +/[1]m, and there is no general expression to apply an
arbitrary function to each row (or each column) of a table.  (I believe
this last statement is correct.)  Also the previous expressions were
origin-dependent.  (No origin-dependence in J.)

--RL
