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.
Create a new complex number with the given real and imaginary parts.
real |
the real part |
imag |
the imaginary part |
a new instance of Complex.
a = Complex(2, 5);
a.real;
a.imag;
The real part of the number.
The imaginary part of the number.
the complex conjugate.
xxxxxxxxxx
Complex(2, 9).conjugate
Complex addition.
xxxxxxxxxx
Complex(2, 9) + Complex(-6, 2)
Complex subtraction
xxxxxxxxxx
Complex(2, 9) - Complex(-6, 2)
Complex multiplication
xxxxxxxxxx
Complex(2, 9) * Complex(-6, 2)
Complex division.
xxxxxxxxxx
Complex(2, 9) / Complex(-6, 2)
Complex exponentiation with base e.
xxxxxxxxxx
exp(Complex(2, 9))
xxxxxxxxxx
exp(Complex(0, pi)) == -1 // Euler's formula: true
Complex self multiplication.
xxxxxxxxxx
squared(Complex(2, 1))
complex triple self multiplication.
xxxxxxxxxx
cubed(Complex(2, 1))
Complex square root
returns the principal root
Complex(4, 1.neg).sqrt
// compare...
Complex(4, 1.neg).pow(2.reciprocal)
Complex exponentiation
not implemented for all combinations - some are mathematically ambiguous.
xxxxxxxxxx
Complex(0, 2) ** 6
xxxxxxxxxx
2.3 ** Complex(0, 2)
xxxxxxxxxx
Complex(2, 9) ** 1.2 // not defined
the comparison of just the real parts.
xxxxxxxxxx
Complex(2, 9) < Complex(5, 1);
the comparison assuming that the reals (floats) are fully embedded in the complex numbers
xxxxxxxxxx
Complex(1, 0) == 1;
Complex(1, 5) == Complex(1, 5);
negation of both parts
xxxxxxxxxx
Complex(2, 9).neg
the reciprocal of a complex number
xxxxxxxxxx
Complex(3, 4).reciprocal
1 / Complex(3, 4) // same, but less efficient
the absolute value of a complex number is its magnitude.
xxxxxxxxxx
Complex(3, 4).abs
distance to the origin.
the distance to the origin.
the angle in radians.
Convert to a Point.
Convert to a Polar
real part as Integer.
real part as Float.
returns this
a hash value
print this on given stream
Basic example:
xxxxxxxxxx
a = Complex(0, 1);
a * a; // returns Complex(-1, 0);
Julia set approximation:
xxxxxxxxxx
f = { |z| z * z + Complex(0.70176, 0.3842) };
(
var n = 80, xs = 400, ys = 400, dx = xs / n, dy = ys / n, zoom = 3, 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.drawFunc = {
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)
)