Vocabulary/slash

From J Wiki
Jump to navigation Jump to search

>> <<   Down to: Dyad   Back to: Vocabulary Thru to: Dictionary

u/ y Insert Adverb

Rank Infinity -- operates on x and y as a whole, by items of y -- WHY IS THIS IMPORTANT?


Inserts (dyad) u between the items of y

   ] z=: >: i.6
1 2 3 4 5 6

   +/z      NB. equivalent to (1+2+3+4+5+6)
21

Common Uses

u/ y is one of the main looping modifiers of J. It can be used whenever you want to create a total, find the maximum, or generally examine items one by one.

1. Find the total of a list of numbers

   +/ 3 1 4 1 5 1 9
24

2. Find the maximum of a list of numbers

   >./ 3 1 4 1 5 1 9
9

3. Count the number of atoms in an array

   */ $ i. 2 3 4
24

4. Sum the columns of a table

   ]a =: 3 4 ?@$ 100
90 47 58 29
22 32 55  5
55 73 58 50
   +/ a
167 152 171 84

5. Sum the rows of a table

   +/"1  a
224 114 236

6. Emulate x #: y by taking x | y for each item of x

   quorem =: (([ %~ ] - |) , |)  NB. returns quotient,remainder of y%x
   antibase =: }.@(((quorem {.) , }.@])/)@,  NB. Applies quorem repeatedly
   24 60 60 antibase 142344
15 32 24
   24 60 60 #: 142344
15 32 24

Details

1. u can be a cyclic gerund. Gerund 0 is inserted between items 0 and 1, gerund 1 between items 1 and 2, and so on. Since they are executed right-to-left, they will be executed in the reverse order.

   +`-/ z   NB. equivalent to (1+2-3+4-5+6)
7

2. If y has 1 item, u is not executed and u/ y is the single item of y. This will have rank one less than y (unless y is an atom). In other words, if y is an array with one item, u/ y is the same as {. y, while if y is an atom, u/ y is the same as y.

3. If y has no items, u/ y produces the identity element for u, as described below.


Identity Elements (Neutrals)

The identity element (or neutral) for a verb u is a value e such that ({.x) -: ({.x) u e or ({.x) -: e u {.x, for every x in the domain (or some significant sub-domain such as boolean) of u.

The identity function of a verb u is a function that, given y with no items, will return an identity element of the correct shape to work with an item of y. This identity element will be the result of u/ y.

The defined identity elements are:

Defined Identity Functions (ifu is the identity function of u)
Verbs Identity Function
< > + - +. ~: | (2 4 5 6 b.) 0 $~ }.@$
= <: >: * % *. %: ^ ! (1 9 11 13 b.) 1 $~ }.@$
<. _ $~ }.@$
>. __ $~ }.@$
u&.v (v^:_1 ifu$0) $~ }.@$
, i.@(0&,)@(2&}.)@$
C. { /:@{.
%. +/ . * =@/:@{.
u/ ifu@#

You can find the identity function of a verb u by executing u b. 1

   + b. 1
0 $~ }.@$
   +&* b. 1
0 $~ }.@$
   *&+ b. 1
1 $~ }.@$
   *@+ b. 1   NB. They're not all defined
|domain error

Use These Combinations

Combinations using u/ y that have exceptionally good performance include those shown in Searching and Matching Items: Fast List Operations (FLOs), Searching and Matching Items: Precomputed searches, Whole-Array Operations, Intervals, Infixes, Partitions, and Diagonals and Polynomials, as well as the following:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Find index of first/last occurrence of largest/smallest value integer or floating-point list (i. <./) y

(i. >./) y

or i: it actually does (i. >.!.0/) etc.; is faster than 0 ({ /:~) y
Bitwise reduction and scan integer x (m b.)/ y

(m is 16 to 31)

/\ /\. in place of / much faster than alternatives
Mean with rank (+/ % #) y Supported as a primitive by (+/ % #)"n
Odometer function (y gives the size of each wheel; result is all readings from 0 0 0 0 to y) integer (#: i.@(*/)) y @: in place of @
Bitwise operations on bytes byte (m b.)/&.(a.&i.) y

x (m b.)&.(a.&i.) y

(u y) -: u"0 y avoids conversion to integer


x u/ y Table Adverb

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



(x u/ y) returns a table having entries (a u b) for every a in x and b in y

provided dyadic verb u has Rank 0

   ] z=: >: i.6
1 2 3 4 5 6
   z */ z          NB. Show multiplication table
1  2  3  4  5  6
2  4  6  8 10 12
3  6  9 12 15 18
4  8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36

If you are looking for APL's dyadic reduction operator, u/ J performs this function with the more general infix, as u/\ .


Common uses

Show the truth table for a given Boolean verb b

   not=: -.
   and=: *.
   b=: (4 : 'not (x and not y)')"0   NB. (x implies y).  Note rank 0
   0 1 b/ 0 1                        NB. Create truth table
1 1
0 1

More Information

1. If the rank of u is not 0, it doesn't produce a table

   'abc' ,/ 'def'
abcdef

2. x u/ y is still meaningful for u of other ranks. Verb u is applied between each operand cell of x (the rank of which is given by the left rank of u) and the entire y. The results of these executions are collected into the final result.

This definition causes much confusion among students, probably because of incorrect generalization from the rank-0 examples. Verb u is applied on each of its cells, not items or atoms.

Example: demonstrate that a matrix is orthonormal by taking the dot product of each of its columns with each other. The matrix is converted to a list of columns and then the rank-1 dot-product verb creates a table of combination

   dotproduct =: +/@:*"1
   ]mtx =: 3 3 $ 0 _0.8 _0.6  0.8 _0.36 0.48  0.6 0.48 _0.64
  0  _0.8  _0.6
0.8 _0.36  0.48
0.6  0.48 _0.64
   (|: mtx) dotproduct/ (|: mtx)  NB. Each column against all columns - looks orthonormal
1           0           0
0           1 5.55112e_17
0 5.55112e_17           1

Details

1. x u/ y is equivalent to x u"(lu,_) y where lu is the left rank of u. Note that u/ is simply u applied with a different rank.