Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!utcsri!rpi!uwm.edu!src.honeywell.com!The-Star.honeywell.com!umn.edu!csus.edu!sfsuvax1.sfsu.edu!emclean
From: emclean@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: Using expand and filling rows and columns with sums
Message-ID: <1993Feb18.190035.18198@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
Distribution: W
Date: Thu, 18 Feb 1993 19:00:35 GMT
Lines: 55

   NB.   Here is something new J'ers might find interesting
   NB.   Requires v6.1 or v6.2

   NB.   Here is a sample go. To run this program  port J to 
   NB.   your account and type "0!:2 <'thismessagename'" .
   
   NB. provide english names to some J functions (a.k.a. J verbs)
   roll =. ?
   shape =. $
   link =. ;
   transpose =. |:
   right =. ]  NB. means right argument
   sumscan =. +/
   from =. {
   appendColSumsToNewRow =. ,sumscan
   appendRowSumsToNewCol =. ,. sumscan"1
   expand =. (* +/\)@[ { 0&,@]
   assignCol =. >@{:@]`($@[ #. (i.@$@>@{: ,. >@{.)@])`[}
   assignRow =. (assignCol right)&.transpose

   NB. define a table with 4 rows and 5 columns between 0 and 19
   a =. roll 4 5 shape 20 

   appendColSumsToNewRow a
   appendRowSumsToNewCol a

   NB. box a and the operations above
   (right link (appendColSumsToNewRow~ link appendRowSumsToNewCol~ )) a

   NB. insert 0 rows into b as specified in 1 1 0 1 1 0
   right b =. 1 1 0 1 1 0 expand roll 4 5 shape 20

   NB. put sum of rows 0 and 1 into row 2
   right b =. b assignRow (2 link sumscan 0 1 from b)
 
   NB. put sum of rows 3 and 4 into row 5
   right b =. b assignRow (5 link sumscan 3 4 from b)

   NB. put some 0 cols in b
   right b =. 1 1 0 1 1 1 0 expand"1 b

   NB. put sum of columns 0 and 1 into row 2
   right b =. b assignCol (2 link sumscan 0 1 from transpose b)

   NB. put sum of columns 0 and 1 into row 2
   right b =. b assignCol (6 link sumscan 3 4 5 from transpose b)

   NB. multiply the rows of b by 10 20 30 40 50 and 60
   b * 10 20 30 40 50 60

   NB. multiply the colums of b by 10 20 30 40 50 60 and 70 )
   b *"1 ( 10 20 30 40 50 60 70)
   
   <'Emmett'

