从实数创建复数。可以使用直接赋值语句或使用complex()函数来创建Python复数。

复数通常在我们使用两个实数的地方使用。例如,由电压(V)和电流(C)定义的电路被用于几何学,科学计算和微积分中。

语法complex([real[, imag]])

在python中创建一个简单的复数>>> c = 3 +6j

>>> print(type(c))

>>> print(c)

(3+6j)

>>>

>>> c1 = complex(3,6)

>>> print(type(c1))

>>> print(c1)

(3+6j)

从以上结果中,我们可以看到python复数是complex类型。每个复数由一个实部和一个虚部组成。

Python复数-属性和函数>>> #Complex Number:

>>> c = (3 + 6j)

>>>

>>> #Real Part of complex number

>>> print('Complex Number: Real Part is = ', c. real)

Complex Number: Real Part is = 3.0

>>>

>>> #Imaginary Part of complex number

>>> print('Complex Number: Imaginary Part is = ', c. imag)

Complex Number: Imaginary Part is = 6.0

>>>

>>> #Conjugate of complex number

>>> print('Complex Number: conjugate Part = ', c. conjugate())

Complex Number: conjugate Part = (3-6j)

复数的数学计算

我们可以对复数进行简单的数学计算:>>> #first complex number

>>> c1 = 3 + 6j

>>> #Second complex number

>>> c2 = 6 + 15j

>>>

>>> #Addition

>>> print("Addition of two complex number =", c1 + c2)

Addition of two complex number = (9+21j)

>>>

>>> #Subtraction

>>> print("Subtraction of two complex number =", c1 - c2)

Subtraction of two complex number = (-3-9j)

>>>

>>> #Multiplication

>>> print("Multiplication of two complex number =", c1 * c2)

Multiplication of two complex number = (-72+81j)

>>>

>>> #Division

>>> print("Division of two complex number =", c1 / c2)

Division of two complex number = (0.4137931034482759-0.03448275862068964j)

但是,复数不支持,<=,=>之类的比较运算符,它将通过TypeError消息显示:>>> c2 <= c2

Traceback (most recent call last):

File "", line 1, in

c2 <= c2

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

Python cmath模块

Python cmath模块提供对复数数学函数的访问。让我们使用数学模块功能查看复数的一些重要功能。

复数相位

复数的相位是实轴与代表虚部的向量之间的夹角。

math和cmath模块返回的相位以弧度表示,我们使用numpy.degrees()函数将其转换为度。import cmath, math, numpy

c = 4+ 4j

# phase

phase = cmath.phase(c)

print('4+ 4j Phase =', phase)

print('Phase in Degrees =', numpy.degrees(phase))

print('-4-4j Phase =', cmath.phase(-4-4j), 'radians. Degrees =', numpy.degrees(cmath.phase(-4-4j)))

# we can get phase using math.atan2() function too

print('Complex number phase using math.atan2() =', math.atan2(2, 1))

结果4+ 4j Phase = 0.7853981633974483

Phase in Degrees = 45.0

-4-4j Phase = -2.356194490192345 radians. Degrees = -135.0

Complex number phase using math.atan2() = 1.1071487177940904

cmath模块常量

cmath模块中有几个常量可用于复数计算:import 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)

结果π = 3.141592653589793

e = 2.718281828459045

tau = 6.283185307179586

Positive infinity = inf

Positive Complex infinity = infj

NaN = nan

NaN Complex = nanj

电源和对数功能

该cmath()模块为对数和幂运算提供了一些有用的功能:import cmath

c = 1 + 2j

print('e^c =', cmath.exp(c))

print('log2(c) =', cmath.log(c, 2))

print('log10(c) =', cmath.log10(c))

print('sqrt(c) =', cmath.sqrt(c))

结果e^c = (-1.1312043837568135+2.4717266720048188j)

log2(c) = (1.1609640474436813+1.5972779646881088j)

log10(c) = (0.3494850021680094+0.480828578784234j)

sqrt(c) = (1.272019649514069+0.7861513777574233j)

三角函数import cmath

c = 2 + 4j

print('arc sine value:\n ', cmath.asin(c))

print('arc cosine value :\n', cmath.acos(c))

print('arc tangent value of complex number c :\n', cmath.atan(c))

print('sine value:\n', cmath.sin(c))

print('cosine value:\n', cmath.cos(c))

print('tangent value:\n', cmath.tan(c))

结果arc sine value:

(0.4538702099631225+2.198573027920936j)

arc cosine value :

(1.1169261168317741-2.198573027920936j)

arc tangent value of complex number c :

(1.4670482135772953+0.20058661813123432j)

sine value:

(24.83130584894638-11.356612711218174j)

cosine value:

(-11.36423470640106-24.814651485634187j)

tangent value:

(-0.0005079806234700387+1.0004385132020523j)

双曲函数import cmath

c = 2 + 4j

print('Inverse hyperbolic sine value: \n', cmath.asinh(c))

print('Inverse hyperbolic cosine value: \n', cmath.acosh(c))

print('Inverse hyperbolic tangent value: \n', cmath.atanh(c))

print('Hyperbolic sine value: \n', cmath.sinh(c))

