如何在Python中将具有负值的数组提升为小数幂? [英] How to raise arrays with negative values to fractional power in Python?

查看:142
本文介绍了如何在Python中将具有负值的数组提升为小数幂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有负值的数组,该数组必须在Python中提高为小数。我需要获取操作生成的复数数组的实部。



MWE


$ b来自__future__进口部门的$ b

  
进口货号为np
a = -10
b = 2.5
n = 0.88
x = np.arange(5,11,1)
y =(a /(x-b))**(1 / n)

我正在使用Python v2.7.6。

解决方案

在使用 numpy ,您可以使用 np.power 以及类似的方法:



<$来自__future__进口部门
的numpy的p $ p> 为np
a = -10 + 0j
b = 2.5
n = 0.88
x = np。 arange(5,11,1)
y = np.power((a /(x-b)),(1 / n))

但是,实际上 ** 只是ndarray的语法糖。 pow ( ),并导致执行与 np.power 相同的代码。请参阅@acjr的评论。


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.

这篇关于如何在Python中将具有负值的数组提升为小数幂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