NumPy数组操作数的负指数 [英] Negative exponent with NumPy array operand

查看:57
本文介绍了NumPy数组操作数的负指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准电源操作( ** )不适用于负电源!当然,我可以用除法和正数来写公式.但是,我正在检查优化例程的结果,有时功效为负,有时为正.这里还是可以使用if语句,但是我想知道是否存在允许负暴露的workarouns和Python库.谢谢和问候.

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.

推荐答案

您正在使用哪个版本的python?在Python 2.6、2.7和3.2中非常适合我:

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

以及numpy 1.6.1:

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)

这篇关于NumPy数组操作数的负指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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