cmath
— Mathematical functions for complex numbers¶
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
Conversions to and from polar coordinates |
|
Return the phase of z |
|
Return the representation of z in polar coordinates |
|
Return the complex number z with polar coordinates r and phi |
|
Power and logarithmic functions |
|
Return e raised to the power z |
|
Return the logarithm of z to the given base (e by default) |
|
Return the base-10 logarithm of z |
|
Return the square root of z |
|
Trigonometric functions |
|
Return the arc cosine of z |
|
Return the arc sine of z |
|
Return the arc tangent of z |
|
Return the cosine of z |
|
Return the sine of z |
|
Return the tangent of z |
|
Hyperbolic functions |
|
Return the inverse hyperbolic cosine of z |
|
Return the inverse hyperbolic sine of z |
|
Return the inverse hyperbolic tangent of z |
|
Return the hyperbolic cosine of z |
|
Return the hyperbolic sine of z |
|
Return the hyperbolic tangent of z |
|
Classification functions |
|
Check if all components of z are finite |
|
Check if any component of z is infinite |
|
Check if any component of z is a NaN |
|
Check if the values a and b are close to each other |
|
Constants |
|
π = 3.141592… |
|
e = 2.718281… |
|
τ = 2π = 6.283185… |
|
Positive infinity |
|
Pure imaginary infinity |
|
“Not a number” (NaN) |
|
Pure imaginary NaN |
극좌표 변환¶
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
.
극좌표(polar coordinates)는 복소수를 나타내는 다른 방법을 제공합니다. 극좌표에서, 복소수 z는 모듈러스(modulus) r과 위상 각(phase angle) phi로 정의됩니다. 모듈러스 r은 z에서 원점까지의 거리이며, 위상 phi는 양의 x축에서 원점과 z를 잇는 선분으로의 라디안(radian)으로 측정한 반 시계 방향 각도입니다.
네이티브 직교 좌표와 극좌표 간의 변환에 다음 함수를 사용할 수 있습니다.
- cmath.phase(z)¶
Return the phase of z (also known as the argument of z), as a float.
phase(z)
is equivalent tomath.atan2(z.imag, z.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 ofz.imag
, even whenz.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 z can be
computed using the built-in abs()
function. There is no
separate cmath
module function for this operation.
- cmath.polar(z)¶
Return the representation of z in polar coordinates. Returns a pair
(r, phi)
where r is the modulus of z and phi is the phase of z.polar(z)
is equivalent to(abs(z), phase(z))
.
- cmath.rect(r, phi)¶
Return the complex number z with polar coordinates r and phi. Equivalent to
complex(r * math.cos(phi), r * math.sin(phi))
.
거듭제곱과 로그 함수¶
- cmath.exp(z)¶
Return e raised to the power z, where e is the base of natural logarithms.
- cmath.log(z[, base])¶
Return the logarithm of z to the given base. If the base is not specified, returns the natural logarithm of z. There is one branch cut, from 0 along the negative real axis to -∞.
삼각 함수¶
- cmath.acos(z)¶
Return the arc cosine of z. 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(z)¶
Return the arc tangent of z. 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(z)¶
Return the cosine of z.
- cmath.sin(z)¶
Return the sine of z.
- cmath.tan(z)¶
Return the tangent of z.
쌍곡선(hyperbolic) 함수¶
- cmath.acosh(z)¶
Return the inverse hyperbolic cosine of z. There is one branch cut, extending left from 1 along the real axis to -∞.
- cmath.asinh(z)¶
Return the inverse hyperbolic sine of z. 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(z)¶
Return the inverse hyperbolic tangent of z. 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(z)¶
Return the hyperbolic cosine of z.
- cmath.sinh(z)¶
Return the hyperbolic sine of z.
- cmath.tanh(z)¶
Return the hyperbolic tangent of z.
분류 함수¶
- cmath.isfinite(z)¶
Return
True
if both the real and imaginary parts of z are finite, andFalse
otherwise.Added in version 3.2.
- cmath.isinf(z)¶
Return
True
if either the real or the imaginary part of z is an infinity, andFalse
otherwise.
- cmath.isnan(z)¶
Return
True
if either the real or the imaginary part of z 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. If no errors occur, the result will be:
abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
.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 nonnegative and less than1.0
.abs_tol is the absolute tolerance; it defaults to
0.0
and it must be nonnegative. When comparingx
to0.0
,isclose(x, 0)
is computed asabs(x) <= rel_tol * abs(x)
, which isFalse
for anyx
and rel_tol less than1.0
. So add an appropriate positive abs_tol argument to the call.IEEE 754 특수 값
NaN
,inf
및-inf
는 IEEE 규칙에 따라 처리됩니다. 특히,NaN
은NaN
을 포함한 다른 모는 값과 가깝다고 간주하지 않습니다.inf
와-inf
는 그들 자신하고만 가깝다고 간주합니다.Added in version 3.5.
더 보기
PEP 485 – 근사 동등을 검사하는 함수.
상수¶
- cmath.pi¶
수학 상수 π의 float 값.
- cmath.e¶
수학 상수 e의 float 값.
- cmath.tau¶
수학 상수 τ의 float 값.
Added in version 3.6.
- cmath.inf¶
부동 소수점 양의 무한대.
float('inf')
와 동등합니다.Added in version 3.6.
- cmath.infj¶
0 실수부와 양의 무한대 허수부를 갖는 복소수.
complex(0.0, float('inf'))
와 동등합니다.Added in version 3.6.
- cmath.nan¶
부동 소수점 “not a number” (NaN) 값.
float('nan')
과 동등합니다.Added in version 3.6.
- cmath.nanj¶
0 실수부와 NaN 허수부를 갖는 복소수.
complex(0.0, float('nan'))
과 동등합니다.Added in version 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.