LABTITLE=: 'Iteration and the Power Operator'

NB. =========================================================
Lab Section ITERATION

The repeated application of a function, such as doubling or halving, is called iteration. For example:
)
x=: 3

+: x

+: +: x

+: +: +: x

-: x

-: -: x

NB. =========================================================
Lab Section POWER OPERATOR

The power operator ^: can be used to produce any specified number of iterations. For example:
)
+:^:2 x

+:^:3 x

+:^:0 1 2 3 4 x

i=: 0 1 2 3 4

+:^:i x

v=: 3 4 5

+: v

+:^:i v

NB. =========================================================
Lab Section

We will illustrate the use of the power operator on other functions, including the square (^&2), subtotals (+/\), and permutations or anagrams (k&A.) .

Further information on any of these expressions may be found by pressing the function key F1 to display the vocabulary, and then clicking the mouse on the desired item:
)
^&2 ^: i i

+/\ ^: i i

3&A.^: i i

3&A.^: i 'ABCDE'

NB. =========================================================
Lab Section

The larger powers in the table ^&2 ^: i i appeared in "scientific" or exponential form. Such large numbers may be produced in "extended precision" as illustrated below:
)
!! i

!! x:i

^&2 ^: i x:i

NB. =========================================================
Lab Section BINOMIAL COEFFICIENTS

A table of binomial coefficients is often presented as a triangle (Pascals triangle) by suppressing the zeros that result from the number of ways that n elements can be chosen from a lesser number of items. Thus:

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

A row of these coefficients can be simply obtained from the preceding row, as illustrated below:
)
r2=: 1 2 1

0,r2

r2,0

(0,r2)+(r2,0)

NB. =========================================================
Lab Section

A function for this process may be defined and iterated as follows:
)
next=: 0&, + ,&0

next r2

next 1

next next 1

bct=: next ^: i 1

bct

NB. =========================================================
Lab Section

The operations of matrix algebra can be applied to this matrix of binomial coefficients in useful and interesting ways, as developed in a companion lab. For example, the inverse yields a table of alternating binomial coefficients:
)
abct=: %. bct

abct

mp=: +/ . *       NB. Matrix product

bct mp abct

NB. =========================================================
Lab Section NEGATIVE POWERS

Negative powers of a function yield powers of its inverse. For example:
)
+: ^: _1 i

+: ^: _2 i

+/\ ^: i i

+/\ ^: (-i) 0 1 6 21 56

NB. =========================================================
Lab Section LIMITS

An iteration may approach a limiting value such that further applications of the function produce no discernible change. The cosine applied to 1 has such a property:
)
Cos=: 2&o.

0 1r4p1 1p1 3r4p1 2p1            NB. Multiples of pi


Cos 0 1r4p1 1p1 3r4p1 2p1

i=: 0 1 2 3 4

Cos^:i 1

k=: i.4 5

k

Cos^: k 1

y=: Cos^: _ (1)

y

Cos y

y=Cos y

NB. =========================================================
Lab Section

Since _ denotes infinity, the expression Cos ^: _ signifies an infinite number of iterations, but terminates automatic- ally when two successive results agree. Consequently, the result y agrees with Cos y, and is therefore a solution of the equation expressed by the final line.
)
