Python operator.__div__() Examples

The following are 11 code examples of operator.__div__(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module operator , or try the search function .
Example #1
Source File: test_solve.py    From pyx with GNU General Public License v2.0 6 votes vote down vote up
def testMath(self):
        self.failUnlessEqual(str(-scalar(name="s")), "unnamed_scalar{=-1.0} * s")
        self.failUnlessEqual(str(scalar(name="s") + scalar(name="t")), "s  +  t")
        self.failUnlessEqual(str(scalar(name="s") + 1), "s  +  unnamed_scalar{=1.0}")
        self.failUnlessEqual(str(1 + scalar(name="s")), "s  +  unnamed_scalar{=1.0}")
        self.failUnlessEqual(str(scalar(name="s") - scalar(name="t")), "unnamed_scalar{=-1.0} * t  +  s")
        self.failUnlessEqual(str(1 - scalar(name="s")), "unnamed_scalar{=-1.0} * s  +  unnamed_scalar{=1.0}")
        self.failUnlessEqual(str(2 * scalar(name="s")), "s * unnamed_scalar{=2.0}")
        self.failUnlessEqual(str(scalar(name="s") * 2), "s * unnamed_scalar{=2.0}")
        self.failUnlessEqual(str(scalar(name="s") * scalar(name="t")), "s * t")
        self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * 2), "s * unnamed_scalar{=2.0}  +  t * unnamed_scalar{=2.0}")
        self.failUnlessEqual(str(scalar(name="s") / 2.0), "unnamed_scalar{=0.5} * s")
        self.failUnlessEqual(str(scalar(name="s") / 2), "unnamed_scalar{=0.0} * s") # integer logic!
        self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) / 2.0), "unnamed_scalar{=0.5} * s  +  unnamed_scalar{=0.5} * t")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, 2, scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1) + vector(1), scalar()) 
