cmath — 복소수를 위한 수학 함수


This module provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. They will also accept any Python object that has either a __complex__() or a __float__() method: these methods are used to convert the object to a complex or floating-point number, respectively, and the function is then applied to the result of the conversion.

참고

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

극좌표 변환

파이썬 복소수 z직교 혹은 데카르트 좌표를 사용하여 내부적으로 저장됩니다. 실수부 z.real허수부 z.imag에 의해 완전히 결정됩니다. 다시 말해:

z == z.real + z.imag*1j

극좌표(polar coordinates)는 복소수를 나타내는 다른 방법을 제공합니다. 극좌표에서, 복소수 z는 모듈러스(modulus) r과 위상 각(phase angle) phi로 정의됩니다. 모듈러스 rz에서 원점까지의 거리이며, 위상 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 to math.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 of x.imag, even when x.imag is zero:

>>> phase(complex(-1.0, 0.0))
3.141592653589793
>>> phase(complex(-1.0, -0.0))
-3.141592653589793

참고

복소수 x의 모듈러스(절댓값)는 내장 abs() 함수를 사용하여 계산할 수 있습니다. 이 연산을 위한 별도의 cmath 모듈 함수는 없습니다.

cmath.polar(x)

x 표현을 극좌표로 반환합니다. 쌍 (r, phi)를 반환합니다. 여기서 rx의 모듈러스이고 phi는 x의 위상입니다. polar(x)(abs(x), phase(x))와 동등합니다.

cmath.rect(r, phi)

극좌표 rphi를 가지는 복소수 x를 반환합니다. r * (math.cos(phi) + math.sin(phi)*1j)와 동등합니다.

거듭제곱과 로그 함수

cmath.exp(x)

ex 거듭제곱을 반환합니다. 여기서 e는 자연로그(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.log10(x)

x의 밑이 10인 로그를 반환합니다. 이것은 log()와 같은 분지 절단을 가집니다.

cmath.sqrt(x)

x의 제곱근을 반환합니다. 이것은 log()와 같은 분지 절단을 가집니다.

삼각 함수

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.asin(x)

x의 아크 사인을 반환합니다. 이것은 acos()와 같은 분지 절단을 가집니다.

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)

x의 코사인을 반환합니다.

cmath.sin(x)

x의 사인을 반환합니다.

cmath.tan(x)

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)

x의 쌍곡선 코사인을 반환합니다.

cmath.sinh(x)

x의 쌍곡선 사인을 반환합니다.

cmath.tanh(x)

x의 쌍곡선 탄젠트를 반환합니다.

분류 함수

cmath.isfinite(x)

x의 실수부와 허수부가 모두 유한이면 True를 반환하고, 그렇지 않으면 False를 반환합니다.

버전 3.2에 추가.

cmath.isinf(x)

x의 실수부나 허수부 중 하나가 무한이면 True를 반환하고, 그렇지 않으면 False를 반환합니다.

cmath.isnan(x)

x의 실수부나 허수부 중 하나가 NaN이면 True를 반환하고, 그렇지 않으면 False를 반환합니다.

cmath.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

ab 값이 서로 가까우면 True를 반환하고, 그렇지 않으면 False를 반환합니다.

두 값을 가까운 것으로 간주하는지는 주어진 절대와 상대 허용 오차에 따라 결정됩니다.

rel_tol은 상대 허용 오차입니다 – ab 사이의 최대 허용 차이이고, ab의 절댓값 중 더 큰 값에 상대적입니다. 예를 들어, 5%의 허용 오차를 설정하려면, rel_tol=0.05를 전달하십시오. 기본 허용 오차는 1e-09이며, 이는 두 값이 약 9자리 십진 숫자 내에서 같음을 보장합니다. rel_tol은 0보다 커야 합니다.

abs_tol은 최소 절대 허용 오차입니다 – 0에 가까운 비교에 유용합니다. abs_tol은 최소한 0이어야 합니다.

에러가 발생하지 않으면, 결과는 다음과 같습니다: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

IEEE 754 특수 값 NaN, inf-inf는 IEEE 규칙에 따라 처리됩니다. 특히, NaNNaN을 포함한 다른 모는 값과 가깝다고 간주하지 않습니다. 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.