Python 包含负值的Numpy第n个奇数根

Python 包含负值的Numpy第n个奇数根,python,numpy,scipy,Python,Numpy,Scipy,我想计算python中某些数字的第n个奇数根。Numpy作为一个立方根函数。使用该函数,我可以计算x^(1/3) 然而,如果我想以一种简单的方式计算其他k次奇数根的相同值,我有点卡住了。我不能直接使用np.power,甚至不能计算立方根: np.power(x,1./3) >>> array([ nan, nan, nan, nan, nan, 2.23144317, 3.21829795, 3.8157

我想计算python中某些数字的第n个奇数根。Numpy作为一个立方根函数。使用该函数,我可以计算x^(1/3)

然而,如果我想以一种简单的方式计算其他k次奇数根的相同值,我有点卡住了。我不能直接使用np.power,甚至不能计算立方根:

np.power(x,1./3)
>>> array([       nan,        nan,        nan,        nan,        nan,
   2.23144317, 3.21829795, 3.81571414, 4.26859722, 4.64158883])

(-100.)**(1./3)
>>> ValueError: negative number cannot be raised to a fractional power
我可以计算x的绝对值的第k个奇数根,然后相应地改变x中负项的符号,但是我想知道是否有更直接的方法。以下是我当前的解决方案:

def kth_root(x,k):
    if k % 2 != 0:
        res = np.power(np.abs(x),1./k)
        return res*np.sign(x)
    else:
        return np.power(np.abs(x),1./k)

kth_root(x,3)
>>> array([-4.64158883, -4.26859722, -3.81571414, -3.21829795, -2.23144317,
    2.23144317,  3.21829795,  3.81571414,  4.26859722,  4.64158883])

我将用我目前的解决方案回答我自己的问题。这并不是说没有更简单或更快的方法

def kth_root(x,k):
    if k % 2 != 0:
        res = np.power(np.abs(x),1./k)
        return res*np.sign(x)
    else:
        return np.power(np.abs(x),1./k)


x = np.linspace(-100,100,100)
kth_root(x,3)
>>> array([-4.64158883, -4.26859722, -3.81571414, -3.21829795, -2.23144317,
2.23144317,  3.21829795,  3.81571414,  4.26859722,  4.64158883])

我有一个类似的问题,从你开始,定义了一个函数,用正确的实数域计算有理指数的幂

def r_pow(x, n, d):
    """
    Compute x to the power of n/d (not reduced to lowest
    expression) with the correct function real domains.
    
    ARGS:
        x (int,float,array): base
        n (int)            : exponent numerator
        d (int)            : exponent denominator
        
    RETURNS:
        x to the power of n/d
    """
    
    # list to array
    if type(x) == list:
        x = np.array(x)
    # check inputs
    if type(n) != int or type(d) != int:
        raise Exception("Exponent numerator and denominator must be integers")
    # if denominator is zero
    if not d:
        raise Exception("Exponent denominator cannot be 0")
        
    # raise x to power of n
    X = x**n
    # even denominator
    if not d % 2:
        # domain is for X>=0 only
        if type(x) == np.ndarray:
            X[X<0] = np.nan
        elif X < 0:
            X = np.nan
        res = np.power(X, 1./d)
        return res
    # odd denominator
    else:
        # domain is all R
        res = np.power(np.abs(X), 1./d)
        res *= np.sign(X)
        return res


或者x^(1/3),其中域都是R,但函数符号为负,因为x比
abs(x)/x
更有效。顺便说一句,之所以需要这样做,是因为一般幂是使用对数计算的。您使用的是Python 2吗?在Python3
print(-100)**(1/3))#(2.3207944168063896+4.019733843830847j)
@DeepSpace那不是Numpy.BTW,请记住
**
操作符的优先级高于
-
@DeepSpace True,但问题被标记为Numpy,其标题以Numpy开头。所以我想他们想用Numpy
def r_pow(x, n, d):
    """
    Compute x to the power of n/d (not reduced to lowest
    expression) with the correct function real domains.
    
    ARGS:
        x (int,float,array): base
        n (int)            : exponent numerator
        d (int)            : exponent denominator
        
    RETURNS:
        x to the power of n/d
    """
    
    # list to array
    if type(x) == list:
        x = np.array(x)
    # check inputs
    if type(n) != int or type(d) != int:
        raise Exception("Exponent numerator and denominator must be integers")
    # if denominator is zero
    if not d:
        raise Exception("Exponent denominator cannot be 0")
        
    # raise x to power of n
    X = x**n
    # even denominator
    if not d % 2:
        # domain is for X>=0 only
        if type(x) == np.ndarray:
            X[X<0] = np.nan
        elif X < 0:
            X = np.nan
        res = np.power(X, 1./d)
        return res
    # odd denominator
    else:
        # domain is all R
        res = np.power(np.abs(X), 1./d)
        res *= np.sign(X)
        return res
x = np.linspace(-10, 10, 1000)
plt.plot(x, r_pow(x, 2, 3));
x = np.linspace(-10, 10, 1000)
plt.plot(x, r_pow(x, 1, 3));
x = np.linspace(-10, 10, 1000)
plt.plot(x, r_pow(x, 1, 6));