"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "mpmath/tests/test_calculus.py" between
mpmath-1.0.0.tar.gz and mpmath-1.1.0.tar.gz

About: mpmath is a Python library for arbitrary-precision floating-point arithmetic.

test_calculus.py  (mpmath-1.0.0):test_calculus.py  (mpmath-1.1.0)
import pytest
from mpmath import * from mpmath import *
def test_approximation(): def test_approximation():
mp.dps = 15 mp.dps = 15
f = lambda x: cos(2-2*x)/x f = lambda x: cos(2-2*x)/x
p, err = chebyfit(f, [2, 4], 8, error=True) p, err = chebyfit(f, [2, 4], 8, error=True)
assert err < 1e-5 assert err < 1e-5
for i in range(10): for i in range(10):
x = 2 + i/5. x = 2 + i/5.
assert abs(polyval(p, x) - f(x)) < err assert abs(polyval(p, x) - f(x)) < err
skipping to change at line 34 skipping to change at line 35
assert polyval(p,4,derivative=True) == (253, 190) assert polyval(p,4,derivative=True) == (253, 190)
def test_polyroots(): def test_polyroots():
p = polyroots([1,-4]) p = polyroots([1,-4])
assert p[0].ae(4) assert p[0].ae(4)
p, q = polyroots([1,2,3]) p, q = polyroots([1,2,3])
assert p.ae(-1 - sqrt(2)*j) assert p.ae(-1 - sqrt(2)*j)
assert q.ae(-1 + sqrt(2)*j) assert q.ae(-1 + sqrt(2)*j)
#this is not a real test, it only tests a specific case #this is not a real test, it only tests a specific case
assert polyroots([1]) == [] assert polyroots([1]) == []
try: pytest.raises(ValueError, lambda: polyroots([0]))
polyroots([0])
assert False
except ValueError:
pass
def test_polyroots_legendre(): def test_polyroots_legendre():
n = 64 n = 64
coeffs = [11975573020964041433067793888190275875, 0, coeffs = [11975573020964041433067793888190275875, 0,
-190100434726484311252477736051902332000, 0, -190100434726484311252477736051902332000, 0,
1437919688271127330313741595496589239248, 0, 1437919688271127330313741595496589239248, 0,
-6897338342113537600691931230430793911840, 0, -6897338342113537600691931230430793911840, 0,
23556405536185284408974715545252277554280, 0, 23556405536185284408974715545252277554280, 0,
-60969520211303089058522793175947071316960, 0, -60969520211303089058522793175947071316960, 0,
124284021969194758465450309166353645376880, 0, 124284021969194758465450309166353645376880, 0,
skipping to change at line 77 skipping to change at line 74
-205277590220215081719131470288800, 0, -205277590220215081719131470288800, 0,
9330799555464321896324157740400, 0, 9330799555464321896324157740400, 0,
-304114948474392713657972548576, 0, -304114948474392713657972548576, 0,
6695289961520387531608984680, 0, 6695289961520387531608984680, 0,
-91048139350447232095702560, 0, -91048139350447232095702560, 0,
659769125727878493447120, 0, 659769125727878493447120, 0,
-1905929106580294155360, 0, -1905929106580294155360, 0,
916312070471295267] 916312070471295267]
with mp.workdps(3): with mp.workdps(3):
try: with pytest.raises(mp.NoConvergence):
roots = polyroots(coeffs, maxsteps=5, cleanup=True, error=False, polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
extraprec=n*10) extraprec=n*10)
raise AssertionError("polyroots() didn't raise NoConvergence")
except (mp.NoConvergence):
pass
roots = polyroots(coeffs, maxsteps=50, cleanup=True, error=False, roots = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
extraprec=n*10) extraprec=n*10)
roots = [str(r) for r in roots] roots = [str(r) for r in roots]
assert roots == \ assert roots == \
['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961', ['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
'-0.946', '-0.93', '-0.911', '-0.889', '-0.866', '-0.841', '-0.946', '-0.93', '-0.911', '-0.889', '-0.866', '-0.841',
'-0.813', '-0.784', '-0.753', '-0.72', '-0.685', '-0.649', '-0.813', '-0.784', '-0.753', '-0.72', '-0.685', '-0.649',
'-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402', '-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402',
'-0.357', '-0.311', '-0.265', '-0.217', '-0.17', '-0.121', '-0.357', '-0.311', '-0.265', '-0.217', '-0.17', '-0.121',
skipping to change at line 151 skipping to change at line 145
'-0.073', '-0.0243', '0.0243', '0.073', '0.121', '-0.073', '-0.0243', '0.0243', '0.073', '0.121',
'0.17', '0.217', '0.265', ' 0.311', '0.357', '0.17', '0.217', '0.265', ' 0.311', '0.357',
'0.402', '0.446', '0.489', '0.531', '0.572', '0.402', '0.446', '0.489', '0.531', '0.572',
'0.611', '0.649', '0.685', '0.72', '0.753', '0.611', '0.649', '0.685', '0.72', '0.753',
'0.784', '0.813', '0.841', '0.866', '0.889', '0.784', '0.813', '0.841', '0.866', '0.889',
'0.911', '0.93', '0.946', '0.961', '0.973', '0.911', '0.93', '0.946', '0.961', '0.973',
'0.983', '0.991', '0.996', '0.999', '1.0']) '0.983', '0.991', '0.996', '0.999', '1.0'])
with mp.workdps(2*mp.dps): with mp.workdps(2*mp.dps):
roots_exact = polyroots(coeffs, maxsteps=50, cleanup=True, error=False, roots_exact = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
extraprec=2*extra_prec) extraprec=2*extra_prec)
try: with pytest.raises(mp.NoConvergence):
roots = polyroots(coeffs, maxsteps=5, cleanup=True, error=False, polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
extraprec=extra_prec) extraprec=extra_prec)
raise AssertionError("polyroots() didn't raise NoConvergence")
except (mp.NoConvergence):
pass
roots,err = polyroots(coeffs, maxsteps=5, cleanup=True, error=True, roots,err = polyroots(coeffs, maxsteps=5, cleanup=True, error=True,
extraprec=extra_prec,roots_init=roots_init) extraprec=extra_prec,roots_init=roots_init)
assert max(matrix(roots_exact)-matrix(roots).apply(abs)) < err assert max(matrix(roots_exact)-matrix(roots).apply(abs)) < err
roots1,err1 = polyroots(coeffs, maxsteps=25, cleanup=True, error=True, roots1,err1 = polyroots(coeffs, maxsteps=25, cleanup=True, error=True,
extraprec=extra_prec,roots_init=roots_init[:60]) extraprec=extra_prec,roots_init=roots_init[:60])
assert max(matrix(roots_exact)-matrix(roots1).apply(abs)) < err1 assert max(matrix(roots_exact)-matrix(roots1).apply(abs)) < err1
def test_pade(): def test_pade():
one = mpf(1) one = mpf(1)
mp.dps = 20 mp.dps = 20
 End of changes. 4 change blocks. 
17 lines changed or deleted 8 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)