Complex Module#
Complex Class#
Contains the class Complex, which implements the notion of complex number
- class complex.complex.Complex(real: Optional[Union[float, Complex]] = None, imaginary: Optional[float] = None, norm: Optional[float] = None, theta: Optional[float] = None, from_string: Optional[str] = None, from_complex: Optional[Complex] = None)#
Complex Number
- Variables:
real (float) –
imaginary (float) –
norm (float) –
theta (float) –
- __add__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> znumber2 = Complex(5, 6) >>> print(znumber + znumber2) 8.0 + 10.0i >>> znumber2 = "5 + 6i" >>> print(znumber + znumber2) 8.0 + 10.0i >>> znumber2 = 5 >>> print(znumber + znumber2) 8.0 + 4.0i
- __eq__(other: Union[int, float, Complex, str]) bool #
Example
>>> znumber = Complex(3, 4) >>> znumber2 = Complex(norm=5, theta=0.9272952180016123) >>> znumber == znumber2 True
- __iadd__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> znumber += Complex(5, 6) >>> print(znumber) 8.0 + 10.0i
- __idiv__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(norm=3, theta=4) >>> znumber /= Complex(norm=5, theta=6) >>> print(znumber.to_string("exp")) 0.6e^-2.0i
- __imul__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(norm=3, theta=4) >>> znumber *= Complex(norm=5, theta=6) >>> print(znumber.to_string("exp")) 15.0e^10.0i
- __init__(real: Optional[Union[float, Complex]] = None, imaginary: Optional[float] = None, norm: Optional[float] = None, theta: Optional[float] = None, from_string: Optional[str] = None, from_complex: Optional[Complex] = None)#
You can initialise the Complex class in one of the following ways:
By giving another complex number as first argument or a from_complex argument
By giving real and imaginary part as first two arguments
By giving explicitely norm= and theta=
By giving a mathematical expression as the from_string argument
See example below. No matter the method you choose, the real and imaginary parts along with norm and argument are set. If you modify any of those, the other 3 will be modified accordingly.
- Parameters:
real (Optional[Union[float, "Complex"]]) –
imaginary (Optional[float]) –
norm (Optional[float]) –
theta (Optional[float]) –
from_string (Optional[str]) – String representation
from_complex (Optional[Complex]) – Another complex number to copy
Examples
>>> znumber = Complex(3, 4) >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 5.0 >>> print(znumber.theta) 0.9272952180016123
>>> znumber = Complex(3, 4, 5, 0.9) Traceback (most recent call last): ... ValueError: You specified both cartesian and trigo representations but the values are not compatible >>> znumber = Complex(3, 4, 5, 0.9272952180016123)
>>> znumber = Complex(norm=5, theta=0.9272952180016123) >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 5.0 >>> print(znumber.theta) 0.9272952180016123
>>> znumber = Complex(from_string="3+4i") >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 5.0 >>> print(znumber.theta) 0.9272952180016123
>>> znumber = Complex(from_string="3") >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 0.0 >>> print(znumber.norm) 3.0 >>> print(znumber.theta) 0.0
>>> znumber = Complex(from_string="4i") >>> print(znumber.real) 0.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 4.0 >>> print(znumber.theta) 1.5707963267948966
>>> znumber = Complex(from_string="3cos(0)") >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 0.0 >>> print(znumber.norm) 3.0 >>> print(znumber.theta) 0.0
>>> znumber = Complex(from_string="4isin(1.5707963267948966)") >>> print(znumber.real) 0.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 4.0 >>> print(znumber.theta) 1.5707963267948966
>>> # In case your string can not be a complex (here we would have two different r for instance), >>> # it is the part in 'cos' that is used. >>> znumber = Complex(from_string="3cos(4) + 4isin(1)") >>> print(znumber.real) -1.960930862590836 >>> print(znumber.imaginary) -2.2704074859237844 >>> print(znumber.norm) 3.0 >>> print(znumber.theta) 4.0
>>> znumber = Complex(from_string="3 + 4 * i") >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 5.0 >>> print(znumber.theta) 0.9272952180016123
>>> znumber = Complex(from_string="5e^0.9272952180016123i") >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 5.0 >>> print(znumber.theta) 0.9272952180016123
>>> znumber = Complex(from_string="5*(cos(0.9272952180016123i) + isin(0.9272952180016123i)") >>> print(znumber.real) 3.0 >>> print(znumber.imaginary) 4.0 >>> print(znumber.norm) 5.0 >>> print(znumber.theta) 0.9272952180016123
>>> znumber_2 = Complex(from_complex=znumber) >>> print(znumber_2.real) 3.0 >>> print(znumber_2.imaginary) 4.0 >>> print(znumber_2.norm) 5.0 >>> print(znumber_2.theta) 0.9272952180016123
>>> znumber_2 = Complex(znumber) >>> print(znumber_2.real) 3.0 >>> print(znumber_2.imaginary) 4.0 >>> print(znumber_2.norm) 5.0 >>> print(znumber_2.theta) 0.9272952180016123
- __ipow__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(norm=3, theta=4) >>> znumber **= Complex(norm=5, theta=6) Traceback (most recent call last): ... TypeError: unsupported operand type(s) for ** or pow(): 'Complex' and 'Complex' >>> znumber **= "5e^6i" Traceback (most recent call last): ... TypeError: unsupported operand type(s) for ** or pow(): 'Complex' and 'str' >>> znumber **= 5 >>> print(znumber.to_string("exp")) 243.0e^20.0i
- __isub__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> znumber -= Complex(5, 6) >>> print(znumber) -2.0 - 2.0i
- __mul__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(norm=3, theta=4) >>> znumber2 = Complex(norm=5, theta=6) >>> print((znumber * znumber2).to_string("exp")) 15.0e^10.0i >>> znumber2 = "5e^6i" >>> print((znumber * znumber2).to_string("exp")) 15.0e^10.0i >>> znumber2 = 5 >>> print((znumber * znumber2).to_string("exp")) 15.0e^4.0i
- __ne__(other: Union[int, float, Complex, str]) bool #
Example
>>> znumber = Complex(3, 4) >>> znumber2 = Complex(norm=5, theta=0.9272952180016123) >>> znumber != znumber2 False
- __neg__() Complex #
Examples
>>> znumber = Complex(3, 4) >>> z2 = -znumber >>> print(z2) -3.0 - 4.0i
- __pow__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(norm=3, theta=4) >>> znumber2 = Complex(norm=5, theta=6) >>> print((znumber ** znumber2).to_string("exp")) Traceback (most recent call last): ... TypeError: unsupported operand type(s) for ** or pow(): 'Complex' and 'Complex' >>> znumber2 = "5e^6i" >>> print((znumber ** znumber2).to_string("exp")) Traceback (most recent call last): ... TypeError: unsupported operand type(s) for ** or pow(): 'Complex' and 'str' >>> znumber2 = 5 >>> print((znumber ** znumber2).to_string("exp")) 243.0e^20.0i
- __radd__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> print(1 + znumber) 4.0 + 4.0i
- __rmul__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> print(2 * znumber) 6.0 + 8.0i
- __rsub__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> print(1 - znumber) 2.0 + 4.0i
- __rtruediv__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> print(3 / znumber) 0.36 - 0.48i
- __setattr__(key, value)#
Examples
>>> z = Complex(3, 4) >>> z.norm 5.0 >>> z.q = 2 Traceback (most recent call last): ... complex.ForbiddenAssignmentError: The Complex class does not allow for new attributes assignment >>> z.real = 4 >>> z.norm 5.656854249492381 >>> z.__norm = 4 Traceback (most recent call last): ... complex.ForbiddenAssignmentError: The Complex class does not allow for new attributes assignment
- __sub__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(3, 4) >>> znumber2 = Complex(5, 6) >>> print(znumber - znumber2) -2.0 - 2.0i >>> znumber2 = "5 + 6i" >>> print(znumber - znumber2) -2.0 - 2.0i >>> znumber2 = 5 >>> print(znumber - znumber2) -2.0 + 4.0i
- __truediv__(other: Union[int, float, Complex, str]) Complex #
Examples
>>> znumber = Complex(norm=3, theta=4) >>> znumber2 = Complex(norm=5, theta=6) >>> print((znumber / znumber2).to_string("exp")) 0.6e^-2.0i >>> znumber2 = "5e^6i" >>> print((znumber / znumber2).to_string("exp")) 0.6e^-2.0i >>> znumber2 = 5 >>> print((znumber / znumber2).to_string("exp")) 0.6e^4.0i
- property cartesian#
Is true if the complex number was created from real and imaginary parts, and not from norm and thera
- ceil() Complex #
Use math.ceil method to create a new complex number
- Return type:
Examples
>>> znumber = Complex(3.123456, 4.789101112) >>> print(znumber.ceil()) 4.0 + 5.0i >>> print((znumber.ceil(repres="exp")).to_string("exp")) 6.0e^1.0i
- floor() Complex #
Use math.floor method to create a new complex number
- Return type:
Examples
>>> znumber = Complex(3.123456, 4.789101112) >>> print(znumber.floor()) 3.0 + 4.0i >>> print((znumber.floor(repres="exp")).to_string("exp")) 5.0e^0.0i
- property imaginary: float#
Imaginary part.
If modified, recomputes norm and argument by using complex.Complex.r_theta_from_ab
- property norm: float#
Norm.
If modified, recomputes real and imaginary parts by using complex.Complex.ab_from_r_theta
- plot(fig: Optional[Figure] = None, **kwargs) Figure #
Plots the complex number and returns a plotly.graph_objs.Figure object
- Parameters:
fig (Optional[Figure]) – Figure to plot in. Will create one if not specified
kwargs – Any keyword argument to pass to plotly.express.scatter (if fig is None) or plotly.graph_objects.Scatter (if fig is not None)
- Return type:
Figure
- property real: float#
Real part.
If modified, recomputes norm and argument by using complex.Complex.r_theta_from_ab
- round(rount_to: int) Complex #
Returns another complex number with rounded attributes using builting round method.
- Parameters:
rount_to (int) –
- Return type:
Examples
>>> znumber = Complex(3.123456, 4.789101112) >>> print(znumber.round(rount_to=2)) 3.12 + 4.79i >>> print((znumber.round(rount_to=2, repres="exp")).to_string("exp")) 5.72e^0.99i
- property theta: float#
Argument.
If modified, recomputes real and imaginary parts by using complex.Complex.ab_from_r_theta
- to_latex(repres: str = 'cartesian') str #
Formats the complex number into a LaTeX expression
- Parameters:
repres (str) – Can be “cartesion”, “exp” or “trigo”
- Return type:
str
Examples
>>> z = Complex(3, 3) >>> print(z.to_latex()) $3.0 + 3.0i$ >>> print(z.to_latex("trigo")) $4.242640687119285 \times (\cos(0.7853981633974483) + i \sin(0.7853981633974483))$ >>> print(z.to_latex("exp")) $4.242640687119285 \text{e}^{0.7853981633974483 i}$
- Raises:
ValueError – If given ‘repres’ argument is not “cartesian”, “trigo” nor “exp”
- to_repr(repres: str = 'cartesian') str #
str readable by exec() or eval()
- Parameters:
repres (str) – Can be “cartesion”, “exp” or “trigo”
- Return type:
str
Examples
>>> z = Complex(3, 3) >>> print(repr(z)) 3.0 + 3.0 * i >>> print(z.to_repr("trigo")) 4.242640687119285 * (cos(0.7853981633974483) + i * sin(0.7853981633974483)) >>> print(z.to_repr("exp")) 4.242640687119285 * e ** (0.7853981633974483 * i)
- Raises:
ValueError – If given ‘repres’ argument is not “cartesian”, “trigo” nor “exp”
- to_string(repres: str = 'cartesian') str #
Human-readable str
- Parameters:
repres (str) – Can be “cartesion”, “exp” or “trigo”
- Return type:
str
Examples
>>> z = Complex(3, 3) >>> print(z) 3.0 + 3.0i >>> print(z.to_string("trigo")) 4.242640687119285 * (cos(0.7853981633974483) + isin(0.7853981633974483)) >>> print(z.to_string("exp")) 4.242640687119285e^0.7853981633974483i
- Raises:
ValueError – If given ‘repres’ argument is not “cartesian”, “trigo” nor “exp”
- exception complex.complex.ForbiddenAssignmentError#
Home-made class used to tell the user that Complex does not allow for new attributes assignment. I.e, one can do ‘some_complex_number.a = 0’, but not ‘some_complex_number.foo = 0’, for ‘foo’ is not a known attribute of the class.
Functions#
Some arithmetic and trigonometric-related functions
- complex.functions.ab_from_r_theta(norm: float, theta: float) Tuple[float, float] #
Returns real and imaginary part of real complex number from its norm and argument
- Parameters:
norm (float) –
theta (float) –
- Returns:
real and imaginary parts
- Return type:
Tuple[float, float]
- complex.functions.compatible_numbers(n_1: float, n_2: float, threshold: float = 1e-08) bool #
Returns True if both numbers are equal or almost the same
- complex.functions.r_theta_from_ab(real: float, imaginary: float) Tuple[float, float] #
Returns norm and argument of a complex number from the real and imaginary parts
- Parameters:
real (float) –
imaginary (float) –
- Returns:
Norm and argument
- Return type:
Tuple[float, float]