# complex numbers?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
A simple web search indicates some classes are available for the simple
usage of complex numbers, providing addition, subtraction, multiplication
and division.

Yet, I don't see how one constructs the complex number to start with, or if
they can be used within arrays.

Any insights you might provide will be welcomed, as well as any
recommendations for any particular class definitions.

## Re: complex numbers?

Greg Russell wrote:

The complex number would be in the class, and how you create it would be
class specific.  I haven't had a need for this in PHP, but I have
constructed a couple on C++ and Java.  It isn't all that much work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: complex numbers?

Greg Russell schrieb:

Well, a complex number has two parts: the real and the imaginary part.
Both are floats. To construct such a number you can use two variables,
an object with two properties or an array. It's up to you.

If you want to use a class you could construct it like:

\$complex = new ComplexNumber(\$real, \$imaginary);

Hm, the four operations are simple, so I would recommend:

class ComplexNumber {
private \$r;
private \$i;

public function __construct(\$r, \$i) {
\$this->r = \$r;
\$this->i = \$i;
}

public function __tostring() {
return \$this->r . (\$this->i < 0 ? ' - ' : ' + ') . \$this->i . 'i';
}

return new ComplexNumber(\$this->r + \$c->r, \$this->i + \$c->i);
}

public function sub(ComplexNumber \$c) {
return new ComplexNumber(\$this->r - \$c->r, \$this->i - \$c->i);
}

public function mul(ComplexNumber \$c) {
return new ComplexNumber(
\$this->r * \$c->r - \$this->i * \$c->i,
\$this->r * \$c->i - \$this->i * \$c->r
);
}

public function div(ComplexNumber \$c) {
return new ComplexNumber(
\$this->r * \$c->r + \$this->i * \$c->i,
\$this->i * \$c->r + \$this->r * \$c->i
);
}
}

\$a = new ComplexNumber(5, 5);
\$b = new ComplexNumber(3, 2);

echo \$a->add(\$b); // 8 + 7i
echo \$a->sub(\$b); // 2 + 3i
...

## Re: complex numbers?

Nice class, but the multiplication and division are incorrect:

return new ComplexNumber(
\$this->r * \$c->r - \$this->i * \$c->i,
\$this->r * \$c->i + \$this->i * \$c->r
);

\$d = \$c->r * \$c->r + \$c->i * \$c->i;
return new ComplexNumber(
(\$this->r * \$c->r + \$this->i * \$c->i)/\$d,
(\$this->i * \$c->r - \$this->r * \$c->i)/\$d
);

Cheers
Tony
--
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

## Re: complex numbers?

Tony Mountifield schrieb:

You are right, thanks for the corrections. I wrote this one from scratch
and accidentally swapped the signs.

Thomas

## Re: complex numbers?

And forgot the denominator for the division...

But don't worry, many are the times I've also posted code written from
scratch while posting - it's easy to forget something til afterwards :-)

Cheers
Tony
--
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

## Re: complex numbers?

Thank you all. Now all that's needed is a Rect2Pol and Pol2Rect (rectangular
to polar etc) and AbsVal (absolute value) and everything will be grand.

## Re: complex numbers?

That's what they call "an exercise for the reader" :-)

Cheers
Tony
--
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

## Re: complex numbers?

Greg Russell wrote:

More homework assignments?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: complex numbers?

More smart-ass comments from you, as always.

## Re: complex numbers?

Greg Russell wrote:

Not at all.  But these are common homework assignments.

I did notice you didn't answer the question.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: complex numbers?

The "question" was merely more of your rhetorical bullshit as usual, not
even worthy of recognition, much less an answer. I asked nobody to compose
the Pol2Rect etc., I merely commented that they were all that were needed in
addition to the helpful class definitions offered by others, not you, of
course.

If you don't have anything useful to say Jerry, just STFU please. You're
consistently acting the big fish in the small ponds of c.l.p. and c.d.m.
Certainly you're capable of greater things, but it's apparent that you have
nothing better to do in life than to exercise your inanities in Usenet.

## Re: complex numbers?

Greg Russell wrote:

Aw, stuff it.  I just made a simple comment.  If I hit the target too

I didn't say ANYTHING about what an asshole you are.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: complex numbers?

Using the term epitomizes the low level of your intellect, for sure.

## Re: complex numbers?

Greg Russell wrote:

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: complex numbers?

No, you're acting like the child you are.