root / pobysoPythonSage / src / testPobyso.sage @ 153
Historique | Voir | Annoter | Télécharger (8,49 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 | 117 | storres | def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10): |
97 | 117 | storres | functionName = inspect.stack()[0][3] |
98 | 117 | storres | print "Running", functionName, "..." |
99 | 117 | storres | def test(): |
100 | 117 | storres | listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];") |
101 | 117 | storres | (listSaSo, elementsNumSa, isEndEllipticSa) = \ |
102 | 117 | storres | pobyso_get_list_elements(listSo) |
103 | 117 | storres | for elementSo in listSaSo: |
104 | 117 | storres | sollya_lib_clear_obj(elementSo) |
105 | 117 | storres | # Notice that we use sollya_lib_clear_obj and not |
106 | 117 | storres | # sollya_lib_clear_object_list. |
107 | 117 | storres | sollya_lib_clear_obj(listSo, None) |
108 | 117 | storres | # |
109 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
110 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
111 | 117 | storres | print "\t...", functionName, "done." |
112 | 117 | storres | return timing |
113 | 117 | storres | # End test_pobyso_get_list_elements |
114 | 117 | storres | |
115 | 120 | storres | def test_pobyso_get_prec_so_sa(repeat=1000, number=10): |
116 | 120 | storres | functionName = inspect.stack()[0][3] |
117 | 120 | storres | print "Running", functionName, "..." |
118 | 120 | storres | def test(): |
119 | 116 | storres | precSa = pobyso_get_prec_so_sa() |
120 | 120 | storres | wrapped = test_pobyso_wrapper(test) |
121 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
122 | 120 | storres | print "\t...", functionName, "done." |
123 | 120 | storres | return timing |
124 | 116 | storres | # End test_pobyso_get_prec_so_sa |
125 | 116 | storres | |
126 | 120 | storres | def test_pobyso_get_prec_of_constant_so_sa(repeat=1000, number=10): |
127 | 120 | storres | functionName = inspect.stack()[0][3] |
128 | 120 | storres | print "Running", functionName, "..." |
129 | 120 | storres | constSo = pobyso_constant_1_sa_so() |
130 | 120 | storres | def test(): |
131 | 116 | storres | precSa = pobyso_get_prec_of_constant_so_sa(constSo) |
132 | 120 | storres | wrapped = test_pobyso_wrapper(test) |
133 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
134 | 120 | storres | sollya_lib_clear_obj(constSo,None) |
135 | 120 | storres | print "\t...", functionName, "done." |
136 | 120 | storres | return timing |
137 | 116 | storres | sollya_lib_clear_obj(constSo) |
138 | 116 | storres | # End test_pobyso_get_prec_of_constant_so_sa |
139 | 116 | storres | |
140 | 153 | storres | def test_pobyso_guess_degree_so_sa(iterationsNum=10000): |
141 | 153 | storres | print "Running", inspect.stack()[0][3] |
142 | 153 | storres | funcExpSo = pobyso_parse_string_sa_so("exp(x)") |
143 | 153 | storres | RRIF = RealIntervalField(113) |
144 | 153 | storres | intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
145 | 153 | storres | print ai.str(style='brackets') |
146 | 153 | storres | intervalSo = pobyso_interval_to_range_sa_so(intervalSa) |
147 | 153 | storres | errorSo = pobyso_constant_sa_so(2^-227) |
148 | 153 | storres | # Compute the other arguments... |
149 | 153 | storres | for index in xrange(0,iterationsNum): |
150 | 153 | storres | pass |
151 | 153 | storres | # End test_pobyso_guess_degree |
152 | 153 | storres | |
153 | 116 | storres | def test_pobyso_lib_init(iterationsNum=10000): |
154 | 116 | storres | """ |
155 | 116 | storres | Must be called exactly once. Leaks lots of memory otherwise. |
156 | 116 | storres | """ |
157 | 116 | storres | print "Running", inspect.stack()[0][3] |
158 | 116 | storres | for index in xrange(0,iterationsNum): |
159 | 116 | storres | pobyso_lib_init() |
160 | 116 | storres | #pobyso_lib_close() |
161 | 116 | storres | # End test_pobyso_lib_init |
162 | 116 | storres | |
163 | 116 | storres | def test_pobyso_parse_string_sa_so(iterationsNum=10000): |
164 | 116 | storres | print "Running", inspect.stack()[0][3] |
165 | 116 | storres | for index in xrange(0,iterationsNum): |
166 | 116 | storres | expressionSo = pobyso_parse_string_sa_so('exp(x)+2*x*sin(x)') |
167 | 116 | storres | sollya_lib_clear_obj(expressionSo) |
168 | 116 | storres | # End pobyso_parse_string_sa_so |
169 | 116 | storres | |
170 | 117 | storres | def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10): |
171 | 117 | storres | functionName = inspect.stack()[0][3] |
172 | 117 | storres | print "Running", functionName, "..." |
173 | 116 | storres | functionSo = pobyso_parse_string('exp(x)') |
174 | 116 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
175 | 116 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
176 | 116 | storres | errorTypeSo = pobyso_absolute_so_so() |
177 | 117 | storres | def test(functionSo, degreeSo, rangeSo, errorTypeSo): |
178 | 116 | storres | teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo, |
179 | 116 | storres | degreeSo, |
180 | 116 | storres | rangeSo, |
181 | 116 | storres | errorTypeSo) |
182 | 116 | storres | sollya_lib_clear_obj(teSo[0]) |
183 | 116 | storres | sollya_lib_clear_obj(teSo[1]) |
184 | 116 | storres | sollya_lib_clear_obj(teSo[2]) |
185 | 117 | storres | # End test |
186 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
187 | 117 | storres | functionSo, |
188 | 117 | storres | degreeSo, |
189 | 117 | storres | rangeSo, |
190 | 117 | storres | errorTypeSo) |
191 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
192 | 116 | storres | sollya_lib_clear_obj(functionSo) |
193 | 116 | storres | sollya_lib_clear_obj(degreeSo) |
194 | 116 | storres | sollya_lib_clear_obj(rangeSo) |
195 | 116 | storres | sollya_lib_clear_obj(errorTypeSo) |
196 | 117 | storres | print "\t...", functionName, "done." |
197 | 117 | storres | return timing |
198 | 116 | storres | # End test_pobyso_taylor_expansion_no_change_var_so_so |
199 | 116 | storres | |
200 | 120 | storres | def test_pobyso_taylorform_so_so_2(repeat=1000, number=10): |
201 | 117 | storres | functionName = inspect.stack()[0][3] |
202 | 117 | storres | print "Running", functionName, "..." |
203 | 117 | storres | functionSo = pobyso_parse_string('exp(x)') |
204 | 117 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
205 | 117 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
206 | 117 | storres | intervalCenterSo = sollya_lib_mid(rangeSo, None) |
207 | 117 | storres | errorTypeSo = pobyso_absolute_so_so() |
208 | 117 | storres | |
209 | 117 | storres | def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo): |
210 | 117 | storres | taylorformSo = pobyso_taylorform_so_so(functionSo, |
211 | 117 | storres | degreeSo, |
212 | 117 | storres | intervalCenterSo, |
213 | 117 | storres | errorTypeSo) |
214 | 117 | storres | sollya_lib_clear_obj(taylorformSo) |
215 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
216 | 117 | storres | functionSo, |
217 | 117 | storres | degreeSo, |
218 | 117 | storres | intervalCenterSo, |
219 | 117 | storres | errorTypeSo) |
220 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
221 | 117 | storres | sollya_lib_clear_obj(functionSo,None) |
222 | 117 | storres | sollya_lib_clear_obj(degreeSo,None) |
223 | 117 | storres | sollya_lib_clear_obj(rangeSo,None) |
224 | 117 | storres | sollya_lib_clear_obj(intervalCenterSo,None) |
225 | 117 | storres | sollya_lib_clear_obj(errorTypeSo,None) |
226 | 117 | storres | return timing |
227 | 117 | storres | # End test_pobyso_taylor_so_so_2 |
228 | 117 | storres | |
229 | 117 | storres | def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1): |
230 | 117 | storres | aString = "2+3" |
231 | 117 | storres | def test(): |
232 | 117 | storres | return 2+3 |
233 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
234 | 117 | storres | timing = timeit.timeit(wrapped, number=executionsNum) |
235 | 117 | storres | return timing |
236 | 120 | storres | print "\t...testPobyso loaded" |
237 | 120 | storres |