Skip to main content

nbmath

一个实用的数学工具包,支持方程求解、几何计算、统计分析等功能。

安装

pip install nbmath #-i https://pypi.tuna.tsinghua.edu.cn/simple/

快速开始

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

  • pi tau e - 数学常数
  • G g k NA - 物理常数

方程模块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_even is_odd奇偶数判断
  • eye单位矩阵 ones zeros全一/全零矩阵
  • lstsq 最小二乘 qfactor二次三项式因式分解

优化算法模块nbmath.optimize

  • brute咆哮算法
  • golden_section黄金分割法
  • newton牛顿法
  • gradient_descent梯度下降
  • simulated_annealing模拟退火
  • BFGS拟牛顿法

绘图模块nbmath.plots

  • point描点 scatter散点图
  • line线段 fun绘制函数F(x)
  • rect矩形
  • mandelbrot newton等多个示例图案

特殊函数

  • gamma伽马函数 zeta黎曼ζ函数
  • 第一、二类贝塞尔
  • LambertW函数
  • pochhammer上升阶乘 hyp_pfq超几何函数

示例代码

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

Metadata

Release files for nbmath 1.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nbmath 1.4.1
File Size Uploaded Provenance Actions
nbmath-1.4.1.tar.gz 18.7 kB Without provenance Details for nbmath-1.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nbmath 1.4.1
File Interpreter ABI Platform Actions
nbmath-1.4.1-py3-none-any.whl Python 3 none any Details for nbmath-1.4.1-py3-none-any.whl

Total release size: 41.6 kB

Release files / nbmath-1.4.1.tar.gz

Download URL nbmath-1.4.1.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1a08b85e632eae939751cbcdda2d546720613b6d2994a6aa3f8ea85699f19e3a
BLAKE2b-256 checksum
How to use checksums
bcd1b3126a609337d6d1e286e71a90b8a393527b5133e5f4a55e3e2837c33987
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / nbmath-1.4.1-py3-none-any.whl

Download URL nbmath-1.4.1-py3-none-any.whl
Size 23.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
62252f5a71084ed51bbb4258e5fcdcc0593de1e4a837f5646cda628d1306b49c
BLAKE2b-256 checksum
How to use checksums
c40a81890309f6a722cda05b32ac1d701609dade19059771b1aca11da0bf438c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for nbmath 1.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page