root / pobysoPythonSage / src / testPobyso.sage @ 155
Historique | Voir | Annoter | Télécharger (11,19 ko)
1 | 116 | storres | """ |
---|---|---|---|
2 | 116 | storres | Test module for pobyso.py functions. |
3 | 116 | storres | |
4 | 116 | storres | AUTHORS: |
5 | 116 | storres | - S.T. (2014-02-26): initial version |
6 | 116 | storres | |
7 | 116 | storres | TODO: |
8 | 116 | storres | - everything! |
9 | 116 | storres | |
10 | 116 | storres | """ |
11 | 116 | storres | |
12 | 116 | storres | print "\ntestPobyso loading..." |
13 | 116 | storres | # |
14 | 116 | storres | import inspect |
15 | 116 | storres | import resource |
16 | 117 | storres | import timeit |
17 | 116 | storres | |
18 | 116 | storres | |
19 | 116 | storres | def memory_usage_resource(): |
20 | 116 | storres | rusage_denom = RR("1024") |
21 | 116 | storres | if sys.platform == 'darwin': |
22 | 116 | storres | # ... it seems that in OSX the output is different units ... |
23 | 116 | storres | rusage_denom = rusage_denom * rusage_denom |
24 | 116 | storres | mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom |
25 | 116 | storres | return mem |
26 | 116 | storres | # End memory_usage_resource() |
27 | 116 | storres | |
28 | 117 | storres | def test_pobyso_wrapper(func, *args, **kwargs): |
29 | 117 | storres | def wrapped(): |
30 | 117 | storres | return func(*args, **kwargs) |
31 | 117 | storres | return wrapped |
32 | 117 | storres | |
33 | 116 | storres | # |
34 | 116 | storres | def test_pobyso_absolute_so_so(): |
35 | 116 | storres | for index in xrange(0,1000000): |
36 | 116 | storres | absoluteSo = pobyso_absolute_so_so() |
37 | 116 | storres | sollya_lib_clear_obj(absoluteSo) |
38 | 116 | storres | # End test_absolute_so_so |
39 | 116 | storres | |
40 | 116 | storres | def test_pobyso_autoprint_so_so(iterationsNum=1000): |
41 | 116 | storres | absoluteSo = pobyso_absolute_so_so() |
42 | 116 | storres | for index in xrange(0,iterationsNum): |
43 | 116 | storres | pobyso_autoprint(absoluteSo) |
44 | 116 | storres | sollya_lib_clear_obj(absoluteSo) |
45 | 116 | storres | # End test_pobyso_autoprint |
46 | 116 | storres | |
47 | 117 | storres | def test_pobyso_bounds_to_range_sa_so(repeat=1000, number=10): |
48 | 117 | storres | functionName = inspect.stack()[0][3] |
49 | 117 | storres | print "Running", inspect.stack()[0][3], "..." |
50 | 116 | storres | lowerBoundSa = RR(1) |
51 | 116 | storres | upperBoundSa = RR(2) |
52 | 116 | storres | precSo = pobyso_get_prec_so() |
53 | 117 | storres | #pobyso_autoprint(precSo) |
54 | 117 | storres | # |
55 | 117 | storres | def test(lowerBoundSa, upperBoundSa): |
56 | 116 | storres | rangeSo = pobyso_bounds_to_range_sa_so(lowerBoundSa, upperBoundSa) |
57 | 116 | storres | sollya_lib_clear_obj(rangeSo) |
58 | 117 | storres | # |
59 | 117 | storres | wrapped = test_pobyso_wrapper(test,lowerBoundSa, upperBoundSa) |
60 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
61 | 117 | storres | # |
62 | 116 | storres | precSo = pobyso_get_prec_so() |
63 | 117 | storres | #pobyso_autoprint(precSo) |
64 | 116 | storres | sollya_lib_clear_obj(precSo) |
65 | 117 | storres | print "\t...", functionName, "done." |
66 | 117 | storres | return timing |
67 | 116 | storres | # End test_pobyso_bounds_to_range_sa_so |
68 | 116 | storres | |
69 | 120 | storres | def test_pobyso_clear_taylorform_sa_so(repeat=1000, number=10): |
70 | 120 | storres | functionName = inspect.stack()[0][3] |
71 | 120 | storres | print "Running", inspect.stack()[0][3], "..." |
72 | 120 | storres | intervalSa = RIF(1,2) |
73 | 120 | storres | functionSo = pobyso_parse_string("exp(x)") |
74 | 120 | storres | degreeSo = pobyso_ |
75 | 120 | storres | #RealIntervalField |
76 | 120 | storres | pointSo = pobyso_constant_sa_so(RR("1.5")) |
77 | 120 | storres | intervalSo = pobyso_interval_to_range_sa_so(intervalSa) |
78 | 120 | storres | # |
79 | 120 | storres | def test(lowerBoundSa, upperBoundSa): |
80 | 120 | storres | taylorformSo = pobyso_taylorform_so_so(functionSo, |
81 | 120 | storres | degreeSo, |
82 | 120 | storres | pointSo, |
83 | 120 | storres | intervalSo) |
84 | 120 | storres | sollya_lib_clear_obj(rangeSo) |
85 | 120 | storres | # |
86 | 120 | storres | wrapped = test_pobyso_wrapper(test,lowerBoundSa, upperBoundSa) |
87 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
88 | 120 | storres | # |
89 | 120 | storres | precSo = pobyso_get_prec_so() |
90 | 120 | storres | #pobyso_autoprint(precSo) |
91 | 120 | storres | sollya_lib_clear_obj(precSo) |
92 | 120 | storres | print "\t...", functionName, "done." |
93 | 120 | storres | return timing |
94 | 120 | storres | # End test_pobyso_bounds_to_range_sa_so |
95 | 120 | storres | |
96 | 155 | storres | def test_pobyso_error_so(repeat=1000, number=10): |
97 | 155 | storres | functionName = inspect.stack()[0][3] |
98 | 155 | storres | print "Running", inspect.stack()[0][3], "..." |
99 | 155 | storres | # |
100 | 155 | storres | def test(): |
101 | 155 | storres | errorSo = pobyso_error_so() |
102 | 155 | storres | sollya_lib_clear_obj(errorSo) |
103 | 155 | storres | # |
104 | 155 | storres | wrapped = test_pobyso_wrapper(test) |
105 | 155 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
106 | 155 | storres | print "\t...", functionName, "done." |
107 | 155 | storres | return timing |
108 | 155 | storres | # End test_pobyso_error_so |
109 | 155 | storres | |
110 | 117 | storres | def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10): |
111 | 117 | storres | functionName = inspect.stack()[0][3] |
112 | 117 | storres | print "Running", functionName, "..." |
113 | 117 | storres | def test(): |
114 | 117 | storres | listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];") |
115 | 117 | storres | (listSaSo, elementsNumSa, isEndEllipticSa) = \ |
116 | 117 | storres | pobyso_get_list_elements(listSo) |
117 | 117 | storres | for elementSo in listSaSo: |
118 | 117 | storres | sollya_lib_clear_obj(elementSo) |
119 | 117 | storres | # Notice that we use sollya_lib_clear_obj and not |
120 | 117 | storres | # sollya_lib_clear_object_list. |
121 | 117 | storres | sollya_lib_clear_obj(listSo, None) |
122 | 117 | storres | # |
123 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
124 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
125 | 117 | storres | print "\t...", functionName, "done." |
126 | 117 | storres | return timing |
127 | 117 | storres | # End test_pobyso_get_list_elements |
128 | 117 | storres | |
129 | 120 | storres | def test_pobyso_get_prec_so_sa(repeat=1000, number=10): |
130 | 120 | storres | functionName = inspect.stack()[0][3] |
131 | 120 | storres | print "Running", functionName, "..." |
132 | 120 | storres | def test(): |
133 | 116 | storres | precSa = pobyso_get_prec_so_sa() |
134 | 120 | storres | wrapped = test_pobyso_wrapper(test) |
135 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
136 | 120 | storres | print "\t...", functionName, "done." |
137 | 120 | storres | return timing |
138 | 116 | storres | # End test_pobyso_get_prec_so_sa |
139 | 116 | storres | |
140 | 120 | storres | def test_pobyso_get_prec_of_constant_so_sa(repeat=1000, number=10): |
141 | 120 | storres | functionName = inspect.stack()[0][3] |
142 | 120 | storres | print "Running", functionName, "..." |
143 | 120 | storres | constSo = pobyso_constant_1_sa_so() |
144 | 120 | storres | def test(): |
145 | 116 | storres | precSa = pobyso_get_prec_of_constant_so_sa(constSo) |
146 | 120 | storres | wrapped = test_pobyso_wrapper(test) |
147 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
148 | 154 | storres | print "\t...", functionName, "done." |
149 | 120 | storres | sollya_lib_clear_obj(constSo,None) |
150 | 120 | storres | return timing |
151 | 116 | storres | # End test_pobyso_get_prec_of_constant_so_sa |
152 | 116 | storres | |
153 | 154 | storres | def test_pobyso_guess_degree_sa_sa(repeat=1000, number=10): |
154 | 154 | storres | functionName = inspect.stack()[0][3] |
155 | 154 | storres | print "Running", functionName, "..." |
156 | 154 | storres | funcExpSa(x) = exp(x) |
157 | 154 | storres | RRIF = RealIntervalField(113) |
158 | 154 | storres | intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
159 | 154 | storres | print "Interval:", intervalSa.str(style='brackets') |
160 | 154 | storres | errorSa = RR(2^-227) |
161 | 154 | storres | weightSa(x) = 1 |
162 | 154 | storres | degreeBoundSa = 150 |
163 | 154 | storres | # Function definition |
164 | 154 | storres | def test(): |
165 | 154 | storres | pobyso_guess_degree_sa_sa(funcExpSa, |
166 | 154 | storres | intervalSa, |
167 | 154 | storres | errorSa, |
168 | 154 | storres | weightSa, |
169 | 154 | storres | degreeBoundSa) |
170 | 154 | storres | wrapped = test_pobyso_wrapper(test) |
171 | 154 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
172 | 154 | storres | print "\t...", functionName, "done." |
173 | 154 | storres | return timing |
174 | 154 | storres | # End test_pobyso_guess_degree_sa_sa |
175 | 154 | storres | |
176 | 154 | storres | def test_pobyso_guess_degree_so_sa(repeat=1000, number=10): |
177 | 154 | storres | functionName = inspect.stack()[0][3] |
178 | 154 | storres | print "Running", functionName, "..." |
179 | 153 | storres | funcExpSo = pobyso_parse_string_sa_so("exp(x)") |
180 | 153 | storres | RRIF = RealIntervalField(113) |
181 | 153 | storres | intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
182 | 154 | storres | print "Interval:", intervalSa.str(style='brackets') |
183 | 153 | storres | intervalSo = pobyso_interval_to_range_sa_so(intervalSa) |
184 | 154 | storres | errorSo = pobyso_constant_sa_so(RR(2^-227)) |
185 | 154 | storres | weightSo=pobyso_parse_string_sa_so("1") |
186 | 154 | storres | degreeBoundSo = pobyso_constant_from_int_sa_so(150) |
187 | 154 | storres | # Function definition |
188 | 154 | storres | def test(): |
189 | 154 | storres | pobyso_guess_degree_so_sa(funcExpSo, |
190 | 154 | storres | intervalSo, |
191 | 154 | storres | errorSo, |
192 | 154 | storres | weightSo, |
193 | 155 | storres | degreeBoundSo) |
194 | 155 | storres | """ |
195 | 155 | storres | pobyso_guess_degree_so_sa(funcExpSo, |
196 | 155 | storres | intervalSo, |
197 | 155 | storres | errorSo, |
198 | 155 | storres | None, |
199 | 155 | storres | None) |
200 | 155 | storres | """ |
201 | 154 | storres | wrapped = test_pobyso_wrapper(test) |
202 | 154 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
203 | 154 | storres | print "\t...", functionName, "done." |
204 | 154 | storres | sollya_lib_clear_obj(funcExpSo) |
205 | 154 | storres | sollya_lib_clear_obj(intervalSo) |
206 | 154 | storres | sollya_lib_clear_obj(errorSo) |
207 | 154 | storres | sollya_lib_clear_obj(weightSo) |
208 | 154 | storres | sollya_lib_clear_obj(degreeBoundSo) |
209 | 154 | storres | return timing |
210 | 154 | storres | # End test_pobyso_guess_degree_so_sa |
211 | 153 | storres | |
212 | 155 | storres | def test_pobyso_is_error_so_sa(repeat=1000, number=10): |
213 | 155 | storres | functionName = inspect.stack()[0][3] |
214 | 155 | storres | print "Running", inspect.stack()[0][3], "..." |
215 | 155 | storres | # |
216 | 155 | storres | def test(): |
217 | 155 | storres | errorSo = pobyso_error_so() |
218 | 155 | storres | if not pobyso_is_error_so_sa(errorSo): |
219 | 155 | storres | print "Error!" |
220 | 155 | storres | sollya_lib_clear_obj(errorSo) |
221 | 155 | storres | # |
222 | 155 | storres | wrapped = test_pobyso_wrapper(test) |
223 | 155 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
224 | 155 | storres | print "\t...", functionName, "done." |
225 | 155 | storres | return timing |
226 | 155 | storres | # End test_pobyso_error_so |
227 | 155 | storres | |
228 | 116 | storres | def test_pobyso_lib_init(iterationsNum=10000): |
229 | 116 | storres | """ |
230 | 116 | storres | Must be called exactly once. Leaks lots of memory otherwise. |
231 | 116 | storres | """ |
232 | 116 | storres | print "Running", inspect.stack()[0][3] |
233 | 116 | storres | for index in xrange(0,iterationsNum): |
234 | 116 | storres | pobyso_lib_init() |
235 | 116 | storres | #pobyso_lib_close() |
236 | 116 | storres | # End test_pobyso_lib_init |
237 | 116 | storres | |
238 | 116 | storres | def test_pobyso_parse_string_sa_so(iterationsNum=10000): |
239 | 116 | storres | print "Running", inspect.stack()[0][3] |
240 | 116 | storres | for index in xrange(0,iterationsNum): |
241 | 116 | storres | expressionSo = pobyso_parse_string_sa_so('exp(x)+2*x*sin(x)') |
242 | 116 | storres | sollya_lib_clear_obj(expressionSo) |
243 | 116 | storres | # End pobyso_parse_string_sa_so |
244 | 116 | storres | |
245 | 117 | storres | def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10): |
246 | 117 | storres | functionName = inspect.stack()[0][3] |
247 | 117 | storres | print "Running", functionName, "..." |
248 | 116 | storres | functionSo = pobyso_parse_string('exp(x)') |
249 | 116 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
250 | 116 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
251 | 116 | storres | errorTypeSo = pobyso_absolute_so_so() |
252 | 117 | storres | def test(functionSo, degreeSo, rangeSo, errorTypeSo): |
253 | 116 | storres | teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo, |
254 | 116 | storres | degreeSo, |
255 | 116 | storres | rangeSo, |
256 | 116 | storres | errorTypeSo) |
257 | 116 | storres | sollya_lib_clear_obj(teSo[0]) |
258 | 116 | storres | sollya_lib_clear_obj(teSo[1]) |
259 | 116 | storres | sollya_lib_clear_obj(teSo[2]) |
260 | 117 | storres | # End test |
261 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
262 | 117 | storres | functionSo, |
263 | 117 | storres | degreeSo, |
264 | 117 | storres | rangeSo, |
265 | 117 | storres | errorTypeSo) |
266 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
267 | 116 | storres | sollya_lib_clear_obj(functionSo) |
268 | 116 | storres | sollya_lib_clear_obj(degreeSo) |
269 | 116 | storres | sollya_lib_clear_obj(rangeSo) |
270 | 116 | storres | sollya_lib_clear_obj(errorTypeSo) |
271 | 117 | storres | print "\t...", functionName, "done." |
272 | 117 | storres | return timing |
273 | 116 | storres | # End test_pobyso_taylor_expansion_no_change_var_so_so |
274 | 116 | storres | |
275 | 120 | storres | def test_pobyso_taylorform_so_so_2(repeat=1000, number=10): |
276 | 117 | storres | functionName = inspect.stack()[0][3] |
277 | 117 | storres | print "Running", functionName, "..." |
278 | 117 | storres | functionSo = pobyso_parse_string('exp(x)') |
279 | 117 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
280 | 117 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
281 | 117 | storres | intervalCenterSo = sollya_lib_mid(rangeSo, None) |
282 | 117 | storres | errorTypeSo = pobyso_absolute_so_so() |
283 | 117 | storres | |
284 | 117 | storres | def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo): |
285 | 117 | storres | taylorformSo = pobyso_taylorform_so_so(functionSo, |
286 | 117 | storres | degreeSo, |
287 | 117 | storres | intervalCenterSo, |
288 | 117 | storres | errorTypeSo) |
289 | 117 | storres | sollya_lib_clear_obj(taylorformSo) |
290 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
291 | 117 | storres | functionSo, |
292 | 117 | storres | degreeSo, |
293 | 117 | storres | intervalCenterSo, |
294 | 117 | storres | errorTypeSo) |
295 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
296 | 117 | storres | sollya_lib_clear_obj(functionSo,None) |
297 | 117 | storres | sollya_lib_clear_obj(degreeSo,None) |
298 | 117 | storres | sollya_lib_clear_obj(rangeSo,None) |
299 | 117 | storres | sollya_lib_clear_obj(intervalCenterSo,None) |
300 | 117 | storres | sollya_lib_clear_obj(errorTypeSo,None) |
301 | 117 | storres | return timing |
302 | 117 | storres | # End test_pobyso_taylor_so_so_2 |
303 | 117 | storres | |
304 | 117 | storres | def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1): |
305 | 117 | storres | aString = "2+3" |
306 | 117 | storres | def test(): |
307 | 117 | storres | return 2+3 |
308 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
309 | 117 | storres | timing = timeit.timeit(wrapped, number=executionsNum) |
310 | 117 | storres | return timing |
311 | 120 | storres | print "\t...testPobyso loaded" |
312 | 120 | storres |