Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Rich H.J for C programmers.2006.pdf
Скачиваний:
19
Добавлен:
23.08.2013
Размер:
1.79 Mб
Скачать

33. Elementary Mathematics in J

Verbs for Mathematics

All the verbs have rank 0 0 .

x *. y Lowest common multiple of x and y x +. y Greatest common divisor of x and y

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

The verbs dyad e. (Member of) and dyad -. (set difference) are useful in working with sets.

Extended Integers, Rational Numbers, and x:

In J, numbers are not limited to 32-bit integers or 64-bit floating point. Extended integer and rational are atomic data types (like numeric, literal, and boxed) that allow representation of numbers with arbitrary accuracy. An extended integer constant is defined by a sequence of digits with the letter x appended; a rational constant is two strings of digits (numerator and denominator) separated by the letter r; examples are

123x and 4r5 .

The various representations of numbers in J can be given a priority order:

boolean (low) - integer - extended integer - rational - floating point - complex (high)

When a dyadic arithmetic operation is performed on operands of different priorities, the lower-priority operand is converted to the higher-priority representation. The simplest example arises in a list constant:

12345678901234567890 4r5

12345678901234567890 4r5

The integer was made into a rational number so it keeps its precision.

3.0 4r5

3 0.8

The floating-point constant forces the rational number to floating point.

2 * 3r4

3r2

The operation was performed on rational operands with a rational result. Results of verbs are given a higher-priority representation if necessary:

190

%: 4r9

2r3

%: 5r9 0.745356

Explicit conversions between extended/rational and floating-point can be performed by the infinite-rank verb x: . x: y converts floating-point y to rational or integer y to extended integer. The inverse, x:^:_1 y, converts in the other direction.

A rational number can be split into numerator and denominator by 2 x: y (rank 0):

2 x: 1r3 5

1 3

5 1

Factors and Primes: Monad p:, Monad and Dyad q:

p:y (rank 0) gives the yth prime (prime number 0 is 2).

q:y (rank 0) gives the prime factors of y

x q: y (rank 0) with positive x is the first x items (or all items, if x is _) in the list of exponents in the prime factorization of y :

_ q: 700 2 0 2 1

*/ (p: 0 1 2 3) ^ 2 0 2 1

700

x q: y with negative x returns a 2-row table. The second row is the nonzero items of (|x) q: y (i. e. the nonzero exponents in the prime factorization); the first row is the corresponding prime numbers:

__ q: 700 2 5 7 2 2 1

Permutations: A. and C.

In the direct representation of a permutation p each item i{p of the permutation vector indicates the item number that moves to position i when the permutation is applied. Applying the permutation in the direct form is as simple as writing p{y .

The standard cycle representation of a permutation gives the permutation as a list of cycles (sets of elements that are replaced by other elements of the set). The standard cycle form is a list of boxes, one for each cycle, with each cycle starting with the largest element and the cycles in ascending order of largest element.

Monad C. y (rank 1) converts between direct and standard-cycle representations of the permutation y :

191

/: 3 1 4 1 5 9 1 3 0 2 4 5

C. /: 3 1 4 1 5 9 +-------+-+-+ |3 2 0 1|4|5| +-------+-+-+

C.C. /: 3 1 4 1 5 9

13 0 2 4 5

xC. y (rank 1 _) permutes the items of y according to the permutation x which may be in either standard-cycle or direct form; other nonstandard forms are also supported as described in the Dictionary.

There are !n possible permutations on n items, so it is possible to give each one a number between 0 and <:!n . Imagine the table of all possible permutations in lexicographic order; the anagram index of a permutation is its index in that table. A. y (rank 0) gives the anagram index for the permutation y, which may be in either direct or standard-cycle form. x A. y (rank 0 _) permutes the items of y according to the permutation whose anagram index is x :

a =. /: 3 1 4 1 5 9 A. a

168

a C. 1 2 3 4 5 6 2 4 1 3 5 6

168 A. 1 2 3 4 5 6

2 4 1 3 5 6

The monad C.!.2 y gives the parity of y : 1 if an even number of pairwise exchanges are needed to convert y to the identity permutation i.#y, _1 if an odd number are needed, 0 if y is not a permutation.

192