我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用operator.imul()。
def cprod(arrays, dtype = None, ignore_nan = False): """ CUDA-enabled product of a stream of arrays. Arrays are multiplied along the streaming axis for performance reasons. Parameters ---------- arrays : iterable Arrays to be multiplied. dtype : numpy.dtype, optional The type of the yielded array and of the accumulator in which the elements are summed. The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used. ignore_nan : bool, optional If True, NaNs are ignored. Default is propagation of NaNs. Yields ------ online_prod : ndarray """ return cuda_inplace_reduce(arrays, operator = imul, dtype = dtype, ignore_nan = ignore_nan, identity = 1)
def test_inplace_multiplication(self, input_tuple, expected): self.ureg.autoconvert_offset_to_baseunit = False (q1v, q1u), (q2v, q2u) = input_tuple # update input tuple with new values to have correct values on failure input_tuple = ((np.array([q1v]*2, dtype=np.float), q1u), (np.array([q2v]*2, dtype=np.float), q2u)) Q_ = self.Q_ qin1, qin2 = input_tuple q1, q2 = Q_(*qin1), Q_(*qin2) q1_cp = copy.copy(q1) if expected == 'error': self.assertRaises(OffsetUnitCalculusError, op.imul, q1_cp, q2) else: expected = np.array([expected[0]]*2, dtype=np.float), expected[1] self.assertEqual(op.imul(q1_cp, q2).units, Q_(*expected).units) q1_cp = copy.copy(q1) self.assertQuantityAlmostEqual(op.imul(q1_cp, q2), Q_(*expected), atol=0.01)
def test_inplace_multiplication_with_autoconvert(self, input_tuple, expected): self.ureg.autoconvert_offset_to_baseunit = True (q1v, q1u), (q2v, q2u) = input_tuple # update input tuple with new values to have correct values on failure input_tuple = ((np.array([q1v]*2, dtype=np.float), q1u), (np.array([q2v]*2, dtype=np.float), q2u)) Q_ = self.Q_ qin1, qin2 = input_tuple q1, q2 = Q_(*qin1), Q_(*qin2) q1_cp = copy.copy(q1) if expected == 'error': self.assertRaises(OffsetUnitCalculusError, op.imul, q1_cp, q2) else: expected = np.array([expected[0]]*2, dtype=np.float), expected[1] self.assertEqual(op.imul(q1_cp, q2).units, Q_(*expected).units) q1_cp = copy.copy(q1) self.assertQuantityAlmostEqual(op.imul(q1_cp, q2), Q_(*expected), atol=0.01)
def __imul__(self, other): if not isinstance(self._magnitude, ndarray): return self._mul_div(other, operator.mul) else: return self._imul_div(other, operator.imul)
def test_unitcontainer_arithmetic(self): x = UnitsContainer(meter=1) y = UnitsContainer(second=1) z = UnitsContainer(meter=1, second=-2) self._test_not_inplace(op.mul, x, y, UnitsContainer(meter=1, second=1)) self._test_not_inplace(op.truediv, x, y, UnitsContainer(meter=1, second=-1)) self._test_not_inplace(op.pow, z, 2, UnitsContainer(meter=2, second=-4)) self._test_not_inplace(op.pow, z, -2, UnitsContainer(meter=-2, second=4)) self._test_inplace(op.imul, x, y, UnitsContainer(meter=1, second=1)) self._test_inplace(op.itruediv, x, y, UnitsContainer(meter=1, second=-1)) self._test_inplace(op.ipow, z, 2, UnitsContainer(meter=2, second=-4)) self._test_inplace(op.ipow, z, -2, UnitsContainer(meter=-2, second=4))
def test_issue52(self): u1 = UnitRegistry() u2 = UnitRegistry() q1 = 1*u1.meter q2 = 1*u2.meter import operator as op for fun in (op.add, op.iadd, op.sub, op.isub, op.mul, op.imul, op.floordiv, op.ifloordiv, op.truediv, op.itruediv): self.assertRaises(ValueError, fun, q1, q2)
def test_imul_scalar(self): self.check_array_scalar_op(operator.imul)
def test_imul_array(self): self.check_array_array_op(operator.imul)
def test_broadcasted_imul(self): self.check_array_broadcasted_op(operator.imul)
def test_buffer(self): a = array.array(self.typecode, self.example) m = memoryview(a) expected = m.tobytes() self.assertEqual(a.tobytes(), expected) self.assertEqual(a.tobytes()[0], expected[0]) # Resizing is forbidden when there are buffer exports. # For issue 4509, we also check after each error that # the array was not modified. if support.check_impl_detail(pypy=True): # PyPy export buffers differently, and allows reallocation # of the underlying object. return self.assertRaises(BufferError, a.append, a[0]) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.extend, a[0:1]) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.remove, a[0]) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.pop, 0) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.fromlist, a.tolist()) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.frombytes, a.tobytes()) self.assertEqual(m.tobytes(), expected) if self.typecode == 'u': self.assertRaises(BufferError, a.fromunicode, a.tounicode()) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.imul, a, 2) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.imul, a, 0) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.delitem, a, 0) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.delitem, a, slice(0, 1)) self.assertEqual(m.tobytes(), expected)
def test_buffer(self): a = array.array(self.typecode, self.example) m = memoryview(a) expected = m.tobytes() self.assertEqual(a.tobytes(), expected) self.assertEqual(a.tobytes()[0], expected[0]) # Resizing is forbidden when there are buffer exports. # For issue 4509, we also check after each error that # the array was not modified. self.assertRaises(BufferError, a.append, a[0]) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.extend, a[0:1]) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.remove, a[0]) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.pop, 0) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.fromlist, a.tolist()) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, a.frombytes, a.tobytes()) self.assertEqual(m.tobytes(), expected) if self.typecode == 'u': self.assertRaises(BufferError, a.fromunicode, a.tounicode()) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.imul, a, 2) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.imul, a, 0) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.delitem, a, 0) self.assertEqual(m.tobytes(), expected) self.assertRaises(BufferError, operator.delitem, a, slice(0, 1)) self.assertEqual(m.tobytes(), expected)
def _imul_div(self, other, magnitude_op, units_op=None): """Perform multiplication or division operation in-place 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 self._magnitude = magnitude_op(self._magnitude, other_magnitude) self._units = units_op(self._units, UnitsContainer()) return self if isinstance(other, self._REGISTRY.Unit): other = 1.0 * other 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: self.ito_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.ito_root_units() self._magnitude = magnitude_op(self._magnitude, other._magnitude) self._units = units_op(self._units, other._units) return self
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)
def test_inplace(self): class C(object): def __iadd__ (self, other): return "iadd" def __iand__ (self, other): return "iand" def __ifloordiv__(self, other): return "ifloordiv" def __ilshift__ (self, other): return "ilshift" def __imod__ (self, other): return "imod" def __imul__ (self, other): return "imul" def __ior__ (self, other): return "ior" def __ipow__ (self, other): return "ipow" def __irshift__ (self, other): return "irshift" def __isub__ (self, other): return "isub" def __itruediv__ (self, other): return "itruediv" def __ixor__ (self, other): return "ixor" def __getitem__(self, other): return 5 # so that C is a sequence c = C() self.assertEqual(operator.iadd (c, 5), "iadd") self.assertEqual(operator.iand (c, 5), "iand") self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") self.assertEqual(operator.ilshift (c, 5), "ilshift") self.assertEqual(operator.imod (c, 5), "imod") self.assertEqual(operator.imul (c, 5), "imul") self.assertEqual(operator.ior (c, 5), "ior") self.assertEqual(operator.ipow (c, 5), "ipow") self.assertEqual(operator.irshift (c, 5), "irshift") self.assertEqual(operator.isub (c, 5), "isub") self.assertEqual(operator.itruediv (c, 5), "itruediv") self.assertEqual(operator.ixor (c, 5), "ixor") self.assertEqual(operator.iconcat (c, c), "iadd") self.assertEqual(operator.__iadd__ (c, 5), "iadd") self.assertEqual(operator.__iand__ (c, 5), "iand") self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv") self.assertEqual(operator.__ilshift__ (c, 5), "ilshift") self.assertEqual(operator.__imod__ (c, 5), "imod") self.assertEqual(operator.__imul__ (c, 5), "imul") self.assertEqual(operator.__ior__ (c, 5), "ior") self.assertEqual(operator.__ipow__ (c, 5), "ipow") self.assertEqual(operator.__irshift__ (c, 5), "irshift") self.assertEqual(operator.__isub__ (c, 5), "isub") self.assertEqual(operator.__itruediv__ (c, 5), "itruediv") self.assertEqual(operator.__ixor__ (c, 5), "ixor") self.assertEqual(operator.__iconcat__ (c, c), "iadd")
def test_inplace(self): class C(object): def __iadd__ (self, other): return "iadd" def __iand__ (self, other): return "iand" def __idiv__ (self, other): return "idiv" def __ifloordiv__(self, other): return "ifloordiv" def __ilshift__ (self, other): return "ilshift" def __imod__ (self, other): return "imod" def __imul__ (self, other): return "imul" def __ior__ (self, other): return "ior" def __ipow__ (self, other): return "ipow" def __irshift__ (self, other): return "irshift" def __isub__ (self, other): return "isub" def __itruediv__ (self, other): return "itruediv" def __ixor__ (self, other): return "ixor" def __getitem__(self, other): return 5 # so that C is a sequence c = C() self.assertEqual(operator.iadd (c, 5), "iadd") self.assertEqual(operator.iand (c, 5), "iand") self.assertEqual(operator.idiv (c, 5), "idiv") self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") self.assertEqual(operator.ilshift (c, 5), "ilshift") self.assertEqual(operator.imod (c, 5), "imod") self.assertEqual(operator.imul (c, 5), "imul") self.assertEqual(operator.ior (c, 5), "ior") self.assertEqual(operator.ipow (c, 5), "ipow") self.assertEqual(operator.irshift (c, 5), "irshift") self.assertEqual(operator.isub (c, 5), "isub") self.assertEqual(operator.itruediv (c, 5), "itruediv") self.assertEqual(operator.ixor (c, 5), "ixor") self.assertEqual(operator.iconcat (c, c), "iadd") self.assertEqual(operator.irepeat (c, 5), "imul") self.assertEqual(operator.__iadd__ (c, 5), "iadd") self.assertEqual(operator.__iand__ (c, 5), "iand") self.assertEqual(operator.__idiv__ (c, 5), "idiv") self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv") self.assertEqual(operator.__ilshift__ (c, 5), "ilshift") self.assertEqual(operator.__imod__ (c, 5), "imod") self.assertEqual(operator.__imul__ (c, 5), "imul") self.assertEqual(operator.__ior__ (c, 5), "ior") self.assertEqual(operator.__ipow__ (c, 5), "ipow") self.assertEqual(operator.__irshift__ (c, 5), "irshift") self.assertEqual(operator.__isub__ (c, 5), "isub") self.assertEqual(operator.__itruediv__ (c, 5), "itruediv") self.assertEqual(operator.__ixor__ (c, 5), "ixor") self.assertEqual(operator.__iconcat__ (c, c), "iadd") self.assertEqual(operator.__irepeat__ (c, 5), "imul")