While reading "Introduction to Mathematics Starting with Python" sold at O'Reilly Japan, I will summarize the points that I was particularly interested in. https://www.oreilly.co.jp/books/9784873117683/
The fractions module is available for handling fractions in Python.
fractions_.py
from fractions import Fraction
a = Fraction(1, 2) # Fraction(1, 2)
b = a + 1 + 1.5 # 3.0
Fractions are available in the form of Fraction (numerator, denominator). If there is a floating point in the middle of the expression, the result is returned in floating point. The fractions module supports rational number calculations.
When dealing with complex numbers in Python, use the letters "j" or "J". (In the field of electrical engineering, "i" or "I" is used as a symbol for electric current.) The complex number 1 + 2i is written as 1 + 2j.
complex_.py
a = 1 + 2j
b = complex(3, 4) # (3+4j)
a + b # (4+6j)
a - b # (-2+2j)
a.real #1 Real part
a.imag #2 imaginary part
c = a.conjugate() # (1-2j)Conjugate complex number
d = abs(a) # 2.23606797749979 Complex size
Complex numbers can be defined using the complex () function. It can be defined by assigning variables such as complex (x, y). A conjugate complex number is a complex number that has the same real part and the opposite sign of the imaginary part. It can be obtained using the conjugate () method. The magnitude of a complex number can be determined by the abs () function. A real number returns an absolute value, but a complex number returns a magnitude.
cmath You can use other functions that handle complex numbers by using the module.
cmath_.py
from cmath import *
a = 1 + 1j
phase(a) # 0.7853981633974483 Returns the argument in floating point. The return value is[-π, π]Range of.
polar(a) # (1.4142135623730951, 0.7853981633974483)It can be converted from Cartesian coordinates to polar coordinates.(x, y) → (r, Θ)
Recommended Posts