Zalgorithm

Complex number class for Processing

This is not a math tutorial. See Why am I writing about math?

https://introcs.cs.princeton.edu/java/97data/Complex.java.html

Basic outline of a complex number class:

class Complex {
  float re, im;

  Complex(float re, float im) {
    this.re = re;
    this.im = im;
  }

  Complex add(Complex other) {
    return new Complex(re + other.re, im + other.im);
  }

  Complex mult(Complex other) {
    return new Complex(
      re * other.re - im * other.im,
      re * other.im + im * other.re
    );
  }

  float magnitude() {
    return sqrt(re*re + im*im);
  }
}

With the hint that “divide() is a bit tricker. It involves multiplying by the conjugate.”

And the further hint when I Google (ask Google AI at this point), “What does it mean to multiply by the conjugate?”

Multiplying by the conjugate means changing the sing (+ to - or - to +) in a binomial expression (like a + b becomes b + a) and then multiplying, which uses the difference of the squares formula:

(a + b)(a - b) = a^2 - b^2

This process simplifies expressions, especially by removing radicals or imaginary parts from denominators (rationalizing the denominator). It eliminates the middle terms, resulting in simpler expressions, often a real number.

To simplify a complex number like 4 + 7i, multiply by its conjugate 4 - 7i:

(4+7i)(4-7i)=4^{2}-(7i)^{2}=16-49i^{2}=16-49(-1)=16+49=65