我想将 a 乘以(3, 3)
np.matrix
包含(3,1)
np.matrix
数量pint
信息的 a。
此代码有效:
import numpy as np
x = np.mat([[1,0,0],[0,1,0],[0,0,1]])
y = np.mat([[1],[0],[0]])
x * y
>>> x * y
matrix([[1],
[0],
[0]])
此代码产生错误:
import numpy as np
import pint
ureg = pint.UnitRegistry()
x = np.mat([[1,0,0],[0,1,0],[0,0,1]])
y = np.mat([[1],[0],[0]]) * ureg("m")
x * y
错误是:
>>> x * y
Traceback (most recent call last):
File "<input>", line 1, in <module>
x * y
File "~/.virtualenvs/py3env/lib/python3.7/site-package
s/pint/quantity.py", line 900, in __mul__
return self._mul_div(other, operator.mul)
File "~/.virtualenvs/py3env/lib/python3.7/site-package
s/pint/quantity.py", line 75, in wrapped
result = f(self, *args, **kwargs)
File "~/.virtualenvs/py3env/lib/python3.7/site-package
s/pint/quantity.py", line 60, in wrapped
result = f(self, *args, **kwargs)
File "~/.virtualenvs/py3env/lib/python3.7/site-package
s/pint/quantity.py", line 866, in _mul_div
magnitude = magnitude_op(self._magnitude, other_magnitude)
File "~/.virtualenvs/py3env/lib/python3.7/site-package
s/numpy/matrixlib/defmatrix.py", line 215, in __mul__
return N.dot(self, asmatrix(other))
ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)
如果我使用np.dot()
我会得到一个结果,但单位已被剥离
>>> np.dot(x, y)
~/.virtualenvs/py3env/lib/python3.7/site-packages/pint/q
uantity.py:1377: UnitStrippedWarning: The unit of the quantity is stripped
.
warnings.warn("The unit of the quantity is stripped.", UnitStrippedWarni
ng)
matrix([[1],
[0],
[0]])
这是预期的行为吗?我应该能够将 NumPy 矩阵数学与pint
Quantities 一起使用吗?有没有办法做到这一点?
我正在使用 Python 3.7 Numpy == 1.15.2 Pint == 0.9