Error in Complex

Hello,

There seems to be a problem in Complex. It gets (-i)^2/3 wrong.

2/3
i = 1/2 + sqrt(3)/2

2/3
(-i) = - 1

With Ruby:
$ irb

require ‘complex’
=> true
Complex(0,1)(2.0/3) # i^2/3
=> Complex(0.5000000000000001, 0.8660254037844386)
Complex(0,-1)
(2.0/3) # (-i)^2/3
=> Complex(0.5000000000000001, -0.8660254037844386)

The weird thing is that Maple gets this wrong too.
I tried to use Mathematica, but I just can’t get the darn thing to work.
So I don’t know what Mathematica gets.

Does anyone know what’s going on?

···


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

There seems to be a problem in Complex. It gets (-i)^2/3 wrong.

[snip]

The weird thing is that Maple gets this wrong too.

···

----- Original Message -----
From: “Daniel Carrera” dcarrera@math.umd.edu


Well, if you can get the Maple people to change this, let us know! :slight_smile:
Doing like Maple does it seems like a pretty good bet.

Anyway, how do you propose we define x^2/3?

Anyway, this seems like the right answer to me. For one thing, I expect
f(x), where f is a complex function defined with real coefficients,
exponents, etc. to be symmetric about the real number line. In other words:

f(conj(x)) = conj(f(x))

