def divide(lhs, rhs):
""" Perform element-wise divide
Parameters
----------
lhs : Array or float value
left hand side operand
rhs : Array of float value
right hand side operand
Returns
-------
out: Array
result array
"""
# pylint: disable= no-member, protected-access
return _ufunc_helper(
lhs,
rhs,
_internal._div,
operator.truediv,
_internal._div_scalar,
_internal._rdiv_scalar)
# pylint: enable= no-member, protected-access
python类truediv()的实例源码
def check_truediv(Poly):
# true division is valid only if the denominator is a Number and
# not a python bool.
p1 = Poly([1,2,3])
p2 = p1 * 5
for stype in np.ScalarType:
if not issubclass(stype, Number) or issubclass(stype, bool):
continue
s = stype(5)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for stype in (int, long, float):
s = stype(5)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for stype in [complex]:
s = stype(5, 0)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for s in [tuple(), list(), dict(), bool(), np.array([1])]:
assert_raises(TypeError, op.truediv, p2, s)
assert_raises(TypeError, op.truediv, s, p2)
for ptype in classes:
assert_raises(TypeError, op.truediv, p2, ptype(1))
def __truediv__(self, trc):
return self.apply_op2(trc, operator.truediv)
def __itruediv__(self, other):
if not isinstance(self._magnitude, ndarray):
return self._mul_div(other, operator.truediv)
else:
return self._imul_div(other, operator.itruediv)
def __truediv__(self, other):
return self._mul_div(other, operator.truediv)
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_quantity_mul_div(self, unit, func):
func(op.mul, unit * 10.0, '4.2*meter', '42*meter', unit)
func(op.mul, '4.2*meter', unit * 10.0, '42*meter', unit)
func(op.mul, '4.2*meter', '10*inch', '42*meter*inch', unit)
func(op.truediv, unit * 42, '4.2*meter', '10/meter', unit)
func(op.truediv, '4.2*meter', unit * 10.0, '0.42*meter', unit)
func(op.truediv, '4.2*meter', '10*inch', '0.42*meter/inch', unit)
def test_truedivision(self, input_tuple, expected):
self.ureg.autoconvert_offset_to_baseunit = False
qin1, qin2 = input_tuple
q1, q2 = self.Q_(*qin1), self.Q_(*qin2)
input_tuple = q1, q2
if expected == 'error':
self.assertRaises(OffsetUnitCalculusError, op.truediv, q1, q2)
else:
expected = self.Q_(*expected)
self.assertEqual(op.truediv(q1, q2).units, expected.units)
self.assertQuantityAlmostEqual(op.truediv(q1, q2), expected,
atol=0.01)
def __truediv__(self, other):
fieldop = FieldOp(operator.truediv, self, other)
return R(fieldop)
def test_truediv_scalar(self):
with testing.NumpyError(divide='ignore'):
self.check_array_scalar_op(operator.truediv)
def test_rtruediv_scalar(self):
with testing.NumpyError(divide='ignore'):
self.check_array_scalar_op(operator.truediv, swap=True)
def test_truediv_array(self):
with testing.NumpyError(divide='ignore'):
self.check_array_array_op(operator.truediv)
def test_broadcasted_truediv(self):
with testing.NumpyError(divide='ignore'):
self.check_array_broadcasted_op(operator.truediv)
def test_doubly_broadcasted_truediv(self):
with testing.NumpyError(divide='ignore', invalid='ignore'):
self.check_array_doubly_broadcasted_op(operator.truediv)
def operate(self, vala, valb, oper):
"""Perform operation
args:
vala (mixed): 1st value
valb (mixed): 2nd value
oper (str): operation
returns:
mixed
"""
operation = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'=': operator.eq,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'=<': operator.le,
}.get(oper)
if operation is None:
raise SyntaxError("Unknown operation %s" % oper)
ret = operation(vala, valb)
if oper in '+-*/' and int(ret) == ret:
ret = int(ret)
return ret
def __truediv__(self, other):
return operator.truediv(self.__wrapped__, other)
def __rtruediv__(self, other):
return operator.truediv(other, self.__wrapped__)
def __rtruediv__(self, other):
return operator.truediv(other, self.__wrapped__)
exp_tree.py 文件源码
项目:Datastructure-and-Algorithm-with-Python
作者: overide
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def evaluate(t): # Evaluate a expression tree
oper = {'+':operator.add, '-':operator.sub, '*':operator.mul, '/':operator.truediv}
left = t.get_left()
right = t.get_right()
if left and right:
return oper[t.get_root()](evaluate(left),evaluate(right))
else:
return t.get_root()