J Labs

Iteration and the Power Operator

(1 of 10) ITERATION

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


In [ ]:
x=: 3

In [ ]:
+: x

In [ ]:
+: +: x

In [ ]:
+: +: +: x

In [ ]:
-: x

In [ ]:
-: -: x

(2 of 10) POWER OPERATOR

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


In [ ]:
+:^:2 x

In [ ]:
+:^:3 x

In [ ]:
+:^:0 1 2 3 4 x

In [ ]:
i=: 0 1 2 3 4

In [ ]:
+:^:i x

In [ ]:
v=: 3 4 5

In [ ]:
+: v

In [ ]:
+:^:i v

(3 of 10) POWER OPERATOR (ctd)

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:


In [ ]:
^&2 ^: i i

In [ ]:
+/\ ^: i i

In [ ]:
3&A.^: i i

In [ ]:
3&A.^: i 'ABCDE'

(4 of 10) POWER OPERATOR (ctd)

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:


In [ ]:
!! i

In [ ]:
!! x:i

In [ ]:
^&2 ^: i x:i

(5 of 10) 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:


In [ ]:
r2=: 1 2 1

In [ ]:
0,r2

In [ ]:
r2,0

In [ ]:
(0,r2)+(r2,0)

(6 of 10) BINOMIAL COEFFICIENTS (ctd)

A function for this process may be defined and iterated as follows:


In [ ]:
next=: 0&, + ,&0

In [ ]:
next r2

In [ ]:
next 1

In [ ]:
next next 1

In [ ]:
bct=: next ^: i 1

In [ ]:
bct

(7 of 10) BINOMIAL COEFFICIENTS (ctd)

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:


In [ ]:
abct=: %. bct

In [ ]:
abct

In [ ]:
mp=: +/ . *       NB. Matrix product

In [ ]:
bct mp abct

(8 of 10) NEGATIVE POWERS

Negative powers of a function yield powers of its inverse. For example:


In [ ]:
+: ^: _1 i

In [ ]:
+: ^: _2 i

In [ ]:
+/\ ^: i i

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

(9 of 10) 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:


In [ ]:
Cos=: 2&o.

In [ ]:
0 1r4p1 1p1 3r4p1 2p1            NB. Multiples of pi

In [ ]:
Cos 0 1r4p1 1p1 3r4p1 2p1

In [ ]:
i=: 0 1 2 3 4

In [ ]:
Cos^:i 1

In [ ]:
k=: i.4 5

In [ ]:
k

In [ ]:
Cos^: k 1

In [ ]:
y=: Cos^: _ (1)

In [ ]:
y

In [ ]:
Cos y

In [ ]:
y=Cos y

(10 of 10) LIMITS (ctd)

Since _ denotes infinity, the expression Cos ^: _ signifies an infinite number of iterations, but terminates automatically 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.

End of Lab


In [ ]: