Python科学计算基础:math模块
Python中的math
模块是一个Python自带的标准库,使用起来和C的标准库很相似,但不支持复数运算,复数运算可以使用cmath
模块。
math模块中的常用函数如下,包含了欧拉常数e、、对数、三角函数、双曲函数等
常用函数
import math
# 常量
print("pi: ", math.pi)
print("欧拉常数: ", math.e)
print("pi的2倍: ", math.tau)
print("正无穷: ", math.inf)
print("NaN: ", math.nan)
# 幂和对数函数
print("2的3次方: ", math.pow(2, 3))
print("根号16: ", math.sqrt(16))
print("自然对数: ", math.log(10))
print("以10为底的对数: ", math.log10(1000))
print("以2为底的对数: ", math.log2(16))
# 三角函数
print("pi/2的正弦值: ", math.sin(math.pi/2))
print("0的余弦值: ", math.cos(0))
print("pi/4的正切值: ", math.tan(math.pi/4))
print("1的反正弦值: ", math.asin(1))
print("1的反余弦值: ", math.acos(1))
print("1的反正切值: ", math.atan(1))
print("(1, 1)的二参数反正切值: ", math.atan2(1, 1))
# 双曲函数
print("1的双曲正弦值: ", math.sinh(1))
print("0的双曲余弦值: ", math.cosh(0))
print("1的双曲正切值: ", math.tanh(1))
print("10的双曲正弦反函数值: ", math.asinh(10))
print("10的双曲余弦反函数值: ", math.acosh(10))
# 特殊函数
print("5的伽玛函数值: ", math.gamma(5))
print("1的误差函数值: ", math.erf(1))
print("1的余误差函数值: ", math.erfc(1))
# 舍入函数
print("2.5的上取整值: ", math.ceil(2.5))
print("2.5的下取整值: ", math.floor(2.5))
print("2.5的截断值: ", math.trunc(2.5))
# 其他函数
print("5的阶乘值: ", math.factorial(5))
print("12和15的最大公约数: ", math.gcd(12, 15))
print("[1, 2, 3]的浮点数值和: ", math.fsum([1, 2, 3]))
print("2.0是有限数吗?", math.isfinite(2.0))
print("判断NaN值", math.isnan(math.nan))
print("判断两数是否接近", math.isclose(2.0, 2.00001))
案例1.
from math import sin, cos, log
v = sin(1) * cos(1) + log(2)
print(v)
结果
1.1477958939727861
案例2.
from math import pi, sinh, exp, e
x = 2 * pi
r1 = sinh(x)
r2 = 0.5 * (exp(x) - e**(-x))
r3 = 0.5 * (e**x - e**(-x))
print(r1, r2, r3)
结果
267.74489404101644 267.74489404101644 267.7448940410163
舍入误差
print('%.6f %.6f %.6f' %(r1, r2, r3))
267.744894 267.744894 267.744894
案例3
通过自定义类来进行函数计算
MyFunc类中的函数定义:
一阶导数:
二阶导数:
MyComplicatedFunc 类中的函数定义:
from math import cos, sin, log, tanh
class FuncWithDerivatives(object):
def __init__(self, h=1.0E-5):
self.h = h
def __call__(self, x):
raise NotImplementedError
('__call__ missing in class %s' %self.__class__.__name__)
def df(self, x):
h = self.h
return (self(x+h)-self(x-h))/(2.0*h)
def ddf(self, x):
h = self.h
return (self(x+h)-2*self(x)+self(x-h))/(float(h*h))
class MyFunc(FuncWithDerivatives):
def __init__(self, a):
super().__init__()
self.a = a
def __call__(self, x):
return cos(self.a * x) + 3 * x ** 2
def df(self, x):
return -self.a * sin(self.a * x) + 6 * x
def ddf(self, x):
return -self.a ** 2 * cos(self.a * x) + 6
class MyComplicatedFunc(FuncWithDerivatives):
def __init__(self, p, q, r, h=1.0E-5):
super().__init__(h)
self.p, self.q, self.r = p, q, r
def __call__(self, x):
return log(abs(self.p * tanh(self.q * x * cos(self.r * x))))
my_func = MyFunc(a=2)
complicated_func = MyComplicatedFunc(p=1, q=2, r=3)
x_value = 1.0
func_value = my_func(x_value)
derivative = my_func.df(x_value)
numerical_derivative = my_func.df(x_value)
second_derivative = my_func.ddf(x_value)
numerical_second_derivative = (my_func(x_value + my_func.h) -
2 * my_func(x_value) + my_func(x_value - my_func.h)) / (my_func.h ** 2)
print(f"MyFunc at x={x_value}: f(x)={func_value}, f'(x)={derivative} (or ~{numerical_derivative} numerically), f''(x)={second_derivative} (or ~{numerical_second_derivative} numerically)")
func_value_complicated = complicated_func(x_value)
numerical_derivative_complicated = complicated_func.df(x_value)
print(f"MyComplicatedFunc at x={x_value}: f(x)={func_value_complicated},f'(x)~={numerical_derivative_complicated} numerically")
运行结果
MyFunc at x=1.0: f(x)=2.5838531634528574, f'(x)=4.181405146348636 (or ~4.181405146348636 numerically), f''(x)=7.664587346188569 (or ~7.664602286183707 numerically)
MyComplicatedFunc at x=1.0: f(x)=-0.03813199271569875,f'(x)~=0.21562803924728144 numerically
原文地址:https://blog.csdn.net/m0_73500130/article/details/136994315
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!