Example #2
Source File: test_solve.py    From pyx with GNU General Public License v2.0 5 votes vote down vote up
def testMath(self):
        self.failUnlessEqual(str(-vector(2, "a")), "unnamed_vector{=(unnamed_scalar{=-1.0} * a[0], unnamed_scalar{=-1.0} * a[1])}")
        self.failUnlessEqual(str(vector(2, "a") + vector(2, "b")), "unnamed_vector{=(a[0]  +  b[0], a[1]  +  b[1])}")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, 1, vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), 1)
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar(), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar() + scalar(), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), vector(3))
        self.failUnlessEqual(str(vector(2, "a") - vector(2, "b")), "unnamed_vector{=(unnamed_scalar{=-1.0} * b[0]  +  a[0], unnamed_scalar{=-1.0} * b[1]  +  a[1])}")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, 1, vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), 1)
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar(), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar() + scalar(), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), vector(3))
        self.failUnlessEqual(str(2 * vector(2, "a")), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")
        self.failUnlessEqual(str(vector(2, "a") * 2), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")
        self.failUnlessEqual(str(scalar(name="s") * vector(2, "a")), "unnamed_vector{=(a[0] * s, a[1] * s)}")
        self.failUnlessEqual(str(scalar(name="s") * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s  +  b[0] * s, a[1] * s  +  b[1] * s)}")
        self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * vector(2, "a")), "unnamed_vector{=(a[0] * s  +  a[0] * t, a[1] * s  +  a[1] * t)}")
        self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s  +  b[0] * s  +  a[0] * t  +  b[0] * t, a[1] * s  +  b[1] * s  +  a[1] * t  +  b[1] * t)}")
        self.failUnlessEqual(str(vector(2, "a") * scalar(name="s")), "unnamed_vector{=(a[0] * s, a[1] * s)}")
        self.failUnlessEqual(str(vector(2, "a") * vector(2, "b")), "a[0] * b[0]  +  a[1] * b[1]")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2, "a"), vector(3))
        self.failUnlessEqual(str(vector(2, "a") / 2.0), "unnamed_vector{=(unnamed_scalar{=0.5} * a[0], unnamed_scalar{=0.5} * a[1])}")
        self.failUnlessEqual(str(vector(2, "a") / 2), "unnamed_vector{=(unnamed_scalar{=0.0} * a[0], unnamed_scalar{=0.0} * a[1])}") # integer logic!
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), vector(1))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), vector(1))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), vector(1)) 
Example #3
Source File: test_solve.py    From pyx with GNU General Public License v2.0 5 votes vote down vote up
def testMath(self):
        self.failUnlessEqual(str(-matrix([2, 3], "A")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * A[0, 0], unnamed_scalar{=-1.0} * A[0, 1], unnamed_scalar{=-1.0} * A[0, 2]), (unnamed_scalar{=-1.0} * A[1, 0], unnamed_scalar{=-1.0} * A[1, 1], unnamed_scalar{=-1.0} * A[1, 2]))}")
        self.failUnlessEqual(str(matrix([2, 3], "A") + matrix([2, 3], "B")), "unnamed_matrix{=((A[0, 0]  +  B[0, 0], A[0, 1]  +  B[0, 1], A[0, 2]  +  B[0, 2]), (A[1, 0]  +  B[1, 0], A[1, 1]  +  B[1, 1], A[1, 2]  +  B[1, 2]))}")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), 1)
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(3))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([2, 4]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([3, 3]))
        self.failUnlessEqual(str(matrix([2, 3], "A") - matrix([2, 3], "B")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * B[0, 0]  +  A[0, 0], unnamed_scalar{=-1.0} * B[0, 1]  +  A[0, 1], unnamed_scalar{=-1.0} * B[0, 2]  +  A[0, 2]), (unnamed_scalar{=-1.0} * B[1, 0]  +  A[1, 0], unnamed_scalar{=-1.0} * B[1, 1]  +  A[1, 1], unnamed_scalar{=-1.0} * B[1, 2]  +  A[1, 2]))}")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), 1)
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(3))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([2, 4]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([3, 3]))
        self.failUnlessEqual(str(2 * matrix([2, 3], "A")), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}")
        self.failUnlessEqual(str(matrix([2, 3], "A") * 2), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}")
        self.failUnlessEqual(str(matrix([2, 3], "A") * vector(3, "a")), "unnamed_vector{=(A[0, 0] * a[0]  +  A[0, 1] * a[1]  +  A[0, 2] * a[2], A[1, 0] * a[0]  +  A[1, 1] * a[1]  +  A[1, 2] * a[2])}")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2, "a"), matrix([2, 3], "A"))
        self.failUnlessEqual(str(matrix([2, 3], "A") * matrix([3, 2], "B")), "unnamed_matrix{=((A[0, 0] * B[0, 0]  +  A[0, 1] * B[1, 0]  +  A[0, 2] * B[2, 0], A[0, 0] * B[0, 1]  +  A[0, 1] * B[1, 1]  +  A[0, 2] * B[2, 1]), (A[1, 0] * B[0, 0]  +  A[1, 1] * B[1, 0]  +  A[1, 2] * B[2, 0], A[1, 0] * B[0, 1]  +  A[1, 1] * B[1, 1]  +  A[1, 2] * B[2, 1]))}")
        self.failUnlessEqual(str(matrix([2, 3], "A") / 2.0), "unnamed_matrix{=((unnamed_scalar{=0.5} * A[0, 0], unnamed_scalar{=0.5} * A[0, 1], unnamed_scalar{=0.5} * A[0, 2]), (unnamed_scalar{=0.5} * A[1, 0], unnamed_scalar{=0.5} * A[1, 1], unnamed_scalar{=0.5} * A[1, 2]))}")
        self.failUnlessEqual(str(matrix([2, 3], "A") / 2), "unnamed_matrix{=((unnamed_scalar{=0.0} * A[0, 0], unnamed_scalar{=0.0} * A[0, 1], unnamed_scalar{=0.0} * A[0, 2]), (unnamed_scalar{=0.0} * A[1, 0], unnamed_scalar{=0.0} * A[1, 1], unnamed_scalar{=0.0} * A[1, 2]))}")
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), matrix([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), matrix([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), matrix([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), vector(1))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), matrix([2, 3])) 
Example #4
Source File: ColorDB.py    From oss-ftp with MIT License 5 votes vote down vote up
def triplet_to_fractional_rgb(rgbtuple):
    return map(operator.__div__, rgbtuple, _maxtuple) 
Example #5
Source File: lineroot.py    From backtrader with GNU General Public License v3.0 5 votes vote down vote up
def __div__(self, other):
        return self._operation(other, operator.__div__) 
