[英]How to mutliply a number with negative power in python
从屏幕截图中,我可以看到这是作业。
A^(-1)
import numpy as np
A = np.array([[1,2,0], [2,4,-2], [0,-2,3]])
np.linalg.inv(A)
array([[-2. , 1.5 , 1. ],
[ 1.5 , -0.75, -0.5 ],
[ 1. , -0.5 , 0. ]])
在 numpy 中,您不能通过负 integer 幂来提高整数(阅读此内容)。
在 python 中, **
运算符返回值而没有任何错误。
In [6]: A = 20
In [7]: print(A ** -1)
0.05
您也可以使用pow()
,
In [1]: A = 20
In [2]: pow(20, -1)
Out[2]: 0.05
如果您使用矩阵,最好确保它们是numpy.matrix
类型而不是更通用的numpy.ndarray
类型的实例。
import numpy as np
M = np.matrix([[ ... ]])
要将现有的通用数组转换为矩阵,您还可以将其传递给np.asmatrix()
。
一旦你有一个matrix
实例M
,获得逆矩阵的一种方法是MI
为避免“不允许整数”问题,请确保矩阵的dtype
是浮点数,而不是 integer(在对matrix()
或asmatrix()
的调用中指定 dtype dtype=float
)
要将幂作为负值插入,假设另一个变量并将其命名为“pow”并分配该负值。 现在把下面的代码。
pow = -3
value = 5**pow
print(value)
执行代码,你会看到结果。 希望能帮助到你...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.