How to multiply (a + bi)(a + bi) with this Complex Number Class?

I found an example for complex number set multiplication, but I’m new to classes and math functions in processing. I know that the class sets up the operations, though have no idea how to use it. Can someone give an example of how to actually use this to solve the multiplication of (a + bi)(a + bi)?

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);
    }
}
Complex first = new Complex(a, b);
complex result =  first.multi(first);
1 Like

The code you have defines what a complex number is. It also defines the operations you can do with complex numbers; there are two such operations:

  • Create a complex number by specifying the real and imaginary part. This is the constructor, Complex().
  • Multiply a complex number with another complex number. This is multi().

You then have two lines of code that appear to be using these two operations. The constructor, however, is using variables a and b that are not defined. The result of squaring the first complex number, first, is then stored in the complex number called result. Sadly, this result is not displayed (nor could it be, as a and b were never defined).

I would add a describe() operation that tells you about a complex number. Then I would define the a and b variables before using them. And I would also log the description of the result.

Like so:

class Complex {
    double real;   // the real part
    double img;   // the imaginary part

    Complex(double real, double img) {
        this.real = real;
        this.img = img;
    }

    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);
    }
    
    void describe(){
      println( "" + this.real + " + " + this.img + "i" );
    }
}

int a = 2;
int b = 6;
Complex first = new Complex(a, b);
Complex result =  first.multi(first);
result.describe();
3 Likes

Ohhh okay thank you that makes a lot more sense now! It actually is functional code now ha.

Would you happen to know the operation which would be exp(a+bi,n), where n is also a defined variable?
This post had several operations for math, but not exponential operations [Exponential grids / mobius transformations]. I’ve found this example elsewhere online, but it’s written for java.

    // return a new Complex object whose value is the complex exponential of this
    public Complex exp() {
        return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
    }

Well, that depends. What is n? Is it a positive integer? Could it be a fraction? 0? Negative? Complex?

(a+bi)^n = (a+bi) * (a+bi) * (a+bi) * ... * (a+bi) // n times
(a+bi)^(c+di) = ... complicated...

Here is the source code for a Complex class that I used in my QScript library you could copy it into your sketch. It provides virtually any Complex number calculation you like including raising a complex number to the power of another complex number.

4 Likes

@quark, any reason not to just import QScript via PDE Contributions Manager and use the class directly that way?

1 Like

Importing QScript would make the Complex class available This would be the simplest way to access it, especially if the user is unfamiliar with creating and using classes in Processing.

1 Like