Vocabulary/bslashdot

From J Wiki
Jump to navigation Jump to search

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

u\. y Suffix Adverb

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


(u\. y) applies verb u to successive suffixes of list y of decreasing length

   ]\. 'banana'
banana
anana
nana
ana
na
a

The main use is the form u/\.

   +/\. 3 1 4 1 5 9   NB. Compute reverse running total
23 20 19 15 14 9

Common Uses

u\. y is magic when u is of the form u/ -- look what happens then:

   u =: dyad define   NB. Displays the arguments of u
x , ' u ' , y
)
   u/\. 'abcdef'
a u b u c u d u e u f
b u c u d u e u f
c u d u e u f
d u e u f
e u f
f

This result is calculated in reverse order, so that u is applied between larger and larger suffixes of y. The effect is of a loop, when on each iteration one more item of y in operated on.

Example: suppose u is: +

   plus =: dyad define
x , ' + ' , y
)
      plus/\. 'abcdef'
a + b + c + d + e + f
b + c + d + e + f
c + d + e + f
d + e + f
e + f
f

Get the idea? The operand u is applied between longer and longer suffixes of y. It's a loop that reports the results of each iteration.

With J's right-to-left execution, u/\. y is much more efficient than u/\ y when u is not a primitive.

   u =: dyad define     NB. Displays the arguments of u
'(' , x , ' u ' , y , ')'
)
   u/\. 'abcdef'
(a u (b u (c u (d u (e u f)))))
(b u (c u (d u (e u f))))
(c u (d u (e u f)))
(d u (e u f))
(e u f)
f

Note that each row can be calculated with a single application of u using the result from the following row.

1. When y has exactly 2 items, u/\.y is a good way to replace the first item with a function of y while leaving the second item unchanged.

   >./\. 2 3   NB. Replace first item with max of items
3 3

2. To apply a complicated verb for each item of y, retaining the results of each iteration

   NB. Replace missing data (represented by _) with the following valid item.
   ]d =: 3 1 _ 1 5 _ _ 2 3 5 _ 9 7 _   NB. Data line
3 1 _ 1 5 _ _ 2 3 5 _ 9 7 _
   [^:(_ ~: [)/\. d                    NB. Use left argument unless it is _; then use right
3 1 1 1 5 2 2 2 3 5 9 9 7 _
   [^:(_ ~: [)/\.&.(,&0) d             NB. append an initial value to handle last item
3 1 1 1 5 2 2 2 3 5 9 9 7 0

If you wanted to replace missing data with the previous item, you could use u/\ y, but that would be a mistake. You should continue to use u/\. y, but reverse the order of y before and after the computation using &.|.

   d
3 1 _ 1 5 _ _ 2 3 5 _ 9 7 _
   [^:(_ ~: [)/\.&.(,&0)&.|. d
3 1 1 1 5 5 5 2 3 5 5 9 7 7

More Information

1. The last item of y is not changed by  u/\. y . (It could be changed by more general u in  u\. y) .


Details

1. u can be a cyclic gerund.

2. The argument to each invocation of u is a list of items of y (this has the same rank as y unless y is an atom).

3. If y has no items, the result of  u\. y will also have no items. However, u will be executed once on a 0-item cell to establish the shape of a result of u, and then the result of u\. y will be a list of 0 of that shape:

   $ ]\. ''       NB. u is executed on cell of shape 0
0 0
   $ {.\. ''      NB. u produces an atomic result
0
   $ {.\. i. 0 3  NB. u is executed on cell of shape 0 3, returns shape 3
0 3
   $ ]\. i. 0 3   NB. u is executed on cell of shape 0 3, returns shape 0 3
0 0 3

Use These Combinations

Combinations using u\. y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Create list of integers from 1 to # y, or the reverse #\ y also #\. which is the reverse faster than >:@i.@# y


x u\. y Outfix Adverb

Rank 0 _ -- operates on atoms of x, and the entirety of y -- WHY IS THIS IMPORTANT?


(x u\. y) applies verb u to successive outfixes of y. Each outfix is y with an infix removed. The infixes depend on x, as described in Infix (\).


Common uses

1. Enumerate the faces of a simplex

    ]simplex =: _3 ]\ 0 0 0  1 0 0  0 1 0  0 0 1  NB. A simplex
0 0 0
1 0 0
0 1 0
0 0 1
   1 ]\. simplex     NB. The 4 faces
1 0 0
0 1 0
0 0 1

0 0 0
0 1 0
0 0 1

0 0 0
1 0 0
0 0 1

0 0 0
1 0 0
0 1 0

2. Return a vector with each successive single item removed.

   _1]\. 1 2 3 4
2 3 4
1 3 4
1 2 4
1 2 3

Details

1. u can be a cyclic gerund.

2. The argument to each invocation of u is a list of items of y (this has the same rank as y unless y is an atom).

3. If y has no items, or not enough items to create an outfix, the result of x u\. y will also have no items. However, u will be executed once on an empty cell to establish the shape of a result of u, and then the result of x u\. y will be a list of 0 of that shape.

4. If x is zero, u is executed on the entire y, (1+#y) times.


Use These Combinations

Combinations using x u\. y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Bitwise reduction and scan integer x (m b.)/ y

m is 16 to 31

/\ /\. in place of / much faster than alternatives