-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
44 lines (31 loc) · 1.15 KB
/
tests.py
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
from infsumpy.methods.fixed_method import FixedSum
from infsumpy.methods.threshold_method import ThresholdSum
from infsumpy.methods.ratio_test_method import RatioTestSum
from infsumpy.methods.integral_test_method import IntegralTestSum
from infsumpy.infsum import infsum
from mpmath import exp, log, mp, pi
#################### Auxiliar function ####################
def f(n):
return n / (2 ** n)
def g(n):
return 1/n**2
def dg(n):
return 1/n
def equivalence(a, b, epsilon=0.0001):
if a == b:
return True
else:
return abs(a - b) < epsilon
#################### TESTS ##########################
def test_methods():
eps = 10**(-3)
fixed = infsum(f, 'fixed', 10**4, 1)
threshold = infsum(f, 'threshold', 10**4, 1, eps=eps)
ratio_test = infsum(f, 'ratio', 10**4, 1, L=1/2, eps=eps)
integral_test = infsum(g, 'integral', 10**3, 1, eps=eps, g=dg)
assert equivalence(fixed[1], 2, epsilon=eps)
assert equivalence(threshold[1], 2, epsilon=eps)
assert equivalence(ratio_test[1], 2, epsilon=eps)
assert equivalence(integral_test[1], pi**2/6, epsilon=eps)
if __name__ == '__main__':
test_methods()