[comp.lang.apl] J and nested vectors

krb@uflorida.cis.ufl.EDU (Ken Block) (03/21/91)

This message is empty.

cs450a03@uc780.umd.edu (03/22/91)

Since no responses have reached my site yet, I guess I'll field this one... 

Ken Block writes:
>I am under the impression that J does not have nested vectors.
>Is this true?

Not really

>How could I do something like the following apl: 
>x function foreach ((1 2 3) (5 6 7) (1 2 5) (2 3 4))

The function part might be written as   f &. >   if you wanted to
deal with it as a nested array.  Note that you could also convert that
thing to a matrix, and use   f " 1   with no loss in information or
functionality.

&.   by the way means "use the function on the right as a
pre-processor (and it's inverse as a post-processor) for the function
on the left."  The granularity is determined by the granularity of the
"function on the right".  So (f) would get evaluated once for the
array 1 2 3, another time for the array 5 6 7, and so on.

"   means simply, apply the function (on the left) using on arrays of
rank n (the number on the right).  So you get a foreach type operation
that's useful on regular arrays.

To construct the nested array, you could write:
   1 2 3; 5 6 7; 1 2 5; 2 3 4

Grab a copy of J and try it out.  Personally, I prefer answering
questions of people who are stuck than I do people who've never even
gotten that far.

Raul Rockwell

krb@uflorida.cis.ufl.EDU (Ken Block) (03/22/91)

I am under the impression that J does not have nested vectors.
Is this true?

How could I do something like the following apl: 

x function foreach ((1 2 3) (5 6 7) (1 2 5) (2 3 4))

 

ljdickey@watmath.waterloo.edu (L.J.Dickey) (03/22/91)

In article <27567@uflorida.cis.ufl.EDU> krb@uflorida.cis.ufl.EDU (Ken Block) writes:
>I am under the impression that J does not have nested vectors.
>Is this true?

J has "boxed" vectors.  The idea is similar, but with box, (given by "<"),
scalars are not a special case, as with nested arrays.

Here is a session script:

   sum =. +/
   avg =. sum % #
   data =. 1 2 3; 5 6 7; 1 2 5; 2 3 4 5 6
   data
+-----+-----+-----+---------+
|1 2 3|5 6 7|1 2 5|2 3 4 5 6|
+-----+-----+-----+---------+
   sum &. > data
+-+--+-+--+
|6|18|8|20|
+-+--+-+--+
   avg &. > data
+-+-+-------+-+
|2|6|2.66667|4|
+-+-+-------+-+

   n234 =. 100 200 +/ 10 20 30 +/ 1 2 3 4
   n234
111 112 113 114
121 122 123 124
131 132 133 134

211 212 213 214
221 222 223 224
231 232 233 234


   sum n234
322 324 326 328
342 344 346 348
362 364 366 368
   avg n234
161 162 163 164
171 172 173 174
181 182 183 184


   sum "1 n234
450 490 530
850 890 930
   avg "1 n234
112.5 122.5 132.5
212.5 222.5 232.5


   sum "2 n234
363 366 369 372
663 666 669 672
   avg "2 n234
121 122 123 124
221 222 223 224


I hope this helps.

Lee Dickey

-- 
Prof L.J. Dickey, Faculty of Mathematics, U of Waterloo, Canada N2L 3G1
	Internet:	ljdickey@watmath.waterloo.edu
	UUCP:		ljdickey@watmath.UUCP	..!uunet!watmath!ljdickey
	X.400:		ljdickey@watmath.UWaterloo.ca

mjab@nanna.think.com (Michael J. A. Berry) (03/22/91)

In a posting, you asked:

   How could I do something like the following apl: 

   x function foreach ((1 2 3) (5 6 7) (1 2 5) (2 3 4))

The straightforward answer is by defining a foreach operator like so:

   foreach =. &.>
   foo foreach  1 2 3; 5 6 7; 1 2 5; 2 3 4

However, J has several more interesting ways of partitioning data which
make the use of boxed arrays less pervasive than in APL2 (which I assume is
the dialect of APL you quote in your example.

In particular, the rank operator lets you apply functions to cells of any
rank within a higher rank array.  In your example:

   foo"1 (4 3$ 1 2 3 5 6 7 1 2 5 2 3 4)

Even more interesting to my way of thinking is partitioning by keys as in:

   text
the quick brown fox jumped over the lazy dog
   Vowel =. e.&'aeiouAEIOU'
   Vowel text
0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0

   (Vowel text) </. text
+--------------------------------+------------+
|th qck brwn fx jmpd vr th lzy dg|euiooueoeeao|
+--------------------------------+------------+
   (Vowel text) $/. text
32
12

Here is an example of Quicksort implemented using /. and boolean keys:


   Qsort
+-------------------------------------------------------------------+--++
|$. =. > (Sorted y.){ recurse;return                                |::||
|recurse) $. =. (Any mask =. y. > Pivot  y.) }. recurse             |  ||
|         y. =. ,. (Collapse mask) { mask $:COLLECTING  y=: y.      |  ||
|return)  y.                                                        |  ||
+-------------------------------------------------------------------+--++
   Sorted
+-------------------------+--++
|*./ (1}. y.) >: (_1}. y.)|::||
+-------------------------+--++
   Any
+--+-+
|+.|/|
+--+-+
   Pivot
+------------+--++
|(''$?$y.){y.|::||
+------------+--++
   Collapse
+-----+--++
|~. y.|::||
+-----+--++
   COLLECTING
+-+--+-------+
|1|::|<&x. /.|
+-+--+-------+
   Qsort 20?20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

[by the way the definition of Collapse is because I can't type the J for it
-- to my system that means hang up the phone!]

--

==============================================
Michael J. A. Berry

Internet:  mjab@think.com
uucp:      {harvard, uunet}!think!mjab
telephone: (617) 234-2056  FAX: (617) 234-4444
==============================================