diff --git a/maths/complex/abs.py b/maths/complex/abs.py new file mode 100644 index 000000000000..150ad0ab3973 --- /dev/null +++ b/maths/complex/abs.py @@ -0,0 +1,21 @@ +from math import sqrt + +from complex import Complex + + +def complex_abs(complex_1: Complex) -> float: + """ + Computes the absolute value of a complex number. + The result is a non-negative real number, that represents the length + of the vector (real_part, imaginary_part) in R². + >>> complex_1 = Complex(-3, 4) + >>> complex_abs(complex_1) + 5.0 + """ + return sqrt(complex_1.real_part ** 2 + complex_1.imaginary_part ** 2) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/complex/complex.py b/maths/complex/complex.py new file mode 100644 index 000000000000..55c4035c9a2b --- /dev/null +++ b/maths/complex/complex.py @@ -0,0 +1,32 @@ +""" +Complex number implementation. + +Definition: + A complex number is an ordered pair of real numbers (a, b). The first + component, a, is called the real part; the second component, b, is called + the imaginary part. +""" + + +class Complex: + def __init__(self, real_part: float, imaginary_part: float) -> None: + """ + Both a and b are real numbers (float in Python). + """ + self.real_part = real_part + self.imaginary_part = imaginary_part + + def __repr__(self) -> str: + """ + Represent the complex number as a 2-tuple. + >>> complex_1 = Complex(1, 2) + >>> complex_1.__repr__() + (1, 2) + """ + return (self.real_part, self.imaginary_part) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/complex/mul.py b/maths/complex/mul.py new file mode 100644 index 000000000000..fec092f4c4c0 --- /dev/null +++ b/maths/complex/mul.py @@ -0,0 +1,24 @@ +from complex import Complex + + +def complex_mul(complex_1: Complex, complex_2: Complex) -> Complex: + """ + Multiplicate two complex numbers and return the result. + >>> complex_1 = Complex(0, 1) + >>> complex_2 = Complex(0, 1) + >>> c = complex_mul(complex_1, complex_2) + >>> c.__repr__() + (-1, 0) + """ + return Complex( + complex_1.real_part * complex_2.real_part + - complex_1.imaginary_part * complex_2.imaginary_part, + complex_1.real_part * complex_2.imaginary_part + + complex_2.real_part * complex_1.imaginary_part, + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/complex/sum.py b/maths/complex/sum.py new file mode 100644 index 000000000000..54ee09436654 --- /dev/null +++ b/maths/complex/sum.py @@ -0,0 +1,22 @@ +from complex import Complex + + +def complex_sum(complex_1: Complex, complex_2: Complex) -> Complex: + """ + Add two complex numbers and return the result. + >>> complex_1 = Complex(1, 2) + >>> complex_2 = Complex(0, -1) + >>> c = complex_sum(complex_1, complex_2) + >>> c.__repr__() + (1, 1) + """ + return Complex( + complex_1.real_part + complex_2.real_part, + complex_1.imaginary_part + complex_2.imaginary_part, + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()