>>  <<  Ndx  Usr  Pri  JfC  LJ  Phr  Dic  Rel  Voc  !:  wd  Help  J for C Programmers

                                                                                                   15. Verbs for Arithmetic

It's time to build your J vocabulary with a few more verbs.

Dyads

x ! y  (rank 0 0) number of ways to choose x things from a population of y things.  More generally, (!y) % (!x) * (!y-x)

x %: y  (rank 0 0) The xth root of y

x #. y  (rank 1 1) y evaluated in base x, i. e. the atoms of y are digits in the base-x representation.  x and y must have the same length, unless x is a scalar in which case it is replicated to the length of .  A place-value list p is calculated, in which each item is the product of all the subsequent corresponding items of x; formally, this is p=.*/\.}.x,1 (the last item of p is always 1 and the first item of x is immaterial).  Then each item in y is multiplied by its place value and the results are summed to give the result (formally this is +/ y * p).  In the simplest case x is a scalar:

   10 #. 2 3 4

234

The digits 2 3 4 interpreted as base-ten digits.

   16 #. 2 3 4

564

The same interpreted as base-sixteen digits.

   16b234

564

as expected (16b234 is 234 in base 16, equivalent to 0x234).

Here is an example where x is a list, converting time in hours/minutes/seconds to seconds since midnight:

   24 60 60 #. 12 30 0

45000

The list p was 3600 60 1 .  The first item in x has no effect on the result.

x #: y  (rank 1 0)  This is the inverse of x #. y except that if the first digit of the result is not between 0 and {.x, it is changed (modulo x) to lie within that range.  For example,

   24 60 60 #: 45000

12 30 0

The normal case, converting the number of seconds since midnight back to hours/minutes/seconds.

   24 60 60 #. 36 30 0

131400

   24 60 60 #: 131400

12 30 0

Here the result would have been 36 30 0, but 36 is outside the range 0 to 24, so it is replaced by 24|36, which is 12 .  If you want the first item of the result to be unconstrained, make the first item of x either 0 or :

   0 60 60 #: 131400

36 30 0

Note that monad #: and monad #. are similar to the dyadic forms with x set to .  To keep #: and #. straight in your mind, remember that the one with a single dot (#.) produces a single scalar result, while the one with multiple dots (#:) produces a list result.

Monads (all rank 0)

! y  factorial of y (more generally, the gamma function Γ(1+y))

^ y  exponential: same as (the base of natural logarithms e) ^ y

^. y  natural logarithm: same as (the base of natural logarithms e) ^. y

+: y  same as y * 2

-: y  same as y % 2

*: y  same as y ^ 2

%: y  same as 2 %: y

You can see the common feature in the arithmetic primitives ending in : .


>>  <<  Ndx  Usr  Pri  JfC  LJ  Phr  Dic  Rel  Voc  !:  wd  Help  J for C Programmers