Skip to main content

python零依赖数学库

Project description

nbmath

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

安装

pip install https://ghproxy.net/https://github.com/tc0512/nbmath/releases/download/v1.0.8/nbmath-1.0.8-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
import os

#解方程
print(solve(1, -3, 2)) #[(2+0j), (1+0j)]

#调用常数
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

模块介绍

常数模块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奇偶数判断

优化算法模块nbmath.optimize

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

绘图模块nbmath.plots

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

示例代码

from nbmath.equation import solve
from nbmath.stats import mode
from nbmath.optimize import simulated_annealing
from nbmath import plots as plt
import math

#求解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': -1.581998612252256, 'fun': -2.249992603725974}

#绘制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.plot_function(lambda x: math.sin(x), -2*math.pi, 2*math.pi)
plt.rect(1, 1, 1.5, 0.5, "red") #绘制一个左下角(1,1),长1.5,宽0.5的红色矩形
plt.keep_window()

许可证

MIT

Project details


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.1.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nbmath-1.1.1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file nbmath-1.1.1.tar.gz.

File metadata

  • Download URL: nbmath-1.1.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for nbmath-1.1.1.tar.gz
Algorithm Hash digest
SHA256 d497de7b6d74880bd8635963442afbb8de97fc79d247f6b4dd27c5b37d62fc7d
MD5 32bb36f216b1cde7e071e4e9dfd932ec
BLAKE2b-256 1b90b0c5f44ec34700058bb33035edf99c754cf015a3a059691c98235bdf3262

See more details on using hashes here.

File details

Details for the file nbmath-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: nbmath-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for nbmath-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 03fb7a628bf0d36db01d599659bbb13b1c405ee5f4715a4c5e82599ad905cb65
MD5 ef46398602432a5b59b64b3e9c718d28
BLAKE2b-256 2ba5a20f40ee325e5156fcf39d76d6116705e183e675308878e6f42d3b77c9ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page