root / pobysoPythonSage / src / testPobyso.sage @ 178
Historique | Voir | Annoter | Télécharger (11,44 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 | 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 | 160 | storres | rusage_denom = rusage_denom^2 |
| 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 | 159 | storres | degreeSo = pobyso_constant_from_int_sa_so(4) |
| 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 | 162 | storres | def test(functionSo, degreeSo, pointSo, intervalSo): |
| 80 | 120 | storres | taylorformSo = pobyso_taylorform_so_so(functionSo, |
| 81 | 120 | storres | degreeSo, |
| 82 | 120 | storres | pointSo, |
| 83 | 120 | storres | intervalSo) |
| 84 | 162 | storres | pobyso_clear_taylorform_sa_so(taylorformSo) |
| 85 | 120 | storres | # |
| 86 | 162 | storres | wrapped = test_pobyso_wrapper(test, |
| 87 | 162 | storres | functionSo, |
| 88 | 162 | storres | degreeSo, |
| 89 | 162 | storres | pointSo, |
| 90 | 162 | storres | intervalSo) |
| 91 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 92 | 120 | storres | # |
| 93 | 162 | storres | sollya_lib_clear_obj(functionSo) |
| 94 | 162 | storres | sollya_lib_clear_obj(degreeSo) |
| 95 | 162 | storres | sollya_lib_clear_obj(pointSo) |
| 96 | 162 | storres | sollya_lib_clear_obj(intervalSo) |
| 97 | 120 | storres | print "\t...", functionName, "done." |
| 98 | 120 | storres | return timing |
| 99 | 120 | storres | # End test_pobyso_bounds_to_range_sa_so |
| 100 | 120 | storres | |
| 101 | 155 | storres | def test_pobyso_error_so(repeat=1000, number=10): |
| 102 | 155 | storres | functionName = inspect.stack()[0][3] |
| 103 | 155 | storres | print "Running", inspect.stack()[0][3], "..." |
| 104 | 155 | storres | # |
| 105 | 155 | storres | def test(): |
| 106 | 155 | storres | errorSo = pobyso_error_so() |
| 107 | 155 | storres | sollya_lib_clear_obj(errorSo) |
| 108 | 155 | storres | # |
| 109 | 155 | storres | wrapped = test_pobyso_wrapper(test) |
| 110 | 155 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 111 | 155 | storres | print "\t...", functionName, "done." |
| 112 | 155 | storres | return timing |
| 113 | 155 | storres | # End test_pobyso_error_so |
| 114 | 155 | storres | |
| 115 | 117 | storres | def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10): |
| 116 | 117 | storres | functionName = inspect.stack()[0][3] |
| 117 | 117 | storres | print "Running", functionName, "..." |
| 118 | 117 | storres | def test(): |
| 119 | 117 | storres | listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];")
|
| 120 | 117 | storres | (listSaSo, elementsNumSa, isEndEllipticSa) = \ |
| 121 | 117 | storres | pobyso_get_list_elements(listSo) |
| 122 | 117 | storres | for elementSo in listSaSo: |
| 123 | 117 | storres | sollya_lib_clear_obj(elementSo) |
| 124 | 117 | storres | # Notice that we use sollya_lib_clear_obj and not |
| 125 | 117 | storres | # sollya_lib_clear_object_list. |
| 126 | 117 | storres | sollya_lib_clear_obj(listSo, None) |
| 127 | 117 | storres | # |
| 128 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
| 129 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 130 | 117 | storres | print "\t...", functionName, "done." |
| 131 | 117 | storres | return timing |
| 132 | 117 | storres | # End test_pobyso_get_list_elements |
| 133 | 117 | storres | |
| 134 | 120 | storres | def test_pobyso_get_prec_so_sa(repeat=1000, number=10): |
| 135 | 120 | storres | functionName = inspect.stack()[0][3] |
| 136 | 120 | storres | print "Running", functionName, "..." |
| 137 | 120 | storres | def test(): |
| 138 | 116 | storres | precSa = pobyso_get_prec_so_sa() |
| 139 | 120 | storres | wrapped = test_pobyso_wrapper(test) |
| 140 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 141 | 120 | storres | print "\t...", functionName, "done." |
| 142 | 120 | storres | return timing |
| 143 | 116 | storres | # End test_pobyso_get_prec_so_sa |
| 144 | 116 | storres | |
| 145 | 120 | storres | def test_pobyso_get_prec_of_constant_so_sa(repeat=1000, number=10): |
| 146 | 120 | storres | functionName = inspect.stack()[0][3] |
| 147 | 120 | storres | print "Running", functionName, "..." |
| 148 | 120 | storres | constSo = pobyso_constant_1_sa_so() |
| 149 | 120 | storres | def test(): |
| 150 | 116 | storres | precSa = pobyso_get_prec_of_constant_so_sa(constSo) |
| 151 | 120 | storres | wrapped = test_pobyso_wrapper(test) |
| 152 | 120 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 153 | 154 | storres | print "\t...", functionName, "done." |
| 154 | 120 | storres | sollya_lib_clear_obj(constSo,None) |
| 155 | 120 | storres | return timing |
| 156 | 116 | storres | # End test_pobyso_get_prec_of_constant_so_sa |
| 157 | 116 | storres | |
| 158 | 154 | storres | def test_pobyso_guess_degree_sa_sa(repeat=1000, number=10): |
| 159 | 154 | storres | functionName = inspect.stack()[0][3] |
| 160 | 154 | storres | print "Running", functionName, "..." |
| 161 | 154 | storres | funcExpSa(x) = exp(x) |
| 162 | 154 | storres | RRIF = RealIntervalField(113) |
| 163 | 154 | storres | intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
| 164 | 154 | storres | print "Interval:", intervalSa.str(style='brackets') |
| 165 | 154 | storres | errorSa = RR(2^-227) |
| 166 | 154 | storres | weightSa(x) = 1 |
| 167 | 154 | storres | degreeBoundSa = 150 |
| 168 | 154 | storres | # Function definition |
| 169 | 154 | storres | def test(): |
| 170 | 154 | storres | pobyso_guess_degree_sa_sa(funcExpSa, |
| 171 | 154 | storres | intervalSa, |
| 172 | 154 | storres | errorSa, |
| 173 | 154 | storres | weightSa, |
| 174 | 154 | storres | degreeBoundSa) |
| 175 | 154 | storres | wrapped = test_pobyso_wrapper(test) |
| 176 | 154 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 177 | 154 | storres | print "\t...", functionName, "done." |
| 178 | 154 | storres | return timing |
| 179 | 154 | storres | # End test_pobyso_guess_degree_sa_sa |
| 180 | 154 | storres | |
| 181 | 154 | storres | def test_pobyso_guess_degree_so_sa(repeat=1000, number=10): |
| 182 | 154 | storres | functionName = inspect.stack()[0][3] |
| 183 | 154 | storres | print "Running", functionName, "..." |
| 184 | 153 | storres | funcExpSo = pobyso_parse_string_sa_so("exp(x)")
|
| 185 | 153 | storres | RRIF = RealIntervalField(113) |
| 186 | 153 | storres | intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
| 187 | 154 | storres | print "Interval:", intervalSa.str(style='brackets') |
| 188 | 153 | storres | intervalSo = pobyso_interval_to_range_sa_so(intervalSa) |
| 189 | 154 | storres | errorSo = pobyso_constant_sa_so(RR(2^-227)) |
| 190 | 154 | storres | weightSo=pobyso_parse_string_sa_so("1")
|
| 191 | 154 | storres | degreeBoundSo = pobyso_constant_from_int_sa_so(150) |
| 192 | 154 | storres | # Function definition |
| 193 | 154 | storres | def test(): |
| 194 | 154 | storres | pobyso_guess_degree_so_sa(funcExpSo, |
| 195 | 154 | storres | intervalSo, |
| 196 | 154 | storres | errorSo, |
| 197 | 154 | storres | weightSo, |
| 198 | 155 | storres | degreeBoundSo) |
| 199 | 155 | storres | """ |
| 200 | 155 | storres | pobyso_guess_degree_so_sa(funcExpSo, |
| 201 | 155 | storres | intervalSo, |
| 202 | 155 | storres | errorSo, |
| 203 | 155 | storres | None, |
| 204 | 155 | storres | None) |
| 205 | 155 | storres | """ |
| 206 | 154 | storres | wrapped = test_pobyso_wrapper(test) |
| 207 | 154 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 208 | 154 | storres | print "\t...", functionName, "done." |
| 209 | 154 | storres | sollya_lib_clear_obj(funcExpSo) |
| 210 | 154 | storres | sollya_lib_clear_obj(intervalSo) |
| 211 | 154 | storres | sollya_lib_clear_obj(errorSo) |
| 212 | 154 | storres | sollya_lib_clear_obj(weightSo) |
| 213 | 154 | storres | sollya_lib_clear_obj(degreeBoundSo) |
| 214 | 154 | storres | return timing |
| 215 | 154 | storres | # End test_pobyso_guess_degree_so_sa |
| 216 | 153 | storres | |
| 217 | 155 | storres | def test_pobyso_is_error_so_sa(repeat=1000, number=10): |
| 218 | 155 | storres | functionName = inspect.stack()[0][3] |
| 219 | 155 | storres | print "Running", inspect.stack()[0][3], "..." |
| 220 | 155 | storres | # |
| 221 | 155 | storres | def test(): |
| 222 | 155 | storres | errorSo = pobyso_error_so() |
| 223 | 155 | storres | if not pobyso_is_error_so_sa(errorSo): |
| 224 | 155 | storres | print "Error!" |
| 225 | 155 | storres | sollya_lib_clear_obj(errorSo) |
| 226 | 155 | storres | # |
| 227 | 155 | storres | wrapped = test_pobyso_wrapper(test) |
| 228 | 155 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 229 | 155 | storres | print "\t...", functionName, "done." |
| 230 | 155 | storres | return timing |
| 231 | 155 | storres | # End test_pobyso_error_so |
| 232 | 155 | storres | |
| 233 | 116 | storres | def test_pobyso_lib_init(iterationsNum=10000): |
| 234 | 116 | storres | """ |
| 235 | 116 | storres | Must be called exactly once. Leaks lots of memory otherwise. |
| 236 | 116 | storres | """ |
| 237 | 116 | storres | print "Running", inspect.stack()[0][3] |
| 238 | 116 | storres | for index in xrange(0,iterationsNum): |
| 239 | 116 | storres | pobyso_lib_init() |
| 240 | 116 | storres | #pobyso_lib_close() |
| 241 | 116 | storres | # End test_pobyso_lib_init |
| 242 | 116 | storres | |
| 243 | 116 | storres | def test_pobyso_parse_string_sa_so(iterationsNum=10000): |
| 244 | 116 | storres | print "Running", inspect.stack()[0][3] |
| 245 | 116 | storres | for index in xrange(0,iterationsNum): |
| 246 | 116 | storres | expressionSo = pobyso_parse_string_sa_so('exp(x)+2*x*sin(x)')
|
| 247 | 116 | storres | sollya_lib_clear_obj(expressionSo) |
| 248 | 116 | storres | # End pobyso_parse_string_sa_so |
| 249 | 116 | storres | |
| 250 | 117 | storres | def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10): |
| 251 | 117 | storres | functionName = inspect.stack()[0][3] |
| 252 | 117 | storres | print "Running", functionName, "..." |
| 253 | 116 | storres | functionSo = pobyso_parse_string('exp(x)')
|
| 254 | 116 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
| 255 | 116 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
| 256 | 116 | storres | errorTypeSo = pobyso_absolute_so_so() |
| 257 | 117 | storres | def test(functionSo, degreeSo, rangeSo, errorTypeSo): |
| 258 | 116 | storres | teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo, |
| 259 | 116 | storres | degreeSo, |
| 260 | 116 | storres | rangeSo, |
| 261 | 116 | storres | errorTypeSo) |
| 262 | 116 | storres | sollya_lib_clear_obj(teSo[0]) |
| 263 | 116 | storres | sollya_lib_clear_obj(teSo[1]) |
| 264 | 116 | storres | sollya_lib_clear_obj(teSo[2]) |
| 265 | 117 | storres | # End test |
| 266 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
| 267 | 117 | storres | functionSo, |
| 268 | 117 | storres | degreeSo, |
| 269 | 117 | storres | rangeSo, |
| 270 | 117 | storres | errorTypeSo) |
| 271 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 272 | 116 | storres | sollya_lib_clear_obj(functionSo) |
| 273 | 116 | storres | sollya_lib_clear_obj(degreeSo) |
| 274 | 116 | storres | sollya_lib_clear_obj(rangeSo) |
| 275 | 116 | storres | sollya_lib_clear_obj(errorTypeSo) |
| 276 | 117 | storres | print "\t...", functionName, "done." |
| 277 | 117 | storres | return timing |
| 278 | 116 | storres | # End test_pobyso_taylor_expansion_no_change_var_so_so |
| 279 | 116 | storres | |
| 280 | 120 | storres | def test_pobyso_taylorform_so_so_2(repeat=1000, number=10): |
| 281 | 117 | storres | functionName = inspect.stack()[0][3] |
| 282 | 117 | storres | print "Running", functionName, "..." |
| 283 | 117 | storres | functionSo = pobyso_parse_string('exp(x)')
|
| 284 | 117 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
| 285 | 117 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
| 286 | 117 | storres | intervalCenterSo = sollya_lib_mid(rangeSo, None) |
| 287 | 117 | storres | errorTypeSo = pobyso_absolute_so_so() |
| 288 | 117 | storres | |
| 289 | 117 | storres | def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo): |
| 290 | 117 | storres | taylorformSo = pobyso_taylorform_so_so(functionSo, |
| 291 | 117 | storres | degreeSo, |
| 292 | 117 | storres | intervalCenterSo, |
| 293 | 117 | storres | errorTypeSo) |
| 294 | 117 | storres | sollya_lib_clear_obj(taylorformSo) |
| 295 | 166 | storres | |
| 296 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
| 297 | 117 | storres | functionSo, |
| 298 | 117 | storres | degreeSo, |
| 299 | 117 | storres | intervalCenterSo, |
| 300 | 117 | storres | errorTypeSo) |
| 301 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 302 | 166 | storres | sollya_lib_clear_obj(functionSo, None) |
| 303 | 166 | storres | sollya_lib_clear_obj(degreeSo, None) |
| 304 | 166 | storres | sollya_lib_clear_obj(rangeSo, None) |
| 305 | 166 | storres | sollya_lib_clear_obj(intervalCenterSo, None) |
| 306 | 166 | storres | sollya_lib_clear_obj(errorTypeSo, None) |
| 307 | 117 | storres | return timing |
| 308 | 117 | storres | # End test_pobyso_taylor_so_so_2 |
| 309 | 117 | storres | |
| 310 | 117 | storres | def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1): |
| 311 | 117 | storres | aString = "2+3" |
| 312 | 117 | storres | def test(): |
| 313 | 117 | storres | return 2+3 |
| 314 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
| 315 | 117 | storres | timing = timeit.timeit(wrapped, number=executionsNum) |
| 316 | 117 | storres | return timing |
| 317 | 120 | storres | print "\t...testPobyso loaded" |
| 318 | 120 | storres |