python零依赖数学库
Project description
nbmath
一个实用的数学工具包,支持方程求解、几何计算、统计分析等功能。
安装
pip install https://ghproxy.net/https://github.com/tc0512/nbmath/releases/download/v1.2.0/nbmath-1.2.0-py3-none-any.whl
快速开始
from nbmath.equation import solve
from nbmath.const import pi
from nbmath.optimize import newton
from nbmath.utils import is_odd, zeros
import os
#解方程
print(solve(1, -3, 2)) #x1=2,x2=1
#调用常数
print(pi()) #3.141592653589793
#最小值
TOL = 1e-6
MAX_ITER = 100
print(newton(lambda x: x**2+6*x+9, 1.5, TOL, MAX_ITER)) #x接近-3,y接近0(有浮点误差)
#绘制康托尔阶梯
os.system("python -m nbmath.plots.examples.cantor_stair")
#判断是否为奇数
print(is_odd(99999)) #True
#生成10×10的全零矩阵
print(zeros(10, 10))
模块介绍
常数模块nbmath.const
pitaue- 数学常数GgkNA- 物理常数
方程模块nbmath.solve
- 一元一次/二次/三次/四次方程求解
- 牛顿迭代法解高次方程
- 不等式
- 高斯消元解线性方程组
- 统一接口
solve
几何模块nbmath.geo
- 点
Point - 线段
Line - 圆
Circle - 多边形
Polygon
统计模块nbmath.stats
mean平均数percentile百分位数mode众数var方差std标准差
工具模块nbmath.utils
gcd最大公约数lcm最小公倍数floor向下取整trunc截断取整fac阶乘diff多项式求导np.linspace纯python实现polyval多项式代入求值timer计时器is_evenis_odd奇偶数判断eye单位矩阵oneszeros全一/全零矩阵
优化算法模块nbmath.optimize
brute咆哮算法golden_section黄金分割法newton牛顿法gradient_descent梯度下降simulated_annealing模拟退火
绘图模块nbmath.plots
point描点scatter散点图line线段fun绘制函数F(x)rect矩形mandelbrotheart等共6个示例图案
示例代码
from nbmath.equation import solve
from nbmath.stats import mode
from nbmath.optimize import simulated_annealing
from nbmath import plots as plt
import math
import os
#求解x^4-10x^2+9=0
roots = solve(1, 0, -10, 0, 9)
print(roots) #接近±1,±3,浮点误差可能存在,请以实际使用为准
print(solve(2, 3, ">")) #x>-1.5
data = [1, 1, 2, 3, 4]
print(mode(data)) #[1]
#求y=x^4-5x^2+4的最小值
def F(x):return x**4-5*x**2+4
TEMP = 100
COOLING = 0.95
STEPS = 1000
TOL = 1e-6
print(simulated_annealing(F, -5, 5, TEMP, COOLING, STEPS, TOL)) #理论值x=±√10/2,f(x)_min=2.25,实际使用中可能存在微小误差,请以实际情况为准
#绘制y=cos(x)
plt.window(800, 600)
plt.setax(-2*math.pi, -1.5*math.pi, 2*math.pi, 1.5*math.pi)
plt.drawaxhline()
plt.fun(lambda x: math.sin(x), -2*math.pi, 2*math.pi, "red", 2, 800)
plt.rect(1, 1, 1.5, 0.5, "red") #绘制一个左下角(1,1),长1.5,宽0.5的红色矩形
plt.keep_window()
#绘制威尔斯特拉斯函数
os.system("python -m nbmath.plots.examples.weierstrass")
许可证
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
nbmath-1.3.0.tar.gz
(17.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
nbmath-1.3.0-py3-none-any.whl
(21.3 kB
view details)
File details
Details for the file nbmath-1.3.0.tar.gz.
File metadata
- Download URL: nbmath-1.3.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a1cb090f4468db1636c82882a7d8d7d6fb76979fcbcb0dadc80e80079eb8530
|
|
| MD5 |
27f5b9102ae00bf5f1c25425514beefa
|
|
| BLAKE2b-256 |
6a6500e16a59ad14a7aa3e442f84de21bee524f6689da1f931a561c5de39ecd5
|
File details
Details for the file nbmath-1.3.0-py3-none-any.whl.
File metadata
- Download URL: nbmath-1.3.0-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8246c2fb0f3421127f689583b74c7aeb334463bafd784bd664bb4f408009bc7b
|
|
| MD5 |
4854ed05e534454abed7232e4e17abd1
|
|
| BLAKE2b-256 |
a0b5e714b0514d3f375beb415039cf765f3b6502697ae54cae8e1d854d81c92a
|