Statistiques
| Révision :

root / pobysoPythonSage / src / testPobyso.sage @ 225

Historique | Voir | Annoter | Télécharger (15,2 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 160 storres
- almost everything!
9 116 storres
10 116 storres
"""
11 204 storres
print "testPobyso loading..."
12 116 storres
#
13 116 storres
import inspect
14 116 storres
import resource
15 117 storres
import timeit
16 116 storres
17 116 storres
18 116 storres
def memory_usage_resource():
19 116 storres
    rusage_denom = RR("1024")
20 116 storres
    if sys.platform == 'darwin':
21 116 storres
        # ... it seems that in OSX the output is different units ...
22 160 storres
        rusage_denom = rusage_denom^2
23 116 storres
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
24 116 storres
    return mem
25 116 storres
# End memory_usage_resource()
26 116 storres
27 117 storres
def test_pobyso_wrapper(func, *args, **kwargs):
28 117 storres
    def wrapped():
29 117 storres
        return func(*args, **kwargs)
30 117 storres
    return wrapped
31 117 storres
32 116 storres
#
33 210 storres
def test_pobyso_absolute_so_so(repeat=10000, number=100):
34 210 storres
    functionName =  inspect.stack()[0][3]
35 210 storres
    print "Running", inspect.stack()[0][3], "..."
36 210 storres
    #
37 210 storres
    def test():
38 116 storres
        absoluteSo = pobyso_absolute_so_so()
39 116 storres
        sollya_lib_clear_obj(absoluteSo)
40 210 storres
    #
41 210 storres
    wrapped = test_pobyso_wrapper(test)
42 210 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
43 210 storres
    print "\t...", functionName, "done."
44 210 storres
    return timing
45 116 storres
# End test_absolute_so_so
46 116 storres
47 116 storres
def test_pobyso_autoprint_so_so(iterationsNum=1000):
48 116 storres
    absoluteSo = pobyso_absolute_so_so()
49 116 storres
    for index in xrange(0,iterationsNum):
50 116 storres
        pobyso_autoprint(absoluteSo)
51 116 storres
    sollya_lib_clear_obj(absoluteSo)
52 116 storres
# End test_pobyso_autoprint
53 116 storres
54 117 storres
def test_pobyso_bounds_to_range_sa_so(repeat=1000, number=10):
55 117 storres
    functionName =  inspect.stack()[0][3]
56 117 storres
    print "Running", inspect.stack()[0][3], "..."
57 116 storres
    lowerBoundSa = RR(1)
58 116 storres
    upperBoundSa = RR(2)
59 116 storres
    precSo = pobyso_get_prec_so()
60 117 storres
    #pobyso_autoprint(precSo)
61 117 storres
    #
62 117 storres
    def test(lowerBoundSa, upperBoundSa):
63 116 storres
        rangeSo = pobyso_bounds_to_range_sa_so(lowerBoundSa, upperBoundSa)
64 116 storres
        sollya_lib_clear_obj(rangeSo)
65 117 storres
    #
66 117 storres
    wrapped = test_pobyso_wrapper(test,lowerBoundSa, upperBoundSa)
67 117 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
68 117 storres
    #
69 116 storres
    precSo = pobyso_get_prec_so()
70 117 storres
    #pobyso_autoprint(precSo)
71 116 storres
    sollya_lib_clear_obj(precSo)
72 117 storres
    print "\t...", functionName, "done."
73 117 storres
    return timing
74 116 storres
# End test_pobyso_bounds_to_range_sa_so
75 116 storres
76 120 storres
def test_pobyso_clear_taylorform_sa_so(repeat=1000, number=10):
77 120 storres
    functionName =  inspect.stack()[0][3]
78 120 storres
    print "Running", inspect.stack()[0][3], "..."
79 120 storres
    intervalSa = RIF(1,2)
80 120 storres
    functionSo = pobyso_parse_string("exp(x)")
81 159 storres
    degreeSo   = pobyso_constant_from_int_sa_so(4)
82 120 storres
    #RealIntervalField
83 120 storres
    pointSo    = pobyso_constant_sa_so(RR("1.5"))
84 120 storres
    intervalSo = pobyso_interval_to_range_sa_so(intervalSa)
85 120 storres
    #
86 162 storres
    def test(functionSo, degreeSo, pointSo, intervalSo):
87 120 storres
        taylorformSo = pobyso_taylorform_so_so(functionSo,
88 120 storres
                                               degreeSo,
89 120 storres
                                               pointSo,
90 120 storres
                                               intervalSo)
91 162 storres
        pobyso_clear_taylorform_sa_so(taylorformSo)
92 120 storres
    #
93 162 storres
    wrapped = test_pobyso_wrapper(test,
94 162 storres
                                  functionSo,
95 162 storres
                                  degreeSo,
96 162 storres
                                  pointSo,
97 162 storres
                                  intervalSo)
98 120 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
99 120 storres
    #
100 162 storres
    sollya_lib_clear_obj(functionSo)
101 162 storres
    sollya_lib_clear_obj(degreeSo)
102 162 storres
    sollya_lib_clear_obj(pointSo)
103 162 storres
    sollya_lib_clear_obj(intervalSo)
104 120 storres
    print "\t...", functionName, "done."
105 120 storres
    return timing
106 120 storres
# End test_pobyso_bounds_to_range_sa_so
107 120 storres
108 210 storres
def test_pobyso_float_poly_sa_so(repeat=100, number=10):
109 210 storres
    functionName = inspect.stack()[0][3]
110 210 storres
    print "Running", functionName,"..."
111 210 storres
    polynomialsList = []
112 210 storres
113 210 storres
    i = var('i')
114 210 storres
    RRR         = RealField(12)
115 210 storres
    P_RRR       = RRR[i]
116 210 storres
    polynomialsList.append(P_RRR(1/7 + 2/7*i + 3/7*i^2))
117 210 storres
    RRR         = RealField(53)
118 210 storres
    P_RRR       = RRR[i]
119 210 storres
    polynomialsList.append(P_RRR(1/7 + 2/7*i + 3/7*i^2))
120 210 storres
    RRR         = RealField(1024)
121 210 storres
    P_RRR       = RRR[i]
122 210 storres
    polynomialsList.append(P_RRR(1/7 + 2/7*i + 3/7*i^2))
123 210 storres
    def test(polynomialsList):
124 210 storres
        for polynomial in polynomialsList:
125 210 storres
            polySo = pobyso_float_poly_sa_so(polynomial)
126 210 storres
            sollya_lib_clear_obj(polySo)
127 210 storres
128 210 storres
    wrapped = test_pobyso_wrapper(test,
129 210 storres
                                  polynomialsList)
130 210 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
131 210 storres
    print "\t...", functionName, "done."
132 210 storres
    return timing
133 210 storres
# End test_pobyso_float_poly_sa_so
134 210 storres
135 155 storres
def test_pobyso_error_so(repeat=1000, number=10):
136 155 storres
    functionName =  inspect.stack()[0][3]
137 155 storres
    print "Running", inspect.stack()[0][3], "..."
138 155 storres
    #
139 155 storres
    def test():
140 155 storres
        errorSo = pobyso_error_so()
141 155 storres
        sollya_lib_clear_obj(errorSo)
142 155 storres
    #
143 155 storres
    wrapped = test_pobyso_wrapper(test)
144 155 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
145 155 storres
    print "\t...", functionName, "done."
146 155 storres
    return timing
147 155 storres
# End test_pobyso_error_so
148 155 storres
149 117 storres
def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10):
150 117 storres
    functionName = inspect.stack()[0][3]
151 117 storres
    print "Running", functionName, "..."
152 117 storres
    def test():
153 117 storres
        listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];")
154 117 storres
        (listSaSo, elementsNumSa, isEndEllipticSa) = \
155 117 storres
            pobyso_get_list_elements(listSo)
156 117 storres
        for elementSo in listSaSo:
157 117 storres
            sollya_lib_clear_obj(elementSo)
158 117 storres
        # Notice that we use sollya_lib_clear_obj and not
159 117 storres
        # sollya_lib_clear_object_list.
160 117 storres
        sollya_lib_clear_obj(listSo, None)
161 117 storres
    #
162 117 storres
    wrapped = test_pobyso_wrapper(test)
163 117 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
164 117 storres
    print "\t...", functionName, "done."
165 117 storres
    return timing
166 117 storres
# End test_pobyso_get_list_elements
167 117 storres
168 120 storres
def test_pobyso_get_prec_so_sa(repeat=1000, number=10):
169 120 storres
    functionName = inspect.stack()[0][3]
170 120 storres
    print "Running", functionName, "..."
171 120 storres
    def test():
172 116 storres
        precSa = pobyso_get_prec_so_sa()
173 120 storres
    wrapped = test_pobyso_wrapper(test)
174 120 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
175 120 storres
    print "\t...", functionName, "done."
176 120 storres
    return timing
177 116 storres
# End test_pobyso_get_prec_so_sa
178 116 storres
179 120 storres
def test_pobyso_get_prec_of_constant_so_sa(repeat=1000, number=10):
180 120 storres
    functionName = inspect.stack()[0][3]
181 120 storres
    print "Running", functionName, "..."
182 120 storres
    constSo = pobyso_constant_1_sa_so()
183 120 storres
    def test():
184 116 storres
        precSa = pobyso_get_prec_of_constant_so_sa(constSo)
185 120 storres
    wrapped = test_pobyso_wrapper(test)
186 120 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
187 154 storres
    print "\t...", functionName, "done."
188 120 storres
    sollya_lib_clear_obj(constSo,None)
189 120 storres
    return timing
190 116 storres
# End test_pobyso_get_prec_of_constant_so_sa
191 116 storres
192 183 storres
def test_pobyso_guess_degree_sa_sa(repeat=10, number=10):
193 154 storres
    functionName = inspect.stack()[0][3]
194 154 storres
    print "Running", functionName, "..."
195 154 storres
    funcExpSa(x) = exp(x)
196 154 storres
    RRIF = RealIntervalField(113)
197 154 storres
    intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68)
198 154 storres
    print "Interval:", intervalSa.str(style='brackets')
199 154 storres
    errorSa = RR(2^-227)
200 154 storres
    weightSa(x) = 1
201 154 storres
    degreeBoundSa = 150
202 154 storres
    # Function definition
203 154 storres
    def test():
204 154 storres
        pobyso_guess_degree_sa_sa(funcExpSa,
205 154 storres
                                  intervalSa,
206 154 storres
                                  errorSa,
207 154 storres
                                  weightSa,
208 154 storres
                                  degreeBoundSa)
209 154 storres
    wrapped = test_pobyso_wrapper(test)
210 154 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
211 154 storres
    print "\t...", functionName, "done."
212 154 storres
    return timing
213 154 storres
# End test_pobyso_guess_degree_sa_sa
214 154 storres
215 154 storres
def test_pobyso_guess_degree_so_sa(repeat=1000, number=10):
216 154 storres
    functionName = inspect.stack()[0][3]
217 154 storres
    print "Running", functionName, "..."
218 153 storres
    funcExpSo = pobyso_parse_string_sa_so("exp(x)")
219 153 storres
    RRIF = RealIntervalField(113)
220 153 storres
    intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68)
221 154 storres
    print "Interval:", intervalSa.str(style='brackets')
222 153 storres
    intervalSo = pobyso_interval_to_range_sa_so(intervalSa)
223 154 storres
    errorSo = pobyso_constant_sa_so(RR(2^-227))
224 154 storres
    weightSo=pobyso_parse_string_sa_so("1")
225 154 storres
    degreeBoundSo = pobyso_constant_from_int_sa_so(150)
226 154 storres
    # Function definition
227 154 storres
    def test():
228 154 storres
        pobyso_guess_degree_so_sa(funcExpSo,
229 154 storres
                                  intervalSo,
230 154 storres
                                  errorSo,
231 154 storres
                                  weightSo,
232 155 storres
                                  degreeBoundSo)
233 155 storres
    """
234 155 storres
        pobyso_guess_degree_so_sa(funcExpSo,
235 155 storres
                                  intervalSo,
236 155 storres
                                  errorSo,
237 155 storres
                                  None,
238 155 storres
                                  None)
239 155 storres
    """
240 154 storres
    wrapped = test_pobyso_wrapper(test)
241 154 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
242 154 storres
    print "\t...", functionName, "done."
243 154 storres
    sollya_lib_clear_obj(funcExpSo)
244 154 storres
    sollya_lib_clear_obj(intervalSo)
245 154 storres
    sollya_lib_clear_obj(errorSo)
246 154 storres
    sollya_lib_clear_obj(weightSo)
247 154 storres
    sollya_lib_clear_obj(degreeBoundSo)
248 154 storres
    return timing
249 154 storres
# End test_pobyso_guess_degree_so_sa
250 153 storres
251 155 storres
def test_pobyso_is_error_so_sa(repeat=1000, number=10):
252 155 storres
    functionName =  inspect.stack()[0][3]
253 155 storres
    print "Running", inspect.stack()[0][3], "..."
254 155 storres
    #
255 155 storres
    def test():
256 155 storres
        errorSo = pobyso_error_so()
257 155 storres
        if not pobyso_is_error_so_sa(errorSo):
258 155 storres
            print "Error!"
259 155 storres
        sollya_lib_clear_obj(errorSo)
260 155 storres
    #
261 155 storres
    wrapped = test_pobyso_wrapper(test)
262 155 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
263 155 storres
    print "\t...", functionName, "done."
264 155 storres
    return timing
265 155 storres
# End test_pobyso_error_so
266 155 storres
267 208 storres
def test_pobyso_lib_init(repeat=1000, number=10):
268 116 storres
    """
269 116 storres
    Must be called exactly once. Leaks lots of memory otherwise.
270 116 storres
    """
271 208 storres
    functionName = inspect.stack()[0][3]
272 208 storres
    print "Running", functionName,"..."
273 208 storres
    def test():
274 116 storres
        pobyso_lib_init()
275 208 storres
        pobyso_lib_close()
276 208 storres
277 208 storres
    wrapped = test_pobyso_wrapper(test)
278 208 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
279 208 storres
    print "\t...", functionName, "done."
280 208 storres
    return timing
281 116 storres
# End test_pobyso_lib_init
282 116 storres
283 208 storres
def test_pobyso_parse_string_sa_so(repeat=1000, number=10):
284 208 storres
    functionName = inspect.stack()[0][3]
285 208 storres
    print "Running", functionName,"..."
286 208 storres
    stringToParse = "exp(x)^(2)/sin(x)*cos(x)"
287 208 storres
    def test(stringToParseSa):
288 208 storres
        expressionSo = pobyso_parse_string_sa_so(stringToParseSa)
289 116 storres
        sollya_lib_clear_obj(expressionSo)
290 208 storres
291 208 storres
    wrapped = test_pobyso_wrapper(test,
292 208 storres
                                  stringToParse)
293 208 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
294 208 storres
    print "\t...", functionName, "done."
295 208 storres
    return timing
296 116 storres
297 208 storres
# End test_pobyso_parse_string_sa_so
298 208 storres
299 210 storres
def test_pobyso_rat_poly_sa_so(repeat=100, number=10):
300 210 storres
    functionName = inspect.stack()[0][3]
301 210 storres
    print "Running", functionName,"..."
302 210 storres
    precisionsList = [12, 53, 163, 1024]
303 210 storres
    i = var('i')
304 210 storres
    P_QQ        = QQ[i]
305 210 storres
    pr          = P_QQ(1/7 + 2/7*i + 3/7*i^2)
306 210 storres
    def test(polynomial, precisionsList):
307 210 storres
        for prec in precisionsList:
308 210 storres
            polySo = pobyso_rat_poly_sa_so(polynomial, prec)
309 210 storres
            sollya_lib_clear_obj(polySo)
310 210 storres
311 210 storres
    wrapped = test_pobyso_wrapper(test,
312 210 storres
                                  pr,
313 210 storres
                                  precisionsList)
314 210 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
315 210 storres
    print "\t...", functionName, "done."
316 210 storres
    return timing
317 210 storres
# End test_pobyso_rat_poly_sa_so
318 210 storres
319 117 storres
def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10):
320 117 storres
    functionName = inspect.stack()[0][3]
321 117 storres
    print "Running", functionName, "..."
322 116 storres
    functionSo  = pobyso_parse_string('exp(x)')
323 116 storres
    degreeSo    = pobyso_constant_from_int_sa_so(20)
324 116 storres
    rangeSo     = pobyso_bounds_to_range_sa_so(RR(1),RR(2))
325 116 storres
    errorTypeSo = pobyso_absolute_so_so()
326 117 storres
    def test(functionSo, degreeSo, rangeSo, errorTypeSo):
327 116 storres
        teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo,
328 116 storres
                                                           degreeSo,
329 116 storres
                                                           rangeSo,
330 116 storres
                                                           errorTypeSo)
331 116 storres
        sollya_lib_clear_obj(teSo[0])
332 116 storres
        sollya_lib_clear_obj(teSo[1])
333 116 storres
        sollya_lib_clear_obj(teSo[2])
334 117 storres
    # End test
335 117 storres
    wrapped = test_pobyso_wrapper(test,
336 117 storres
                                  functionSo,
337 117 storres
                                  degreeSo,
338 117 storres
                                  rangeSo,
339 117 storres
                                  errorTypeSo)
340 117 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
341 116 storres
    sollya_lib_clear_obj(functionSo)
342 116 storres
    sollya_lib_clear_obj(degreeSo)
343 116 storres
    sollya_lib_clear_obj(rangeSo)
344 116 storres
    sollya_lib_clear_obj(errorTypeSo)
345 117 storres
    print "\t...", functionName, "done."
346 117 storres
    return timing
347 116 storres
# End test_pobyso_taylor_expansion_no_change_var_so_so
348 116 storres
349 181 storres
def test_pobyso_taylor_expansion_with_change_var_so_so(repeat=1000, number=10):
350 181 storres
    functionName = inspect.stack()[0][3]
351 181 storres
    print "Running", functionName, "..."
352 181 storres
    functionSo  = pobyso_parse_string('exp(x)')
353 181 storres
    degreeSo    = pobyso_constant_from_int_sa_so(20)
354 181 storres
    rangeSo     = pobyso_bounds_to_range_sa_so(RR(1),RR(2))
355 181 storres
    errorTypeSo = pobyso_absolute_so_so()
356 181 storres
    def test(functionSo, degreeSo, rangeSo, errorTypeSo):
357 181 storres
        teSo = pobyso_taylor_expansion_with_change_var_so_so(functionSo,
358 181 storres
                                                             degreeSo,
359 181 storres
                                                             rangeSo,
360 181 storres
                                                             errorTypeSo)
361 181 storres
        sollya_lib_clear_obj(teSo[0])
362 181 storres
        sollya_lib_clear_obj(teSo[1])
363 181 storres
        sollya_lib_clear_obj(teSo[2])
364 181 storres
    # End test
365 181 storres
    wrapped = test_pobyso_wrapper(test,
366 181 storres
                                  functionSo,
367 181 storres
                                  degreeSo,
368 181 storres
                                  rangeSo,
369 181 storres
                                  errorTypeSo)
370 181 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
371 181 storres
    sollya_lib_clear_obj(functionSo)
372 181 storres
    sollya_lib_clear_obj(degreeSo)
373 181 storres
    sollya_lib_clear_obj(rangeSo)
374 181 storres
    sollya_lib_clear_obj(errorTypeSo)
375 181 storres
    print "\t...", functionName, "done."
376 181 storres
    return timing
377 181 storres
# End test_pobyso_taylor_expansion_with_change_var_so_so
378 181 storres
379 120 storres
def test_pobyso_taylorform_so_so_2(repeat=1000, number=10):
380 117 storres
    functionName = inspect.stack()[0][3]
381 117 storres
    print "Running", functionName, "..."
382 117 storres
    functionSo  = pobyso_parse_string('exp(x)')
383 117 storres
    degreeSo    = pobyso_constant_from_int_sa_so(20)
384 117 storres
    rangeSo     = pobyso_bounds_to_range_sa_so(RR(1),RR(2))
385 117 storres
    intervalCenterSo = sollya_lib_mid(rangeSo, None)
386 117 storres
    errorTypeSo = pobyso_absolute_so_so()
387 117 storres
388 117 storres
    def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo):
389 117 storres
        taylorformSo = pobyso_taylorform_so_so(functionSo,
390 117 storres
                                               degreeSo,
391 117 storres
                                               intervalCenterSo,
392 117 storres
                                               errorTypeSo)
393 117 storres
        sollya_lib_clear_obj(taylorformSo)
394 166 storres
395 117 storres
    wrapped = test_pobyso_wrapper(test,
396 117 storres
                                  functionSo,
397 117 storres
                                  degreeSo,
398 117 storres
                                  intervalCenterSo,
399 117 storres
                                  errorTypeSo)
400 117 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
401 166 storres
    sollya_lib_clear_obj(functionSo, None)
402 166 storres
    sollya_lib_clear_obj(degreeSo, None)
403 166 storres
    sollya_lib_clear_obj(rangeSo, None)
404 166 storres
    sollya_lib_clear_obj(intervalCenterSo, None)
405 166 storres
    sollya_lib_clear_obj(errorTypeSo, None)
406 117 storres
    return timing
407 117 storres
# End   test_pobyso_taylor_so_so_2
408 117 storres
409 117 storres
def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1):
410 117 storres
    aString = "2+3"
411 117 storres
    def test():
412 117 storres
        return 2+3
413 117 storres
    wrapped = test_pobyso_wrapper(test)
414 117 storres
    timing = timeit.timeit(wrapped, number=executionsNum)
415 117 storres
    return timing
416 204 storres
print "\t...testPobyso loaded"