Statistiques
| Révision :

root / pobysoPythonSage / src / testPobyso.sage @ 282

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