Search code examples
javamathprocessingcomplex-numbers

Processing: Library for Complex numbers?


I've just started learning Processing and I'm curious if there is a library for modeling complex numbers of the form a + bi. Particularly one that can handle multiplication of complex numbers numbers like:

(a + bi)(a + bi)



Solution

  • You can write your own class in java or be inspired by this class. Also you can import classic java libraries like common-math.

    If you need only multiplication just add this class to your sketch:

    class Complex {
        double real;   // the real part
        double img;   // the imaginary part
    
        public Complex(double real, double img) {
            this.real = real;
            this.img = img;
        }
    
        public Complex multi(Complex b) {
            double real = this.real * b.real - this.img * b.img;
            double img = this.real * b.img + this.img * b.real;
            return new Complex(real, img);
        }
    }
    

    And then simple use for you example:

    Complex first = new Complex(a, b);
    complex result =  first.multi(first);