/usr/lib/python3/dist-packages/mpmath/tests/test_calculus.py is in python3-mpmath 0.19-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | from mpmath import *
def test_approximation():
mp.dps = 15
f = lambda x: cos(2-2*x)/x
p, err = chebyfit(f, [2, 4], 8, error=True)
assert err < 1e-5
for i in range(10):
x = 2 + i/5.
assert abs(polyval(p, x) - f(x)) < err
def test_limits():
mp.dps = 15
assert limit(lambda x: (x-sin(x))/x**3, 0).ae(mpf(1)/6)
assert limit(lambda n: (1+1/n)**n, inf).ae(e)
def test_polyval():
assert polyval([], 3) == 0
assert polyval([0], 3) == 0
assert polyval([5], 3) == 5
# 4x^3 - 2x + 5
p = [4, 0, -2, 5]
assert polyval(p,4) == 253
assert polyval(p,4,derivative=True) == (253, 190)
def test_polyroots():
p = polyroots([1,-4])
assert p[0].ae(4)
p, q = polyroots([1,2,3])
assert p.ae(-1 - sqrt(2)*j)
assert q.ae(-1 + sqrt(2)*j)
#this is not a real test, it only tests a specific case
assert polyroots([1]) == []
try:
polyroots([0])
assert False
except ValueError:
pass
def test_polyroots_legendre():
n = 64
coeffs = [11975573020964041433067793888190275875, 0,
-190100434726484311252477736051902332000, 0,
1437919688271127330313741595496589239248, 0,
-6897338342113537600691931230430793911840, 0,
23556405536185284408974715545252277554280, 0,
-60969520211303089058522793175947071316960, 0,
124284021969194758465450309166353645376880, 0,
-204721258548015217049921875719981284186016, 0,
277415422258095841688223780704620656114900, 0,
-313237834141273382807123548182995095192800, 0,
297432255354328395601259515935229287637200, 0,
-239057700565161140389797367947941296605600, 0,
163356095386193445933028201431093219347160, 0,
-95158890516229191805647495979277603503200, 0,
47310254620162038075933656063247634556400, 0,
-20071017111583894941305187420771723751200, 0,
7255051932731034189479516844750603752850, 0,
-2228176940331017311443863996901733412640, 0,
579006552594977616773047095969088431600, 0,
-126584428502545713788439446082310831200, 0,
23112325428835593809686977515028663000, 0,
-3491517141958743235617737161547844000, 0,
431305058712550634988073414073557200, 0,
-42927166660756742088912492757452000, 0,
3378527005707706553294038781836500, 0,
-205277590220215081719131470288800, 0,
9330799555464321896324157740400, 0,
-304114948474392713657972548576, 0,
6695289961520387531608984680, 0,
-91048139350447232095702560, 0,
659769125727878493447120, 0,
-1905929106580294155360, 0,
916312070471295267]
with mp.workdps(3):
try:
roots = polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
extraprec=n*10)
raise AssertionError("polyroots() didn't raise NoConvergence")
except (mp.NoConvergence):
pass
roots = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
extraprec=n*10)
roots = [str(r) for r in roots]
assert roots == \
['-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.813', '-0.784', '-0.753', '-0.72', '-0.685', '-0.649',
'-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.073', '-0.0243', '0.0243', '0.073', '0.121', '0.17', '0.217',
'0.265', '0.311', '0.357', '0.402', '0.446', '0.489', '0.531',
'0.572', '0.611', '0.649', '0.685', '0.72', '0.753', '0.784',
'0.813', '0.841', '0.866', '0.889', '0.911', '0.93', '0.946',
'0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
def test_pade():
one = mpf(1)
mp.dps = 20
N = 10
a = [one]
k = 1
for i in range(1, N+1):
k *= i
a.append(one/k)
p, q = pade(a, N//2, N//2)
for x in arange(0, 1, 0.1):
r = polyval(p[::-1], x)/polyval(q[::-1], x)
assert(r.ae(exp(x), 1.0e-10))
mp.dps = 15
def test_fourier():
mp.dps = 15
c, s = fourier(lambda x: x+1, [-1, 2], 2)
#plot([lambda x: x+1, lambda x: fourierval((c, s), [-1, 2], x)], [-1, 2])
assert c[0].ae(1.5)
assert c[1].ae(-3*sqrt(3)/(2*pi))
assert c[2].ae(3*sqrt(3)/(4*pi))
assert s[0] == 0
assert s[1].ae(3/(2*pi))
assert s[2].ae(3/(4*pi))
assert fourierval((c, s), [-1, 2], 1).ae(1.9134966715663442)
def test_differint():
mp.dps = 15
assert differint(lambda t: t, 2, -0.5).ae(8*sqrt(2/pi)/3)
|