Example #6
Source File: lineroot.py    From backtrader with GNU General Public License v3.0 5 votes vote down vote up
def __rdiv__(self, other):
        return self._roperation(other, operator.__div__) 
Example #7
Source File: ColorDB.py    From datafari with Apache License 2.0 5 votes vote down vote up
def triplet_to_fractional_rgb(rgbtuple):
    return map(operator.__div__, rgbtuple, _maxtuple) 
Example #8
Source File: soupy.py    From soupy with MIT License 5 votes vote down vote up
def __div__(self, other):
        return Null() 
Example #9
Source File: soupy.py    From soupy with MIT License 5 votes vote down vote up
def __div__(self, other):
        if isinstance(other, BaseNull):
            return other
        return self.map(Q / _unwrap(other)) 
Example #10
Source File: soupy.py    From soupy with MIT License 5 votes vote down vote up
def __div__(self, other):
        return BinaryOp(operator.__div__, '/', self, other) 
Example #11
Source File: test_solve.py    From pyx with GNU General Public License v2.0 4 votes vote down vote up
def testMath(self):
        self.failUnlessEqual(str(-trafo([2, 3], "A")), str(trafo((-matrix([2, 3], "A_matrix"), -vector(2, "A_vector")))))
        self.failUnlessEqual(str(trafo([2, 3], "A") + trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") + matrix([2, 3], "B_matrix"), vector(2, "A_vector") + vector(2, "B_vector")))))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, 1, trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), 1)
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar(), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar() + scalar(), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(3), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(3))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), matrix([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([2, 4]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([3, 3]))
        self.failUnlessEqual(str(trafo([2, 3], "A") - trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") - matrix([2, 3], "B_matrix"), vector(2, "A_vector") - vector(2, "B_vector")))))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, 1, trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), 1)
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar(), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar() + scalar(), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(3), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(3))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), matrix([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([2, 4]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([3, 3]))
        self.failUnlessEqual(str(trafo([2, 3], "A") * trafo([3, 4], "B")), str(trafo((matrix([2, 3], "A_matrix") * matrix([3, 4], "B_matrix"), vector(2, "A_vector") + matrix([2, 3], "A_matrix") * vector(3, "B_vector")))))
        self.failUnlessEqual(str(2 * trafo([2, 3], "A")), str(trafo((2 * matrix([2, 3], "A_matrix"), 2 * vector(2, "A_vector")))))
        self.failUnlessEqual(str(trafo([2, 3], "A") * 2), str(trafo((matrix([2, 3], "A_matrix") * 2, vector(2, "A_vector") * 2))))
        self.failUnlessEqual(str(scalar() * trafo([2, 3], "A")), str(trafo((scalar() * matrix([2, 3], "A_matrix"), scalar() * vector(2, "A_vector")))))
        self.failUnlessEqual(str(trafo([2, 3], "A") * scalar()), str(trafo((matrix([2, 3], "A_matrix") * scalar(), vector(2, "A_vector") * scalar()))))
        self.failUnlessEqual(str((scalar() + scalar()) * trafo([2, 3], "A")), str(trafo(((scalar() + scalar()) * matrix([2, 3], "A_matrix"), (scalar() + scalar()) * vector(2, "A_vector")))))
        self.failUnlessEqual(str(trafo([2, 3], "A") * (scalar() + scalar())), str(trafo((matrix([2, 3], "A_matrix") * (scalar() + scalar()), vector(2, "A_vector") * (scalar() + scalar())))))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2), trafo([2, 3]))
        self.failUnlessEqual(str(trafo([2, 3], "A") * vector(3, "a")), str(matrix([2, 3], "A_matrix") * vector(3, "a") + vector(2, "A_vector")))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), vector(2))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, matrix([3, 2]), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), matrix([3, 2]))
        self.failUnlessEqual(str(trafo([2, 3], "A") / 2.0), str(trafo((matrix([2, 3], "A_matrix") / 2.0, vector(2, "A_vector") / 2.0))))
        self.failUnlessEqual(str(trafo([2, 3], "A") / 2), str(trafo((matrix([2, 3], "A_matrix") / 2, vector(2, "A_vector") / 2))))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), trafo([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar() + scalar())
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), vector(1))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), matrix([2, 3]))
        self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), trafo([2, 3]))