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

**URL:** https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073
**Category:** Coding Questions
**Created:** [October 3, 2018, 9:40am UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073 "2018-10-03T09:40:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ProsExplorer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/prosexplorer/32/1649_2.png) [@ProsExplorer](https://discourse.processing.org/u/ProsExplorer)
#### Post date: [October 3, 2018, 9:40am UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073/1 "2018-10-03T09:40:21Z")

</div>

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)?

```auto
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);
    }
}

```

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

```

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [October 3, 2018, 10:51am UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073/2 "2018-10-03T10:51:35Z")

</div>

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:

```auto
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();

```

---

<div class="post-metadata">

### Author: ![ProsExplorer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/prosexplorer/32/1649_2.png) [@ProsExplorer](https://discourse.processing.org/u/ProsExplorer)
#### Post date: [October 3, 2018, 11:22am UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073/3 "2018-10-03T11:22:13Z")

</div>

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](https://discourse.processing.org/t/exponential-grids-mobius-transformations/1347/5)]. I’ve found this example elsewhere online, but it’s written for java.

```auto
    // 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));
    }

```

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [October 3, 2018, 4:22pm UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073/4 "2018-10-03T16:22:38Z")

</div>

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

```auto
(a+bi)^n = (a+bi) * (a+bi) * (a+bi) * ... * (a+bi) // n times

```

```auto
(a+bi)^(c+di) = ... complicated...

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [October 3, 2018, 5:56pm UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073/5 "2018-10-03T17:56:06Z")

</div>

Here is the source code for a [Complex class](https://sourceforge.net/p/qscript/code/ci/master/tree/Q-script/src/org/qscript/Complex.java) 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.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 4, 2018, 5:48pm UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073/6 "2018-10-04T17:48:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [October 4, 2018, 6:43pm UTC](https://discourse.processing.org/t/how-to-multiply-a-bi-a-bi-with-this-complex-number-class/4073/7 "2018-10-04T18:43:19Z")

</div>

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.
