我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用operator.ipow()。
def test_exponentiation_array_exp(self): arr = np.array(range(3), dtype=np.float) q = self.Q_(arr, 'meter') for op_ in [op.pow, op.ipow]: q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, 2., q_cp) arr_cp = copy.copy(arr) arr_cp = copy.copy(arr) q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, q_cp, arr_cp) q_cp = copy.copy(q) q2_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, q_cp, q2_cp)
def test_exponentiation_array_exp_2(self): arr = np.array(range(3), dtype=np.float) #q = self.Q_(copy.copy(arr), None) q = self.Q_(copy.copy(arr), 'meter') arr_cp = copy.copy(arr) q_cp = copy.copy(q) # this fails as expected since numpy 1.8.0 but... self.assertRaises(DimensionalityError, op.pow, arr_cp, q_cp) # ..not for op.ipow ! # q_cp is treated as if it is an array. The units are ignored. # _Quantity.__ipow__ is never called arr_cp = copy.copy(arr) q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op.ipow, arr_cp, q_cp)
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_inplace_exponentiation(self, input_tuple, expected): self.ureg.default_as_delta = False in1, in2 = input_tuple if type(in1) is tuple and type(in2) is tuple: (q1v, q1u), (q2v, q2u) = in1, in2 in1 = self.Q_(*(np.array([q1v]*2, dtype=np.float), q1u)) in2 = self.Q_(q2v, q2u) elif not type(in1) is tuple and type(in2) is tuple: in2 = self.Q_(*in2) else: in1 = self.Q_(*in1) input_tuple = in1, in2 expected_copy = expected[:] for i, mode in enumerate([False, True]): self.ureg.autoconvert_offset_to_baseunit = mode in1_cp = copy.copy(in1) if expected_copy[i] == 'error': self.assertRaises((OffsetUnitCalculusError, DimensionalityError), op.ipow, in1_cp, in2) else: if type(expected_copy[i]) is tuple: expected = self.Q_(np.array([expected_copy[i][0]]*2, dtype=np.float), expected_copy[i][1]) self.assertEqual(op.ipow(in1_cp, in2).units, expected.units) else: expected = np.array([expected_copy[i]]*2, dtype=np.float) in1_cp = copy.copy(in1) self.assertQuantityAlmostEqual(op.ipow(in1_cp, in2), expected)
def check_ipow_scalar(self, xp, x_type, y_type): a = xp.array([[1, 2, 3], [4, 5, 6]], x_type) return operator.ipow(a, y_type(3))
def check_ipow_array(self, xp, x_type, y_type): a = xp.array([[1, 2, 3], [4, 5, 6]], x_type) b = xp.array([[6, 5, 4], [3, 2, 1]], y_type) return operator.ipow(a, b)
def check_broadcasted_ipow(self, xp, x_type, y_type): a = xp.array([[1, 2, 3], [4, 5, 6]], x_type) b = xp.array([[1], [2]], y_type) return operator.ipow(a, b)
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")