Python中的复数 |第 2 组(重要函数和常量)
Python复数介绍: Python中的复数|第 1 套(介绍)
本文讨论了一些更重要的函数和常量。
复数运算:
1. exp() :- 该函数返回其参数中提到的复数的指数。
2. log(x,b) :- 该函数返回以b 为底的 x 的对数值,两者都在其参数中提及。如果未指定基数,则返回 x 的自然对数。
# Python code to demonstrate the working of
# exp(), log()
# importing "cmath" for complex number operations
import cmath
import math
# Initializing real numbers
x = 1.0
y = 1.0
# converting x and y into complex number
z = complex(x, y);
# printing exponent of complex number
print ("The exponent of complex number is : ", end="")
print (cmath.exp(z))
# printing log form of complex number
print ("The log(base 10) of complex number is : ", end="")
print (cmath.log(z,10))
输出:
The exponent of complex number is : (1.4686939399158851+2.2873552871788423j)
The log(base 10) of complex number is : (0.15051499783199057+0.3410940884604603j)
3. log10() :- 此函数返回复数的以10 为底的对数。
4. sqrt() :- 计算复数的平方根。
# Python code to demonstrate the working of
# log10(), sqrt()
# importing "cmath" for complex number operations
import cmath
import math
# Initializing real numbers
x = 1.0
y = 1.0
# converting x and y into complex number
z = complex(x, y);
# printing log10 of complex number
print ("The log10 of complex number is : ", end="")
print (cmath.log10(z))
# printing square root form of complex number
print ("The square root of complex number is : ", end="")
print (cmath.sqrt(z))
输出:
The log10 of complex number is : (0.15051499783199057+0.3410940884604603j)
The square root of complex number is : (1.09868411346781+0.45508986056222733j)
5. isfinite() :-如果复数的实部和虚部都是有限的,则返回 true,否则返回 false。
6. isinf() :-如果复数的实部或虚部是无限的,则返回 true,否则返回 false。
7. isnan() :- 如果复数的实部或虚部是NaN ,则返回 true,否则返回 false。
# Python code to demonstrate the working of
# isnan(), isinf(), isfinite()
# importing "cmath" for complex number operations
import cmath
import math
# Initializing real numbers
x = 1.0
y = 1.0
a = math.inf
b = math.nan
# converting x and y into complex number
z = complex(x,y);
# converting x and a into complex number
w = complex(x,a);
# converting x and b into complex number
v = complex(x,b);
# checking if both numbers are finite
if cmath.isfinite(z):
print ("Complex number is finite")
else : print ("Complex number is infinite")
# checking if either number is/are infinite
if cmath.isinf(w):
print ("Complex number is infinite")
else : print ("Complex number is finite")
# checking if either number is/are infinite
if cmath.isnan(v):
print ("Complex number is NaN")
else : print ("Complex number is not NaN")
输出:
Complex number is finite
Complex number is infinite
Complex number is NaN
常数
cmath 模块中定义了两个常量, “pi” ,它返回 pi 的数值。第二个是“e” ,它返回指数的数值。
# Python code to demonstrate the working of
# pi and e
# importing "cmath" for complex number operations
import cmath
import math
# printing the value of pi
print ("The value of pi is : ", end="")
print (cmath.pi)
# printing the value of e
print ("The value of exponent is : ", end="")
print (cmath.e)
输出:
The value of pi is : 3.141592653589793
The value of exponent is : 2.718281828459045
Python中的复数 |第 3 组(三角函数和双曲函数)