[SciPy-User] numpy array root operation

David Warde-Farley wardefar at iro.umontreal.ca
Wed Mar 21 23:26:21 EDT 2012


On 2012-03-21, at 10:43 PM, Odin Den wrote:

> 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?

I suspect not, not without writing your own function that handles negative numbers correctly in all cases. 

Note that the C standard pow() doesn't support non-integer powers on negative numbers, nor does plain Python itself. You might have luck at the symbolic/multiprecision math packages like SymPy or mpmath, which might implement the correct algorithm, but if you want to operate on arrays you probably need to write a custom function borrowing one of their algorithms and making it operate on array data.


More information about the SciPy-User mailing list