where `conj(x)’ is the complex conjugate of x (which is, of course, a field
isomorphism of C… that’s my point).

Chris

-i = exp(-i * pi/2)
(-i)^(2/3) = (1)^2/3 * exp(-i * pi/2 * 2/3)
= exp(-i * pi/3)
= cos(pi/3) - i sin(pi/3)
= 1/2 - sqrt(3)/2

martin

···

Daniel Carrera dcarrera@math.umd.edu wrote:

Hello,

There seems to be a problem in Complex. It gets (-i)^2/3 wrong.

2/3
i = 1/2 + sqrt(3)/2

2/3
(-i) = - 1

require ‘complex’
=> true
irb(main):005:0> Complex(0.0,1.0)**(2.0/3.0)
=> Complex(0.5000000000000001, 0.8660254037844386)

Looks ok to me.

···

On Thursday, 6 February 2003 at 7:08:37 +0900, Martin DeMello wrote:

Daniel Carrera dcarrera@math.umd.edu wrote:

Hello,

There seems to be a problem in Complex. It gets (-i)^2/3 wrong.

2/3
i = 1/2 + sqrt(3)/2

2/3
(-i) = - 1

-i = exp(-i * pi/2)
(-i)^(2/3) = (1)^2/3 * exp(-i * pi/2 * 2/3)
= exp(-i * pi/3)
= cos(pi/3) - i sin(pi/3)
= 1/2 - sqrt(3)/2

martin


Jim Freeze

Egotist, n.:
A person of low taste, more interested in himself than me.
– Ambrose Bierce, “The Devil’s Dictionary”

Hello,

There seems to be a problem in Complex. It gets (-i)^2/3 wrong.

2/3
i = 1/2 + sqrt(3)/2

2/3
(-i) = - 1

-i = exp(-i * pi/2)
(-i)^(2/3) = (1)^2/3 * exp(-i * pi/2 * 2/3)
= exp(-i * pi/3)
= cos(pi/3) - i sin(pi/3)
= 1/2 - sqrt(3)/2

OK, I admit I’m showing my ignorance here.
How do you reconcile that with:

(-i)**(2/3.0)
((-i)**2)**3.0)
(-1)**3.0
-1

??
Hal

···

----- Original Message -----
From: “Martin DeMello” martindemello@yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, February 05, 2003 4:08 PM
Subject: Re: Error in Complex

Daniel Carrera dcarrera@math.umd.edu wrote:

Martin,

I can see that your answer is right. Perhaps you can show me what I’m
doing wrong. My colleagues and I can’t figure it out:

-i = exp(i3pi/2)

2/3
(-i) = exp(i * 3pi/2 *(2/3))
= exp(i * pi)
= -1

What am I doing wrong?

···

On Thu, Feb 06, 2003 at 07:08:37AM +0900, Martin DeMello wrote:

Daniel Carrera dcarrera@math.umd.edu wrote:

Hello,

There seems to be a problem in Complex. It gets (-i)^2/3 wrong.

2/3
i = 1/2 + sqrt(3)/2

2/3
(-i) = - 1

-i = exp(-i * pi/2)
(-i)^(2/3) = (1)^2/3 * exp(-i * pi/2 * 2/3)
= exp(-i * pi/3)
= cos(pi/3) - i sin(pi/3)
= 1/2 - sqrt(3)/2

martin


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

-i = exp(-i * pi/2)

···

----- Original Message -----
From: “Martin DeMello” martindemello@yahoo.com


But I think what Daniel was saying is that:

-i = exp(i * 3pi/2)

also.

Chris

Sorry to amend my post, but what I meant
of course was:
(-i)(2/3.0)
((-i)2)(1/3.0)
(-1)
(1/3.0)
-1

Hal

···

----- Original Message -----
From: “Hal E. Fulton” hal9000@hypermetrics.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, February 05, 2003 4:19 PM
Subject: Re: Error in Complex

----- Original Message -----
From: “Martin DeMello” martindemello@yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, February 05, 2003 4:08 PM
Subject: Re: Error in Complex

Daniel Carrera dcarrera@math.umd.edu wrote:

Hello,

There seems to be a problem in Complex. It gets (-i)^2/3 wrong.

2/3
i = 1/2 + sqrt(3)/2

2/3
(-i) = - 1

-i = exp(-i * pi/2)
(-i)^(2/3) = (1)^2/3 * exp(-i * pi/2 * 2/3)
= exp(-i * pi/3)
= cos(pi/3) - i sin(pi/3)
= 1/2 - sqrt(3)/2

OK, I admit I’m showing my ignorance here.
How do you reconcile that with:

(-i)**(2/3.0)
((-i)**2)**3.0)
(-1)**3.0
-1

??
Hal

What am I doing wrong?

···

----- Original Message -----

Nothing. There are three reasonable answers. They are the cube roots
of -1.

When you raise x to the a/b power (a/b being a reduced fraction), expect b
answers.

Chris

-i = exp(-i * pi/2)
(-i)^(2/3) = (1)^2/3 * exp(-i * pi/2 * 2/3)
= exp(-i * pi/3)
= cos(pi/3) - i sin(pi/3)
= 1/2 - sqrt(3)/2

OK, I admit I’m showing my ignorance here.
How do you reconcile that with:

(-i)**(2/3.0)
((-i)**2)**3.0)
^^^^^^
((-i)^2)^(1/3)

···

On Thu, Feb 06, 2003 at 07:19:00AM +0900, Hal E. Fulton wrote:

(-1)**3.0
-1

??
Hal


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Problem solving under Linux has never been the circus that it is under
AIX.
– Pete Ehlke in comp.unix.aix

Nothing, really.

Remember that z^x is multivalued in general - but the primary value is
the one obtained when -pi/2 <= theta <= pi/2

(we have z = r exp(i theta) = r exp i(theta + 2npi)
z ^ x = r^x exp(ixtheta + i2nxpi) = r^x [exp(ixtheta) * exp(i 2npi x)]

and for noninteger x, the second term is not 1. In your case, it’s
exp(4/3 i pi) which accounts for the difference)

martin

···

Daniel Carrera dcarrera@math.umd.edu wrote:

Martin,

I can see that your answer is right. Perhaps you can show me what I’m
doing wrong. My colleagues and I can’t figure it out:

-i = exp(i3pi/2)

2/3
(-i) = exp(i * 3pi/2 *(2/3))
= exp(i * pi)
= -1

What am I doing wrong?

^^^
1/3.0

= cbrt(-1), if your arithmetic is exact, and there are three cube roots
of a complex number. See my answer to Daniel’s post for more detail.

martin

···

Hal E. Fulton hal9000@hypermetrics.com wrote:

OK, I admit I’m showing my ignorance here.
How do you reconcile that with:

(-i)**(2/3.0)
((-i)**2)**3.0)

Nothing :slight_smile: My TI says that (-1)^(3/2) = -i .
So does (1/2-sqrt(3)/2).
For (1/2+sqrt(3)/2) ^(3/2) it says +i because it is doing
(1/2+sqrt(3)/2)^3 == -1
and then taking the square root and only considering one of the 2 values :slight_smile:

See:

(-i)^(2/3) = exp(i*(3pi/2 - 2Pi) * 2/3)
= exp(i*(pi -4pi/3))
= exp(-i*pi/3)
= 1/2 - sqrt(3)/2

(-i)^(2/3) = exp(i*(3pi/2 + 2Pi) * 2/3)
= exp(i*(pi +4pi/3))
= exp(+i*pi/3)
= 1/2 + sqrt(3)/2

1 has in n n-order roots. You must add their arguments to that of your
(only) result.

Consider the following:

(-i)^(2/3) = ((-i) ^ (1/3)) ^ 2

It turns out that
a^(1/3) = |a|^(1/3) * exp( j * 2PI / 3 * n + j * arg(a)/3 ) with n = 0,1,2

In this case
(-i) ^ (1 / 3) = 1 * exp ( j * -PI/6 + j * 2PI/3*n )
( ) ^2 = exp(j (-pi/3))
exp(j)
exp(j (pi/3))

You can also see it graphically:

(-i) ^(2/3)
means the cube (?) root of -1

If I could draw here, I’d point at the 3 cube (is that the terminology
in English?) roots of the “minus unity” :slight_smile:

What should a well-behaved Complex library do about multiple roots? It
even happens for 1!

sqrt(1) ===> 1 taken by convention
-1 silently discarded

PS: eqs.gsub!(/j/, “i”)
My EE background haunts me :slight_smile:

···

On Thu, Feb 06, 2003 at 07:20:33AM +0900, Daniel Carrera wrote:

Martin,

I can see that your answer is right. Perhaps you can show me what I’m
doing wrong. My colleagues and I can’t figure it out:

-i = exp(i3pi/2)

2/3
(-i) = exp(i * 3pi/2 *(2/3))
= exp(i * pi)
= -1

What am I doing wrong?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

‘Ooohh… “FreeBSD is faster over loopback, when compared to Linux
over the wire”. Film at 11.’
– Linus Torvalds

Oh, sure. I think that sums it up.

Thanks,
Hal

···

----- Original Message -----
From: “Chris Pine” nemo@hellotree.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, February 05, 2003 4:32 PM
Subject: Re: Error in Complex

----- Original Message -----
What am I doing wrong?

Nothing. There are three reasonable answers. They are the cube roots
of -1.

When you raise x to the a/b power (a/b being a reduced fraction), expect b
answers.

1 has in n n-order roots.

[Correct explanation]

What should a well-behaved Complex library do about multiple roots?

There’s a convention which result to choose when doing exponentiation.
It’s an extension of the convention that sqrt(4) returns 2 and not -2. See
your favourite Math Handbook.

I’m not doing exponentiation of complexes with non-integer exponents, so I
have no intention to investigate if complex.rb does this right (but I
guess it does).

A quick preliminary test might be to compare test calculation results
against maple, matlab, octave, other standard math tools (“select isn’t
broken”).

Here’s a real Error in complex.rb:

Complex(0,0) == :symbol

raises an exception instead of returning false

PS: eqs.gsub!(/j/, “i”)
My EE background haunts me :slight_smile:

What was their stated reason for prefering j over i?

T

···

On Thu, 6 Feb 2003, Mauricio Fernández wrote:

Hi,

···

In message “Re: Error in Complex” on 03/02/06, Tobias Peters tpeters@invalid.uni-oldenburg.de writes:

Here’s a real Error in complex.rb:

Complex(0,0) == :symbol

raises an exception instead of returning false

Indeed. I will fix this, and rational.rb as well.

						matz.

We do already use i for current intensity.

···

On Thu, Feb 06, 2003 at 06:30:12PM +0900, Tobias Peters wrote:

Here’s a real Error in complex.rb:

Complex(0,0) == :symbol

raises an exception instead of returning false

PS: eqs.gsub!(/j/, “i”)
My EE background haunts me :slight_smile:

What was their stated reason for prefering j over i?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

<|ryan|> I don’t use deb
u poor man
netgod: heh
apt-get install task-p0rn

i is already used in electrical and electronic disciplines to represent
current (flow, strength etc). Which means that EE courses all use j, whilst
the pure maths courses I’m taking use i, and I have to do mental backflips
when I go from one to the other. Especially since electrical engineering uses
complex numbers quite extensively, you come up with these expressions with
both i and j in them, and have to concentrate quite hard to remember which is
which.

Tim Bates

···

On Thu, 6 Feb 2003 08:00 pm, Tobias Peters wrote:

What was their stated reason for prefering j over i?


tim@bates.id.au