Vocabulary/numberdot

From J Wiki
Jump to navigation Jump to search

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

#. y Base 2

Rank 1 -- operates on lists of y, producing an atom for each one -- WHY IS THIS IMPORTANT?


The corresponding number of a binary numeral, given as a Boolean list

   #. 1 0 1 0 1
21
   #. 1 0 1 0
10
   #. 1 1 1 1 1
31
   #. 1 1 1
7

Generalizes to a Boolean table, which gets treated as a list of binary numerals

   ] z=: > 1 0 1 0 1 ; 0 1 0 1 0 ; 1 1 1 1 1 ; 0 0 1 1 1
1 0 1 0 1
0 1 0 1 0
1 1 1 1 1
0 0 1 1 1
   #. z
21 10 31 7

Common uses

1. Binary to decimal conversion

   z=: '.....X.X'   NB. sample binary form
   'X'=z
0 0 0 0 0 1 0 1
   #. 'X'=z
5

Related Primitives

Antibase 2 (#: y)


x #. y Base

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


Generalizes  #.y to bases other than 2 (including mixed bases)

     #. 1 0 1 0 1   NB. base-2 numeral --> number
21
   2 #. 1 0 1 0 1   NB. ditto, but base (=2) is explicitly specified
21
   10 #. 1 0 1 0 1  NB. base-10 numeral --> number
10101

If x is an atom, it is reshaped to the shape of y.

Each atom of x gives the place value of the corresponding position of y.


Common uses

1. Convert list of decimal digits to number

   numberOf=: 10 & #.
   numberOf 9 0 8 0 1
90801

2. Convert time-interval in (hours,minutes,seconds) to seconds

   seconds=: 24 60 60 & #.   NB. use of mixed bases, viz. 24 and 60
   seconds 23 59 59
86399
   */24 60 60
86400

3. Evaluate a polynomial, specified by its coefficients in y, at the value of the variable given by x. The coefficients are ordered in descending powers of the variable.

x must be an atom.

Example: sum the exponential series to 10 terms, to approximate the value of  exp y

   exp=: ^
   ] a=: % !i.10   NB. The first 10 coefficients of the exponential series, in ascending-power order
1 1 0.5 0.166667 0.0416667 0.00833333 0.00138889 0.000198413 2.48016e_5 2.75573e_6
   1 #. |. a       NB. Use |. to put into descending-power order
2.71828
   exp 1
2.71828
   2 #. |. a
7.38871
   exp 2
7.38906

Can also use (p.) to evaluate the polynomial

   a p. 1          NB. coefficients of p. are in ascending-power order
2.71828
   a p. 2
7.38871

Related Primitives

Polynomial (x p. y), Antibase (x #: y)


More Information

1. To remember which is which, note that #. (whose inflection is a single dot) produces an atom. Whereas #: (multiple dots) produces a list.


Details

1. x is converted to a list of weights w =. */\.}.x,1; then x #. y is +/w*y. It can be seen from this definition that the first atom of x is immaterial

   24 60 60 #. 4 0 0
14400
   0 60 60 #. 4 0 0
14400

2. y must be numeric, even if it is empty.