Iterate operator

The iterate operator feeds an expression's output back into its input a chosen number of times. Where other looping operators like Start sum from to , end sum, and for combine many independent terms, iterate uses the result of the previous step to compute the next one. That unlocks recurrences, numerical methods like Newton's method, and fractals.

Writing an iterate

Type iterate to insert the operator. It has three fields. The one inside the loop names the recursion variable and gives its starting value, for example g=1. Below and above it are the bounds of a counter, written like the bounds of a sum: the counter variable and its lower bound below, the upper bound above. The expression that follows the operator is the step.

Recording of the calculator performing the steps described above

For each value of the counter, the step is evaluated and its result becomes the new value of the recursion variable. The value of the whole operator is the final value of the recursion variable. The example below multiplies g by n five times, so it computes 5 !:

Start iterate with "g" equals 1 from "n" equals 1 to 5, end iterate, "g" times "n"equals 120

The step does not have to use the counter. The counter is then just a way of saying how many times to run:

Start iterate with "g" equals 1 from "n" equals 1 to 4, end iterate, 2 "g"equals 16

The bounds are inclusive: the counter takes every value from the lower bound up to and including the upper bound, so the number of steps is the upper bound minus the lower bound plus one. If the upper bound is smaller than the lower bound the step never runs and the result is the starting value. The bounds can be any expression that gives a plain number, including a value defined in another box:

"k" equals 6
Start iterate with "g" equals 1 from "n" equals 1 to "k" , end iterate, 2 "g"equals 64

What counts as the step

Like Start sum from to , end sum,, iterate only uses the term immediately following it as its step. If the step is a sum of terms, it has to be wrapped in parentheses; otherwise only the first term is iterated and the rest is added to the result afterwards. With a=5:

Start iterate with "g" equals 0 from "n" equals 1 to 3, end iterate, "g" plus "a"equals 5
Start iterate with "g" equals 0 from "n" equals 1 to 3, end iterate, left parenthesis, "g" plus "a" , right parenthesisequals 15
On the left the +a is outside the operator, which iterates the step g three times and then adds a. On the right the parentheses make g+a the step.

Stopping early

The upper bound can be followed by a comma and a condition, such as 100, |z|<2. The condition is checked before every step, and the loop stops as soon as it is false, so the iteration runs until either the condition fails or the upper bound is reached, whichever comes first. The result is the value of the recursion variable at that point. The condition can use the recursion variable and the counter, and it can be a chain like 0<g<1.

A normal upper bound is still always required, even with a condition. It puts a hard limit on the number of steps, so an iteration can never run forever if the condition never fails.

Start iterate with "g" equals 1 from "n" equals 1 to 100 , "g" less than 50, end iterate, 2 "g"equals 64

Here the doubling stops at the first value that is not below 50, long before the cap of 100 steps.

Examples

Newton's method for StartRoot, 2 , EndRoot starts from a guess and repeatedly improves it. Five steps are already exact to the displayed precision:

Start iterate with "g" equals 1 from "n" equals 1 to 5, end iterate, left parenthesis, "g" minus StartFraction, "g" squared minus 2 Over 2 "g" , EndFraction , right parenthesisequals 1.4 1 4 2 1

The logistic map "p" maps to "r" "p" ( 1 minus "p" ) after fifty steps:

"r" equals 3.2
Start iterate with "p" equals 0.5 from "n" equals 1 to 50, end iterate, "r" "p" left parenthesis, 1 minus "p" , right parenthesisequals 0.5 1 3 0 4 5

The recursion variable can be complex:

Start iterate with "z" equals 0 from "n" equals 1 to 3, end iterate, left parenthesis, "z" squared plus "i" , right parenthesisequals negative "i"

Vectors, matrices and arrays as the state

The recursion variable can be a vector or matrix, which lets a single iterate carry several values at once. Here a 2-vector holds two consecutive Fibonacci numbers, and each step shifts it along:

Start iterate with "v" equals Start 2 by 1 matrix, Row 1: 1; Row 2: 0, End matrix from "n" equals 1 to 10, end iterate, Start 2 by 1 matrix, Row 1: "v" . "x" plus "v" . "y" ; Row 2: "v" . "x" , End matrixequals Start 2 by 1 matrix, Row 1: 89; Row 2: 55, End matrix

Multiplying by a matrix each step computes a matrix power:

"M" equals Start 2 by 2 matrix, Row 1: 1, 1; Row 2: 0, 1, End matrix
Start iterate with "g" equals Start 2 by 2 matrix, Row 1: 1, 0; Row 2: 0, 1, End matrix from "n" equals 1 to 3, end iterate, "g" "M"equals Start 2 by 2 matrix, Row 1: 1, 3; Row 2: 0, 1, End matrix

The recursion variable has to keep the same type from one step to the next. Seeding the example above with the number 1 instead of the identity matrix is an error, because after one step g would be a matrix.

Arrays work the same way, and operations on the recursion variable apply to every entry:

Start iterate with "g" equals left bracket, 1 , 1 , right bracket from "n" equals 1 to 3, end iterate, 2 "g"equals left bracket, 8 , 8 , right bracket

Keeping every step

iterate gives you the final value only. To see the whole sequence, wrap it in a for operator whose counter sets the number of steps:

Start for from "k" equals 0 to 5, end for, Start iterate with "g" equals 1 from "n" equals 1 to "k" , end iterate, 2 "g"equals left bracket, 1 , 2 , 4 , 8 , 16 , 32 , right bracket

Each entry is a separate iteration from the start, so a long sequence costs more than a single iterate; for a short one that does not matter.

Iterate in graphs

The seed and the step can use x, y or t, and the result is graphed like any other expression. Seeding with x makes the result a function of x. Below, four steps of Newton's method are run from every starting point x, so the curve sits at plus-or-minus StartRoot, 2 , EndRoot almost everywhere, apart from a spike near zero where the first step overshoots.

Screenshot of the calculator showing the result of the steps described above

Inside an implicit equation, an iterate runs on your GPU for every pixel, which is what makes fractals practical. The Mandelbrot set is the set of points c for which iterating "z" maps to "z" squared plus "c" from zero stays bounded. Thirty steps and a threshold of 2 give a good approximation of its outline:

Screenshot of the calculator showing the result of the steps described above

You can draw a Julia set instead by fixing the constant and seeding with the pixel, "z" equals "x" plus "i" "y".

A stopping condition makes each pixel stop as soon as it escapes, so the iteration can run for many more steps without the escaped points blowing up. That also makes the result well behaved enough to differentiate. Here the magnitude of the escaped point is defined as m in one box, the set itself is filled in with the inequality "m" less than 2, and the gradient of m is drawn as a vector field with its density turned up. The arrows point away from the set and grow towards its boundary:

Screenshot of the calculator showing the result of the steps described above

Because the whole iteration runs in single precision on the GPU, a very deep iteration of a chaotic map is limited by that precision; see the implicit equations page for what that means in practice.