def test_add_real_part(): c1 = ComplexNumber(1, 2) c2 = ComplexNumber(3, 4) c3 = c1 + c2 assert c3.real_value == (c1.real_value + c2.real_value)
def test_multiply_complex_numbers(): c1 = ComplexNumber(1, 2) c2 = ComplexNumber(3, 4) c3 = c1 * c2 assert c3.imaginary_value == 10 assert c3.real_value == -5
def test_sub_real_part(): c1 = ComplexNumber(1, 0) c2 = ComplexNumber(3, 0) c3 = c1 - c2 assert c3.real_value == (c1.real_value - c2.real_value)
def test_mul_real_value(): c1 = ComplexNumber(1, 0) c2 = ComplexNumber(3, 0) c3 = c1 * c2 assert c3.real_value == 3
def test_sub_img_value(): c1 = ComplexNumber(1, 2) c2 = ComplexNumber(3, 4) c3 = c1 - c2 assert c3.real_value == (c1.real_value - c2.real_value) assert c3.imaginary_value == (c1.imaginary_value - c2.imaginary_value)
def test_add_method(self): a = ComplexNumber('rec', 2, 2) b = ComplexNumber('rec', 2, 2) result = a + b expected_result = ComplexNumber('rec', 4, 4) self.assertEqual(expected_result.real, result.real) self.assertEqual(expected_result.imag, result.imag)
def parsing(text): token_map = ('\+', '\-', '\*', '/', '(', ')', 'i') split_expr = rexpr.findall('[\d.]+|[%s]' % ''.join(token_map), text) complexNumber = [] allEq = [] isBracket = False for i in range(len(split_expr)): if split_expr[i] == '(': isBracket = True complex = [] w = i + 1 while split_expr[w] != ')' and split_expr[w] != 'i': if '-' == split_expr[w] or '+' == split_expr[w]: negative_number = split_expr[w] + split_expr[w + 1] complex.append(negative_number) w += 2 else: complex.append(split_expr[w]) w += 1 complexNumber.append(complex) elif split_expr[i] == ')': isBracket = False elif isBracket is False: allEq.append(split_expr[i]) Complex_number_1 = cn(float(complexNumber[0][0]), float(complexNumber[0][1])) Complex_number_2 = cn(float(complexNumber[1][0]), float(complexNumber[1][1])) if '+' == allEq[0]: print(cn.add_complex_num(Complex_number_1, Complex_number_2)) elif '-' == allEq[0]: print(cn.sub_complex_num(Complex_number_1, Complex_number_2)) elif '*' == allEq[0]: print(cn.mux_complex_num(Complex_number_1, Complex_number_2))
def test__get_cartesian_representation(self): c1 = ComplexNumber(1, 1) self.assertEqual((1, 1), c1.get_cartesian_representation()) c2 = ComplexNumber(real=None, imaginary=None, modulus=1.41, angle_degree=45) self.assertEqual((1, 1), c2.get_cartesian_representation(rounded=True))
def test_class_complex_number(): complex_number = ComplexNumber(real_value=0, imaginary_value=0) complex_number.real_value == 0 complex_number.imaginary_value == 0
def test_get_conjugate_complex_number(): complex_number = ComplexNumber(real_value=1, imaginary_value=2) con = complex_number.conjugate() assert con.real_value == 1 assert con.imaginary_value == -2
def __truediv__(self,other): real=complex(self.real_part,self.imaginary_part) imaginary=complex(other.real_part,other.imaginary_part) final=real/imaginary return ComplexNumber(final.real,final.imag)
def test_absolute_v2_complex_number(): c1 = ComplexNumber(3, 4) result = c1.absolute_v2() assert result == 5.00
def test_conjugate_real_part(): complex_number = ComplexNumber(real_value=1, imaginary_value=2) complex_number.conjugate() assert complex_number.real_value == 1
def test_phase_complex_number(): c1 = ComplexNumber(1, 2) result = c1.phase() assert result == 1.11
def test_absolute_v2_real_part(): c1 = ComplexNumber(3, 0) result = c1.absolute_v2() assert result == 3
def test_div_img_value(): c1 = ComplexNumber(1, 2) c2 = ComplexNumber(3, 4) c3 = c1 / c2 assert c3.imaginary_value == 0.08
def test_polar_complex_number(): c1 = ComplexNumber(1, 2) result = c1.polar() assert result == (1.11, 63.435)
def test_division_of_complex_number(): c1 = ComplexNumber(1, 2) c2 = ComplexNumber(3, 4) c3 = c1 / c2 assert c3.imaginary_value == 0.08 assert c3.real_value == 0.44
def test_complex_number_attributes(): complex_number = ComplexNumber(real_value=2, imaginary_value=3) assert complex_number.real_value == 2 assert complex_number.imaginary_value == 3
def test_absolute_complex_number(): c1 = ComplexNumber(1, 2) result = c1.absolute() assert result == 63.435
def test_absolute_img_part_zero_division_error(): c1 = ComplexNumber(0, 1) with pytest.raises(ZeroDivisionError): c1.absolute()
def test_absolute_real_part(): c1 = ComplexNumber(1, 0) result = c1.absolute() assert result == 0.00
def __mul__(self,other): re=self.real_part*other.real_part-self.imaginary_part*other.imaginary_part im=other.imaginary_part*self.real_part+other.real_part*self.imaginary_part return ComplexNumber(re,im)
def test_absolute_v2_definition(): c1 = ComplexNumber(0, 0) result = c1.absolute_v2() assert result == 0
def test_polar_real_part(): c1 = ComplexNumber(1, 0) result = c1.polar() assert result == (0.00, 0.00)
def test_phase_real_part(): c1 = ComplexNumber(1, 0) result = c1.phase() assert result == 0.00
def test_mul_img_value(): c1 = ComplexNumber(0, -1) c2 = ComplexNumber(0, 2) c3 = c1 * c2 assert c3.imaginary_value == 0
def test_instantiation_complex_number(): complex_number = ComplexNumber(real_value=1, imaginary_value=2) assert str(complex_number) == '1+2i'
def test_div_real_value(): c1 = ComplexNumber(1, 0) c2 = ComplexNumber(3, 0) c3 = c1 / c2 assert c3.real_value == 0.33
def test_polar_img_part_zero_division_error(): c1 = ComplexNumber(0, 1) with pytest.raises(ZeroDivisionError): c1.polar()