Complex complex number

superclass: Number

A class representing complex numbers.

Note that this is a simplified representation of a complex number, which does not implement the full mathematical notion of a complex number.

Creation

new(real, imag)

Create a new complex number with the given real and imaginary parts.

Accessing

<>real

The real part of the number.

<>imag

The imaginary part of the number.

Math

+ aNumber

Complex addition.

- aNumber

Complex subtraction.

* aNumber

Complex multiplication.

/ aNumber

Complex division.

< aNumber

Answer the comparision of just the real parts.

neg

Negation of both parts.

conjugate

Answer the complex conjugate.

Conversion

magnitude

Answer the distance to the origin.

rho

Answer the distance to the origin.

angle

Answer the angle in radians.

phase

Answer the angle in radians.

theta

Answer the angle in radians.

asPoint

Convert to a Point.

asPolar

Convert to a Polar

asInteger

Answer real part as Integer.

asFloat

Answer real part as Float.

// example

a = Complex(0, 1);

a * a; // returns Complex(-1, 0);

// julia set approximation

f = { |z| z * z + Complex(0.70176, 0.3842) };

(

var n = 80, xs = 400, ys = 400, dx = xs / n, dy = ys / n, zoom = 5, offset = -0.5;

var field = { |x| { |y| Complex(x / n + offset * zoom, y / n + offset * zoom) } ! n } ! n;

w = Window("Julia set", bounds:Rect(200, 200, xs, ys)).front;

w.view.background_(Color.black);

w.drawHook = {

n.do { |x|

n.do { |y|

var z = field[x][y];

z = f.(z);

field[x][y] = z;

Pen.color = Color.gray(z.rho.linlin(-100, 100, 1, 0));

Pen.addRect(

Rect(x * dx, y * dy, dx, dy)

);

Pen.fill

}

}

};

fork({ 6.do { w.refresh; 2.wait } }, AppClock)

)