I have an array with negative values that has to be raised to fractional power in Python. I need to obtain the real part of the complex number array generated by the operation.
MWE
from __future__ import division
import numpy as np
a = -10
b = 2.5
n = 0.88
x = np.arange(5, 11, 1)
y = (a / (x - b)) ** (1 / n)
I am using Python v2.7.6.
解决方案
As you are using numpy, you could use np.power as well like this:
from __future__ import division
import numpy as np
a = -10+0j
b = 2.5
n = 0.88
x = np.arange(5, 11, 1)
y = np.power((a / (x - b)),(1 / n))
However, actually ** of an numpy array is just syntactic sugar for ndarray.pow() and will result in the same code being executed as np.power. See @acjr's comment.