What does power operator ^ returns?

View: New views
8 Messages — Rating Filter:   Alert me  

What does power operator ^ returns?

by coccoinomane :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all!

I need to compute the cubic root of many negative numbers. My problem  
is real-valued, hence I am not interested in the imaginary solutions.  
However, if I use the power operator '^', Octave returns just the  
first imaginary solution. The same applies when doing any odd root.

Example:
octave> a=-8
a = -8
octave> a^(1/3)
ans = 1.000000000000000e+00 + 1.732050807568877e+00i

How can I make octave to return the real solution, which in my example  
is just -2? I do not want just the absolute value (i.e. abs(a^(1/3)),  
since it does not preserve the sign.  I could do with a check on the  
sign, but it would be inefficient.

I am using Octave 3.2.3 on Mac Os X 10.6.

Thank you very much,

Guido
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

RE: What does power operator ^ returns?

by Richardson, Anthony :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There may be a more efficient way, but

  sign(A) .* abs(A)  .^ (1/3)

will give you what you want when A is a matrix.  
(This is the same cube root most calculators return.)

Tony Richardson

-----Original Message-----
From: Guido Walter Pettinari [mailto:coccoinomane@...]
Sent: Monday, October 19, 2009 11:42 AM
To: help-octave@...
Subject: What does power operator ^ returns?

Hi all!

I need to compute the cubic root of many negative numbers. My problem  
is real-valued, hence I am not interested in the imaginary solutions.  
However, if I use the power operator '^', Octave returns just the  
first imaginary solution. The same applies when doing any odd root.

Example:
octave> a=-8
a = -8
octave> a^(1/3)
ans = 1.000000000000000e+00 + 1.732050807568877e+00i

How can I make octave to return the real solution, which in my example  
is just -2? I do not want just the absolute value (i.e. abs(a^(1/3)),  
since it does not preserve the sign.  I could do with a check on the  
sign, but it would be inefficient.

I am using Octave 3.2.3 on Mac Os X 10.6.

Thank you very much,

Guido
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: What does power operator ^ returns?

by coccoinomane :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you Tony, I had implemented exactly the same solution and it  
works nicely.

However, I was hoping there were less computational expensive solutions.

Cheers,

Guido


On Oct 19, 2009, at 16:37 , Richardson, Anthony wrote:

> There may be a more efficient way, but
>
> sign(A) .* abs(A)  .^ (1/3)
>
> will give you what you want when A is a matrix.
> (This is the same cube root most calculators return.)
>
> Tony Richardson
>
> -----Original Message-----
> From: Guido Walter Pettinari [mailto:coccoinomane@...]
> Sent: Monday, October 19, 2009 11:42 AM
> To: help-octave@...
> Subject: What does power operator ^ returns?
>
> Hi all!
>
> I need to compute the cubic root of many negative numbers. My problem
> is real-valued, hence I am not interested in the imaginary solutions.
> However, if I use the power operator '^', Octave returns just the
> first imaginary solution. The same applies when doing any odd root.
>
> Example:
> octave> a=-8
> a = -8
> octave> a^(1/3)
> ans = 1.000000000000000e+00 + 1.732050807568877e+00i
>
> How can I make octave to return the real solution, which in my example
> is just -2? I do not want just the absolute value (i.e. abs(a^(1/3)),
> since it does not preserve the sign.  I could do with a check on the
> sign, but it would be inefficient.
>
> I am using Octave 3.2.3 on Mac Os X 10.6.
>
> Thank you very much,
>
> Guido
> _______________________________________________
> Help-octave mailing list
> Help-octave@...
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: What does power operator ^ returns?

by macy-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

this worked fine for me:

>> sign(a)*abs(a)^(1/3)

preserves the sign and only returns the real root.


> Hi all!
>
> I need to compute the cubic root of many negative numbers. My problem
> is real-valued, hence I am not interested in the imaginary solutions.
> However, if I use the power operator '^', Octave returns just the
> first imaginary solution. The same applies when doing any odd root.
>
> Example:
> octave> a=-8
> a = -8
> octave> a^(1/3)
> ans = 1.000000000000000e+00 + 1.732050807568877e+00i
>
> How can I make octave to return the real solution, which in my example
> is just -2? I do not want just the absolute value (i.e. abs(a^(1/3)),
> since it does not preserve the sign.  I could do with a check on the
> sign, but it would be inefficient.
>
> I am using Octave 3.2.3 on Mac Os X 10.6.
>
> Thank you very much,
>
> Guido
> _______________________________________________
> Help-octave mailing list
> Help-octave@...
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>


_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

RE: What does power operator ^ returns?

by dastew :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.


> From: coccoinomane@...
> Subject: What does power operator ^ returns?
> Date: Mon, 19 Oct 2009 16:42:06 +0000
> To: help-octave@...
>
> Hi all!
>
> I need to compute the cubic root of many negative numbers. My problem
> is real-valued, hence I am not interested in the imaginary solutions.
> However, if I use the power operator '^', Octave returns just the
> first imaginary solution. The same applies when doing any odd root.
>
> Example:
> octave> a=-8
> a = -8
> octave> a^(1/3)
> ans = 1.000000000000000e+00 + 1.732050807568877e+00i
>
> How can I make octave to return the real solution, which in my example
> is just -2? I do not want just the absolute value (i.e. abs(a^(1/3)),
> since it does not preserve the sign. I could do with a check on the
> sign, but it would be inefficient.
>
> I am using Octave 3.2.3 on Mac Os X 10.6.
>
> Thank you very much,
>
> Guido
> _______________________________________________
> Help-octave mailing list
> Help-octave@...
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave



if you want to see all the roots try

roots([1 0 0 8])

that will give you all the cube roots of -8

and roots([1 0 0 0 0 8])
will give you all 5,   fifth roots of -8  
:-)

Doug





_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: What does power operator ^ returns?

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Oct 19, 2009 at 6:42 PM, Guido Walter Pettinari
<coccoinomane@...> wrote:

> Hi all!
>
> I need to compute the cubic root of many negative numbers. My problem
> is real-valued, hence I am not interested in the imaginary solutions.
> However, if I use the power operator '^', Octave returns just the
> first imaginary solution. The same applies when doing any odd root.
>
> Example:
> octave> a=-8
> a = -8
> octave> a^(1/3)
> ans = 1.000000000000000e+00 + 1.732050807568877e+00i
>
> How can I make octave to return the real solution, which in my example
> is just -2? I do not want just the absolute value (i.e. abs(a^(1/3)),
> since it does not preserve the sign.  I could do with a check on the
> sign, but it would be inefficient.
>
> I am using Octave 3.2.3 on Mac Os X 10.6.
>
> Thank you very much,
>
> Guido

What you get is the standard complex power. The problem here is
consistency. In fact, 1/3 is not exactly representable, so -8^(1/3) is
not actually a cube root. We could optimize the x^(1/y) syntax so that
the expected thing happens if y is an odd integer, but that would make
x^(1/y) discontinuous w.r.t. y, so I don't think it's a good idea.
The nthroot function should calculate a real root; however, currently
it's an m-file implementation and is not more efficient than what you
can do yourself. An optimized compiled implementation would no doubt
be welcome.

regards

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: What does power operator ^ returns?

by coccoinomane :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you for all the answers! I will stick to the sign-abs trick for  
the time being, even though the 'root' solution Doug pointed out is  
really cool :)

@Jaroslav
Thank you for explaining this strange behaviour. I tried to use the  
nthroot function instead of the hat operator but, as you pointed out,  
the calculations were much slower.

Cheers,

Guido

On Oct 19, 2009, at 19:17 , <dastew@...> wrote:

>
>
> > From: coccoinomane@...
> > Subject: What does power operator ^ returns?
> > Date: Mon, 19 Oct 2009 16:42:06 +0000
> > To: help-octave@...
> >
> > Hi all!
> >
> > I need to compute the cubic root of many negative numbers. My  
> problem
> > is real-valued, hence I am not interested in the imaginary  
> solutions.
> > However, if I use the power operator '^', Octave returns just the
> > first imaginary solution. The same applies when doing any odd root.
> >
> > Example:
> > octave> a=-8
> > a = -8
> > octave> a^(1/3)
> > ans = 1.000000000000000e+00 + 1.732050807568877e+00i
> >
> > How can I make octave to return the real solution, which in my  
> example
> > is just -2? I do not want just the absolute value (i.e. abs(a^
> (1/3)),
> > since it does not preserve the sign. I could do with a check on the
> > sign, but it would be inefficient.
> >
> > I am using Octave 3.2.3 on Mac Os X 10.6.
> >
> > Thank you very much,
> >
> > Guido
> > _______________________________________________
> > Help-octave mailing list
> > Help-octave@...
> > https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>
>
>
> if you want to see all the roots try
>
> roots([1 0 0 8])
>
> that will give you all the cube roots of -8
>
> and roots([1 0 0 0 0 8])
> will give you all 5,   fifth roots of -8
> :-)
>
> Doug
>
>
>
>

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: What does power operator ^ returns?

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Oct 20, 2009 at 1:58 AM, Guido Walter Pettinari
<coccoinomane@...> wrote:

> Thank you for all the answers! I will stick to the sign-abs trick for
> the time being, even though the 'root' solution Doug pointed out is
> really cool :)
>
> @Jaroslav
> Thank you for explaining this strange behaviour. I tried to use the
> nthroot function instead of the hat operator but, as you pointed out,
> the calculations were much slower.
>
> Cheers,
>
> Guido
>

It wouldn't be hard to implement a compiled version. Someone needs to
do the work, though. I'm adding this to my list.

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave