def test_substract(self): "2 b) test -" c1 = Complex(1, 1.5) c2 = Complex(-1, 2) sub_our = c2 - c1 sub_python = complex(c2.real, c2.imag) - complex(c1.real, c1.imag) self.assertEqual(sub_our, Complex(sub_python.real, sub_python.imag))
def test_mul_two_complex_numbers(self): "2 d) test multiplication of two complex numbers" c1 = Complex(1, 1.5) c2 = Complex(-1, 2) prod_our = c2 * c1 prod_python = complex(c2.real, c2.imag) * complex(c1.real, c1.imag) self.assertEqual(prod_our, Complex(prod_python.real, prod_python.imag))
def test_add(self): "2 b) test +" c1 = Complex(1, 1.5) c2 = Complex(-1, 2) sum_our = c2 + c1 sum_python = complex(c2.real, c2.imag) + complex(c1.real, c1.imag) self.assertEqual(sum_our, Complex(sum_python.real, sum_python.imag))
def test_mul_with_scalar(self): "2 d) test multiplication with scalar" self.assertEqual(Complex(1, 1.5) * 2, Complex(2, 3)) self.assertEqual(2.0 * Complex(1, 1.5), Complex(2, 3))
def test_to_polar(self): "2 c) test to_polar" c = Complex(1, 1) r, phi = c.to_polar() self.assertAlmostEqual(phi, pi / 4) self.assertAlmostEqual(r, sqrt(2))
def test_conjugate(self): "2 c) test conjugate" c = Complex(1, 1.5) self.assertEqual(c.conjugate(), Complex(1, -1.5))
def test_magnitude(self): "2 c) test magnitude" c = Complex(1, 1.5) self.assertEqual(c.magnitude(), Vector2D(c.real, c.imag).magnitude())
def test_equal(self): "2 b) test ==" c1 = Complex(42, 13) c2 = Complex(c1.real, c1.imag) self.assertEqual(c1, c2)
def test_init(self): "2. a) test __init__" c = Complex(42, 13) self.assertEqual(c.real, 42) self.assertEqual(c.imag, 13)