Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
numpy array root operation
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Odin Den  
View profile  
 More options Mar 21 2012, 10:43 pm
From: Odin Den <hate_...@yahoo.com>
Date: Thu, 22 Mar 2012 02:43:37 +0000 (UTC)
Local: Wed, Mar 21 2012 10:43 pm
Subject: [SciPy-User] numpy array root operation
Hi,
5th root of -32 can be computed correctly as follows:

>>> -32**(1/5)
>>> -2.0

However, when I try to perform same operation on numpy arrays I get
the following:

>>> array([-32])**(1/5)
>>> array([ nan])

Is there anyway to compute roots of numpy arrays? I have a huge matrix which
contains both negative and positive values. What is the easiest way of making
python compute the "nth" roots of each element of this matrix without knowing
the value of "n" a priory?

_______________________________________________
SciPy-User mailing list
SciPy-U...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Oleksandr Huziy  
View profile  
 More options Mar 21 2012, 11:22 pm
From: Oleksandr Huziy <guziy.sa...@gmail.com>
Date: Wed, 21 Mar 2012 23:22:46 -0400
Local: Wed, Mar 21 2012 11:22 pm
Subject: Re: [SciPy-User] numpy array root operation
Maybe like this,

>>> import numpy as np
>>> x = np.array([-81,25])
>>> np.sign(x) * np.absolute(x) ** (1.0/5.0)

array([-2.40822469,  1.90365394])
>>> np.sign(x) * np.absolute(x) ** (1.0/2.0)

array([-9.,  5.])

try this way and you'll be also in trouble
(-32)**(1.0/5.0)

Cheers
--
Oleksandr Huziy

2012/3/21 Odin Den <hate_...@yahoo.com>:

_______________________________________________
SciPy-User mailing list
SciPy-U...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Warde-Farley  
View profile  
 More options Mar 21 2012, 11:31 pm
From: David Warde-Farley <warde...@iro.umontreal.ca>
Date: Wed, 21 Mar 2012 23:31:13 -0400
Local: Wed, Mar 21 2012 11:31 pm
Subject: Re: [SciPy-User] numpy array root operation
On 2012-03-21, at 10:43 PM, Odin Den wrote:

>>>> -32**(1/5)
>>>> -2.0

I just noticed that this was probably a REPL prompt (my mail client shows it as quotation indentation, which failed to register in my mind).

You have your order of operations wrong. Exponentiation is higher priority than multiplication (i.e. the unary -) so what you are getting is -1 * (32 ** (1/5)).

>>> (-32)**(1/5.)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power

_______________________________________________
SciPy-User mailing list
SciPy-U...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Friedrich Romstedt  
View profile  
 More options Mar 22 2012, 3:24 pm
From: Friedrich Romstedt <friedrichromst...@gmail.com>
Date: Thu, 22 Mar 2012 20:24:49 +0100
Local: Thurs, Mar 22 2012 3:24 pm
Subject: Re: [SciPy-User] numpy array root operation
Am 22.03.2012 um 03:43 schrieb Odin Den <hate_...@yahoo.com>:

> Hi,
> 5th root of -32 can be computed correctly as follows:
>>>> -32**(1/5)
>>>> -2.0

Warning, mathematician (physicist to be precise) speaking.

Additional to the operator precedence issue pointed out by David, notice that powers to non-integer numbers are cumbersome to define. In fact, let q be a rational number q = n/d, and v a complex number v = |v| exp(i phi), e.g. –42 with |v| = 42 and phi = pi. Then the root v^(1/d) is d-fold, and can be defined as the set of all (complex) numbers whose d-th power is v, namely the set {r exp(2 pi i f/d + i phi/d), f = 0...(d – 1)}, with a real positive number r such that r^d = |v|. The d-th power of each of this numbers is v. v^q is then just (v^(1/d))^n.

For real v phi is either 0 or pi, so for positive v phi/d in the set equation will be zero, so there's always a positive root, which we call just "root" in daily language and in Python. For negative v, phi = pi, and (2 pi)/d is just twice that large, so there's no longer a positive root. But for odd d, the term 2 pi f/d + phi/d will be, for f = (d – 1)/2, just pi, so there's then a negative root, amongst all this roots. For even d, there's neither a positive nor a negative root of v, but only d complex ones.

Notice that taking the numbers in the set to the n-th power multiplies their angle with n.

You can calculate the first root (f = 0) in Python and numpy by taking a complex number to the power, e.g. in Python (–42 + 0j) ** (0.2). But this will never be real, always complex for negative v and noninteger exponent. Notice that for numpy, the convention for phi is –pi < phi <= pi. Negative numbers have phi = pi.

The "first" root can be defined for any kind of exponent, but for irrational numbers as exponent there will be an infinite set of roots, AISI. But don't bet on that. It cannot be finite because then the irrational number would have been rational. :-)

As I warned you, mathematically inclined people can speak an hour about the most obvious thing to CS people, but normally I found it worth doing it once and then just keeping in mind that there is no such thing as natural intuition.

cu
Friedrich

> However, when I try to perform same operation on numpy arrays I get
> the following:
>>>> array([-32])**(1/5)
>>>> array([ nan])

So ya, it's Not A Number, but A Set Of Numbers. :-)

Maybe it could spit out the complex first root instead, as it will do for numpy.asarray(–42 + 0j) ** 0.2. But I'm not involved enough to be knowledgable about the design here.

> Is there anyway to compute roots of numpy arrays? I have a huge matrix which
> contains both negative and positive values. What is the easiest way of making
> python compute the "nth" roots of each element of this matrix without knowing
> the value of "n" a priory?

The other proposed approach, by using just the modulus and keeping the sign, is, as I pointed out above, not in all cases mathematically valid. I would guess you screwed up your model if you ran across taking fractional powers of negative numbers.
_______________________________________________
SciPy-User mailing list
SciPy-U...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathaniel Smith  
View profile  
 More options Mar 22 2012, 4:26 pm
From: Nathaniel Smith <n...@pobox.com>
Date: Thu, 22 Mar 2012 20:26:35 +0000
Local: Thurs, Mar 22 2012 4:26 pm
Subject: Re: [SciPy-User] numpy array root operation

On Thu, Mar 22, 2012 at 2:43 AM, Odin Den <hate_...@yahoo.com> wrote:
> Hi,
> 5th root of -32 can be computed correctly as follows:
>>>> -32**(1/5)
>>>> -2.0

> However, when I try to perform same operation on numpy arrays I get
> the following:
>>>> array([-32])**(1/5)
>>>> array([ nan])

> Is there anyway to compute roots of numpy arrays? I have a huge matrix which
> contains both negative and positive values. What is the easiest way of making
> python compute the "nth" roots of each element of this matrix without knowing
> the value of "n" a priory?

As long as you *know* that what you're computing is an odd root, and
that what you want for negative numbers is the real root, then you
could just work around this:
  roots = np.sign(a) * (a * np.sign(a))**(1./5)

-- Nathaniel
_______________________________________________
SciPy-User mailing list
SciPy-U...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »