# Can you determine the sign of the polar form of a complex number?

Discussion in 'Python' started by schaefer.mp@gmail.com, Oct 17, 2007.

1. ### Guest

To compute the absolute value of a negative base raised to a
fractional exponent such as:

z = (-3)^4.5

you can compute the real and imaginary parts and then convert to the
polar form to get the correct value:

real_part = ( 3^-4.5 ) * cos( -4.5 * pi )
imag_part = ( 3^-4.5 ) * sin( -4.5 * pi )

|z| = sqrt( real_part^2 + imag_part^2 )

Is there any way to determine the correct sign of z, or perform this
calculation in another way that allows you to get the correct value of
z expressed without imaginary parts?

For example, I can compute:

z1 = (-3)^-4 = 0,012345679
and
z3 = (-3)^-5 = -0,004115226

and I can get what the correct absolute value of z2 should be by
computing the real and imaginary parts:

|z2| = (-3)^-4.5 = sqrt( 3,92967E-18^2 + -0,007127781^2 ) =
0,007127781

but I need to know the sign.

Any help is appreciated.

but I can know the correct sign for this value.

, Oct 17, 2007

2. ### Guest

Just to clarify what I'm after:

If you plot (-3)^n where n is a set of negative real numbers between 0
and -20 for example, then you get a discontinuos line due to the
problem mentioned above with fractional exponents. However, you can
compute what the correct absolute value of the the missing points
should be (see z2 above for an example), but I would like to know how
to determine what the correct sign of z2 should be so that it fits the
graph.

, Oct 17, 2007

3. ### Roy SmithGuest

In article <>,
wrote:

> Just to clarify what I'm after:
>
> If you plot (-3)^n where n is a set of negative real numbers between 0
> and -20 for example, then you get a discontinuos line due to the
> problem mentioned above with fractional exponents. However, you can
> compute what the correct absolute value of the the missing points
> should be (see z2 above for an example), but I would like to know how
> to determine what the correct sign of z2 should be so that it fits the
> graph.

You need to ask this question on a math group. It's not a Python question
at all.

Roy Smith, Oct 17, 2007
4. ### Paul RubinGuest

writes:
> Just to clarify what I'm after:
> If you plot (-3)^n where n is a set of negative real numbers between 0

I still can't figure out for certain what you're asking, but you might
look at the article

http://en.wikipedia.org/wiki/De_Moivre's_formula

Paul Rubin, Oct 17, 2007
5. ### J. RobertsonGuest

wrote:
> Just to clarify what I'm after:
>
> If you plot (-3)^n where n is a set of negative real numbers between 0
> and -20 for example, then you get a discontinuos line due to the
> problem mentioned above with fractional exponents.
>
> ..
>

It looks like you crash-landed in imaginary space, you may want to think
again about what you're up to Complex numbers are not positive or
negative, as such.

If you want to obtain a continuous curve, then take the real part of the
complex number you obtain, as in "((-3+0j)**(-x)).real", it will fit
with what you obtain for integers.

J. Robertson, Oct 17, 2007
6. ### Gerard FlanaganGuest

On Oct 17, 3:17 pm, wrote:
> To compute the absolute value of a negative base raised to a
> fractional exponent such as:
>
> z = (-3)^4.5
>
> you can compute the real and imaginary parts and then convert to the
> polar form to get the correct value:
>
> real_part = ( 3^-4.5 ) * cos( -4.5 * pi )
> imag_part = ( 3^-4.5 ) * sin( -4.5 * pi )
>
> |z| = sqrt( real_part^2 + imag_part^2 )
>
> Is there any way to determine the correct sign of z, or perform this
> calculation in another way that allows you to get the correct value of
> z expressed without imaginary parts?
>

Your question is not clear. (There is a cmath module if that helps).

>>> z1 = complex(-3)**4.5
>>> z1

(7.7313381458154376e-014+140.29611541307906j)
>>> import cmath
>>> z2 = cmath.exp(4.5 * cmath.log(-3))
>>> z2

