Vocabulary/lt

From J Wiki
Jump to: navigation, search

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

< y Box

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



Convert the noun y to boxed form, by creating a box whose contents are y. A box is treated as an atom and can be put into an array with other boxes, regardless of the contents of the boxes.

   < 1 2 3
+-----+
|1 2 3|
+-----+
   < 'abc'
+---+
|abc|
+---+

Boxes are used to create heterogeneous arrays. Arrays in J must be rectangular, and must contain only values of the same type and precision. But if you put a noun into a box, it becomes an atom that you can array with other boxes.

Consider a list of strings of varying length: you can't make a table of varying-length strings, but you can box each string and make a list of those boxes. Words (;:) does this:

   ;: 'Three different words'
+-----+---------+-----+
|Three|different|words|
+-----+---------+-----+

You might need to join dissimilar types into an array. Link (;) can join them by boxing them:

   _2 ]\ 'front nine';36;'back nine';38
+----------+--+
|front nine|36|
+----------+--+
|back nine |38|
+----------+--+

Common uses

1. To add elements to a vector of boxed strings:

   ]zub=: ;:'alpha bravo charlie'
+-----+-----+-------+
|alpha|bravo|charlie|
+-----+-----+-------+
   zub,<'delta'
+-----+-----+-------+-----+
|alpha|bravo|charlie|delta|
+-----+-----+-------+-----+

2. To compare a string with contents of a vector of boxed strings:

   zub = <'bravo'
0 1 0

3. Some primitives and Foreigns (!:), especially those designed to work on a boxed list of names, demand a boxed argument if only one name is given.

   xx=: yy=: zz=: 99
   4!:55 'zz'     NB. try to erase word: zz
|domain error
|       4!:55'zz'
   4!:55 <'zz'    NB. erase word: zz
1
   4!:55 'xx' ; 'yy'
1 1

The z-locale verb: erase, which invokes 4!:55, accepts either a boxed or an unboxed string.

4. The z-locale verb: boxopen boxes its argument, but only if unboxed or empty:

   boxopen 'zz'
+--+
|zz|
+--+
   boxopen <'zz'
+--+
|zz|
+--+

5. The z-locale verb: boxxopen boxes its argument, but only if unboxed and not empty:

   boxxopen 'notempty'
+--------+
|notempty|
+--------+
   boxxopen ''

   boxopen ''
++
||
++

Related Primitives

Open (> y)


Use These Combinations

Combinations using < y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Box items of list ;/ y linear time (like <"_1 y)
Join contents of boxed items along first axis ,&.>/ y Bug warning: Atomic replication is inaccurate. OK if contents of same rank. Better to use <@; y
Extract multiple substrings into a list Boolean list, byte list x ;@:(<;.0) y or [: ; <;.0 avoids boxing, avoids creating subarrays
Fetch from multiple index lists (each row of x is one index list into y) x (<"1@[ { ]) y avoids boxing x
Boxing intervals <@f/;.n y

x <@f/;.n y
(f is any verb)

n e. _2 _1 1 2
Concatenated results on intervals ;@:(<@u;.n) y n e. _2 _1 1 2;

also [: ; (<@:u;.n);
also <@u;.n

Bug Warning: This is computed as <@:u even if <@u is coded
x is a Boolean list (not an atom or an integer list with values 0 or 1) x ;@:(<@u;.n) y
Concatenated running totals on intervals (running total, but total is reset at start of each interval) ;@:(<@(f/\);.n) y

x ;@:(<@(f/\);.n) y
(f is any verb)

n e. _2 _1 1 2; also [: ; (f... also <@:(f/\); also <@:
Box the indexes of identical items (</. i.@#) y

y </. i. # y

(</. i.@#) y uses less space


x < y Less Than

Rank 0 0 -- operates on individual atoms of x and y, producing a result of the same shape -- WHY IS THIS IMPORTANT?



Compares x and y atom-by-atom. See Equal (=) for the details.

Wherever x is greater than, or equal to, y, returns 1 in that position, else 0.

   z=: 7 8 9
   z < 8
1 0 0

x and y must both be numeric, else you get a domain error

   'a' < 9
|domain error
|   'a'    <9

This differs from what happens with Equal (=)

   'a' = 9
0

Less Than (<) uses tolerant comparison in the same way as Equal (=).

To require exact comparison, use (<!.0) in place of (<) to temporarily set the comparison tolerance to zero

   1.99999999999999 < 2
0
   1.99999999999999 (<!.0) 2
1

Common uses

1. Make a conditional phrase in a verb definition (explicit definition)

if. x<y do.  'x y'=. y;x  end.

2. Make a mask of the same shape as array, to process array in some chosen way

   array=: 3 1 4 1 5 9 2 6
   mask=: array < 5

   array ,: mask
3 1 4 1 5 9 2 6
1 1 1 1 0 0 1 0

   mask # array       NB. keep only the atoms that are < 5
3 1 4 1 2

NOTE: The boolean atoms of mask are not only truth values but also valid numbers, 0 or 1. Therefore you can use mask directly to process array like this:

   array + 3 * mask   NB. add 3 to the atoms that are < 5
6 4 7 4 5 9 5 6

Related Primitives

Equal (x = y), Not-Equal (x ~: y), Larger Or Equal (x >: y), Less Or Equal (x <: y), Larger Than (x > y)


More Information

1. If an argument to x < y is complex, its imaginary part must be tolerantly equal to 0, using the default tolerance of 2^_44 even if the comparison x < y itself uses a different tolerance.


Use These Combinations

Combinations using x < y that have exceptionally good performance include those shown in Searching and Matching Items: Fast List Operations (FLOs), as well as the following:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Boolean reductions along diagonals Boolean +.//. y *. = ~: < <: > >: in place of +. avoids building argument cells