python 常見數(shù)學(xué)公式函數(shù)使用詳解(最新推薦)
Python 數(shù)學(xué)公式與函數(shù)大全
Python 提供了豐富的數(shù)學(xué)計算支持,包括內(nèi)置函數(shù)、標(biāo)準(zhǔn)庫(math、cmath、numpy)和第三方庫(sympy、scipy)。以下是常用數(shù)學(xué)公式和函數(shù)的分類整理:
1. 基本數(shù)學(xué)運算
1.1 算術(shù)運算
| 運算符/函數(shù) | 描述 | 示例 |
|---|---|---|
+, -, *, / | 加減乘除 | 3 + 2 → 5 |
// | 整除 | 7 // 2 → 3 |
% | 取模(余數(shù)) | 7 % 2 → 1 |
** | 冪運算 | 2 ** 3 → 8 |
abs(x) | 絕對值 | abs(-5) → 5 |
pow(x, y) | 冪運算(等價于 x ** y) | pow(2, 3) → 8 |
round(x, n) | 四舍五入(n 位小數(shù)) | round(3.14159, 2) → 3.14 |
1.2 分?jǐn)?shù)與小數(shù)
from fractions import Fraction
from decimal import Decimal
# 分?jǐn)?shù)運算
a = Fraction(1, 3) # 1/3
b = Fraction(1, 2) # 1/2
print(a + b) # 輸出: 5/6
# 高精度小數(shù)
c = Decimal('0.1') + Decimal('0.2')
print(c) # 輸出: 0.3(避免浮點誤差)2. 數(shù)學(xué)函數(shù)庫(math 和 cmath)
2.1 基本函數(shù)
| 函數(shù) | 描述 | 示例 |
|---|---|---|
math.sqrt(x) | 平方根 | math.sqrt(4) → 2.0 |
math.exp(x) | 自然指數(shù)(exe**x) | math.exp(1) → 2.718... |
math.log(x, base) | 對數(shù)(默認(rèn)自然對數(shù)) | math.log(8, 2) → 3.0 |
math.log10(x) | 以 10 為底的對數(shù) | math.log10(100) → 2.0 |
2.2 三角函數(shù)
| 函數(shù) | 描述 | 示例 |
|---|---|---|
math.sin(x) | 正弦(弧度制) | math.sin(math.pi/2) → 1.0 |
math.cos(x) | 余弦 | math.cos(0) → 1.0 |
math.tan(x) | 正切 | math.tan(math.pi/4) → 1.0 |
math.degrees(x) | 弧度轉(zhuǎn)角度 | math.degrees(math.pi) → 180.0 |
math.radians(x) | 角度轉(zhuǎn)弧度 | math.radians(180) → 3.14159... |
2.3 復(fù)數(shù)運算(cmath)
import cmath z = 1 + 2j print(cmath.sqrt(z)) # 輸出: (1.272...+0.786...j) print(cmath.phase(z)) # 輻角(弧度)
3. 高級數(shù)學(xué)庫(numpy 和 scipy)
3.1 線性代數(shù)(numpy)
import numpy as np # 矩陣運算 A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) print(A @ B) # 矩陣乘法 # 特征值與特征向量 eigenvalues, eigenvectors = np.linalg.eig(A)
3.2 科學(xué)計算(scipy)
from scipy.integrate import quad from scipy.optimize import minimize # 數(shù)值積分 result, error = quad(lambda x: x**2, 0, 1) # ∫x2 dx (0→1) print(result) # 輸出: 0.333... # 優(yōu)化問題 res = minimize(lambda x: (x[0]-1)**2 + (x[1]-2)**2, x0=[0, 0]) print(res.x) # 最優(yōu)解: [1.0, 2.0]
4. 符號計算(sympy)
用于解析數(shù)學(xué)公式(如求導(dǎo)、積分、解方程):
from sympy import symbols, diff, integrate, solve
x, y = symbols('x y')
expr = x**2 + 2*x + 1
# 求導(dǎo)
print(diff(expr, x)) # 輸出: 2*x + 2
# 積分
print(integrate(expr, x)) # 輸出: x**3/3 + x**2 + x
# 解方程
solution = solve(x**2 - 4, x)
print(solution) # 輸出: [-2, 2]5. 統(tǒng)計與概率
5.1 隨機(jī)數(shù)(random)
import random print(random.random()) # [0, 1) 隨機(jī)浮點數(shù) print(random.randint(1, 10)) # 1~10 隨機(jī)整數(shù)
5.2 統(tǒng)計計算(statistics)
from statistics import mean, median, stdev data = [1, 2, 3, 4, 5] print(mean(data)) # 平均值: 3.0 print(stdev(data)) # 標(biāo)準(zhǔn)差: 1.581...
6. 特殊函數(shù)
| 函數(shù) | 描述 | 示例 |
|---|---|---|
math.gamma(x) | Gamma 函數(shù) | math.gamma(5) → 24.0 |
math.erf(x) | 誤差函數(shù) | math.erf(1) → 0.842... |
scipy.special.besselj(n, x) | 貝塞爾函數(shù) | 需安裝 scipy |
總結(jié)
| 需求 | 推薦庫 | 示例 |
|---|---|---|
| 基本運算 | 內(nèi)置函數(shù) | abs(-5) |
| 三角函數(shù)/對數(shù) | math | math.sin(math.pi) |
| 復(fù)數(shù)運算 | cmath | cmath.sqrt(-1) |
| 矩陣運算 | numpy | np.linalg.inv(A) |
| 符號計算 | sympy | solve(x**2 - 1, x) |
| 科學(xué)計算 | scipy | scipy.integrate.quad |
掌握這些工具后,你可以輕松實現(xiàn)從基礎(chǔ)算術(shù)到復(fù)雜科學(xué)計算的各類數(shù)學(xué)任務(wù)! ??
到此這篇關(guān)于python 常見數(shù)學(xué)公式函數(shù)使用詳解(最新推薦)的文章就介紹到這了,更多相關(guān)python數(shù)學(xué)公式函數(shù)使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python?cv2.waitKey()函數(shù)的使用
這篇文章主要介紹了python?cv2.waitKey()函數(shù)的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Python實現(xiàn)接受任意個數(shù)參數(shù)的函數(shù)方法
下面小編就為大家分享一篇Python實現(xiàn)接受任意個數(shù)參數(shù)的函數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python中時間類型的JSON數(shù)據(jù)轉(zhuǎn)換
在Python中,處理時間和日期數(shù)據(jù)以及與JSON數(shù)據(jù)的相互轉(zhuǎn)換是常見的任務(wù),本文主要為大家詳細(xì)如何在Python中處理時間類型的JSON數(shù)據(jù)轉(zhuǎn)換,需要的小伙伴可以參考下2024-02-02
Python統(tǒng)計日志中每個IP出現(xiàn)次數(shù)的方法
這篇文章主要介紹了Python統(tǒng)計日志中每個IP出現(xiàn)次數(shù)的方法,實例分析了Python基于正則表達(dá)式解析日志文件的相關(guān)技巧,需要的朋友可以參考下2015-07-07
pycharm?使用conda虛擬環(huán)境的詳細(xì)配置過程
這篇文章主要介紹了pycharm?使用conda虛擬環(huán)境,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

