欢迎光临散文网 会员登陆 & 注册

Python cmath模块函数

2021-08-09 09:21 作者:皮卡丘上大学啦  | 我要投稿

 cmath模块函数与math模块函数基本一致。 

 包括圆周率pi、自然常数e等常量。 

 复数的幂指数、对数函数、平方根函数、三角函 数等。

 cmath模块函数名和math模块函数名相同。

 只是math模块对实数运算,cmath模块对复数运算。

 例: 

 >>> import cmath 

 >>> cmath.pi 

 3.141592653589793 

 >>> cmath.sqrt(-1) 

 1j 

 >>> cmath.sin(1) 

 (0.8414709848078965+0j) 

 >>> cmath.log10(100) 

 (2+0j) 

 >>> cmath.exp(100+10j) 

 (-2.255522560520288e+43-1.4623924736915717e+43j)

 cmath模块包括复数运算特有的函数。

 复数x=a+bi,phase(x)函数返回复数x的幅角,即 atan(b/a)。 

 例: 

 >>> from cmath import * 

 >>> phase(1+1j) 

 0.7853981633974483 

 >>> phase(1+2j) 

 1.1071487177940904

 cmath模块的polar()函数和rect()函数可以对复数进行极坐标表示和笛卡儿表示方法的转换。  polar(x)函数将复数的笛卡儿坐标表示转换为极坐标表示,输出为一个二元组(r,p),复数的模 r=abs(x),幅角p=phase(x)。 

 rect(r, p)函数将复数的极坐标表示转换为笛卡儿坐 标表示,输出为r*cos(p)+r*sin(p)*1j。

 例: 

 >>> c=3+4j 

 >>> r,p=polar(c) 

 >>> print(r,p) 

 5.0 0.9272952180016122 

 >>> rect(r,p) 

 (3.0000000000000004+3.9999999999999996j)

Python cmath模块函数的评论 (共 条)

分享到微博请遵守国家法律