Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative exponent with NumPy array operand

standard power operation (**) in Python does not work for negative power! Sure I could write the formula otherwise, with divide and positive power. However, I am checking optimization routine result, and sometimes power is negative, sometimes it is positive. Here again a if statement could do, but I am wondering if there is a workarouns and a Python library where negative exposant is allowed. Thanks and Regards.

like image 978
kiriloff Avatar asked Mar 27 '12 10:03

kiriloff


People also ask

How do you do negative exponents in Python?

Answer. Yes, in Python, exponentiation does work with negative exponent values, the same way it would apply in mathematics. When performing exponentiation with a negative exponent, it is the same as getting the inverse of exponentiation done with the exponent as a positive value.

How do you do exponents in numpy?

exp() in Python. numpy. exp(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate exponential of all the elements in the input array.

How do you negative an array in Python?

negative() function is used when we want to compute the negative of array elements. It returns element-wise negative value of an array or negative value of a scalar. Parameters : arr : [array_like or scalar] Input array.

How do you use element wise power in numpy?

First array elements raised to powers from second array, element-wise. Raise each base in x1 to the positionally-corresponding power in x2. x1 and x2 must be broadcastable to the same shape. An integer type raised to a negative integer power will raise a ValueError .


2 Answers

Which version of python are you using? Perfectly works for me in Python 2.6, 2.7 and 3.2:

>>> 3**-3 == 1.0/3**3
True

and with numpy 1.6.1:

>>> import numpy as np
>>> arr = np.array([1,2,3,4,5], dtype='float32')
>>> arr**-3 == 1/arr**3
array([ True,  True,  True,  True,  True], dtype=bool)
like image 84
dmytro Avatar answered Oct 18 '22 10:10

dmytro


It may be a Python 3 thing as I'm using 3.5.1 and I believe this is the error you have...

for c in np.arange(-5, 5):
    print(10 ** c)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-79-7232b8da64c7> in <module>()
      1 for c in np.arange(-5, 5):
----> 2     print(10 ** c)

ValueError: Integers to negative integer powers are not allowed.

Just change it to a float and it'll should work.

for c in np.arange(-5, 5):
    print(10 ** float(c))

1e-05
0.0001
0.001
0.01
0.1
1.0
10.0
100.0
1000.0
10000.0

oddly enough, it works in base python 3:

for i in range(-5, 5):
    print(10 ** i)

1e-05
0.0001
0.001
0.01
0.1
1
10
100
1000
10000

it seemed to work just fine for Python 2.7.12:

Python 2.7.12 (default, Oct 11 2016, 05:24:00) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> for c in np.arange(-5, 5):
...     print(10 ** c)
... 
1e-05
0.0001
0.001
0.01
0.1
1
10
100
1000
10000
like image 5
Paul Avatar answered Oct 18 '22 10:10

Paul