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)
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
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
def testGetRing(self): self.assertEqual(imaginary.theComplexField, imaginary.Complex(1).getRing())
def testNonzero(self): a = imaginary.Complex(8.4, 5) assert a b = imaginary.Complex(rational.Rational(0), rational.Rational(0)) assert not b
def testConjugate(self): a = imaginary.Complex(1, 1) b = imaginary.Complex(1, -1) self.assertEqual(a.conjugate(), b) self.assertEqual(a, a.conjugate().conjugate())
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())