-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
240 lines (197 loc) · 8.88 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from extrapolation.esum import (
esum,
acelsum,
partial_sum_list,
partial_sum_mp,
no_transform,
Aitken_transform,
Richardson_transform,
Epsilon_transform,
G_transform,
Levin_t_transform,
Levin_u_transform,
Levin_v_transform
)
from extrapolation.utils import create_lognumber
from mpmath import exp, log, mp
#################### Auxiliar function ####################
def basel_series(n: int):
return 1/(n)**2
def equivalence(a, b, epsilon=0.0001):
if a == b:
return True
else:
return abs(a - b) < epsilon
def check_lognumber(a, b):
if equivalence(a.value()[1], b.value()[1]):
return equivalence(a.value()[1], create_lognumber(0).value()[1]) or a.value()[0] == b.value()[0]
else:
return False
#################### TESTS utils ####################
def test_operations():
a = create_lognumber(-1)
b = create_lognumber(-2)
c = create_lognumber(-0.25)
d = create_lognumber(0)
e = create_lognumber(5)
### Test operators ###
## Addition ##
assert check_lognumber((a + b), create_lognumber(-3))
assert check_lognumber((a + c), create_lognumber(-1.25))
assert check_lognumber((a + d), create_lognumber(-1))
assert check_lognumber((a + e), create_lognumber(4))
assert check_lognumber((a + 2), create_lognumber(1))
assert check_lognumber((a + (-2)), create_lognumber(-3))
## Subtraction ##
assert check_lognumber((a - b), create_lognumber(1))
assert check_lognumber((a - c), create_lognumber(-0.75))
assert check_lognumber((a - d), create_lognumber(-1))
assert check_lognumber((a - e), create_lognumber(-6))
assert check_lognumber((a - 2), create_lognumber(-3))
assert check_lognumber((a - (-2)), create_lognumber(1))
## Multiplication ##
assert check_lognumber((a * b), create_lognumber(2))
assert check_lognumber((a * c), create_lognumber(0.25))
assert check_lognumber((a * d), create_lognumber(0))
assert check_lognumber((a * e), create_lognumber(-5))
assert check_lognumber((a * 2), create_lognumber(-2))
assert check_lognumber((a * (-2)), create_lognumber(2))
## Division ##
assert check_lognumber((a / b), create_lognumber(0.5))
assert check_lognumber((a / c), create_lognumber(4))
assert check_lognumber((a / e), create_lognumber(-0.2))
assert check_lognumber((a / 2), create_lognumber(-0.5))
assert check_lognumber((a / (-2)), create_lognumber(0.5))
## Negation ##
assert check_lognumber((-a), create_lognumber(1))
assert check_lognumber((-e), create_lognumber(-5))
## Power ##
assert check_lognumber((a ** 2), create_lognumber(1))
assert check_lognumber((a ** 1), create_lognumber(-1))
assert check_lognumber((a ** 0), create_lognumber(1))
assert check_lognumber((a ** -1), create_lognumber(-1))
### Test comparison operators ###
assert (a == b) == False
assert (a == c) == False
assert a < b
assert a <= b
assert a <= a
assert b > a
assert b >= a
### Test return number ###
assert equivalence(a.exp(), (-1) * exp(log(1)))
assert equivalence(e.exp(), (+1) * exp(log(5)))
### Test properties ###
assert check_lognumber((a + (-a)), create_lognumber(0))
assert check_lognumber((a * (a**-1)), create_lognumber(1))
assert check_lognumber((b + (-b)), create_lognumber(0))
assert check_lognumber((b * (b**-1)), create_lognumber(1))
assert check_lognumber((c + (-c)), create_lognumber(0))
assert check_lognumber((c * (c**-1)), create_lognumber(1))
assert check_lognumber((e + (-e)), create_lognumber(0))
assert check_lognumber((e * (e**-1)), create_lognumber(1))
### Test examples ###
assert check_lognumber((- b * e - e ** 2), create_lognumber(-15))
assert check_lognumber((a * b * c * d * e), create_lognumber(0))
assert check_lognumber((c / e + a ** 2), create_lognumber(0.95))
assert check_lognumber((b + (-b)), create_lognumber(0))
assert check_lognumber((b * (b**-1)), create_lognumber(1))
#################### TESTS lists ####################
def test_partial_sum_list():
array = partial_sum_list(basel_series, 4)
assert type(array) == list
assert len(array) == 4
assert equivalence(array[0].exp(), 1)
assert equivalence(array[1].exp(), 5/4)
assert equivalence(array[2].exp(), 49/36)
assert equivalence(array[3].exp(), 205/144)
def test_len_transformations_list():
assert len(no_transform(partial_sum_list(basel_series, 10), lib='math')) == 10
assert len(Aitken_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
assert len(Richardson_transform(partial_sum_list(basel_series, 10), lib='math')) == 5
assert len(Epsilon_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
assert len(G_transform(partial_sum_list(basel_series, 10), lib='math')) == 7
assert len(Levin_t_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
assert len(Levin_u_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
assert len(Levin_v_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
def test_simple_acceleration_list():
n, acel = esum(basel_series, "None", error=0.1)
assert type(n) == int
assert type(acel) == list
assert len(acel) == n
n, acel = esum(basel_series, "Aitken", error=0.01)
assert type(n) == int
assert type(acel) == list
assert len(acel) == n-2
acel = acelsum(basel_series, "None", n=10, logarithm=False)
assert type(acel) == list
assert type(acel[-1]) == float
assert len(acel) == 10
acel = acelsum(basel_series, "Aitken", n=10, logarithm=False)
assert type(acel) == list
assert type(acel[-1]) == float
assert len(acel) == 8
#################### TESTS mpmath ####################
def test_partial_sum_mp():
array = partial_sum_mp(basel_series, 4)
assert type(array) == list
assert len(array) == 4
assert equivalence(array[0].exp(), 1)
assert equivalence(array[1].exp(), 5/4)
assert equivalence(array[2].exp(), 49/36)
assert equivalence(array[3].exp(), 205/144)
def test_len_transformations_mp():
mp.prec = 100
assert len(no_transform(partial_sum_mp(basel_series, 10), lib='mpmath')) == 10
assert len(Aitken_transform(partial_sum_mp(basel_series, 10), lib='mpmath')) == 8
assert len(Richardson_transform(partial_sum_mp(basel_series, 10), lib='mpmath')) == 5
assert len(Epsilon_transform(partial_sum_mp(basel_series, 10), lib='mpmath')) == 8
assert len(G_transform(partial_sum_mp(basel_series, 10), lib='mpmath')) == 7
assert len(Levin_t_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
assert len(Levin_u_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
assert len(Levin_v_transform(partial_sum_list(basel_series, 10), lib='math')) == 8
def test_simple_acceleration_mp():
n, acel = esum(basel_series, "None", error=0.1, precision=100)
assert type(n) == int
assert type(acel) == list
assert len(acel) == n
n, acel = esum(basel_series, "Aitken", error=0.01, precision=100)
assert type(n) == int
assert type(acel) == list
assert len(acel) == n-2
acel = acelsum(basel_series, "None", n=10, logarithm=False, precision=100)
assert type(acel) == list
assert type(acel[-1]) == mp.mpf
assert len(acel) == 10
acel = acelsum(basel_series, "Aitken", n=10, logarithm=False, precision=100)
assert type(acel) == list
assert type(acel[-1]) == mp.mpf
assert len(acel) == 8
#################### TESTS transformations in acelsum and esum ####################
def test_transformation_in_acelsum():
assert type(acelsum(basel_series, "None", n=10)) == list
assert type(acelsum(basel_series, "Aitken", n=10)) == list
assert type(acelsum(basel_series, "Richardson", n=10)) == list
assert type(acelsum(basel_series, "Epsilon", n=10)) == list
assert type(acelsum(basel_series, "G", n=10)) == list
assert type(acelsum(basel_series, "Levin-t", n=10)) == list
assert type(acelsum(basel_series, "Levin-u", n=10)) == list
assert type(acelsum(basel_series, "Levin-v", n=10)) == list
def test_transformation_in_esum():
assert type(esum(basel_series, "None", error=0.1)) == tuple
assert type(esum(basel_series, "Aitken", error=0.1)) == tuple
assert type(esum(basel_series, "Richardson", error=0.1)) == tuple
assert type(esum(basel_series, "Epsilon", error=0.1)) == tuple
assert type(esum(basel_series, "G", error=0.1)) == tuple
assert type(esum(basel_series, "Levin-t", error=0.1)) == tuple
assert type(esum(basel_series, "Levin-u", error=0.1)) == tuple
assert type(esum(basel_series, "Levin-v", error=0.1)) == tuple
if __name__ == '__main__':
test_partial_sum_list()
test_len_transformations_list()
test_simple_acceleration_list()
test_partial_sum_mp()
test_len_transformations_mp()
test_simple_acceleration_mp()
test_transformation_in_acelsum()
test_transformation_in_esum()