cmath
— Mathematical functions for complex numbers¶
이 모듈은 복소수를 위한 수학 함수에 대한 액세스를 제공합니다. 이 모듈의 함수는 정수, 부동 소수점 수 또는 복소수를 인자로 받아들입니다. 이들은 또한 __complex__()
나 __float__()
메서드를 가진 임의의 파이썬 객체를 받아들일 것입니다: 이 메서드는 객체를 각각 복소수나 부동 소수점 수로 변환하기 위해 사용되며, 함수는 변환 결과에 적용됩니다.
참고
For functions involving branch cuts, we have the problem of deciding how to define those functions on the cut itself. Following Kahan’s “Branch cuts for complex elementary functions” paper, as well as Annex G of C99 and later C standards, we use the sign of zero to distinguish one side of the branch cut from the other: for a branch cut along (a portion of) the real axis we look at the sign of the imaginary part, while for a branch cut along the imaginary axis we look at the sign of the real part.
For example, the cmath.sqrt()
function has a branch cut along the
negative real axis. An argument of complex(-2.0, -0.0)
is treated as
though it lies below the branch cut, and so gives a result on the negative
imaginary axis:
>>> cmath.sqrt(complex(-2.0, -0.0))
-1.4142135623730951j
But an argument of complex(-2.0, 0.0)
is treated as though it lies above
the branch cut:
>>> cmath.sqrt(complex(-2.0, 0.0))
1.4142135623730951j
극좌표 변환¶
A Python complex number z
is stored internally using rectangular
or Cartesian coordinates. It is completely determined by its real
part z.real
and its imaginary part z.imag
. In other
words:
z == z.real + z.imag*1j
극좌표(polar coordinates)는 복소수를 나타내는 다른 방법을 제공합니다. 극좌표에서, 복소수 z는 모듈러스(modulus) r과 위상 각(phase angle) phi로 정의됩니다. 모듈러스 r은 z에서 원점까지의 거리이며, 위상 phi는 양의 x축에서 원점과 z를 잇는 선분으로의 라디안(radian)으로 측정한 반 시계 방향 각도입니다.
네이티브 직교 좌표와 극좌표 간의 변환에 다음 함수를 사용할 수 있습니다.
- cmath.phase(x)¶
Return the phase of x (also known as the argument of x), as a float.
phase(x)
is equivalent tomath.atan2(x.imag, x.real)
. The result lies in the range [-π, π], and the branch cut for this operation lies along the negative real axis. The sign of the result is the same as the sign ofx.imag
, even whenx.imag
is zero:>>> phase(complex(-1.0, 0.0)) 3.141592653589793 >>> phase(complex(-1.0, -0.0)) -3.141592653589793
참고
The modulus (absolute value) of a complex number x can be
computed using the built-in abs()
function. There is no
separate cmath
module function for this operation.
- cmath.polar(x)¶
Return the representation of x in polar coordinates. Returns a pair
(r, phi)
where r is the modulus of x and phi is the phase of x.polar(x)
is equivalent to(abs(x), phase(x))
.
- cmath.rect(r, phi)¶
Return the complex number x with polar coordinates r and phi. Equivalent to
r * (math.cos(phi) + math.sin(phi)*1j)
.
거듭제곱과 로그 함수¶
- cmath.exp(x)¶
Return e raised to the power x, where e is the base of natural logarithms.
- cmath.log(x[, base])¶
Returns the logarithm of x to the given base. If the base is not specified, returns the natural logarithm of x. There is one branch cut, from 0 along the negative real axis to -∞.
삼각 함수¶
- cmath.acos(x)¶
Return the arc cosine of x. There are two branch cuts: One extends right from 1 along the real axis to ∞. The other extends left from -1 along the real axis to -∞.
- cmath.atan(x)¶
Return the arc tangent of x. There are two branch cuts: One extends from
1j
along the imaginary axis to∞j
. The other extends from-1j
along the imaginary axis to-∞j
.
- cmath.cos(x)¶
Return the cosine of x.
- cmath.sin(x)¶
Return the sine of x.
- cmath.tan(x)¶
Return the tangent of x.
쌍곡선(hyperbolic) 함수¶
- cmath.acosh(x)¶
Return the inverse hyperbolic cosine of x. There is one branch cut, extending left from 1 along the real axis to -∞.
- cmath.asinh(x)¶
Return the inverse hyperbolic sine of x. There are two branch cuts: One extends from
1j
along the imaginary axis to∞j
. The other extends from-1j
along the imaginary axis to-∞j
.
- cmath.atanh(x)¶
Return the inverse hyperbolic tangent of x. There are two branch cuts: One extends from
1
along the real axis to∞
. The other extends from-1
along the real axis to-∞
.
- cmath.cosh(x)¶
Return the hyperbolic cosine of x.
- cmath.sinh(x)¶
Return the hyperbolic sine of x.
- cmath.tanh(x)¶
Return the hyperbolic tangent of x.
분류 함수¶
- cmath.isfinite(x)¶
Return
True
if both the real and imaginary parts of x are finite, andFalse
otherwise.버전 3.2에 추가.
- cmath.isinf(x)¶
Return
True
if either the real or the imaginary part of x is an infinity, andFalse
otherwise.
- cmath.isnan(x)¶
Return
True
if either the real or the imaginary part of x is a NaN, andFalse
otherwise.
- cmath.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)¶
a와 b 값이 서로 가까우면
True
를 반환하고, 그렇지 않으면False
를 반환합니다.Whether or not two values are considered close is determined according to given absolute and relative tolerances.
rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass
rel_tol=0.05
. The default tolerance is1e-09
, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than zero.abs_tol is the minimum absolute tolerance – useful for comparisons near zero. abs_tol must be at least zero.
If no errors occur, the result will be:
abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
.IEEE 754 특수 값
NaN
,inf
및-inf
는 IEEE 규칙에 따라 처리됩니다. 특히,NaN
은NaN
을 포함한 다른 모는 값과 가깝다고 간주하지 않습니다.inf
와-inf
는 그들 자신하고만 가깝다고 간주합니다.버전 3.5에 추가.
더 보기
PEP 485 – 근사 동등을 검사하는 함수.
상수¶
- cmath.pi¶
수학 상수 π의 float 값.
- cmath.e¶
수학 상수 e의 float 값.
- cmath.tau¶
수학 상수 τ의 float 값.
버전 3.6에 추가.
- cmath.inf¶
부동 소수점 양의 무한대.
float('inf')
와 동등합니다.버전 3.6에 추가.
- cmath.infj¶
0 실수부와 양의 무한대 허수부를 갖는 복소수.
complex(0.0, float('inf'))
와 동등합니다.버전 3.6에 추가.
- cmath.nan¶
부동 소수점 “not a number” (NaN) 값.
float('nan')
과 동등합니다.버전 3.6에 추가.
- cmath.nanj¶
0 실수부와 NaN 허수부를 갖는 복소수.
complex(0.0, float('nan'))
과 동등합니다.버전 3.6에 추가.
함수 선택은 모듈 math
에서와 유사하지만 동일하지는 않습니다. 두 개의 모듈이 있는 이유는 일부 사용자가 복소수에 관심이 없고, 어쩌면 복소수가 무엇인지 모를 수도 있기 때문입니다. 그들에게는 math.sqrt(-1)
이 복소수를 반환하기보다 예외를 발생시키는 것이 좋습니다. 또한, cmath
에 정의된 함수는, 결과를 실수로 표현할 수 있을 때도 항상 복소수를 반환합니다 (이때 복소수의 허수부는 0입니다).
분지 절단에 대한 참고 사항: 주어진 함수가 연속적이지 않은 점을 지나는 곡선입니다. 이것들은 많은 복소수 기능에서 필요한 기능입니다. 복소수 함수로 계산해야 할 때, 분지 절단에 대해 이해가 필요하다고 가정합니다. 이해를 위해서는 복소 변수에 관한 (너무 기초적이지 않은) 아무 책이나 참고하면 됩니다. 수치 계산의 목적으로 분지 절단을 적절히 선택하는 방법에 대한 정보에 대해서는, 다음과 같은 좋은 참고 문헌이 있습니다:
더 보기
Kahan, W: Branch cuts for complex elementary functions; or, Much ado about nothing’s sign bit. Iserles, A., and Powell, M. (eds.), The state of the art in numerical analysis. Clarendon Press (1987) pp165–211.