def _mul_div(self, other, magnitude_op, units_op=None):
"""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
"""
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
magnitude = magnitude_op(self._magnitude, other_magnitude)
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()
magnitude = magnitude_op(new_self._magnitude, other._magnitude)
units = units_op(new_self._units, other._units)
return self.__class__(magnitude, units)
评论列表
文章目录