Skip to content

Complex numbers operations: sum, mul, abs #5054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions maths/complex/abs.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions maths/complex/complex.py
Original file line number Diff line number Diff line change
@@ -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()
24 changes: 24 additions & 0 deletions maths/complex/mul.py
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 22 additions & 0 deletions maths/complex/sum.py
Original file line number Diff line number Diff line change
@@ -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()