(7.7313381458154401e-014+140.29611541307909j)
>>>

Gerard

Gerard Flanagan, Oct 17, 2007
7. ### MatimusGuest

On Oct 17, 6:51 am, wrote:
> Just to clarify what I'm after:
>
> If you plot (-3)^n where n is a set of negative real numbers between 0
> and -20 for example, then you get a discontinuos line due to the
> problem mentioned above with fractional exponents. However, you can
> compute what the correct absolute value of the the missing points
> should be (see z2 above for an example), but I would like to know how
> to determine what the correct sign of z2 should be so that it fits the
> graph.

I know this isn't specifically what you are asking, but since you
aren't asking a Python question and this is a Python group I figure
I'm justified in giving you a slightly unrelated Python answer.

If you want to raise a negative number to a fractional exponent in
Python you simply have to make sure that you use complex numbers to
begin with:

>>> (-3+0j)**4.5

(7.7313381458154376e-014+140.29611541307906j)

Then if you want the absolute value of that, you can simply use the
abs function:

>>> x = (-3+0j)**4.5
>>> abs(x)

140.29611541307906

The absolute value will always be positive. If you want the angle you
can use atan.

>>> x = (-3+0j)**4.5
>>> math.atan(x.imag/x.real)

1.5707963267948961

I would maybe do this:

>>> def ang(x):

.... return math.atan(x.imag/x.real)

So, now that you have the angle and the magnitude, you can do this:

>>> abs(x) * cmath.exp(1j * ang(x))

(7.0894366756400186e-014+140.29611541307906j)

Which matches our original answer. Well, there is a little rounding
error because we are using floats.

So, if you have a negative magnitude, that should be exactly the same
as adding pi (180 degrees) to the angle.

>>> (-abs(x)) * cmath.exp(1j * (ang(x)+cmath.pi))

(2.5771127152718125e-014+140.29611541307906j)

Which should match our original answer. It is a little different, but
notice the magnitude of the real and imaginary parts. The real part
looks different, but is so small compared to the imaginary part that
it can almost be ignored.

Matt

Matimus, Oct 17, 2007
8. ### JasonGuest

On Oct 17, 7:51 am, wrote:
> Just to clarify what I'm after:
>
> If you plot (-3)^n where n is a set of negative real numbers between 0
> and -20 for example, then you get a discontinuos line due to the
> problem mentioned above with fractional exponents. However, you can
> compute what the correct absolute value of the the missing points
> should be (see z2 above for an example), but I would like to know how
> to determine what the correct sign of z2 should be so that it fits the
> graph.

As Roy said, a math newsgroup may be able to help you better, as you
seem to be having fundamental issues with imaginary numbers. The
imaginary part isn't an artifact of computing (-3+0j)**(-4.5), it is
an integral part of the answer. Without the imaginary part, the
result is very, very incorrect.

Actually, the graph result of (-3)^n is not necessarily discontinuous
at the intervals you specified. You just need to graph the result
with the proper number of dimensions. If you want to plot the results
of (-3)^n for n=0 to -20, you need to make a three dimensional graph,
a two dimensional graph with two sets of lines, or a circular graph
with labeled values of n.

Complex numbers can be viewed as having a magnitude and a rotation in
the real/imaginary plane. This is called polar form. Complex numbers
can also be represented using a Cartesian form, which is how Python
displays complex numbers.

Python's complex numbers allow you to extract the real or imaginary
part separately, via the "real" and "imag" attributes. To convert to
polar form, you'll need to use the abs built-in to retrieve the
magnitude, and math.atan2 to retrieve the angle. (Remember that the
imaginary part is considered the Y-axis component.)

Depending on what you're doing, you might need the real part or the
magnitude. It sounds a little bit like you're trying to represent
something as a flatlander when you should be in Spaceland. (http://
en.wikipedia.org/wiki/Flatland)

--Jason

Jason, Oct 17, 2007