Code Samples





Hosted by sourceforge.net

Code Samples :: Overview

The following pages depict some of Agena's main features:
Code Samples :: Procedures, Complex Arithmetic, Conditions, and Loops

The procedure mandelbrot determines whether a point [x, y] in the complex domain is part of the famous Mandelbrot set by determining whether it leaves a certain radius after a given number of iterations.

> mandelbrot := proc(x :: number, y :: number, iter) :: number is
>    local c, z;
>    if unassigned(iter) then
>       iter := 128
>    fi;
>    z := x!y;
>    c := z;
>    for i from 0 to iter while abs(z) < 2 do
>       z := z^2 + c
>    od;
>    return i
> end;

As can be seen in this example, the function first checks whether its first two arguments x and y are numbers. If the number of iterations iter has not been passed, the if unassigned .. statement sets the default to 128.

The procedure then converts the numbers x and y to the complex number z (sixth line of the procedure), sets a fixed point c (seventh line), and iterates z until either the stop value iter is reached or the while condition abs(z) < 2 evaluates to false.

The number of iterations conducted is returned (last but one line of the procedure).

The point [0, 0] is part of the Mandelbrot set ...

> mandelbrot(0, 0):

... even after 1024 iterations:

> mandelbrot(0, 0, 1024):

   Back | Next