print('Hyperbolic cosine value: \n', cmath.cosh(c))

print('Hyperbolic tangent value: \n', cmath.tanh(c))

结果Inverse hyperbolic sine value:

(2.183585216564564+1.096921548830143j)

Inverse hyperbolic cosine value:

(2.198573027920936+1.1169261168317741j)

Inverse hyperbolic tangent value:

(0.09641562020299617+1.3715351039616865j)

Hyperbolic sine value:

(-2.370674169352002-2.8472390868488278j)

Hyperbolic cosine value:

(-2.4591352139173837-2.744817006792154j)

Hyperbolic tangent value:

(1.0046823121902348+0.03642336924740368j)

python中复数用法_Python中的复数?相关推荐

  1. python的继承用法_python中继承有什么用法?python继承的用法详解

    本篇文章给大家带来的内容是关于python中继承有什么用法?python继承的用法详解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 面向对象三大特征 1.封装:根据职责将属性和方法 ...

  2. python if函数用法_python中if条件中的Contains()函数“in”

    我对contains()in的功能做了一些研究,特别是与eq()==进行了比较,发现它可以完成许多任务.我设法回答了其中许多问题(见下文). 除了下面的用法之外,in还有其他有用的用法吗,例如对象?在 ...

  3. python中sorted用法_Python中sorted()方法的用法

    1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象.iterable主要包括3类: 第一类是所有 ...

  4. python中rjust用法_python中rjust的用法

    英文对话是要加引号的,而且是双引号和单引号的使用方法与中文一样的用法,接下来小编在这里给大家带来,我们一起来看看吧!引号分单引号(singlequotationmarks)和双引号(doublequo ...

  5. python or的用法_python中and和or的用法

    原博文 2013-01-19 13:40 − From <dive into python> python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第 ...

  6. python isnumeric函数用法_Python中isnumeric()方法的使用简介

    Python中isnumeric()方法的使用简介 更新时间:2015年05月19日 09:35:06 投稿:goldensun 这篇文章主要介绍了Python中isnumeric()方法的使用,is ...

  7. python的继承用法_Python 中的继承之Super用法

    以下Copy自官方文档说明,可点击查看官网源文 翻译内容属于德德自译,有不当之处请指正,勿喷... 翻译括弧中是德德自己理解,通过代码验证的,勿喷... super(type[, object-or- ...

  8. python中的用法_Python中使用@的理解

    Python函数中使用@ 稍提一下的基础 fun 和fun()的区别 以一段代码为例: def fun(): print('fun') return None a = fun() #fun函数并将返回 ...

  9. python中type用法_Python中type的用法

    (TOC)描述python类型的函数有两个用法,当只有一个参数,它将返回对象的类型.当有三个参数,返回一个类对象.语法类型(对象)类型(名称.基地.dict)使用一个参数类型(对象)返回一个对象的类型 ...

最新文章

  1. Java中public,private,protected以及default的访问权限区别
  2. Android应用截图嵌入到真实设备
  3. Java工作笔记-注解的进一步理解
  4. 解决xlwt保存的xlsx文件无法打开的问题
  5. “乐享生活,随心而行”,第四届APEC车联网研讨会即将在上海召开
  6. 页面s升级中_你的电脑要不要升级内存?怎么升级?答案都在这里
  7. mysql 查询语法基础_入门MySQL——查询语法练习
  8. ZR提高失恋测2(9.7)
  9. u盘装服务器系统还原c盘失败,云骑士一键重装系统win10还原c盘失败怎么办
  10. css文字加边框镂空文字_如何使用CSS创建镂空边框设计
  11. 装完虚拟机后键盘无法使用
  12. C语言将循环小数/有限小数转换为分数
  13. 华为认证工程师HCNE培训资料
  14. hapi mysql项目实战路由初始化_hapi框架搭建记录(三):Joi数据校验和Sequelize(数据迁移、填充数据)...
  15. 大数据会给我们带来什么影响?
  16. 算法基础、算法比赛快速入门(java)
  17. 培训机构出来的程序员进不了大厂?
  18. 调试WebService的一个很好的工具
  19. 爬虫网易云音乐,热评,词云,prettytable。
  20. 三朝元老经验(转,推荐)

热门文章

  1. 雷军说,焦虑的小米找到了“AI + 物联网 + 5G”的路
  2. WPF 控件自定义模板之:圆形进度条
  3. mysql5.7固态硬盘查询速度_使用ssd硬盘加速 mysql index查询
  4. 【C++要笑着学】虚函数表(VBTL) | 观察虚表指针 | 运行时决议与编译时决议 | 动态绑定与静态绑定 | 静态多态与动态多态 | 单继承与多继承关系的虚表
  5. Python问题:SyntaxError: Non-ASCII character '\xe2'
  6. 全球及中国信息安全产业应用前景及投融资状况分析报告2021-2027年版
  7. 图片的一些效果处理方法
  8. pmbook 知识领域 第六版_PMBOK第六版10大知识领域ITTO思维导图-干货!
  9. 解决tensorflow.python.framework.errors_impl.NotFoundError错误
  10. [附源码]Java计算机毕业设计SSM合肥市公务员报名管理系统