示例#1
0
 def testComparison(self):
     a = imaginary.Complex(1, 2)
     b = imaginary.Complex(2, 1)
     self.assertRaises(TypeError, a.__lt__, b)
     self.assertRaises(TypeError, a.__le__, b)
     self.assertRaises(TypeError, a.__gt__, b)
     self.assertRaises(TypeError, a.__ge__, b)
示例#2
0
 def testWithFloat(self):
     a = imaginary.Complex(8, 1)
     b = rational.Rational(1, 8)
     a_add_b = imaginary.Complex(8 + rational.Rational(1, 8), 1)
     a_mul_b = imaginary.Complex(1, rational.Rational(1, 8))
     assert a_add_b == a + b
     assert a_add_b == b + a
     assert a_mul_b == a * b
     assert a_mul_b == b * a
示例#3
0
def getRingInstance(obj):
    """
    Return a RingElement instance which eqauls 'obj'.

    Mainly for python built-in objects such as int or float.
    """
    if isinstance(obj, RingElement):
        return obj
    elif isinstance(obj, int):
        import nzmath.rational as rational
        return rational.Integer(obj)
    elif isinstance(obj, float):
        import nzmath.real as real
        return real.Real(obj)
    elif isinstance(obj, complex):
        import nzmath.imaginary as imaginary
        return imaginary.Complex(obj)
    return None
示例#4
0
 def testGetRing(self):
     self.assertEqual(imaginary.theComplexField, imaginary.Complex(1).getRing())
示例#5
0
 def testNonzero(self):
     a = imaginary.Complex(8.4, 5)
     assert a
     b = imaginary.Complex(rational.Rational(0), rational.Rational(0))
     assert not b
示例#6
0
 def testConjugate(self):
     a = imaginary.Complex(1, 1)
     b = imaginary.Complex(1, -1)
     self.assertEqual(a.conjugate(), b)
     self.assertEqual(a, a.conjugate().conjugate())
示例#7
0
 def testInverse(self):
     a = imaginary.Complex(1, 1)
     self.assertEqual(a, a.inverse().inverse())
     b = imaginary.Complex(2, 0)
     self.assertTrue(b.inverse() in real.theRealField, b.inverse())