Vocabulary/dollar

From J Wiki
Jump to navigation Jump to search

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

$ y Shape Of

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



The shape of the noun y .

The shape of the noun y is always a list, even if that list contains just 1 atom (i.e. y itself is a list).

Contrast this behavior with Tally (# y).

The shape of a given noun is...

  • empty (i.0) for an atom (eg the single letter 'a')
  • the number of atoms for a list (eg a list of numbers)
    • 0 for the empty list
  • (3 2) for a 3-by-2 table.

Common Uses

1. Find the shape of a noun

   $ 'abc'                  NB. A 3-atom list
3
   $ 'a'                    NB. An atom - no axes

   $ ''                     NB. An empty list - 1 axis, 0 elements
0
   $ i. 2 3                 NB. A 2x3 table
2 3
   ] empty =: 0 # i. 3 4    NB. Create a table, then delete all the rows
   $ empty                  NB. The rows are still 4 long, even though there are 0 of them!
0 4

2. Find the rank of a noun using # $ y

   # $ 'a'                  NB. Rank of an atom is 0
0
   # $ 'abc'                NB. Rank of a list is ALWAYS 1
1
   # $ ''                   NB. ... even an empty list
1
   # $ i. 3 2               NB. Rank of a table is 2
2

Related Primitives

Tally (# y)


More Information

1. Shape Of ($ y) gives the shape of y (always a list), while Tally (# y) gives the number of items of y (always an atom).

   ($ ; #) y=: ''
+-+-+
|0|0|
+-+-+
   ($ ; #) y=: 'a'
++-+
||1|
++-+
   ($ ; #) y=: ,'a'
+-+-+
|1|1|
+-+-+
   ($ ; #) y=: 'abc'
+-+-+
|3|3|
+-+-+

Use These Combinations

Combinations using $ y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Calculate rank of a noun #@$ y
([: # $) y
@: in place of @
Calculate number of atoms in a noun #@, y
*/@$ y
([: # ,) y
([: */ $) y
@: in place of @
Is the noun empty? (0 e. $) y
Is the noun nonempty? *@#@, y
Does the noun have items? *@# y


x $ y Shape

Rank 1 _ -- operates on lists of x and the entirety of y -- WHY IS THIS IMPORTANT?


Creates an array whose shape depends on x and the shape of y, and whose items are taken from y .

   2 3 $ 'ab'
aba
bab

Common Uses

1. Create a noun with a given shape and items

   2 3 $ 5
5 5 5
5 5 5

2. Reshape a noun to a different shape x.

In general, it's wise to use ($,) rather than  $ .

If y is an atom or a list, it's ok to use  $ instead of ($,) .

   ]i. 2 3
0 1 2
3 4 5
   2 3 4 ($,) i. 2 3
0 1 2 3
4 5 0 1
2 3 4 5

0 1 2 3
4 5 0 1
2 3 4 5

More Information

The items of the result of  x $ y are the items of y, taken cyclically (i.e. when y is exhausted, the first item of y is reused).

1. If y is an atom or a list, the shape of the result is x.

   $ y=: 'abcdefghijkl'
12
   ] z=: 2 2 $ y
ab
cd
   $ z
2 2

2. If y has rank > 1, the shape of the result is (x,}.$y); i.e. the result is an array of items of y whose frame is x.

To be pedantic, the shape of the result is always (x,}.$y) no matter the rank of y.

   i. 2 3          NB. A 2x3 array
0 1 2
3 4 5
   2 3 $ i. 2 3    NB. A 2x3 array of 3-atom lists, i. e. shape 2 3 3
0 1 2
3 4 5
0 1 2

3 4 5
0 1 2
3 4 5
   $ 2 3 $ i. 2 3  NB. the shape of the above
2 3 3

3. x ($,) y always produces a result whose shape is x.

4. If a fill atom f is specified (by using $!.f), cyclic reuse of y is suppressed: after y is exhausted further items of the result are filled with the fill atom.

   5 $ 'a'
aaaaa
   5 $!.'b' 'a'    NB. With fill atom, y is used only once
abbbb

5. If the fill atom is an empty list, an atom of the same type as y is used.

   5 $!.'' 6       NB. integer type, use 0 for fill
6 0 0 0 0
   5 $!.'' <'a'    NB. boxed type, use a: for fill
+-+++++
|a|||||
+-+++++

6. x may be empty. The result will be consistent with the rules given above.

   '' $ 5 6 7      NB. Shape of result is empty, i. e. result is an atom
5
   '' $ i. 2 3     NB. Shape of result is 3
0 1 2

'' ($,) y is a good way to get the first atom of y, as long as y is not empty.


Details

1. If y is empty, the result must also be empty, i. e. its shape must contain a 0 .

The reason is that the values of the result must come from y, and if y has no values, what could the result possibly be?

   5 $ ''
|length error
|   5    $''

2. Exception to the previous rule: if a fill atom is specified (even an empty one), x need not contain a 0 when y is empty, because the fill atom can be used for the values.

   5 $!.6 ''
6 6 6 6 6

'' ($!.f ,) y will get the first atom of y even if y is empty.


Use These Combinations

Combinations using x $ y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Reshape to exactly x x ($ ,) y Supported as a primitive by ($ ,)"n
Arrays of random numbers x ?@$ y

x([: ? $)y

?. in place of ?

@: in place of @
# in place of $

does not create x $ y