• Aucun résultat trouvé

Factorial and Binomial Functions

Dans le document APL/PC Version 2.1 (Page 150-156)

The factorial function, ! N, is defined, for positive integer arguments, as the product of all positive integers up to N. An important consequence of this definition is that IN equals Nx !N-l, or equivalently, !N-l equals (!N)+N. This

relation is used to extend the function to all arguments except negative integers. For example:

Chapter 5. Primitive Functions and Operators 5-15

N+1 2 3 4 5 IN

1 2 6 24 120 ( IN)+N 1 1 2 6 24

10 1 2 3 4 1 1 2 6 24

F+.5 1 1.5 2 2.5

!F

IBM Internal Use Only

0.8862269255 1 1.329340388 2 3.32335097 ( !F)+F

1.772453851 1 0.8862269255 1 1.329340388 1-0.5 0 .5 1 1.5

1.772453851 1 0.8862269255 1 1.329340388 This extension leads to the expression (! 0 ) + 0 or 1 T 0 for

! -1, and -1 is therefore excluded from the domain of the factorial function, as are all negative integers.

The binomial function, MIN, is defined, for non-negative integer arguments, as the number of distinct ways in which M things can be chosen from N things. The expression

(IN)+ (!M)x (!N-M) yields an equivalent definition that is used to extend the definition to all numbers. Although the domain of factorial excludes negative integers, the domain of the binomial does not, because any implied division by 0 in the numerator 1 N is usually accompanied by a corresponding division by 0 in the denominator; the function, therefore, extends smoothly to all numbers, except where N is a negative integer and M is not an integer.

The result of I! N is equivalent to coefficient I in the binomial expansion (X+1 )*N. For example:

o

1 2 313 1 3 3 1

5-16 APLjPC, Version 2.1

IBM Internal Use Only

Operators

An operator may be applied to a function to get a different function. For example, the outer product operator, denoted by the symbol o. may be applied to any of the primitive scalar dyadic functions to derive a corresponding "table function", as shown in the following for times and power:

Four of the APL operators -reduction, scan, inner product, and outer product - may apply to any primitive scalar dyadic

function. The axis operator applies to functions derived from reduction and scan, and also to certain of the mixed functions.

Reduction

expression obtained by placing the function symbol f between adjacent pairs of items of the vector V:

The last example emphasises that the general rule for the order of execution from right to left is applied, and that as a

consequence, the expression -IV yields the alternating sum of

Chapter 5. Primitive Functions and Operators 5-17

IBM Internal Use Only

the itetns of V. The alternating sum is the sum obtained after first weighting the items by nlultiplying alternate eletnents by 1 and -1. Thus:

A+1 -1 1 -1 1 VxA

1 -2 3 -4 5 +IVxA

3 -IV

3

Similarly, .. IV yields the alternating product:

1 0.5 V*A 3 0.25 5 xIV*A 1.875

1.875 "IV

The result of applying reduction to any scalar or vector is a scalar; the value for a scalar or one-element vector argument is the single item itself. (The application of reduction to other arrays is treated in the discussion of the axis operator).

Reduction of an empty vector by any function is the identity element of the function, if one exists, and a domain error if one does not exist. Thus if V is an empty vector, +IV equals 0, and AIV equals 1.

The reason for this definition is the extension to empty vectors of an important relation between the reductions of two

vectors, P and Q, and the reduction of the vector V+P, Q, which is obtained by chaining them together. For example:

+IV equals (+ IP)+ (+IQ) xIV equals (xIP)x (xIQ)

If P is an empty vector, then + IP must equal 0 (the identity element of +), and xlP must equal 1.

5-18 APL/PC, Version 2.1

IBM Internal Use Only

Scan

The scan operator is denoted by the symbol \ and applies to the function that precedes it. When the resulting function is applied to a vector V, it yields a vector of the same shape, the Kth element of which is equal to the corresponding reduction over the first K elements of V. For example:

+\1 2 3 4 5 1 3 6 10 15

X\l 2 3 4 5 1 2 6 24 120

v\O 0 1 0 1

o

0 1 1 1

A\l 1 0 1 0 1 1 0 0 0

<\0 0 1 0 1 1 0

o

0 1 0 0 0 0

The extension of scan to arrays other than vectors is treated in the discussion of the axis operator.

Axis

A matrix can be viewed as a collection of either columns or rows, and an array of higher rank can be viewed as a collection of planes or hyperplanes. For example, a three-dimensional array of shape 2 3 4 is normally represented as two planes of 3-by-4 matrices, but it can also be viewed as three planes of 2-by-4 matrices, or as four planes of 2-by-3 matrices. For any chosen representation, the resulting (hyper)planes are

orthogonal to the chosen axis, and are said to lie along that axis. Thus, in the preceding example, the 3-by-4 matrices lie along the first axis.

In previous sections, the reduction and scan operators were defined for a vector. This definition is extended to arrays of higher rank by applying the function argument of the operator bet\veen successive (hyper)planes. As the preceding example shows, a multi-dimensional array can be viewed as a collection of arrays of lesser rank which lie along any chosen axis. The axis operator is used to select the chosen axis, and determines the direction of application of the scan or reduction operators.

Chapter 5. Primitive Functions and Operators 5-19

IBl\1 Intcinal Usc Only

The axis operator is denoted by brackets immediately following a scan or reduction operator. The brackets enclose an

expression yielding the index of the desired axis as a scalar or one-element vector. If a scan or reduction operator is applied to an array without the axis operator, the direction of

application will be along the last axis. For example:

D+M+3 4P112 1 2 3 4

5 6 7 8 9 10 11 12

+\ [l]M +/ [l]M

1 2 3 4 15 18 21 24

6 8 10 12 15 18 21 24

+\[2]M +/[2]M

1 3 6 10 10 26 42

5 11 18 26 9 19 30 42

+\M +/M

1 3 6 10 10 26 42

5 11 18 26 9 19 30 42

The result of the scan operation has the same shape as the argument. The result of a reduction operation has a shape similar to the shape of the argument, but with the indicated axis of reduction removed. Indexing of axes is dependent on the current value of the index origin, DIO. With DIO+l, the leftmost or first axis has an index value of 1. The symbols f and ~ also denote reduction and scan operations, which are equivalent to the standard reduction and scan operators when used with the axis operator. When used without an axis operator however, these symbols cause the reduction or scan operation to be applied along the FIRST axis.

The axis operator is also used to specify the axis of application of the mixed functions, reverse, rotate, catenate, compress, and expand. The axis operator cannot be used with the inner product or outer product operators.

5-20 APLjPC, Version 2.1

IBM Internal Use Only

Dans le document APL/PC Version 2.1 (Page 150-156)

Documents relatifs