Skip to main content

Use newton method to iterate infinitely to find an approximate solution to any equation.

Project description

NewtonMathSolver

使用牛顿法无限迭代求任意方程近似解

基础调用

from NewtonMathSolver import Tolerance, NewtonMathSolver

n = NewtonMathSolver('x - 1 = 10', 'x', 2, Tolerance(level=6))
print(n.iterate(10))
print(n.result)

与之等效的是

from NewtonMathSolver import Tolerance, NewtonMathSolver

n = NewtonMathSolver('x - 1 = 10', 'x', 2, 1e-6)
print(n.iterate(10))
print(n.result)

进一步探究

这样可以看到计算步骤

from NewtonMathSolver import Tolerance, NewtonMathSolver

n = NewtonMathSolver('x - 1 = 10', 'x', 2, Tolerance(level=6))

while 1 + 1 == 2:
    n.iterate()
    print(n.result)
    if n.result.result:
        break

容忍与误差

使用牛顿法通常会有误差,于是我们会将误差与设定的容忍度进行对比,容忍度即误差允许的最大值

这样可以设定一个可操作的误差

from NewtonMathSolver import Tolerance

t = Tolerance(1e-6)

与之等价的是

from NewtonMathSolver import Tolerance

t = Tolerance(level=6)

以及

from NewtonMathSolver import Tolerance

t = Tolerance(1e-6, 666)
# 若是同时存在两个参数,则tolerance优先

其可以进行多种操作

自增

from NewtonMathSolver import Tolerance

t = Tolerance(level=6)

以下操作均允许

t + 1
print('t + 1', t)

t + 1 1.000001

t - 1
print('t - 1', t)

t - 1 9.999999999177334e-07

t * 2
print('t * 2', t)

t * 2 1.9999999998354667e-06

t / 2
print('t / 2', t)

t / 2 9.999999999177334e-07

t ** 2
print('t ** 2', t)

t ** 2 9.999999998354668e-13

t ** (1 / 2)
print('t ** (1 / 2)', t)

t ** (1 / 2) 9.999999999177334e-07

t.more()
print('more', t)

more 9.999999999177333e-08

t.less()
print('less', t)

less 9.999999999177334e-07

逻辑运算

print(t == 0)
print(t > 0)
print(t >= 0)
print(t < 1)
print(t <= 1e-7)

False

True

True

True

False

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

NewtonMathSolver-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

NewtonMathSolver-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file NewtonMathSolver-0.1.0.tar.gz.

File metadata

  • Download URL: NewtonMathSolver-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for NewtonMathSolver-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3303168a9958a4e4ff896702435d7a216369fe31d2feb6286cbc464e0a2d1578
MD5 9381811b775df004f9b17ad48856c0a3
BLAKE2b-256 c9d63f55cfe4f2101321f98a0ba2dce6486235ed9da65889ed801b55b11610b3

See more details on using hashes here.

File details

Details for the file NewtonMathSolver-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for NewtonMathSolver-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7e238ca106950b1ef517f9ad87d9b5881abdc02bb7fac3d357c3642109163cb
MD5 db4ea88cdc8f744e34bd4f166462997c
BLAKE2b-256 b633c3e2a253b92d4ee6c68dd9e3dc869b17f4a996f6b24b8a9a6de7bdcbbb19

See more details on using hashes here.

Supported by

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