Python学习精粹019:复数及其运算

本文介绍了复数在数学中的基本概念,如实部和虚部的定义,以及如何在Python中使用complex()函数创建和操作复数。重点讲解了cmath模块在处理复数运算和比较中的应用,包括平方根、比较函数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在数学中,复数(complex number)通常用z表示:

z = a + bi

其中:

(1)a、b为任意实数;

(2)实数a称为复数的实部(real part),记作Re z=a;

(3)实数b称为复数的虚部(imaginary part),记作Im z=b;

(4)i为虚数单位,且

(5)当虚部的b = 0时,z为实数;

(6)当虚部的b ≠ 0,且实部a = 0时,z为纯虚数,如5i、-25i、

(7)复数的集合用C表示,由实数和虚数共同构成。实数的集合用R表示,又分为有理数和无理数两类:有理数是整数((正整数、0、负整数))和分数的集合;无理数是无限不循环小数的集合;

(8)数学中将纯虚数定义为偶指数幂是负数的数,如方程x2 = -1无法找到实数解,在复数系统中,通过加入虚数单位i,是正确的,从而得到方程的解。

(9)在对虚数进行计算时,整数部分的性质不变,如:

意味着5i是-25的一个平方根,其中i2 = -1。又如:

再看:

(10)复数集是无序的,不能对此建立大小顺序。

在Python中,如使用math模块的sqrt()函数试图对负数进行平方根运算,会引发值错误(ValueError),且提示不在定义域内。

如下所示:

>>> import math

>>> math.sqrt(6)

2.449489742783178

>>> math.sqrt(-1)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: math domain error

在Python中,复数的虚部以字母j或J来表示,使用内置的complex()函数来创建一个函数,并使用其.real属性、.imag属性来分别访问复数的实部、虚部。

如下所示:

>>> x = complex(5)

>>> print(x)

(5+0j)

>>> x = complex(0)

>>> print(x)

0j

>>> x = complex(5, 2)

>>> print(x)

(5+2j)

>>> x.real

5.0

>>> x.imag

2.0

complex()函数允许字符串作为参数值,但需要注意的是:

(1)第一个参数值为数字(如整数、浮点数、复数)对应的字符串,会被认为是一个复数,不允许有第二个参数值。

如下所示:

>>> complex('5')

(5+0j)

>>> complex('3.1415')

(3.1415+0j)

>>> complex('5+2j')

(5+2j)

>>> complex('5', '2')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: complex() can't take second arg if first is a string

>>> complex('5', 2)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: complex() can't take second arg if first is a string

(2)第一个参数值的字符串中,“+”或“-”运算符号的前后不允许有空格,且紧挨字母“j”的前面不允许有空格。

如下所示:

>>> complex('5+2j')

(5+2j)

>>> complex(' 5+2j')

(5+2j)

>>> complex(5+2j )

(5+2j)

>>> complex('5+ 2j')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: complex() arg is a malformed string

>>> complex('5 + 2j')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: complex() arg is a malformed string

>>> complex('5- 2j')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: complex() arg is a malformed string

>>> complex('5+2 j')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: complex() arg is a malformed string

(3)第一个参数值为数字(如整数、小数、复数),第二个参数值不能为字符串。

如下所示:

>>> complex(5, '2')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: complex() second arg can't be a string

>>> complex(5, '3.1415')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: complex() second arg can't be a string

可对复数进行加、减、乘、除、绝对值、幂的运算,以及复数的共轭等。

如下所示:

>>> x = complex(5, 2)

>>> y = complex(3, 9)

>>> print(x - y)

(2-7j)

>>> print(x + y)

(8+11j)

>>> print(x - y)

(2-7j)

>>> print(x * y)

(-3+51j)

>>> print(x / y)

(0.36666666666666664-0.4333333333333333j)

>>> abs(complex(5,2))

5.385164807134504

>>> pow(complex(5j), 2)

(-25+0j)

>>> complex(5, 2)**2

(21+20j)

>>> pow(5+2j, 2)

(21+20j)

>>> pow(complex(5, 2), 2)

(21+20j)

>>> pow(complex('j'), 4)

(1+0j)

>>> pow(complex('j'), 7)

(-0-1j)

>>> pow(complex('j'), 8)

(1+0j)

>>> complex(5+2j).conjugate()

(5-2j)

Python内置的cmath模块提供了一些适用于复数的数学函数,如平方根、正弦、余弦等,更多信息,可参见https://docs.python.org/zh-cn/3/library/cmath.html。

还可通过dir()函数查看cmath模块的所有函数、方法及属性,如下所示:

>>> import cmath

>>> dir(cmath)

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']

>>> import cmath

>>> cmath.sqrt(-45)

6.708203932499369j

>>> cmath.sqrt(complex(5, 2))

(2.27872385417085+0.4388421169022545j)

>>> cmath.sqrt(complex('5+2j'))

(2.27872385417085+0.4388421169022545j)

>>> cmath.sin(complex('5+2j'))

(-3.6076607742131563+1.0288031496599335j)

>>> cmath.cos(complex('5+2j'))

(1.0671926518731156+3.477884485899157j)

此外,复数可以进行相等比较,但不能像实数一样直接进行大小比较,否则会引发值错误(ValueError)。

如下所示:

>>> complex(5, 2) == complex(5, 2)

True

>>> complex(6, 2) == complex(5, 2)

False

>>> complex(6, 2) >= complex(5, 2)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: '>=' not supported between instances of 'complex' and 'complex'

此外,类似于在浮点数中使用math模块的isclose()函数以根据给定的相对和绝对容差来比较两个数是否为接近的,使用cmath模块的isclose()函数也可获得相应效果。

如下所示:

>>> import cmath

>>> cmath.isclose(complex(5+2j), complex(6+2j), rel_tol = 1e-09, abs_tol = 0.0)

False

>>> cmath.isclose(complex(5+0.1j), complex(5+0.2j), rel_tol = 0.2, abs_tol = 0.0)

True

>>> cmath.isclose(complex(5+0.1j), complex(5+0.2j), rel_tol = 0.1, abs_tol = 0.0)

True

>>> cmath.isclose(complex(5.1+2j), complex(5.2+2j), rel_tol = 0.1)

True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值