本文共 7311 字,大约阅读时间需要 24 分钟。
python cmath
A complex number is created from two real numbers. Python complex number can be created using function as well as using direct assignment statement.
从两个实数创建一个复数。 可以使用函数以及直接赋值语句创建Python复数。
Complex numbers are mostly used where we define something using two real numbers. For example, a circuit element that is defined by Voltage (V) and Current (I). They are mostly used in geometry, calculus and scientific calculations.
复数通常用在我们使用两个实数定义事物的地方。 例如,由电压(V)和电流(I)定义的电路元件。 它们主要用于几何,微积分和科学计算。
Let’s first learn how to create complex numbers in python.
让我们首先学习如何在python中创建复数。
c = 1 + 2jprint(type(c))print(c)c1 = complex(2, 4)print(type(c1))print(c1)
Output:
输出:
(1+2j) (2+4j)
Python complex numbers are of type complex
. Every complex number contains one real part and one imaginary part.
Python复数的类型为complex
。 每个复数包含一个实部和一个虚部。
Let’s look at some attributes and instance functions of complex numbers.
让我们看一下复数的一些属性和实例函数。
c = 1 + 2jprint('Real Part =', c.real)print('Imaginary Part =', c.imag)print('Complex conjugate =', c.conjugate())
Output:
输出:
Real Part = 1.0Imaginary Part = 2.0Complex conjugate = (1-2j)
Complex numbers support mathematical caluclations such as addition, subtraction, multiplication and division.
复数支持数学计算,例如加,减,乘和除。
c = 1 + 2jc1 = 2 + 4jprint('Addition =', c + c1)print('Subtraction =', c - c1)print('Multiplication =', c * c1)print('Division =', c1 / c)
Output:
输出:
Addition = (3+6j)Subtraction = (-1-2j)Multiplication = (-6+8j)Division = (2+0j)
Complex numbers don’t support comparison operators. If we try to execute c < c1
then the error message will be thrown as TypeError: '<' not supported between instances of 'complex' and 'complex'
.
复数不支持比较运算符。 如果我们尝试执行c < c1
则错误消息将作为TypeError: '<' not supported between instances of 'complex' and 'complex'
。
Python cmath module provides access to mathematical functions for complex numbers. Let’s look at some of the important features of complex numbers and how we can use cmath module function to calculate them.
Python cmath模块提供对复数数学函数的访问。 让我们看一下复数的一些重要特征,以及如何使用cmath模块函数来计算它们。
The phase of a complex number is the angle between the real axis and the vector representing the imaginary part. Below image illustrates the phase of a complex number and how to get this value using cmath and modules.
复数的相位是实轴与代表虚部的向量之间的夹角。 下图说明了复数的相位以及如何使用cmath和模块获得该值。
Note that the phase returned by math and cmath modules are in radians, we can use numpy.degrees()
function to convert it to degrees. The range of phase is from -π to +π (-pi to +pi) in radians and it’s equivalent to -180 to +180 degrees.
请注意,math和cmath模块返回的相位以弧度为单位,我们可以使用numpy.degrees()
函数将其转换为度。 相位范围是从-π到+π(-pi到+ pi)弧度,它等效于-180到+180度。
import cmath, math, numpyc = 2 + 2j# phasephase = cmath.phase(c)print('2 + 2j Phase =', phase)print('Phase in Degrees =', numpy.degrees(phase))print('-2 - 2j Phase =', cmath.phase(-2 - 2j), 'radians. Degrees =', numpy.degrees(cmath.phase(-2 - 2j)))# we can get phase using math.atan2() function tooprint('Complex number phase using math.atan2() =', math.atan2(2, 1))
Output:
输出:
2 + 2j Phase = 0.7853981633974483Phase in Degrees = 45.0-2 - 2j Phase = -2.356194490192345 radians. Degrees = -135.0Complex number phase using math.atan2() = 1.1071487177940904
We can write a complex number in polar coordinates, which is a of modulus and phase of the complex number.
我们可以在极坐标中写一个复数,它是复数的模数和相位的 。
We can use cmath.rect() function to create a complex number in rectangular format by passing modulus and phase as arguments.
我们可以使用cmath.rect()函数通过传递模数和相位作为参数来创建矩形格式的复数。
c = 1 + 2jmodulus = abs(c)phase = cmath.phase(c)polar = cmath.polar(c)print('Modulus =', modulus)print('Phase =', phase)print('Polar Coordinates =', polar)print('Rectangular Coordinates =', cmath.rect(modulus, phase))
Output:
输出:
Modulus = 2.23606797749979Phase = 1.1071487177940904Polar Coordinates = (2.23606797749979, 1.1071487177940904)Rectangular Coordinates = (1.0000000000000002+2j)
There are a bunch of constants in cmath module that are used in the complex number calculations.
cmath模块中有一堆用于复数计算的常量。
print('π =', cmath.pi)print('e =', cmath.e)print('tau =', cmath.tau)print('Positive infinity =', cmath.inf)print('Positive Complex infinity =', cmath.infj)print('NaN =', cmath.nan)print('NaN Complex =', cmath.nanj)
Output:
输出:
π = 3.141592653589793e = 2.718281828459045tau = 6.283185307179586Positive infinity = infPositive Complex infinity = infjNaN = nanNaN Complex = nanj
There are some useful functions for logarithmic and power operations.
对数和幂运算有一些有用的功能。
c = 2 + 2jprint('e^c =', cmath.exp(c))print('log2(c) =', cmath.log(c, 2))print('log10(c) =', cmath.log10(c))print('sqrt(c) =', cmath.sqrt(c))
Output:
输出:
e^c = (-3.074932320639359+6.71884969742825j)log2(c) = (1.5000000000000002+1.1330900354567985j)log10(c) = (0.4515449934959718+0.3410940884604603j)sqrt(c) = (1.5537739740300374+0.6435942529055826j)
c = 2 + 2jprint('arc sine =', cmath.asin(c))print('arc cosine =', cmath.acos(c))print('arc tangent =', cmath.atan(c))print('sine =', cmath.sin(c))print('cosine =', cmath.cos(c))print('tangent =', cmath.tan(c))
Output:
输出:
arc sine = (0.7542491446980459+1.7343245214879666j)arc cosine = (0.8165471820968505-1.7343245214879666j)arc tangent = (1.311223269671635+0.2388778612568591j)sine = (3.4209548611170133-1.5093064853236156j)cosine = (-1.5656258353157435-3.2978948363112366j)tangent = (-0.028392952868232294+1.0238355945704727j)
c = 2 + 2jprint('inverse hyperbolic sine =', cmath.asinh(c))print('inverse hyperbolic cosine =', cmath.acosh(c))print('inverse hyperbolic tangent =', cmath.atanh(c))print('hyperbolic sine =', cmath.sinh(c))print('hyperbolic cosine =', cmath.cosh(c))print('hyperbolic tangent =', cmath.tanh(c))
Output:
输出:
inverse hyperbolic sine = (1.7343245214879666+0.7542491446980459j)inverse hyperbolic cosine = (1.7343245214879666+0.8165471820968505j)inverse hyperbolic tangent = (0.2388778612568591+1.311223269671635j)hyperbolic sine = (-1.5093064853236156+3.4209548611170133j)hyperbolic cosine = (-1.5656258353157435+3.2978948363112366j)hyperbolic tangent = (1.0238355945704727-0.028392952868232294j)
There are some miscellaneous functions to check if the complex number is finite, infinite or nan. There is also a function to check if two complex numbers are close.
有一些其他函数可以检查复数是有限的,无限的还是nan。 还有一个功能可以检查两个复数是否接近。
print(cmath.isfinite(2 + 2j)) # Trueprint(cmath.isfinite(cmath.inf + 2j)) # Falseprint(cmath.isinf(2 + 2j)) # Falseprint(cmath.isinf(cmath.inf + 2j)) # Trueprint(cmath.isinf(cmath.nan + 2j)) # Falseprint(cmath.isnan(2 + 2j)) # Falseprint(cmath.isnan(cmath.inf + 2j)) # Falseprint(cmath.isnan(cmath.nan + 2j)) # Trueprint(cmath.isclose(2+2j, 2.01+1.9j, rel_tol=0.05)) # Trueprint(cmath.isclose(2+2j, 2.01+1.9j, abs_tol=0.005)) # False
The output of each statement is provided in the comments.
注释中提供了每个语句的输出。
Reference:
参考:
翻译自:
python cmath
转载地址:http://mamzd.baihongyu.com/