如何在python 3中使用品脱数量执行numpy矩阵乘法?

时间:2019-03-28 14:04:58

标签: python numpy multiplication pint

我想将(3, 3) np.matrix与包含(3,1)数量信息的np.matrix pint相乘。

此代码有效:

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数量吗?有办法吗?

我正在使用Python 3.7 脾气暴躁== 1.15.2 品脱== 0.9

1 个答案:

答案 0 :(得分:1)

正如hpaulj指出的那样,品脱数量类别正在将顺序切换为y * x。

这是因为pint不会为右乘 rmul 创建单独的函数,而是使用__rmul__ = __mul__

有几种解决方法

解决方案1 ​​

我能够通过修改pint / quantity.py来解决此问题,使其具有独立的自我。 rmul 功能

    def __mul__(self, other):
        return self._mul_div(other, operator.mul)

    def __rmul__(self, other):
        return self._mul_div(other, operator.mul, rmul=True)

    # __rmul__ = __mul__

并更改self._mul_div以有选择地通过两个更改交换self和other:

    @check_implemented
    @ireduce_dimensions
    def _mul_div(self, other, magnitude_op, units_op=None, rmul=False):
        """Perform multiplication or division operation and return the result.

        :param other: object to be multiplied/divided with self
        :type other: Quantity or any type accepted by :func:`_to_magnitude`
        :param magnitude_op: operator function to perform on the magnitudes
            (e.g. operator.mul)
        :type magnitude_op: function
        :param units_op: operator function to perform on the units; if None,
            *magnitude_op* is used
        :type units_op: function or None
        :param rmul: for self.__rmul__ which means the multiplication is 
        happening like:         other * self  
        rather than the normal: self. * other
        """
        print(F"self is {self} other is {other}")
        if units_op is None:
            units_op = magnitude_op

        offset_units_self = self._get_non_multiplicative_units()
        no_offset_units_self = len(offset_units_self)

        if not self._check(other):

            if not self._ok_for_muldiv(no_offset_units_self):
                raise OffsetUnitCalculusError(self._units,
                                              getattr(other, 'units', ''))
            if len(offset_units_self) == 1:
                if (self._units[offset_units_self[0]] != 1
                        or magnitude_op not in [operator.mul, operator.imul]):
                    raise OffsetUnitCalculusError(self._units,
                                                  getattr(other, 'units', ''))
            try:
                other_magnitude = _to_magnitude(other, self.force_ndarray)
            except TypeError:
                return NotImplemented

            # ++++++++++++++++++++++++++++++++++++++++++++++++
            # +++++++++++++++ Change 1 +++++++++++++++++++++++
            # ++++++++++++++++++++++++++++++++++++++++++++++++
            # magnitude = magnitude_op(self._magnitude, other_magnitude)
            op_params = (other_magnitude, self._magnitude) if rmul else (self._magnitude, other_magnitude)
            magnitude = magnitude_op(*op_params)
            # ++++++++++++++++++++++++++++++++++++++++++++++++
            # +++++++++++++++ End Change 1  ++++++++++++++++++
            # ++++++++++++++++++++++++++++++++++++++++++++++++
            units = units_op(self._units, UnitsContainer())

            return self.__class__(magnitude, units)

        if isinstance(other, self._REGISTRY.Unit):
            other = 1.0 * other

        new_self = self

        if not self._ok_for_muldiv(no_offset_units_self):
            raise OffsetUnitCalculusError(self._units, other._units)
        elif no_offset_units_self == 1 and len(self._units) == 1:
            new_self = self.to_root_units()

        no_offset_units_other = len(other._get_non_multiplicative_units())

        if not other._ok_for_muldiv(no_offset_units_other):
            raise OffsetUnitCalculusError(self._units, other._units)
        elif no_offset_units_other == 1 and len(other._units) == 1:
            other = other.to_root_units

        # ++++++++++++++++++++++++++++++++++++++++++++++++
        # +++++++++++++++ Change 2 +++++++++++++++++++++++
        # ++++++++++++++++++++++++++++++++++++++++++++++++
        # magnitude = magnitude_op(new_self._magnitude, other._magnitude)
        op_params = (other._magnitude, new_self._magnitude) if rmul else (new_self._magnitude, other._magnitude)
        magnitude = magnitude_op(*op_params)
        # ++++++++++++++++++++++++++++++++++++++++++++++++
        # +++++++++++++++ End Change 2  ++++++++++++++++++
        # ++++++++++++++++++++++++++++++++++++++++++++++++
        units = units_op(new_self._units, other._units)

        return self.__class__(magnitude, units)

解决方案2

如果将x设为无量纲品脱数量,则乘法将以正确的顺序给出。

import numpy as np
import pint
ureg = pint.UnitRegistry()
x = np.mat([[1,0,0],[0,1,0],[0,0,1]]) *ureg("")
y = np.mat([[1],[0],[0]]) * ureg("m")
>>> x * y
<Quantity([[1]
 [0]
 [0]], 'meter')>