root / pobysoPythonSage / src / pobyso.py @ 217
Historique | Voir | Annoter | Télécharger (66,68 ko)
1 | 5 | storres | """
|
---|---|---|---|
2 | 209 | storres | @file pobyso.py
|
3 | 5 | storres | Actual functions to use in Sage
|
4 | 5 | storres | ST 2012-11-13
|
5 | 5 | storres |
|
6 | 5 | storres | Command line syntax:
|
7 | 5 | storres | use from Sage (via the "load" or the "attach" commands)
|
8 | 5 | storres |
|
9 | 38 | storres | pobyso functions come in five flavors:
|
10 | 57 | storres | - the _so_so (arguments and returned objects are pointers to Sollya objects,
|
11 | 57 | storres | includes the void function and the no arguments function that return a
|
12 | 57 | storres | pointer to a Sollya object);
|
13 | 38 | storres | - the _so_sa (argument are pointers to Sollya objects, returned objects are
|
14 | 57 | storres | Sage/Python objects or, more generally, information is transfered from the
|
15 | 57 | storres | Sollya world to Sage/Python world; e.g. functions without arguments that
|
16 | 57 | storres | return a Sage/Python object);
|
17 | 38 | storres | - the _sa_so (arguments are Sage/Python objects, returned objects are
|
18 | 38 | storres | pointers to Sollya objects);
|
19 | 38 | storres | - the sa_sa (arguments and returned objects are all Sage/Python objects);
|
20 | 51 | storres | - a catch all flavor, without any suffix, (e. g. functions that have no argument
|
21 | 51 | storres | nor return value).
|
22 | 57 | storres | This classification is not always very strict. Conversion functions from Sollya
|
23 | 57 | storres | to Sage/Python are sometimes decorated with Sage/Python arguments to set
|
24 | 57 | storres | the precision. These functions remain in the so_sa category.
|
25 | 5 | storres | NOTES:
|
26 | 5 | storres | Reported errors in Eclipse come from the calls to
|
27 | 5 | storres | the Sollya library
|
28 | 5 | storres |
|
29 | 10 | storres | ToDo (among other things):
|
30 | 10 | storres | -memory management.
|
31 | 5 | storres | """
|
32 | 5 | storres | from ctypes import * |
33 | 37 | storres | import re |
34 | 37 | storres | from sage.symbolic.expression_conversions import polynomial |
35 | 59 | storres | from sage.symbolic.expression_conversions import PolynomialConverter |
36 | 38 | storres | """
|
37 | 38 | storres | Create the equivalent to an enum for the Sollya function types.
|
38 | 38 | storres | """
|
39 | 5 | storres | (SOLLYA_BASE_FUNC_ABS, |
40 | 5 | storres | SOLLYA_BASE_FUNC_ACOS, |
41 | 5 | storres | SOLLYA_BASE_FUNC_ACOSH, |
42 | 5 | storres | SOLLYA_BASE_FUNC_ADD, |
43 | 5 | storres | SOLLYA_BASE_FUNC_ASIN, |
44 | 5 | storres | SOLLYA_BASE_FUNC_ASINH, |
45 | 5 | storres | SOLLYA_BASE_FUNC_ATAN, |
46 | 5 | storres | SOLLYA_BASE_FUNC_ATANH, |
47 | 5 | storres | SOLLYA_BASE_FUNC_CEIL, |
48 | 5 | storres | SOLLYA_BASE_FUNC_CONSTANT, |
49 | 5 | storres | SOLLYA_BASE_FUNC_COS, |
50 | 5 | storres | SOLLYA_BASE_FUNC_COSH, |
51 | 5 | storres | SOLLYA_BASE_FUNC_DIV, |
52 | 5 | storres | SOLLYA_BASE_FUNC_DOUBLE, |
53 | 5 | storres | SOLLYA_BASE_FUNC_DOUBLEDOUBLE, |
54 | 5 | storres | SOLLYA_BASE_FUNC_DOUBLEEXTENDED, |
55 | 5 | storres | SOLLYA_BASE_FUNC_ERF, |
56 | 5 | storres | SOLLYA_BASE_FUNC_ERFC, |
57 | 5 | storres | SOLLYA_BASE_FUNC_EXP, |
58 | 5 | storres | SOLLYA_BASE_FUNC_EXP_M1, |
59 | 5 | storres | SOLLYA_BASE_FUNC_FLOOR, |
60 | 5 | storres | SOLLYA_BASE_FUNC_FREE_VARIABLE, |
61 | 5 | storres | SOLLYA_BASE_FUNC_HALFPRECISION, |
62 | 5 | storres | SOLLYA_BASE_FUNC_LIBRARYCONSTANT, |
63 | 5 | storres | SOLLYA_BASE_FUNC_LIBRARYFUNCTION, |
64 | 5 | storres | SOLLYA_BASE_FUNC_LOG, |
65 | 5 | storres | SOLLYA_BASE_FUNC_LOG_10, |
66 | 5 | storres | SOLLYA_BASE_FUNC_LOG_1P, |
67 | 5 | storres | SOLLYA_BASE_FUNC_LOG_2, |
68 | 5 | storres | SOLLYA_BASE_FUNC_MUL, |
69 | 5 | storres | SOLLYA_BASE_FUNC_NEARESTINT, |
70 | 5 | storres | SOLLYA_BASE_FUNC_NEG, |
71 | 5 | storres | SOLLYA_BASE_FUNC_PI, |
72 | 5 | storres | SOLLYA_BASE_FUNC_POW, |
73 | 5 | storres | SOLLYA_BASE_FUNC_PROCEDUREFUNCTION, |
74 | 5 | storres | SOLLYA_BASE_FUNC_QUAD, |
75 | 5 | storres | SOLLYA_BASE_FUNC_SIN, |
76 | 5 | storres | SOLLYA_BASE_FUNC_SINGLE, |
77 | 5 | storres | SOLLYA_BASE_FUNC_SINH, |
78 | 5 | storres | SOLLYA_BASE_FUNC_SQRT, |
79 | 5 | storres | SOLLYA_BASE_FUNC_SUB, |
80 | 5 | storres | SOLLYA_BASE_FUNC_TAN, |
81 | 5 | storres | SOLLYA_BASE_FUNC_TANH, |
82 | 5 | storres | SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44)) |
83 | 56 | storres | print "\nSuperficial pobyso check..." |
84 | 5 | storres | print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS |
85 | 5 | storres | print "Last constant - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE |
86 | 5 | storres | |
87 | 5 | storres | pobyso_max_arity = 9
|
88 | 5 | storres | |
89 | 85 | storres | def pobyso_absolute_so_so(): |
90 | 85 | storres | return(sollya_lib_absolute(None)) |
91 | 85 | storres | |
92 | 5 | storres | def pobyso_autoprint(arg): |
93 | 5 | storres | sollya_lib_autoprint(arg,None)
|
94 | 5 | storres | |
95 | 38 | storres | def pobyso_autoprint_so_so(arg): |
96 | 38 | storres | sollya_lib_autoprint(arg,None)
|
97 | 54 | storres | |
98 | 84 | storres | def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa, \ |
99 | 84 | storres | precisionSa=None):
|
100 | 84 | storres | """
|
101 | 84 | storres | Return a Sollya range from to 2 RealField Sage elements.
|
102 | 84 | storres | The Sollya range element has a sufficient precision to hold all
|
103 | 162 | storres | the digits of the widest of the Sage bounds.
|
104 | 84 | storres | """
|
105 | 84 | storres | # Sanity check.
|
106 | 84 | storres | if rnLowerBoundSa > rnUpperBoundSa:
|
107 | 84 | storres | return None |
108 | 115 | storres | # Precision stuff.
|
109 | 85 | storres | if precisionSa is None: |
110 | 84 | storres | # Check for the largest precision.
|
111 | 84 | storres | lbPrecSa = rnLowerBoundSa.parent().precision() |
112 | 84 | storres | ubPrecSa = rnLowerBoundSa.parent().precision() |
113 | 84 | storres | maxPrecSa = max(lbPrecSa, ubPrecSa)
|
114 | 84 | storres | else:
|
115 | 84 | storres | maxPrecSa = precisionSa |
116 | 115 | storres | # From Sage to Sollya bounds.
|
117 | 162 | storres | # lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa),
|
118 | 162 | storres | # maxPrecSa)
|
119 | 162 | storres | lowerBoundSo = pobyso_constant_sa_so(rnLowerBoundSa, |
120 | 162 | storres | maxPrecSa) |
121 | 162 | storres | upperBoundSo = pobyso_constant_sa_so(rnUpperBoundSa, |
122 | 154 | storres | maxPrecSa) |
123 | 162 | storres | |
124 | 115 | storres | # From Sollya bounds to range.
|
125 | 84 | storres | rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo) |
126 | 84 | storres | # Back to original precision.
|
127 | 84 | storres | # Clean up
|
128 | 84 | storres | sollya_lib_clear_obj(lowerBoundSo) |
129 | 84 | storres | sollya_lib_clear_obj(upperBoundSo) |
130 | 154 | storres | return rangeSo
|
131 | 84 | storres | # End pobyso_bounds_to_range_sa_so
|
132 | 84 | storres | |
133 | 215 | storres | def pobyso_build_end_elliptic_list_so_so(*args): |
134 | 215 | storres | """
|
135 | 215 | storres | From argumrny Sollya objects, create a Sollya end elliptic list.
|
136 | 215 | storres | Elements of the list are "eaten" (should not be cleared individualy,
|
137 | 215 | storres | are cleared when the list is cleared).
|
138 | 215 | storres | """
|
139 | 215 | storres | if len(args) == 0: |
140 | 215 | storres | ## Called with an empty list produced "error".
|
141 | 215 | storres | return sollya_lib_build_end_elliptic_list(None) |
142 | 215 | storres | index = 0
|
143 | 215 | storres | ## One can not append elements to an elliptic list, prepend only is
|
144 | 215 | storres | # permitted.
|
145 | 215 | storres | for argument in reversed(args): |
146 | 215 | storres | if index == 0: |
147 | 215 | storres | listSo = sollya_lib_build_end_elliptic_list(argument, None)
|
148 | 215 | storres | else:
|
149 | 215 | storres | listSo = sollya_lib_prepend(argument, listSo) |
150 | 215 | storres | index += 1
|
151 | 215 | storres | return listSo
|
152 | 215 | storres | |
153 | 215 | storres | # End pobyso_build_end_elliptic_list_so_so
|
154 | 215 | storres | |
155 | 54 | storres | def pobyso_build_function_sub_so_so(exp1So, exp2So): |
156 | 54 | storres | return(sollya_lib_build_function_sub(exp1So, exp2So))
|
157 | 54 | storres | |
158 | 85 | storres | def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo): |
159 | 54 | storres | """
|
160 | 85 | storres | Variable change in a function.
|
161 | 85 | storres | """
|
162 | 85 | storres | return(sollya_lib_evaluate(funcSo,chvarExpSo))
|
163 | 85 | storres | # End pobyso_change_var_in_function_so_so
|
164 | 85 | storres | |
165 | 85 | storres | def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo): |
166 | 85 | storres | resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo) |
167 | 85 | storres | return(resultSo)
|
168 | 85 | storres | # End pobyso_chebyshevform_so_so.
|
169 | 85 | storres | |
170 | 117 | storres | def pobyso_clear_taylorform_sa_so(taylorFormSaSo): |
171 | 117 | storres | """
|
172 | 117 | storres | This method is necessary to correctly clean up the memory from Taylor forms.
|
173 | 117 | storres | These are made of a Sollya object, a Sollya object list, a Sollya object.
|
174 | 117 | storres | For no clearly understood reason, sollya_lib_clear_object_list crashed
|
175 | 117 | storres | when applied to the object list.
|
176 | 117 | storres | Here, we decompose it into Sage list of Sollya objects references and we
|
177 | 117 | storres | clear them one by one.
|
178 | 117 | storres | """
|
179 | 117 | storres | sollya_lib_clear_obj(taylorFormSaSo[0])
|
180 | 117 | storres | (coefficientsErrorsListSaSo, numElementsSa, isEndEllipticSa) = \ |
181 | 117 | storres | pobyso_get_list_elements_so_so(taylorFormSaSo[1])
|
182 | 117 | storres | for element in coefficientsErrorsListSaSo: |
183 | 117 | storres | sollya_lib_clear_obj(element) |
184 | 117 | storres | sollya_lib_clear_obj(taylorFormSaSo[1])
|
185 | 117 | storres | sollya_lib_clear_obj(taylorFormSaSo[2])
|
186 | 117 | storres | # End pobyso_clear_taylorform_sa_so
|
187 | 117 | storres | |
188 | 85 | storres | def pobyso_cmp(rnArgSa, cteSo): |
189 | 85 | storres | """
|
190 | 54 | storres | Compare the MPFR value a RealNumber with that of a Sollya constant.
|
191 | 54 | storres |
|
192 | 54 | storres | Get the value of the Sollya constant into a RealNumber and compare
|
193 | 54 | storres | using MPFR. Could be optimized by working directly with a mpfr_t
|
194 | 54 | storres | for the intermediate number.
|
195 | 54 | storres | """
|
196 | 115 | storres | # Get the precision of the Sollya constant to build a Sage RealNumber
|
197 | 115 | storres | # with enough precision.to hold it.
|
198 | 5 | storres | precisionOfCte = c_int(0)
|
199 | 5 | storres | # From the Sollya constant, create a local Sage RealNumber.
|
200 | 85 | storres | sollya_lib_get_prec_of_constant(precisionOfCte, cteSo) |
201 | 5 | storres | #print "Precision of constant: ", precisionOfCte
|
202 | 5 | storres | RRRR = RealField(precisionOfCte.value) |
203 | 85 | storres | rnLocalSa = RRRR(0)
|
204 | 85 | storres | sollya_lib_get_constant(get_rn_value(rnLocalSa), cteSo) |
205 | 115 | storres | #
|
206 | 115 | storres | ## Compare the Sage RealNumber version of the Sollya constant with rnArg.
|
207 | 85 | storres | return(cmp_rn_value(rnArgSa, rnLocal))
|
208 | 83 | storres | # End pobyso_smp
|
209 | 5 | storres | |
210 | 54 | storres | def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \ |
211 | 54 | storres | upperBoundSa): |
212 | 54 | storres | """
|
213 | 85 | storres | TODO: completely rework and test.
|
214 | 54 | storres | """
|
215 | 85 | storres | pobyso = pobyso_name_free_variable_sa_so(funcSa.variables()[0])
|
216 | 159 | storres | funcSo = pobyso_parse_string(funcSa._assume_str().replace('_SAGE_VAR_', '')) |
217 | 54 | storres | rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa) |
218 | 54 | storres | infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo) |
219 | 83 | storres | # Sollya return the infnorm as an interval.
|
220 | 54 | storres | fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo) |
221 | 54 | storres | # Get the top bound and compute the binade top limit.
|
222 | 54 | storres | fMaxUpperBoundSa = fMaxSa.upper() |
223 | 54 | storres | binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
|
224 | 54 | storres | # Put up together the function to use to compute the lower bound.
|
225 | 54 | storres | funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) + \
|
226 | 159 | storres | '-(' + f._assume_str().replace('_SAGE_VAR_', '') + ')') |
227 | 54 | storres | pobyso_autoprint(funcAuxSo) |
228 | 83 | storres | # Clear the Sollya range before a new call to infnorm and issue the call.
|
229 | 54 | storres | sollya_lib_clear_obj(infnormSo) |
230 | 54 | storres | infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo) |
231 | 54 | storres | fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo) |
232 | 54 | storres | sollya_lib_clear_obj(infnormSo) |
233 | 85 | storres | fMinLowerBoundSa = binadeTopLimitSa - fMinSa.lower() |
234 | 54 | storres | # Compute the maximum of the precisions of the different bounds.
|
235 | 54 | storres | maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
|
236 | 54 | storres | fMaxUpperBoundSa.parent().precision()]) |
237 | 54 | storres | # Create a RealIntervalField and create an interval with the "good" bounds.
|
238 | 54 | storres | RRRI = RealIntervalField(maxPrecSa) |
239 | 54 | storres | imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa) |
240 | 83 | storres | # Free the unneeded Sollya objects
|
241 | 54 | storres | sollya_lib_clear_obj(funcSo) |
242 | 54 | storres | sollya_lib_clear_obj(funcAuxSo) |
243 | 54 | storres | sollya_lib_clear_obj(rangeSo) |
244 | 54 | storres | return(imageIntervalSa)
|
245 | 83 | storres | # End pobyso_compute_pos_function_abs_val_bounds_sa_sa
|
246 | 54 | storres | |
247 | 215 | storres | def pobyso_compute_precision_decay_ratio_function_sa_so(): |
248 | 215 | storres | """
|
249 | 215 | storres | Compute the precision decay ratio function for polynomial
|
250 | 215 | storres | coefficient progressive trucation.
|
251 | 215 | storres | """
|
252 | 215 | storres | functionText = """
|
253 | 215 | storres | proc(deg, a, b, we, wq)
|
254 | 215 | storres | {
|
255 | 215 | storres | k = we * (exp(x/a)-1) + wq * (b*x)^2 + (1-we-wq) * x;
|
256 | 215 | storres | return k/k(d);
|
257 | 215 | storres | };
|
258 | 215 | storres | """
|
259 | 215 | storres | return pobyso_parse_string_sa_so(functionText)
|
260 | 215 | storres | # End pobyso_compute_precision_decay_ratio_function.
|
261 | 215 | storres | |
262 | 215 | storres | |
263 | 5 | storres | def pobyso_constant(rnArg): |
264 | 38 | storres | """ Legacy function. See pobyso_constant_sa_so. """
|
265 | 38 | storres | return(pobyso_constant_sa_so(rnArg))
|
266 | 5 | storres | |
267 | 84 | storres | def pobyso_constant_sa_so(rnArgSa, precisionSa=None): |
268 | 52 | storres | """
|
269 | 115 | storres | Create a Sollya constant from a Sage RealNumber.
|
270 | 209 | storres | The sollya_lib_constant() function creates a constant
|
271 | 209 | storres | with the same precision as the source.
|
272 | 52 | storres | """
|
273 | 209 | storres | ## Precision stuff. If one wants to change precisions,
|
274 | 209 | storres | # everything takes place in Sage. That only makes
|
275 | 209 | storres | # sense if one wants to reduce the precision.
|
276 | 209 | storres | if not precisionSa is None: |
277 | 209 | storres | RRR = RealField(precisionSa) |
278 | 209 | storres | rnArgSa = RRR(rnArgSa) |
279 | 209 | storres | #print rnArgSa, rnArgSa.precision()
|
280 | 115 | storres | # Sollya constant creation takes place here.
|
281 | 209 | storres | return sollya_lib_constant(get_rn_value(rnArgSa))
|
282 | 115 | storres | # End pobyso_constant_sa_so
|
283 | 115 | storres | |
284 | 55 | storres | def pobyso_constant_0_sa_so(): |
285 | 115 | storres | """
|
286 | 115 | storres | Obvious.
|
287 | 115 | storres | """
|
288 | 215 | storres | return pobyso_constant_from_int_sa_so(0) |
289 | 55 | storres | |
290 | 5 | storres | def pobyso_constant_1(): |
291 | 115 | storres | """
|
292 | 115 | storres | Obvious.
|
293 | 115 | storres | Legacy function. See pobyso_constant_so_so.
|
294 | 115 | storres | """
|
295 | 215 | storres | return pobyso_constant_1_sa_so()
|
296 | 5 | storres | |
297 | 52 | storres | def pobyso_constant_1_sa_so(): |
298 | 115 | storres | """
|
299 | 115 | storres | Obvious.
|
300 | 115 | storres | """
|
301 | 38 | storres | return(pobyso_constant_from_int_sa_so(1)) |
302 | 38 | storres | |
303 | 5 | storres | def pobyso_constant_from_int(anInt): |
304 | 38 | storres | """ Legacy function. See pobyso_constant_from_int_sa_so. """
|
305 | 215 | storres | return pobyso_constant_from_int_sa_so(anInt)
|
306 | 38 | storres | |
307 | 38 | storres | def pobyso_constant_from_int_sa_so(anInt): |
308 | 115 | storres | """
|
309 | 115 | storres | Get a Sollya constant from a Sage int.
|
310 | 115 | storres | """
|
311 | 215 | storres | return sollya_lib_constant_from_int64(long(anInt)) |
312 | 5 | storres | |
313 | 84 | storres | def pobyso_constant_from_int_so_sa(constSo): |
314 | 84 | storres | """
|
315 | 117 | storres | Get a Sage int from a Sollya int constant.
|
316 | 115 | storres | Usefull for precision or powers in polynomials.
|
317 | 84 | storres | """
|
318 | 200 | storres | constSa = c_long(0)
|
319 | 200 | storres | sollya_lib_get_constant_as_int64(byref(constSa), constSo) |
320 | 215 | storres | return constSa.value
|
321 | 84 | storres | # End pobyso_constant_from_int_so_sa
|
322 | 84 | storres | |
323 | 209 | storres | def pobyso_constant_from_mpq_sa_so(rationalSa): |
324 | 200 | storres | """
|
325 | 200 | storres | Make a Sollya constant from Sage rational.
|
326 | 209 | storres | The Sollya constant is an unevaluated expression.
|
327 | 209 | storres | Hence no precision argument is needed.
|
328 | 209 | storres | It is better to leave this way since Sollya has its own
|
329 | 209 | storres | optimized evaluation mecanism that tries very hard to
|
330 | 209 | storres | return exact values or at least faithful ones.
|
331 | 200 | storres | """
|
332 | 200 | storres | ratExprSo = \ |
333 | 200 | storres | sollya_lib_constant_from_mpq(sgmp_get_rational_value(rationalSa)) |
334 | 209 | storres | return ratExprSo
|
335 | 200 | storres | # End pobyso_constant_from_mpq_sa_so.
|
336 | 200 | storres | |
337 | 209 | storres | def pobyso_constant_sollya_prec_sa_so(rnArgSa): |
338 | 209 | storres | """
|
339 | 209 | storres | Create a Sollya constant from a Sage RealNumber at the
|
340 | 209 | storres | current precision in Sollya.
|
341 | 209 | storres | """
|
342 | 209 | storres | currentSollyaPrecSa = pobyso_get_prec_so_sa() |
343 | 209 | storres | return pobyso_constant_sa_so(rnArgSa, currentSollyaPrecSa)
|
344 | 209 | storres | # End pobyso_constant_sollya_prec_sa_so
|
345 | 215 | storres | |
346 | 215 | storres | def pobyso_end_elliptic_list_so_sa_so(objectsListSo, intCountSa): |
347 | 215 | storres | """
|
348 | 215 | storres | Create a Sollya end elliptic list made of the objectListSo[0] to
|
349 | 215 | storres | objectsListSo[intCountSa-1] objects.
|
350 | 215 | storres | """
|
351 | 215 | storres | return sollya_lib_end_elliptic_list(objectSo, int(intCountSa)) |
352 | 215 | storres | |
353 | 155 | storres | def pobyso_error_so(): |
354 | 155 | storres | return sollya_lib_error(None) |
355 | 155 | storres | # End pobyso_error().
|
356 | 155 | storres | |
357 | 215 | storres | def pobyso_evaluate_so_so(funcSo, argumentSo): |
358 | 215 | storres | """
|
359 | 215 | storres | Evaluates funcSo for arguemntSo through sollya_lib_evaluate().
|
360 | 215 | storres | """
|
361 | 215 | storres | return sollya_lib_evaluate(funcSo, argumentSo)
|
362 | 215 | storres | # End pobyso_evaluate_so_so.
|
363 | 215 | storres | |
364 | 209 | storres | def pobyso_float_poly_sa_so(polySa, precSa = None): |
365 | 209 | storres | """
|
366 | 209 | storres | Create a Sollya polynomial from a Sage RealField polynomial.
|
367 | 209 | storres | """
|
368 | 209 | storres | ## TODO: filter arguments.
|
369 | 209 | storres | ## Precision. If a precision is given, convert the polynomial
|
370 | 209 | storres | # into the right polynomial field. If not convert it straight
|
371 | 209 | storres | # to Sollya.
|
372 | 209 | storres | if not precSa is None: |
373 | 209 | storres | RRR = RealField(precSa) |
374 | 209 | storres | ## Create a Sage polynomial in the "right" precision.
|
375 | 209 | storres | P_RRR = RRR[polySa.variables()[0]]
|
376 | 209 | storres | polyFloatSa = P_RRR(polySa) |
377 | 209 | storres | else:
|
378 | 209 | storres | polyFloatSa = polySa |
379 | 209 | storres | precSa = polySa.parent().base_ring().precision() |
380 | 209 | storres | ## Get exponents and coefficients.
|
381 | 213 | storres | exponentsSa = polyFloatSa.exponents() |
382 | 209 | storres | coefficientsSa = polyFloatSa.coefficients() |
383 | 209 | storres | ## Build the polynomial.
|
384 | 209 | storres | polySo = None
|
385 | 213 | storres | for coefficientSa, exponentSa in zip(coefficientsSa, exponentsSa): |
386 | 209 | storres | #print coefficientSa.n(prec=precSa), exponentSa
|
387 | 209 | storres | coefficientSo = \ |
388 | 209 | storres | pobyso_constant_sa_so(coefficientSa) |
389 | 209 | storres | #pobyso_autoprint(coefficientSo)
|
390 | 209 | storres | exponentSo = \ |
391 | 209 | storres | pobyso_constant_from_int_sa_so(exponentSa) |
392 | 209 | storres | #pobyso_autoprint(exponentSo)
|
393 | 209 | storres | monomialSo = sollya_lib_build_function_pow( |
394 | 209 | storres | sollya_lib_build_function_free_variable(), |
395 | 209 | storres | exponentSo) |
396 | 209 | storres | if polySo is None: |
397 | 209 | storres | polySo = sollya_lib_build_function_mul(coefficientSo, |
398 | 209 | storres | monomialSo) |
399 | 209 | storres | else:
|
400 | 209 | storres | polyTermSo = sollya_lib_build_function_mul(coefficientSo, |
401 | 209 | storres | monomialSo) |
402 | 209 | storres | polySo = sollya_lib_build_function_add(polySo, polyTermSo) |
403 | 209 | storres | return polySo
|
404 | 209 | storres | # End pobyso_float_poly_sa_so
|
405 | 209 | storres | |
406 | 209 | storres | def pobyso_float_poly_so_sa(polySo, realFieldSa=None): |
407 | 209 | storres | """
|
408 | 209 | storres | Convert a Sollya polynomial into a Sage floating-point polynomial.
|
409 | 209 | storres | We assume that the polynomial is in canonical form.
|
410 | 209 | storres | If no realField is given, a RealField corresponding to the maximum
|
411 | 209 | storres | precision of the coefficients is internally computed.
|
412 | 209 | storres | The real field is not returned but can be easily retrieved from
|
413 | 209 | storres | the polynomial itself.
|
414 | 209 | storres | ALGORITHM:
|
415 | 209 | storres | - (optional) compute the RealField of the coefficients;
|
416 | 209 | storres | - convert the Sollya expression into a Sage expression;
|
417 | 209 | storres | - convert the Sage expression into a Sage polynomial
|
418 | 209 | storres | TODO: the canonical thing for the polynomial.
|
419 | 209 | storres | """
|
420 | 209 | storres | if realFieldSa is None: |
421 | 209 | storres | expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo) |
422 | 215 | storres | print "Maximum precision of Sollya polynomial coefficients:", expressionPrecSa |
423 | 209 | storres | realFieldSa = RealField(expressionPrecSa) |
424 | 209 | storres | #print "Sollya expression before...",
|
425 | 209 | storres | #pobyso_autoprint(polySo)
|
426 | 209 | storres | |
427 | 209 | storres | expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, |
428 | 209 | storres | realFieldSa) |
429 | 209 | storres | #print "...Sollya expression after.",
|
430 | 209 | storres | #pobyso_autoprint(polySo)
|
431 | 209 | storres | polyVariableSa = expressionSa.variables()[0]
|
432 | 209 | storres | polyRingSa = realFieldSa[str(polyVariableSa)]
|
433 | 209 | storres | #print polyRingSa
|
434 | 209 | storres | # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
|
435 | 209 | storres | polynomialSa = polyRingSa(expressionSa) |
436 | 215 | storres | polyCoeffsListSa = polynomialSa.coefficients() |
437 | 215 | storres | #for coeff in polyCoeffsListSa:
|
438 | 215 | storres | # print coeff.abs().n()
|
439 | 209 | storres | return polynomialSa
|
440 | 209 | storres | # End pobyso_float_poly_so_sa
|
441 | 209 | storres | |
442 | 215 | storres | def pobyso_free_variable(): |
443 | 215 | storres | """
|
444 | 215 | storres | Ultra thin wrapper around the sollya_lib_function_build_free_variable function.
|
445 | 215 | storres | """
|
446 | 215 | storres | return sollya_lib_build_function_free_variable()
|
447 | 209 | storres | |
448 | 5 | storres | def pobyso_function_type_as_string(funcType): |
449 | 38 | storres | """ Legacy function. See pobyso_function_type_as_string_so_sa. """
|
450 | 38 | storres | return(pobyso_function_type_as_string_so_sa(funcType))
|
451 | 38 | storres | |
452 | 38 | storres | def pobyso_function_type_as_string_so_sa(funcType): |
453 | 38 | storres | """
|
454 | 38 | storres | Numeric Sollya function codes -> Sage mathematical function names.
|
455 | 38 | storres | Notice that pow -> ^ (a la Sage, not a la Python).
|
456 | 38 | storres | """
|
457 | 5 | storres | if funcType == SOLLYA_BASE_FUNC_ABS:
|
458 | 5 | storres | return "abs" |
459 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ACOS:
|
460 | 5 | storres | return "arccos" |
461 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ACOSH:
|
462 | 5 | storres | return "arccosh" |
463 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ADD:
|
464 | 5 | storres | return "+" |
465 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ASIN:
|
466 | 5 | storres | return "arcsin" |
467 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ASINH:
|
468 | 5 | storres | return "arcsinh" |
469 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ATAN:
|
470 | 5 | storres | return "arctan" |
471 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ATANH:
|
472 | 5 | storres | return "arctanh" |
473 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_CEIL:
|
474 | 5 | storres | return "ceil" |
475 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
|
476 | 5 | storres | return "cte" |
477 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_COS:
|
478 | 5 | storres | return "cos" |
479 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_COSH:
|
480 | 5 | storres | return "cosh" |
481 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DIV:
|
482 | 5 | storres | return "/" |
483 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
|
484 | 5 | storres | return "double" |
485 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
|
486 | 5 | storres | return "doubleDouble" |
487 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
|
488 | 5 | storres | return "doubleDxtended" |
489 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ERF:
|
490 | 5 | storres | return "erf" |
491 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ERFC:
|
492 | 5 | storres | return "erfc" |
493 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_EXP:
|
494 | 5 | storres | return "exp" |
495 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
|
496 | 5 | storres | return "expm1" |
497 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_FLOOR:
|
498 | 5 | storres | return "floor" |
499 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
500 | 5 | storres | return "freeVariable" |
501 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
|
502 | 5 | storres | return "halfPrecision" |
503 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
|
504 | 5 | storres | return "libraryConstant" |
505 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
|
506 | 5 | storres | return "libraryFunction" |
507 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG:
|
508 | 5 | storres | return "log" |
509 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG_10:
|
510 | 5 | storres | return "log10" |
511 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
|
512 | 5 | storres | return "log1p" |
513 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG_2:
|
514 | 5 | storres | return "log2" |
515 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_MUL:
|
516 | 5 | storres | return "*" |
517 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
|
518 | 5 | storres | return "round" |
519 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_NEG:
|
520 | 5 | storres | return "__neg__" |
521 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_PI:
|
522 | 5 | storres | return "pi" |
523 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_POW:
|
524 | 5 | storres | return "^" |
525 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
|
526 | 5 | storres | return "procedureFunction" |
527 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_QUAD:
|
528 | 5 | storres | return "quad" |
529 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SIN:
|
530 | 5 | storres | return "sin" |
531 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SINGLE:
|
532 | 5 | storres | return "single" |
533 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SINH:
|
534 | 5 | storres | return "sinh" |
535 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SQRT:
|
536 | 5 | storres | return "sqrt" |
537 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SUB:
|
538 | 5 | storres | return "-" |
539 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_TAN:
|
540 | 5 | storres | return "tan" |
541 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_TANH:
|
542 | 5 | storres | return "tanh" |
543 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
|
544 | 5 | storres | return "tripleDouble" |
545 | 5 | storres | else:
|
546 | 5 | storres | return None |
547 | 5 | storres | |
548 | 85 | storres | def pobyso_get_constant(rnArgSa, constSo): |
549 | 38 | storres | """ Legacy function. See pobyso_get_constant_so_sa. """
|
550 | 209 | storres | return pobyso_get_constant_so_sa(rnArgSa, constSo)
|
551 | 209 | storres | # End pobyso_get_constant
|
552 | 38 | storres | |
553 | 84 | storres | def pobyso_get_constant_so_sa(rnArgSa, constSo): |
554 | 52 | storres | """
|
555 | 85 | storres | Set the value of rnArgSo to the value of constSo in MPFR_RNDN mode.
|
556 | 52 | storres | rnArg must already exist and belong to some RealField.
|
557 | 85 | storres | We assume that constSo points to a Sollya constant.
|
558 | 52 | storres | """
|
559 | 209 | storres | outcome = sollya_lib_get_constant(get_rn_value(rnArgSa), constSo) |
560 | 209 | storres | if outcome == 0: # Failure because constSo is not a constant expression. |
561 | 209 | storres | return None |
562 | 209 | storres | else:
|
563 | 209 | storres | return outcome
|
564 | 209 | storres | # End pobyso_get_constant_so_sa
|
565 | 209 | storres | |
566 | 57 | storres | def pobyso_get_constant_as_rn(ctExpSo): |
567 | 83 | storres | """
|
568 | 83 | storres | Legacy function. See pobyso_get_constant_as_rn_so_sa.
|
569 | 83 | storres | """
|
570 | 57 | storres | return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
|
571 | 38 | storres | |
572 | 56 | storres | def pobyso_get_constant_as_rn_so_sa(constExpSo): |
573 | 83 | storres | """
|
574 | 83 | storres | Get a Sollya constant as a Sage "real number".
|
575 | 83 | storres | The precision of the floating-point number returned is that of the Sollya
|
576 | 83 | storres | constant.
|
577 | 83 | storres | """
|
578 | 209 | storres | precisionSa = pobyso_get_prec_of_constant_so_sa(constExpSo) |
579 | 209 | storres | ## If the expression can not be exactly converted, None is returned.
|
580 | 209 | storres | # In this case opt for the Sollya current expression.
|
581 | 209 | storres | if precisionSa is None: |
582 | 209 | storres | precisionSa = pobyso_get_prec_so_sa() |
583 | 56 | storres | RRRR = RealField(precisionSa) |
584 | 56 | storres | rnSa = RRRR(0)
|
585 | 209 | storres | outcome = sollya_lib_get_constant(get_rn_value(rnSa), constExpSo) |
586 | 209 | storres | if outcome == 0: |
587 | 209 | storres | return None |
588 | 209 | storres | else:
|
589 | 209 | storres | return rnSa
|
590 | 83 | storres | # End pobyso_get_constant_as_rn_so_sa
|
591 | 38 | storres | |
592 | 38 | storres | def pobyso_get_constant_as_rn_with_rf(ctExp, realField): |
593 | 83 | storres | """
|
594 | 83 | storres | Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
|
595 | 83 | storres | """
|
596 | 209 | storres | return pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField)
|
597 | 209 | storres | # End pobyso_get_constant_as_rn_with_rf
|
598 | 5 | storres | |
599 | 56 | storres | def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None): |
600 | 83 | storres | """
|
601 | 83 | storres | Get a Sollya constant as a Sage "real number".
|
602 | 83 | storres | If no real field is specified, the precision of the floating-point number
|
603 | 85 | storres | returned is that of the Sollya constant.
|
604 | 83 | storres | Otherwise is is that of the real field. Hence rounding may happen.
|
605 | 83 | storres | """
|
606 | 56 | storres | if realFieldSa is None: |
607 | 209 | storres | return pobyso_get_constant_as_rn_so_sa(ctExpSo)
|
608 | 56 | storres | rnSa = realFieldSa(0)
|
609 | 209 | storres | outcome = sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo) |
610 | 209 | storres | if outcome == 0: |
611 | 209 | storres | return None |
612 | 209 | storres | else:
|
613 | 209 | storres | return rnSa
|
614 | 83 | storres | # End pobyso_get_constant_as_rn_with_rf_so_sa
|
615 | 38 | storres | |
616 | 5 | storres | def pobyso_get_free_variable_name(): |
617 | 83 | storres | """
|
618 | 83 | storres | Legacy function. See pobyso_get_free_variable_name_so_sa.
|
619 | 83 | storres | """
|
620 | 38 | storres | return(pobyso_get_free_variable_name_so_sa())
|
621 | 38 | storres | |
622 | 38 | storres | def pobyso_get_free_variable_name_so_sa(): |
623 | 209 | storres | return sollya_lib_get_free_variable_name()
|
624 | 5 | storres | |
625 | 38 | storres | def pobyso_get_function_arity(expressionSo): |
626 | 83 | storres | """
|
627 | 83 | storres | Legacy function. See pobyso_get_function_arity_so_sa.
|
628 | 83 | storres | """
|
629 | 38 | storres | return(pobyso_get_function_arity_so_sa(expressionSo))
|
630 | 38 | storres | |
631 | 38 | storres | def pobyso_get_function_arity_so_sa(expressionSo): |
632 | 5 | storres | arity = c_int(0)
|
633 | 38 | storres | sollya_lib_get_function_arity(byref(arity),expressionSo) |
634 | 209 | storres | return int(arity.value) |
635 | 5 | storres | |
636 | 38 | storres | def pobyso_get_head_function(expressionSo): |
637 | 83 | storres | """
|
638 | 83 | storres | Legacy function. See pobyso_get_head_function_so_sa.
|
639 | 83 | storres | """
|
640 | 38 | storres | return(pobyso_get_head_function_so_sa(expressionSo))
|
641 | 38 | storres | |
642 | 38 | storres | def pobyso_get_head_function_so_sa(expressionSo): |
643 | 5 | storres | functionType = c_int(0)
|
644 | 38 | storres | sollya_lib_get_head_function(byref(functionType), expressionSo, None)
|
645 | 209 | storres | return int(functionType.value) |
646 | 5 | storres | |
647 | 56 | storres | def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ): |
648 | 53 | storres | """
|
649 | 53 | storres | Return the Sage interval corresponding to the Sollya range argument.
|
650 | 83 | storres | If no reaIntervalField is passed as an argument, the interval bounds are not
|
651 | 56 | storres | rounded: they are elements of RealIntervalField of the "right" precision
|
652 | 56 | storres | to hold all the digits.
|
653 | 53 | storres | """
|
654 | 53 | storres | prec = c_int(0)
|
655 | 56 | storres | if realIntervalFieldSa is None: |
656 | 56 | storres | retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
|
657 | 56 | storres | if retval == 0: |
658 | 209 | storres | return None |
659 | 56 | storres | realIntervalFieldSa = RealIntervalField(prec.value) |
660 | 56 | storres | intervalSa = realIntervalFieldSa(0,0) |
661 | 53 | storres | retval = \ |
662 | 53 | storres | sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\ |
663 | 53 | storres | soRange) |
664 | 53 | storres | if retval == 0: |
665 | 209 | storres | return None |
666 | 209 | storres | return intervalSa
|
667 | 56 | storres | # End pobyso_get_interval_from_range_so_sa
|
668 | 56 | storres | |
669 | 5 | storres | def pobyso_get_list_elements(soObj): |
670 | 38 | storres | """ Legacy function. See pobyso_get_list_elements_so_so. """
|
671 | 209 | storres | return pobyso_get_list_elements_so_so(soObj)
|
672 | 38 | storres | |
673 | 117 | storres | def pobyso_get_list_elements_so_so(objectListSo): |
674 | 51 | storres | """
|
675 | 118 | storres | Get the Sollya list elements as a Sage/Python array of Sollya objects.
|
676 | 118 | storres |
|
677 | 118 | storres | INPUT:
|
678 | 118 | storres | - objectListSo: a Sollya list of Sollya objects.
|
679 | 118 | storres |
|
680 | 118 | storres | OUTPUT:
|
681 | 118 | storres | - a Sage/Python tuple made of:
|
682 | 118 | storres | - a Sage/Python list of Sollya objects,
|
683 | 118 | storres | - a Sage/Python int holding the number of elements,
|
684 | 118 | storres | - a Sage/Python int stating (!= 0) that the list is end-elliptic.
|
685 | 118 | storres | NOTE::
|
686 | 118 | storres | We recover the addresses of the Sollya object from the list of pointers
|
687 | 118 | storres | returned by sollya_lib_get_list_elements. The list itself is freed.
|
688 | 118 | storres | TODO::
|
689 | 118 | storres | Figure out what to do with numElements since the number of elements
|
690 | 118 | storres | can easily be recovered from the list itself.
|
691 | 118 | storres | Ditto for isEndElliptic.
|
692 | 51 | storres | """
|
693 | 5 | storres | listAddress = POINTER(c_longlong)() |
694 | 5 | storres | numElements = c_int(0)
|
695 | 5 | storres | isEndElliptic = c_int(0)
|
696 | 117 | storres | listAsSageList = [] |
697 | 5 | storres | result = sollya_lib_get_list_elements(byref(listAddress),\ |
698 | 54 | storres | byref(numElements),\ |
699 | 54 | storres | byref(isEndElliptic),\ |
700 | 117 | storres | objectListSo) |
701 | 5 | storres | if result == 0 : |
702 | 5 | storres | return None |
703 | 5 | storres | for i in xrange(0, numElements.value, 1): |
704 | 118 | storres | #listAsSageList.append(sollya_lib_copy_obj(listAddress[i]))
|
705 | 118 | storres | listAsSageList.append(listAddress[i]) |
706 | 117 | storres | # Clear each of the elements returned by Sollya.
|
707 | 118 | storres | #sollya_lib_clear_obj(listAddress[i])
|
708 | 117 | storres | # Free the list itself.
|
709 | 117 | storres | sollya_lib_free(listAddress) |
710 | 209 | storres | return (listAsSageList, numElements.value, isEndElliptic.value)
|
711 | 5 | storres | |
712 | 38 | storres | def pobyso_get_max_prec_of_exp(soExp): |
713 | 38 | storres | """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
|
714 | 209 | storres | return pobyso_get_max_prec_of_exp_so_sa(soExp)
|
715 | 5 | storres | |
716 | 85 | storres | def pobyso_get_max_prec_of_exp_so_sa(expSo): |
717 | 38 | storres | """
|
718 | 38 | storres | Get the maximum precision used for the numbers in a Sollya expression.
|
719 | 52 | storres |
|
720 | 52 | storres | Arguments:
|
721 | 52 | storres | soExp -- a Sollya expression pointer
|
722 | 52 | storres | Return value:
|
723 | 52 | storres | A Python integer
|
724 | 38 | storres | TODO:
|
725 | 38 | storres | - error management;
|
726 | 38 | storres | - correctly deal with numerical type such as DOUBLEEXTENDED.
|
727 | 38 | storres | """
|
728 | 5 | storres | maxPrecision = 0
|
729 | 52 | storres | minConstPrec = 0
|
730 | 52 | storres | currentConstPrec = 0
|
731 | 85 | storres | operator = pobyso_get_head_function_so_sa(expSo) |
732 | 5 | storres | if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \ |
733 | 5 | storres | (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
734 | 85 | storres | (arity, subexpressions) = pobyso_get_subfunctions_so_sa(expSo) |
735 | 5 | storres | for i in xrange(arity): |
736 | 5 | storres | maxPrecisionCandidate = \ |
737 | 38 | storres | pobyso_get_max_prec_of_exp_so_sa(subexpressions[i]) |
738 | 5 | storres | if maxPrecisionCandidate > maxPrecision:
|
739 | 5 | storres | maxPrecision = maxPrecisionCandidate |
740 | 209 | storres | return maxPrecision
|
741 | 5 | storres | elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
742 | 85 | storres | #minConstPrec = pobyso_get_min_prec_of_constant_so_sa(expSo)
|
743 | 52 | storres | #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
|
744 | 52 | storres | #print minConstPrec, " - ", currentConstPrec
|
745 | 209 | storres | return pobyso_get_min_prec_of_constant_so_sa(expSo)
|
746 | 52 | storres | |
747 | 5 | storres | elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
748 | 209 | storres | return 0 |
749 | 5 | storres | else:
|
750 | 38 | storres | print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator." |
751 | 209 | storres | return 0 |
752 | 5 | storres | |
753 | 85 | storres | def pobyso_get_min_prec_of_constant_so_sa(constExpSo): |
754 | 52 | storres | """
|
755 | 52 | storres | Get the minimum precision necessary to represent the value of a Sollya
|
756 | 52 | storres | constant.
|
757 | 52 | storres | MPFR_MIN_PREC and powers of 2 are taken into account.
|
758 | 209 | storres | We assume that constExpSo is a pointer to a Sollay constant expression.
|
759 | 52 | storres | """
|
760 | 85 | storres | constExpAsRnSa = pobyso_get_constant_as_rn_so_sa(constExpSo) |
761 | 85 | storres | return(min_mpfr_size(get_rn_value(constExpAsRnSa)))
|
762 | 52 | storres | |
763 | 200 | storres | def pobyso_get_poly_so_sa(polySo, realFieldSa=None): |
764 | 200 | storres | """
|
765 | 200 | storres | Convert a Sollya polynomial into a Sage polynomial.
|
766 | 209 | storres | Legacy function. Use pobyso_float_poly_so_sa() instead.
|
767 | 200 | storres | """
|
768 | 213 | storres | return pobyso_float_poly_so_sa(polySo,realFieldSa)
|
769 | 200 | storres | # End pobyso_get_poly_so_sa
|
770 | 200 | storres | |
771 | 200 | storres | def pobyso_get_prec(): |
772 | 200 | storres | """ Legacy function. See pobyso_get_prec_so_sa(). """
|
773 | 209 | storres | return pobyso_get_prec_so_sa()
|
774 | 200 | storres | |
775 | 200 | storres | def pobyso_get_prec_so(): |
776 | 200 | storres | """
|
777 | 200 | storres | Get the current default precision in Sollya.
|
778 | 200 | storres | The return value is a Sollya object.
|
779 | 200 | storres | Usefull when modifying the precision back and forth by avoiding
|
780 | 200 | storres | extra conversions.
|
781 | 200 | storres | """
|
782 | 209 | storres | return sollya_lib_get_prec(None) |
783 | 200 | storres | |
784 | 200 | storres | def pobyso_get_prec_so_sa(): |
785 | 200 | storres | """
|
786 | 200 | storres | Get the current default precision in Sollya.
|
787 | 200 | storres | The return value is Sage/Python int.
|
788 | 200 | storres | """
|
789 | 200 | storres | precSo = sollya_lib_get_prec(None)
|
790 | 200 | storres | precSa = c_int(0)
|
791 | 200 | storres | sollya_lib_get_constant_as_int(byref(precSa), precSo) |
792 | 200 | storres | sollya_lib_clear_obj(precSo) |
793 | 200 | storres | return int(precSa.value) |
794 | 200 | storres | # End pobyso_get_prec_so_sa.
|
795 | 200 | storres | |
796 | 209 | storres | def pobyso_get_prec_so_so_sa(): |
797 | 209 | storres | """
|
798 | 209 | storres | Return the current precision both as a Sollya object and a
|
799 | 209 | storres | Sage integer as hybrid tuple.
|
800 | 209 | storres | To avoid multiple calls for precision manipulations.
|
801 | 209 | storres | """
|
802 | 209 | storres | precSo = sollya_lib_get_prec(None)
|
803 | 209 | storres | precSa = c_int(0)
|
804 | 209 | storres | sollya_lib_get_constant_as_int(byref(precSa), precSo) |
805 | 209 | storres | return (precSo, precSa)
|
806 | 200 | storres | |
807 | 200 | storres | def pobyso_get_prec_of_constant(ctExpSo): |
808 | 200 | storres | """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
|
809 | 209 | storres | return pobyso_get_prec_of_constant_so_sa(ctExpSo)
|
810 | 200 | storres | |
811 | 200 | storres | def pobyso_get_prec_of_constant_so_sa(ctExpSo): |
812 | 200 | storres | """
|
813 | 200 | storres | Tries to find a precision to represent ctExpSo without rounding.
|
814 | 200 | storres | If not possible, returns None.
|
815 | 200 | storres | """
|
816 | 200 | storres | prec = c_int(0)
|
817 | 200 | storres | retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
|
818 | 200 | storres | if retc == 0: |
819 | 209 | storres | return None |
820 | 209 | storres | return int(prec.value) |
821 | 200 | storres | |
822 | 200 | storres | def pobyso_get_prec_of_range_so_sa(rangeSo): |
823 | 200 | storres | """
|
824 | 200 | storres | Returns the number of bits elements of a range are coded with.
|
825 | 200 | storres | """
|
826 | 200 | storres | prec = c_int(0)
|
827 | 200 | storres | retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
|
828 | 200 | storres | if retc == 0: |
829 | 200 | storres | return(None) |
830 | 209 | storres | return int(prec.value) |
831 | 200 | storres | # End pobyso_get_prec_of_range_so_sa()
|
832 | 200 | storres | |
833 | 85 | storres | def pobyso_get_sage_exp_from_sollya_exp(sollyaExpSo, realField = RR): |
834 | 38 | storres | """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
|
835 | 209 | storres | return pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo,
|
836 | 209 | storres | realField = RR) |
837 | 38 | storres | |
838 | 85 | storres | def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, realFieldSa = RR): |
839 | 5 | storres | """
|
840 | 38 | storres | Get a Sage expression from a Sollya expression.
|
841 | 38 | storres | Currently only tested with polynomials with floating-point coefficients.
|
842 | 5 | storres | Notice that, in the returned polynomial, the exponents are RealNumbers.
|
843 | 5 | storres | """
|
844 | 5 | storres | #pobyso_autoprint(sollyaExp)
|
845 | 85 | storres | operatorSa = pobyso_get_head_function_so_sa(sollyaExpSo) |
846 | 83 | storres | sollyaLibFreeVariableName = sollya_lib_get_free_variable_name() |
847 | 213 | storres | ## Get rid of the "_"'s in "_x_", if any.
|
848 | 213 | storres | sollyaLibFreeVariableName = re.sub('_', '', sollyaLibFreeVariableName) |
849 | 5 | storres | # Constants and the free variable are special cases.
|
850 | 5 | storres | # All other operator are dealt with in the same way.
|
851 | 85 | storres | if (operatorSa != SOLLYA_BASE_FUNC_CONSTANT) and \ |
852 | 85 | storres | (operatorSa != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
853 | 85 | storres | (aritySa, subexpressionsSa) = pobyso_get_subfunctions_so_sa(sollyaExpSo) |
854 | 85 | storres | if aritySa == 1: |
855 | 85 | storres | sageExpSa = eval(pobyso_function_type_as_string_so_sa(operatorSa) + \
|
856 | 85 | storres | "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], \ |
857 | 85 | storres | realFieldSa) + ")")
|
858 | 85 | storres | elif aritySa == 2: |
859 | 63 | storres | # We do not get through the preprocessor.
|
860 | 63 | storres | # The "^" operator is then a special case.
|
861 | 85 | storres | if operatorSa == SOLLYA_BASE_FUNC_POW:
|
862 | 85 | storres | operatorAsStringSa = "**"
|
863 | 5 | storres | else:
|
864 | 85 | storres | operatorAsStringSa = \ |
865 | 85 | storres | pobyso_function_type_as_string_so_sa(operatorSa) |
866 | 85 | storres | sageExpSa = \ |
867 | 85 | storres | eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], realFieldSa)"\ |
868 | 85 | storres | + " " + operatorAsStringSa + " " + \ |
869 | 85 | storres | "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[1], realFieldSa)")
|
870 | 63 | storres | # We do not know yet how to deal with arity >= 3
|
871 | 63 | storres | # (is there any in Sollya anyway?).
|
872 | 5 | storres | else:
|
873 | 85 | storres | sageExpSa = eval('None') |
874 | 209 | storres | return sageExpSa
|
875 | 85 | storres | elif operatorSa == SOLLYA_BASE_FUNC_CONSTANT:
|
876 | 5 | storres | #print "This is a constant"
|
877 | 85 | storres | return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExpSo, realFieldSa)
|
878 | 85 | storres | elif operatorSa == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
879 | 5 | storres | #print "This is free variable"
|
880 | 209 | storres | return eval(sollyaLibFreeVariableName) |
881 | 5 | storres | else:
|
882 | 5 | storres | print "Unexpected" |
883 | 5 | storres | return eval('None') |
884 | 185 | storres | # End pobyso_get_sage_exp_from_sollya_exp_so_sa
|
885 | 73 | storres | |
886 | 185 | storres | |
887 | 38 | storres | def pobyso_get_subfunctions(expressionSo): |
888 | 38 | storres | """ Legacy function. See pobyso_get_subfunctions_so_sa. """
|
889 | 209 | storres | return pobyso_get_subfunctions_so_sa(expressionSo)
|
890 | 200 | storres | # End pobyso_get_subfunctions.
|
891 | 200 | storres | |
892 | 38 | storres | def pobyso_get_subfunctions_so_sa(expressionSo): |
893 | 38 | storres | """
|
894 | 38 | storres | Get the subfunctions of an expression.
|
895 | 38 | storres | Return the number of subfunctions and the list of subfunctions addresses.
|
896 | 55 | storres | S.T.: Could not figure out another way than that ugly list of declarations
|
897 | 83 | storres | to recover the addresses of the subfunctions.
|
898 | 83 | storres | We limit ourselves to arity 8 functions.
|
899 | 38 | storres | """
|
900 | 5 | storres | subf0 = c_int(0)
|
901 | 5 | storres | subf1 = c_int(0)
|
902 | 5 | storres | subf2 = c_int(0)
|
903 | 5 | storres | subf3 = c_int(0)
|
904 | 5 | storres | subf4 = c_int(0)
|
905 | 5 | storres | subf5 = c_int(0)
|
906 | 5 | storres | subf6 = c_int(0)
|
907 | 5 | storres | subf7 = c_int(0)
|
908 | 5 | storres | subf8 = c_int(0)
|
909 | 5 | storres | arity = c_int(0)
|
910 | 5 | storres | nullPtr = POINTER(c_int)() |
911 | 38 | storres | sollya_lib_get_subfunctions(expressionSo, byref(arity), \ |
912 | 83 | storres | byref(subf0), byref(subf1), byref(subf2), byref(subf3), \ |
913 | 83 | storres | byref(subf4), byref(subf5),\ |
914 | 83 | storres | byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
|
915 | 83 | storres | # byref(cast(subfunctions[0], POINTER(c_int))), \
|
916 | 83 | storres | # byref(cast(subfunctions[0], POINTER(c_int))), \
|
917 | 83 | storres | # byref(cast(subfunctions[2], POINTER(c_int))), \
|
918 | 83 | storres | # byref(cast(subfunctions[3], POINTER(c_int))), \
|
919 | 83 | storres | # byref(cast(subfunctions[4], POINTER(c_int))), \
|
920 | 83 | storres | # byref(cast(subfunctions[5], POINTER(c_int))), \
|
921 | 83 | storres | # byref(cast(subfunctions[6], POINTER(c_int))), \
|
922 | 83 | storres | # byref(cast(subfunctions[7], POINTER(c_int))), \
|
923 | 5 | storres | # byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
|
924 | 83 | storres | subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \ |
925 | 83 | storres | subf8] |
926 | 5 | storres | subs = [] |
927 | 5 | storres | if arity.value > pobyso_max_arity:
|
928 | 38 | storres | return(0,[]) |
929 | 5 | storres | for i in xrange(arity.value): |
930 | 5 | storres | subs.append(int(subfunctions[i].value))
|
931 | 5 | storres | #print subs[i]
|
932 | 209 | storres | return (int(arity.value), subs) |
933 | 200 | storres | # End pobyso_get_subfunctions_so_sa
|
934 | 5 | storres | |
935 | 155 | storres | def pobyso_guess_degree_sa_sa(functionSa, intervalSa, approxErrorSa, |
936 | 155 | storres | weightSa=None, degreeBoundSa=None): |
937 | 155 | storres | """
|
938 | 155 | storres | Sa_sa variant of the solly_guessdegree function.
|
939 | 155 | storres | Return 0 if something goes wrong.
|
940 | 155 | storres | """
|
941 | 159 | storres | functionAsStringSa = functionSa._assume_str().replace('_SAGE_VAR_', '') |
942 | 154 | storres | functionSo = pobyso_parse_string_sa_so(functionAsStringSa) |
943 | 155 | storres | if pobyso_is_error_so_sa(functionSo):
|
944 | 155 | storres | sollya_lib_clear_obj(functionSo) |
945 | 155 | storres | return 0 |
946 | 154 | storres | rangeSo = pobyso_interval_to_range_sa_so(intervalSa) |
947 | 155 | storres | # The approximation error is expected to be a floating point number.
|
948 | 155 | storres | if pobyso_is_floating_point_number_sa_sa(approxErrorSa):
|
949 | 155 | storres | approxErrorSo = pobyso_constant_sa_so(approxErrorSa) |
950 | 155 | storres | else:
|
951 | 155 | storres | approxErrorSo = pobyso_constant_sa_so(RR(approxErrorSa)) |
952 | 154 | storres | if not weightSa is None: |
953 | 159 | storres | weightAsStringSa = weightSa._assume_str().replace('_SAGE_VAR_', '') |
954 | 154 | storres | weightSo = pobyso_parse_string_sa_so(weightAsStringSa) |
955 | 166 | storres | if pobyso_is_error_so_sa(weightSo):
|
956 | 155 | storres | sollya_lib_clear_obj(functionSo) |
957 | 155 | storres | sollya_lib_clear_obj(rangeSo) |
958 | 155 | storres | sollya_lib_clear_obj(approxErrorSo) |
959 | 155 | storres | sollya_lib_clear_obj(weightSo) |
960 | 155 | storres | return 0 |
961 | 154 | storres | else:
|
962 | 154 | storres | weightSo = None
|
963 | 154 | storres | if not degreeBoundSa is None: |
964 | 154 | storres | degreeBoundSo = pobyso_constant_from_int_sa_so(degreeBoundSa) |
965 | 154 | storres | else:
|
966 | 154 | storres | degreeBoundSo = None
|
967 | 154 | storres | guessedDegreeSa = pobyso_guess_degree_so_sa(functionSo, |
968 | 162 | storres | rangeSo, |
969 | 162 | storres | approxErrorSo, |
970 | 162 | storres | weightSo, |
971 | 162 | storres | degreeBoundSo) |
972 | 154 | storres | sollya_lib_clear_obj(functionSo) |
973 | 154 | storres | sollya_lib_clear_obj(rangeSo) |
974 | 155 | storres | sollya_lib_clear_obj(approxErrorSo) |
975 | 154 | storres | if not weightSo is None: |
976 | 154 | storres | sollya_lib_clear_obj(weightSo) |
977 | 154 | storres | if not degreeBoundSo is None: |
978 | 154 | storres | sollya_lib_clear_obj(degreeBoundSo) |
979 | 154 | storres | return guessedDegreeSa
|
980 | 154 | storres | # End poyso_guess_degree_sa_sa
|
981 | 154 | storres | |
982 | 153 | storres | def pobyso_guess_degree_so_sa(functionSo, rangeSo, errorSo, weightSo=None, \ |
983 | 154 | storres | degreeBoundSo=None):
|
984 | 154 | storres | """
|
985 | 154 | storres | Thin wrapper around the guessdegree function.
|
986 | 154 | storres | Nevertheless, some precision control stuff has been appended.
|
987 | 154 | storres | """
|
988 | 154 | storres | # Deal with Sollya internal precision issues: if it is too small,
|
989 | 154 | storres | # compared with the error, increases it to about twice -log2(error).
|
990 | 154 | storres | errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(errorSo) |
991 | 154 | storres | log2ErrorSa = errorSa.log2() |
992 | 154 | storres | if log2ErrorSa < 0: |
993 | 154 | storres | neededPrecisionSa = int(2 * int(-log2ErrorSa) / 64) * 64 |
994 | 154 | storres | else:
|
995 | 154 | storres | neededPrecisionSa = int(2 * int(log2ErrorSa) / 64) * 64 |
996 | 154 | storres | #print "Needed precision:", neededPrecisionSa
|
997 | 154 | storres | currentPrecSa = pobyso_get_prec_so_sa() |
998 | 154 | storres | if neededPrecisionSa > currentPrecSa:
|
999 | 154 | storres | currentPrecSo = pobyso_get_prec_so() |
1000 | 154 | storres | pobyso_set_prec_sa_so(neededPrecisionSa) |
1001 | 166 | storres | #print "Guessing degree..."
|
1002 | 153 | storres | # weightSo and degreeBoundsSo are optional arguments.
|
1003 | 162 | storres | # As declared, sollya_lib_guessdegree must take 5 arguments.
|
1004 | 153 | storres | if weightSo is None: |
1005 | 162 | storres | degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, errorSo, |
1006 | 162 | storres | 0, 0, None) |
1007 | 154 | storres | elif degreeBoundSo is None: |
1008 | 153 | storres | degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, \ |
1009 | 162 | storres | errorSo, weightSo, 0, None) |
1010 | 153 | storres | else:
|
1011 | 153 | storres | degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, errorSo, \ |
1012 | 154 | storres | weightSo, degreeBoundSo, None)
|
1013 | 166 | storres | #print "...degree guess done."
|
1014 | 154 | storres | # Restore internal precision, if applicable.
|
1015 | 154 | storres | if neededPrecisionSa > currentPrecSa:
|
1016 | 154 | storres | pobyso_set_prec_so_so(currentPrecSo) |
1017 | 154 | storres | sollya_lib_clear_obj(currentPrecSo) |
1018 | 154 | storres | degreeIntervalSa = pobyso_range_to_interval_so_sa(degreeRangeSo) |
1019 | 154 | storres | sollya_lib_clear_obj(degreeRangeSo) |
1020 | 154 | storres | # When ok, both bounds match.
|
1021 | 154 | storres | # When the degree bound is too low, the upper bound is the degree
|
1022 | 154 | storres | # for which the error can be honored.
|
1023 | 154 | storres | # When it really goes wrong, the upper bound is infinity.
|
1024 | 154 | storres | if degreeIntervalSa.lower() == degreeIntervalSa.upper():
|
1025 | 154 | storres | return int(degreeIntervalSa.lower()) |
1026 | 154 | storres | else:
|
1027 | 154 | storres | if degreeIntervalSa.upper().is_infinity():
|
1028 | 154 | storres | return None |
1029 | 154 | storres | else:
|
1030 | 154 | storres | return int(degreeIntervalSa.upper()) |
1031 | 154 | storres | # End pobyso_guess_degree_so_sa
|
1032 | 153 | storres | |
1033 | 215 | storres | def pobyso_inf_so_so(intervalSo): |
1034 | 215 | storres | """
|
1035 | 215 | storres | Very thin wrapper around sollya_lib_inf().
|
1036 | 215 | storres | """
|
1037 | 215 | storres | return sollya_lib_inf(intervalSo)
|
1038 | 215 | storres | # End pobyso_inf_so_so.
|
1039 | 215 | storres | |
1040 | 53 | storres | def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None): |
1041 | 54 | storres | print "Do not use this function. User pobyso_supnorm_so_so instead." |
1042 | 209 | storres | return None |
1043 | 53 | storres | |
1044 | 84 | storres | def pobyso_interval_to_range_sa_so(intervalSa, precisionSa=None): |
1045 | 84 | storres | if precisionSa is None: |
1046 | 84 | storres | precisionSa = intervalSa.parent().precision() |
1047 | 84 | storres | intervalSo = pobyso_bounds_to_range_sa_so(intervalSa.lower(),\ |
1048 | 84 | storres | intervalSa.upper(),\ |
1049 | 84 | storres | precisionSa) |
1050 | 209 | storres | return intervalSo
|
1051 | 84 | storres | # End pobyso_interval_to_range_sa_so
|
1052 | 84 | storres | |
1053 | 155 | storres | def pobyso_is_error_so_sa(objSo): |
1054 | 155 | storres | """
|
1055 | 155 | storres | Thin wrapper around the sollya_lib_obj_is_error() function.
|
1056 | 155 | storres | """
|
1057 | 155 | storres | if sollya_lib_obj_is_error(objSo) != 0: |
1058 | 155 | storres | return True |
1059 | 155 | storres | else:
|
1060 | 155 | storres | return False |
1061 | 155 | storres | # End pobyso_is_error-so_sa
|
1062 | 155 | storres | |
1063 | 155 | storres | def pobyso_is_floating_point_number_sa_sa(numberSa): |
1064 | 155 | storres | """
|
1065 | 209 | storres | Check whether a Sage number is floating point.
|
1066 | 209 | storres | Exception stuff added because numbers other than
|
1067 | 209 | storres | floating-point ones do not have the is_real() attribute.
|
1068 | 155 | storres | """
|
1069 | 209 | storres | try:
|
1070 | 209 | storres | return numberSa.is_real()
|
1071 | 209 | storres | except AttributeError: |
1072 | 209 | storres | return False |
1073 | 209 | storres | # End pobyso_is_floating_piont_number_sa_sa
|
1074 | 155 | storres | |
1075 | 37 | storres | def pobyso_lib_init(): |
1076 | 37 | storres | sollya_lib_init(None)
|
1077 | 116 | storres | |
1078 | 116 | storres | def pobyso_lib_close(): |
1079 | 116 | storres | sollya_lib_close(None)
|
1080 | 37 | storres | |
1081 | 85 | storres | def pobyso_name_free_variable(freeVariableNameSa): |
1082 | 38 | storres | """ Legacy function. See pobyso_name_free_variable_sa_so. """
|
1083 | 85 | storres | pobyso_name_free_variable_sa_so(freeVariableNameSa) |
1084 | 38 | storres | |
1085 | 85 | storres | def pobyso_name_free_variable_sa_so(freeVariableNameSa): |
1086 | 83 | storres | """
|
1087 | 83 | storres | Set the free variable name in Sollya from a Sage string.
|
1088 | 83 | storres | """
|
1089 | 85 | storres | sollya_lib_name_free_variable(freeVariableNameSa) |
1090 | 37 | storres | |
1091 | 5 | storres | def pobyso_parse_string(string): |
1092 | 38 | storres | """ Legacy function. See pobyso_parse_string_sa_so. """
|
1093 | 209 | storres | return pobyso_parse_string_sa_so(string)
|
1094 | 38 | storres | |
1095 | 38 | storres | def pobyso_parse_string_sa_so(string): |
1096 | 83 | storres | """
|
1097 | 155 | storres | Get the Sollya expression computed from a Sage string or
|
1098 | 155 | storres | a Sollya error object if parsing failed.
|
1099 | 83 | storres | """
|
1100 | 209 | storres | return sollya_lib_parse_string(string)
|
1101 | 5 | storres | |
1102 | 200 | storres | def pobyso_precision_so_sa(ctExpSo): |
1103 | 209 | storres | """
|
1104 | 209 | storres | Computes the necessary precision to represent a number.
|
1105 | 209 | storres | If x is not zero, it can be uniquely written as x = m · 2e
|
1106 | 209 | storres | where m is an odd integer and e is an integer.
|
1107 | 209 | storres | precision(x) returns the number of bits necessary to write m
|
1108 | 209 | storres | in binary (i.e. ceil(log2(m))).
|
1109 | 209 | storres | """
|
1110 | 209 | storres | #TODO: take care of the special case: 0, @NaN@, @Inf@
|
1111 | 200 | storres | precisionSo = sollya_lib_precision(ctExpSo) |
1112 | 200 | storres | precisionSa = pobyso_constant_from_int_so_sa(precisionSo) |
1113 | 200 | storres | sollya_lib_clear_obj(precisionSo) |
1114 | 200 | storres | return precisionSa
|
1115 | 200 | storres | # End pobyso_precision_so_sa
|
1116 | 215 | storres | |
1117 | 217 | storres | def pobyso_polynomial_coefficients_progressive_round_so_so(polySo, |
1118 | 217 | storres | funcSo, |
1119 | 217 | storres | icSo, |
1120 | 217 | storres | intervalSo, |
1121 | 217 | storres | itpSo, |
1122 | 217 | storres | ftpSo, |
1123 | 217 | storres | maxPrecSo, |
1124 | 217 | storres | maxErrSo): |
1125 | 215 | storres | print "Input arguments:" |
1126 | 215 | storres | pobyso_autoprint(polySo) |
1127 | 215 | storres | pobyso_autoprint(funcSo) |
1128 | 215 | storres | pobyso_autoprint(icSo) |
1129 | 215 | storres | pobyso_autoprint(intervalSo) |
1130 | 215 | storres | pobyso_autoprint(itpSo) |
1131 | 215 | storres | pobyso_autoprint(ftpSo) |
1132 | 215 | storres | pobyso_autoprint(maxPrecSo) |
1133 | 215 | storres | pobyso_autoprint(maxErrSo) |
1134 | 215 | storres | print "________________" |
1135 | 200 | storres | |
1136 | 217 | storres | ## Higher order function see:
|
1137 | 217 | storres | # http://effbot.org/pyfaq/how-do-you-make-a-higher-order-function-in-python.htm
|
1138 | 217 | storres | def precision_decay_ratio_function(degreeSa): |
1139 | 217 | storres | def outer(x): |
1140 | 217 | storres | def inner(x): |
1141 | 217 | storres | we = 3/8 |
1142 | 217 | storres | wq = 2/8 |
1143 | 217 | storres | a = 2.2
|
1144 | 217 | storres | b = 2
|
1145 | 217 | storres | return we*(exp(x/a)-1) + wq*((b*x)**2) + (1-we-wq)*x |
1146 | 217 | storres | return inner(x)/inner(degreeSa)
|
1147 | 217 | storres | return outer
|
1148 | 217 | storres | |
1149 | 217 | storres | #
|
1150 | 217 | storres | degreeSa = pobyso_polynomial_degree_so_sa(polySo) |
1151 | 217 | storres | ratio = precision_decay_ratio_function(degreeSa) |
1152 | 217 | storres | itpSa = pobyso_constant_from_int_so_sa(itpSo) |
1153 | 217 | storres | ftpSa = pobyso_constant_from_int_so_sa(ftpSo) |
1154 | 217 | storres | maxPrecSa = pobyso_constant_from_int_so_sa(maxPrecSo) |
1155 | 217 | storres | maxErrSa = pobyso_get_constant_as_rn_so_sa(maxErrSo) |
1156 | 217 | storres | resPolySo = pobyso_constant_0_sa_so() |
1157 | 217 | storres | lastResPolySo = None
|
1158 | 217 | storres | while True: |
1159 | 217 | storres | pDeltaSa = ftpSa - itpSa |
1160 | 217 | storres | for indexSa in reversed(xrange(0,degreeSa+1)): |
1161 | 217 | storres | print "Index:", indexSa |
1162 | 217 | storres | indexSo = pobyso_constant_from_int_sa_so(indexSa) |
1163 | 217 | storres | #pobyso_autoprint(indexSo)
|
1164 | 217 | storres | #print ratio(indexSa)
|
1165 | 217 | storres | ctpSa = floor(ftpSa - (pDeltaSa * ratio(indexSa))) |
1166 | 217 | storres | ctpSo = pobyso_constant_from_int_sa_so(ctpSa) |
1167 | 217 | storres | print "Index:", indexSa, " - Target precision:", |
1168 | 217 | storres | pobyso_autoprint(ctpSo) |
1169 | 217 | storres | cmonSo = \ |
1170 | 217 | storres | sollya_lib_build_function_mul(sollya_lib_coeff(polySo, indexSo), |
1171 | 217 | storres | sollya_lib_build_function_pow( \ |
1172 | 217 | storres | sollya_lib_build_function_free_variable(), \ |
1173 | 217 | storres | indexSo)) |
1174 | 217 | storres | #pobyso_autoprint(cmonSo)
|
1175 | 217 | storres | cmonrSo = pobyso_round_coefficients_single_so_so(cmonSo, ctpSo) |
1176 | 217 | storres | sollya_lib_clear_obj(cmonSo) |
1177 | 217 | storres | #pobyso_autoprint(cmonrSo)
|
1178 | 217 | storres | resPolySo = sollya_lib_build_function_add(resPolySo, |
1179 | 217 | storres | cmonrSo) |
1180 | 217 | storres | pobyso_autoprint(resPolySo) |
1181 | 217 | storres | # End for index
|
1182 | 217 | storres | freeVarSo = sollya_lib_build_function_free_variable() |
1183 | 217 | storres | changeVarSo = sollya_lib_sub(freeVarSo, icSo) |
1184 | 217 | storres | resPolyCvSo = sollya_lib_evaluate(resPolySo, changeVarSo) |
1185 | 217 | storres | errFuncSo = sollya_lib_build_function_sub(funcSo, resPolyCvSo) |
1186 | 217 | storres | infNormSo = sollya_lib_dirtyinform(errFuncSo, intervalSo) |
1187 | 217 | storres | print "Infnorm (Sollya):", ; pobyso_autoprint(infNormSo) |
1188 | 217 | storres | cerrSa = pobyso_get_constant_as_rn_so_sa(infNormSo) |
1189 | 217 | storres | sollya_lib_clear_obj(resPolyCvSo) |
1190 | 217 | storres | sollya_lib_clear_obj(infNormSo) |
1191 | 217 | storres | print "Infnorm (Sage):", cerrSa |
1192 | 217 | storres | if (cerrSa > maxErrSa):
|
1193 | 217 | storres | print "Error is too large." |
1194 | 217 | storres | if not lastResPolySo is None: |
1195 | 217 | storres | print "Enlarging prec." |
1196 | 217 | storres | ntpSa = floor(ftpSa + ftpSa/50)
|
1197 | 217 | storres | ## Can't enlarge (numerical)
|
1198 | 217 | storres | if ntpSa == ftpSa:
|
1199 | 217 | storres | sollya_lib_clear_obj(lastResPolySo) |
1200 | 217 | storres | sollya_lib_clear_obj(resPolySo) |
1201 | 217 | storres | return None |
1202 | 217 | storres | ## Can't enlarge (not enough precision left)
|
1203 | 217 | storres | if ntpSa > maxPrecSa:
|
1204 | 217 | storres | sollya_lib_clear_obj(lastResPolySo) |
1205 | 217 | storres | sollya_lib_clear_obj(resPolySo) |
1206 | 217 | storres | return None |
1207 | 217 | storres | ftpSa = ntpSa |
1208 | 217 | storres | lastResPolySo = resPolySo |
1209 | 217 | storres | continue
|
1210 | 217 | storres | ## One enlargement took place.
|
1211 | 217 | storres | else:
|
1212 | 217 | storres | sollya_lib_clear_obj(resPolySo) |
1213 | 217 | storres | return lastResPolySo
|
1214 | 217 | storres | # cerrSa <= maxErrSa: scrap more bits.
|
1215 | 217 | storres | else:
|
1216 | 217 | storres | print "Shrinking prec." |
1217 | 217 | storres | ntpSa = floor(ftpSa - ftpSa/50)
|
1218 | 217 | storres | ## Can't shrink (numerical)
|
1219 | 217 | storres | if ntpSa == ftpSa:
|
1220 | 217 | storres | if not lastResPolySo is None: |
1221 | 217 | storres | sollya_lib_clear_obj(lastResPolySo) |
1222 | 217 | storres | return resPolySo
|
1223 | 217 | storres | ## Can't enlarge (not enough precision left)
|
1224 | 217 | storres | if ntpSa <= itpSa:
|
1225 | 217 | storres | if not lastResPolySo is None: |
1226 | 217 | storres | sollya_lib_clear_obj(lastResPolySo) |
1227 | 217 | storres | return resPolySo
|
1228 | 217 | storres | ftpSa = ntpSa |
1229 | 217 | storres | continue
|
1230 | 217 | storres | # End wile True
|
1231 | 215 | storres | return None |
1232 | 217 | storres | # End pobyso_polynomial_coefficients_progressive_truncate_so_so.
|
1233 | 217 | storres | |
1234 | 217 | storres | def pobyso_polynomial_degree_so_sa(polySo): |
1235 | 217 | storres | """
|
1236 | 217 | storres | Return the degree of a Sollya polynomial as a Sage int.
|
1237 | 217 | storres | """
|
1238 | 217 | storres | degreeSo = sollya_lib_degree(polySo) |
1239 | 217 | storres | return pobyso_constant_from_int_so_sa(degreeSo)
|
1240 | 217 | storres | # End pobyso_polynomial_degree_so_sa
|
1241 | 217 | storres | |
1242 | 217 | storres | def pobyso_polynomial_degree_so_so(polySo): |
1243 | 217 | storres | """
|
1244 | 217 | storres | Thin wrapper around lib_sollya_degree().
|
1245 | 217 | storres | """
|
1246 | 217 | storres | return sollya_lib_degree(polySo)
|
1247 | 217 | storres | # End pobyso_polynomial_degree_so_so
|
1248 | 217 | storres | |
1249 | 5 | storres | def pobyso_range(rnLowerBound, rnUpperBound): |
1250 | 38 | storres | """ Legacy function. See pobyso_range_sa_so. """
|
1251 | 209 | storres | return pobyso_range_sa_so(rnLowerBound, rnUpperBound)
|
1252 | 38 | storres | |
1253 | 5 | storres | |
1254 | 85 | storres | def pobyso_range_to_interval_so_sa(rangeSo, realIntervalFieldSa = None): |
1255 | 83 | storres | """
|
1256 | 83 | storres | Get a Sage interval from a Sollya range.
|
1257 | 83 | storres | If no realIntervalField is given as a parameter, the Sage interval
|
1258 | 83 | storres | precision is that of the Sollya range.
|
1259 | 85 | storres | Otherwise, the precision is that of the realIntervalField. In this case
|
1260 | 85 | storres | rounding may happen.
|
1261 | 83 | storres | """
|
1262 | 85 | storres | if realIntervalFieldSa is None: |
1263 | 56 | storres | precSa = pobyso_get_prec_of_range_so_sa(rangeSo) |
1264 | 85 | storres | realIntervalFieldSa = RealIntervalField(precSa) |
1265 | 56 | storres | intervalSa = \ |
1266 | 85 | storres | pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalFieldSa) |
1267 | 209 | storres | return intervalSa
|
1268 | 209 | storres | # End pobyso_range_to_interval_so_sa
|
1269 | 56 | storres | |
1270 | 209 | storres | def pobyso_rat_poly_sa_so(polySa, precSa = None): |
1271 | 209 | storres | """
|
1272 | 209 | storres | Create a Sollya polynomial from a Sage rational polynomial.
|
1273 | 209 | storres | """
|
1274 | 209 | storres | ## TODO: filter arguments.
|
1275 | 209 | storres | ## Precision. If no precision is given, use the current precision
|
1276 | 209 | storres | # of Sollya.
|
1277 | 209 | storres | if precSa is None: |
1278 | 209 | storres | precSa = pobyso_get_prec_so_sa() |
1279 | 209 | storres | #print "Precision:", precSa
|
1280 | 209 | storres | RRR = RealField(precSa) |
1281 | 209 | storres | ## Create a Sage polynomial in the "right" precision.
|
1282 | 209 | storres | P_RRR = RRR[polySa.variables()[0]]
|
1283 | 209 | storres | polyFloatSa = P_RRR(polySa) |
1284 | 213 | storres | ## Make sure no precision is provided: pobyso_float_poly_sa_so will
|
1285 | 213 | storres | # recover it all by itself and not make an extra conversion.
|
1286 | 209 | storres | return pobyso_float_poly_sa_so(polyFloatSa)
|
1287 | 209 | storres | |
1288 | 209 | storres | # End pobyso_rat_poly_sa_so
|
1289 | 209 | storres | |
1290 | 52 | storres | def pobyso_remez_canonical_sa_sa(func, \ |
1291 | 52 | storres | degree, \ |
1292 | 52 | storres | lowerBound, \ |
1293 | 52 | storres | upperBound, \ |
1294 | 52 | storres | weight = None, \
|
1295 | 52 | storres | quality = None):
|
1296 | 52 | storres | """
|
1297 | 52 | storres | All arguments are Sage/Python.
|
1298 | 52 | storres | The functions (func and weight) must be passed as expressions or strings.
|
1299 | 52 | storres | Otherwise the function fails.
|
1300 | 83 | storres | The return value is a Sage polynomial.
|
1301 | 52 | storres | """
|
1302 | 83 | storres | var('zorglub') # Dummy variable name for type check only. Type of |
1303 | 83 | storres | # zorglub is "symbolic expression".
|
1304 | 52 | storres | polySo = pobyso_remez_canonical_sa_so(func, \ |
1305 | 52 | storres | degree, \ |
1306 | 52 | storres | lowerBound, \ |
1307 | 52 | storres | upperBound, \ |
1308 | 85 | storres | weight, \ |
1309 | 85 | storres | quality) |
1310 | 83 | storres | # String test
|
1311 | 52 | storres | if parent(func) == parent("string"): |
1312 | 52 | storres | functionSa = eval(func)
|
1313 | 52 | storres | # Expression test.
|
1314 | 52 | storres | elif type(func) == type(zorglub): |
1315 | 52 | storres | functionSa = func |
1316 | 83 | storres | else:
|
1317 | 83 | storres | return None |
1318 | 83 | storres | #
|
1319 | 52 | storres | maxPrecision = 0
|
1320 | 52 | storres | if polySo is None: |
1321 | 52 | storres | return(None) |
1322 | 52 | storres | maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
1323 | 85 | storres | RRRRSa = RealField(maxPrecision) |
1324 | 85 | storres | polynomialRingSa = RRRRSa[functionSa.variables()[0]]
|
1325 | 85 | storres | expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRRSa) |
1326 | 85 | storres | polySa = polynomial(expSa, polynomialRingSa) |
1327 | 83 | storres | sollya_lib_clear_obj(polySo) |
1328 | 52 | storres | return(polySa)
|
1329 | 85 | storres | # End pobyso_remez_canonical_sa_sa
|
1330 | 52 | storres | |
1331 | 38 | storres | def pobyso_remez_canonical(func, \ |
1332 | 5 | storres | degree, \ |
1333 | 5 | storres | lowerBound, \ |
1334 | 5 | storres | upperBound, \ |
1335 | 38 | storres | weight = "1", \
|
1336 | 5 | storres | quality = None):
|
1337 | 38 | storres | """ Legacy function. See pobyso_remez_canonical_sa_so. """
|
1338 | 51 | storres | return(pobyso_remez_canonical_sa_so(func, \
|
1339 | 51 | storres | degree, \ |
1340 | 51 | storres | lowerBound, \ |
1341 | 51 | storres | upperBound, \ |
1342 | 51 | storres | weight, \ |
1343 | 51 | storres | quality)) |
1344 | 200 | storres | # End pobyso_remez_canonical.
|
1345 | 200 | storres | |
1346 | 38 | storres | def pobyso_remez_canonical_sa_so(func, \ |
1347 | 38 | storres | degree, \ |
1348 | 38 | storres | lowerBound, \ |
1349 | 38 | storres | upperBound, \ |
1350 | 52 | storres | weight = None, \
|
1351 | 38 | storres | quality = None):
|
1352 | 38 | storres | """
|
1353 | 38 | storres | All arguments are Sage/Python.
|
1354 | 51 | storres | The functions (func and weight) must be passed as expressions or strings.
|
1355 | 51 | storres | Otherwise the function fails.
|
1356 | 38 | storres | The return value is a pointer to a Sollya function.
|
1357 | 38 | storres | """
|
1358 | 83 | storres | var('zorglub') # Dummy variable name for type check only. Type of |
1359 | 83 | storres | # zorglub is "symbolic expression".
|
1360 | 85 | storres | currentVariableNameSa = None
|
1361 | 52 | storres | # The func argument can be of different types (string,
|
1362 | 52 | storres | # symbolic expression...)
|
1363 | 38 | storres | if parent(func) == parent("string"): |
1364 | 85 | storres | localFuncSa = eval(func)
|
1365 | 85 | storres | if len(localFuncSa.variables()) > 0: |
1366 | 85 | storres | currentVariableNameSa = localFuncSa.variables()[0]
|
1367 | 85 | storres | sollya_lib_name_free_variable(str(currentVariableNameSa))
|
1368 | 159 | storres | functionSo = \ |
1369 | 159 | storres | sollya_lib_parse_string(localFuncSa._assume_str().replace('_SAGE_VAR_', '')) |
1370 | 51 | storres | # Expression test.
|
1371 | 52 | storres | elif type(func) == type(zorglub): |
1372 | 52 | storres | # Until we are able to translate Sage expressions into Sollya
|
1373 | 52 | storres | # expressions : parse the string version.
|
1374 | 85 | storres | if len(func.variables()) > 0: |
1375 | 85 | storres | currentVariableNameSa = func.variables()[0]
|
1376 | 85 | storres | sollya_lib_name_free_variable(str(currentVariableNameSa))
|
1377 | 159 | storres | functionSo = \ |
1378 | 159 | storres | sollya_lib_parse_string(func._assume_str().replace('_SAGE_VAR_', '')) |
1379 | 38 | storres | else:
|
1380 | 38 | storres | return(None) |
1381 | 85 | storres | if weight is None: # No weight given -> 1. |
1382 | 52 | storres | weightSo = pobyso_constant_1_sa_so() |
1383 | 85 | storres | elif parent(weight) == parent("string"): # Weight given as string: parse it. |
1384 | 51 | storres | weightSo = sollya_lib_parse_string(func) |
1385 | 85 | storres | elif type(weight) == type(zorglub): # Weight given as symbolice expression. |
1386 | 159 | storres | functionSo = \ |
1387 | 159 | storres | sollya_lib_parse_string_sa_so(weight._assume_str().replace('_SAGE_VAR_', '')) |
1388 | 51 | storres | else:
|
1389 | 51 | storres | return(None) |
1390 | 5 | storres | degreeSo = pobyso_constant_from_int(degree) |
1391 | 85 | storres | rangeSo = pobyso_bounds_to_range_sa_so(lowerBound, upperBound) |
1392 | 38 | storres | if not quality is None: |
1393 | 38 | storres | qualitySo= pobyso_constant_sa_so(quality) |
1394 | 52 | storres | else:
|
1395 | 52 | storres | qualitySo = None
|
1396 | 83 | storres | |
1397 | 83 | storres | remezPolySo = sollya_lib_remez(functionSo, \ |
1398 | 83 | storres | degreeSo, \ |
1399 | 83 | storres | rangeSo, \ |
1400 | 83 | storres | weightSo, \ |
1401 | 83 | storres | qualitySo, \ |
1402 | 83 | storres | None)
|
1403 | 83 | storres | sollya_lib_clear_obj(functionSo) |
1404 | 83 | storres | sollya_lib_clear_obj(degreeSo) |
1405 | 83 | storres | sollya_lib_clear_obj(rangeSo) |
1406 | 83 | storres | sollya_lib_clear_obj(weightSo) |
1407 | 83 | storres | if not qualitySo is None: |
1408 | 85 | storres | sollya_lib_clear_obj(qualitySo) |
1409 | 83 | storres | return(remezPolySo)
|
1410 | 83 | storres | # End pobyso_remez_canonical_sa_so
|
1411 | 83 | storres | |
1412 | 38 | storres | def pobyso_remez_canonical_so_so(funcSo, \ |
1413 | 38 | storres | degreeSo, \ |
1414 | 38 | storres | rangeSo, \ |
1415 | 52 | storres | weightSo = pobyso_constant_1_sa_so(),\ |
1416 | 38 | storres | qualitySo = None):
|
1417 | 38 | storres | """
|
1418 | 38 | storres | All arguments are pointers to Sollya objects.
|
1419 | 38 | storres | The return value is a pointer to a Sollya function.
|
1420 | 38 | storres | """
|
1421 | 38 | storres | if not sollya_lib_obj_is_function(funcSo): |
1422 | 38 | storres | return(None) |
1423 | 38 | storres | return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None)) |
1424 | 200 | storres | # End pobyso_remez_canonical_so_so.
|
1425 | 200 | storres | |
1426 | 215 | storres | def pobyso_round_coefficients_single_so_so(polySo, precSo): |
1427 | 215 | storres | """
|
1428 | 215 | storres | Create a rounded coefficients polynomial from polynomial argument to
|
1429 | 215 | storres | the number of bits in size argument.
|
1430 | 215 | storres | All coefficients are set to the same precision.
|
1431 | 215 | storres | """
|
1432 | 215 | storres | ## TODO: check arguments.
|
1433 | 215 | storres | endEllipListSo = pobyso_build_end_elliptic_list_so_so(precSo) |
1434 | 215 | storres | polySo = sollya_lib_roundcoefficients(polySo, endEllipListSo, None)
|
1435 | 217 | storres | sollya_lib_clear_obj(endEllipListSo) |
1436 | 215 | storres | #sollya_lib_clear_obj(endEllipListSo)
|
1437 | 215 | storres | return polySo
|
1438 | 215 | storres | |
1439 | 215 | storres | # End pobyso_round_coefficients_single_so_so
|
1440 | 215 | storres | |
1441 | 5 | storres | def pobyso_set_canonical_off(): |
1442 | 5 | storres | sollya_lib_set_canonical(sollya_lib_off()) |
1443 | 5 | storres | |
1444 | 5 | storres | def pobyso_set_canonical_on(): |
1445 | 5 | storres | sollya_lib_set_canonical(sollya_lib_on()) |
1446 | 5 | storres | |
1447 | 5 | storres | def pobyso_set_prec(p): |
1448 | 38 | storres | """ Legacy function. See pobyso_set_prec_sa_so. """
|
1449 | 85 | storres | pobyso_set_prec_sa_so(p) |
1450 | 38 | storres | |
1451 | 38 | storres | def pobyso_set_prec_sa_so(p): |
1452 | 5 | storres | a = c_int(p) |
1453 | 5 | storres | precSo = c_void_p(sollya_lib_constant_from_int(a)) |
1454 | 85 | storres | sollya_lib_set_prec(precSo, None)
|
1455 | 215 | storres | # End pobyso_set_prec_sa_so.
|
1456 | 5 | storres | |
1457 | 85 | storres | def pobyso_set_prec_so_so(newPrecSo): |
1458 | 85 | storres | sollya_lib_set_prec(newPrecSo, None)
|
1459 | 215 | storres | # End pobyso_set_prec_so_so.
|
1460 | 54 | storres | |
1461 | 215 | storres | def pobyso_inf_so_so(intervalSo): |
1462 | 215 | storres | """
|
1463 | 215 | storres | Very thin wrapper around sollya_lib_inf().
|
1464 | 215 | storres | """
|
1465 | 215 | storres | return sollya_lib_inf(intervalSo)
|
1466 | 215 | storres | # End pobyso_inf_so_so.
|
1467 | 215 | storres | |
1468 | 85 | storres | def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo = None,\ |
1469 | 85 | storres | accuracySo = None):
|
1470 | 58 | storres | """
|
1471 | 85 | storres | Computes the supnorm of the approximation error between the given
|
1472 | 85 | storres | polynomial and function.
|
1473 | 85 | storres | errorTypeSo defaults to "absolute".
|
1474 | 85 | storres | accuracySo defaults to 2^(-40).
|
1475 | 85 | storres | """
|
1476 | 85 | storres | if errorTypeSo is None: |
1477 | 85 | storres | errorTypeSo = sollya_lib_absolute(None)
|
1478 | 85 | storres | errorTypeIsNone = True
|
1479 | 85 | storres | else:
|
1480 | 85 | storres | errorTypeIsNone = False
|
1481 | 85 | storres | #
|
1482 | 85 | storres | if accuracySo is None: |
1483 | 85 | storres | # Notice the **!
|
1484 | 85 | storres | accuracySo = pobyso_constant_sa_so(RR(2**(-40))) |
1485 | 85 | storres | accuracyIsNone = True
|
1486 | 85 | storres | else:
|
1487 | 85 | storres | accuracyIsNone = False
|
1488 | 85 | storres | pobyso_autoprint(accuracySo) |
1489 | 85 | storres | resultSo = \ |
1490 | 85 | storres | sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \ |
1491 | 85 | storres | accuracySo) |
1492 | 85 | storres | if errorTypeIsNone:
|
1493 | 85 | storres | sollya_lib_clear_obj(errorTypeSo) |
1494 | 85 | storres | if accuracyIsNone:
|
1495 | 85 | storres | sollya_lib_clear_obj(accuracySo) |
1496 | 85 | storres | return resultSo
|
1497 | 85 | storres | # End pobyso_supnorm_so_so
|
1498 | 85 | storres | |
1499 | 162 | storres | def pobyso_taylor_expansion_no_change_var_so_so(functionSo, |
1500 | 162 | storres | degreeSo, |
1501 | 162 | storres | rangeSo, |
1502 | 162 | storres | errorTypeSo=None,
|
1503 | 162 | storres | sollyaPrecSo=None):
|
1504 | 85 | storres | """
|
1505 | 162 | storres | Compute the Taylor expansion without the variable change
|
1506 | 162 | storres | x -> x-intervalCenter.
|
1507 | 58 | storres | """
|
1508 | 58 | storres | # No global change of the working precision.
|
1509 | 58 | storres | if not sollyaPrecSo is None: |
1510 | 58 | storres | initialPrecSo = sollya_lib_get_prec(None)
|
1511 | 58 | storres | sollya_lib_set_prec(sollyaPrecSo) |
1512 | 85 | storres | # Error type stuff: default to absolute.
|
1513 | 85 | storres | if errorTypeSo is None: |
1514 | 85 | storres | errorTypeIsNone = True
|
1515 | 85 | storres | errorTypeSo = sollya_lib_absolute(None)
|
1516 | 85 | storres | else:
|
1517 | 85 | storres | errorTypeIsNone = False
|
1518 | 162 | storres | intervalCenterSo = sollya_lib_mid(rangeSo, None)
|
1519 | 162 | storres | taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, |
1520 | 162 | storres | intervalCenterSo, |
1521 | 58 | storres | rangeSo, errorTypeSo, None)
|
1522 | 117 | storres | # taylorFormListSaSo is a Python list of Sollya objects references that
|
1523 | 117 | storres | # are copies of the elements of taylorFormSo.
|
1524 | 117 | storres | # pobyso_get_list_elements_so_so clears taylorFormSo.
|
1525 | 162 | storres | (taylorFormListSaSo, numElementsSa, isEndEllipticSa) = \ |
1526 | 58 | storres | pobyso_get_list_elements_so_so(taylorFormSo) |
1527 | 162 | storres | polySo = sollya_lib_copy_obj(taylorFormListSaSo[0])
|
1528 | 162 | storres | #print "Num elements:", numElementsSa
|
1529 | 162 | storres | sollya_lib_clear_obj(taylorFormSo) |
1530 | 162 | storres | #polySo = taylorFormListSaSo[0]
|
1531 | 162 | storres | #errorRangeSo = sollya_lib_copy_obj(taylorFormListSaSo[2])
|
1532 | 162 | storres | errorRangeSo = taylorFormListSaSo[2]
|
1533 | 181 | storres | # No copy_obj needed here: a new objects are created.
|
1534 | 181 | storres | maxErrorSo = sollya_lib_sup(errorRangeSo) |
1535 | 181 | storres | minErrorSo = sollya_lib_inf(errorRangeSo) |
1536 | 181 | storres | absMaxErrorSo = sollya_lib_abs(maxErrorSo) |
1537 | 181 | storres | absMinErrorSo = sollya_lib_abs(minErrorSo) |
1538 | 181 | storres | sollya_lib_clear_obj(maxErrorSo) |
1539 | 181 | storres | sollya_lib_clear_obj(minErrorSo) |
1540 | 181 | storres | absMaxErrorSa = pobyso_get_constant_as_rn_so_sa(absMaxErrorSo) |
1541 | 181 | storres | absMinErrorSa = pobyso_get_constant_as_rn_so_sa(absMinErrorSo) |
1542 | 58 | storres | # If changed, reset the Sollya working precision.
|
1543 | 58 | storres | if not sollyaPrecSo is None: |
1544 | 58 | storres | sollya_lib_set_prec(initialPrecSo) |
1545 | 83 | storres | sollya_lib_clear_obj(initialPrecSo) |
1546 | 85 | storres | if errorTypeIsNone:
|
1547 | 85 | storres | sollya_lib_clear_obj(errorTypeSo) |
1548 | 162 | storres | pobyso_clear_taylorform_sa_so(taylorFormListSaSo) |
1549 | 181 | storres | if absMaxErrorSa > absMinErrorSa:
|
1550 | 181 | storres | sollya_lib_clear_obj(absMinErrorSo) |
1551 | 181 | storres | return((polySo, intervalCenterSo, absMaxErrorSo))
|
1552 | 181 | storres | else:
|
1553 | 181 | storres | sollya_lib_clear_obj(absMaxErrorSo) |
1554 | 181 | storres | return((polySo, intervalCenterSo, absMinErrorSo))
|
1555 | 162 | storres | # end pobyso_taylor_expansion_no_change_var_so_so
|
1556 | 58 | storres | |
1557 | 162 | storres | def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, \ |
1558 | 162 | storres | rangeSo, \ |
1559 | 162 | storres | errorTypeSo=None, \
|
1560 | 162 | storres | sollyaPrecSo=None):
|
1561 | 58 | storres | """
|
1562 | 162 | storres | Compute the Taylor expansion with the variable change
|
1563 | 162 | storres | x -> (x-intervalCenter) included.
|
1564 | 58 | storres | """
|
1565 | 56 | storres | # No global change of the working precision.
|
1566 | 56 | storres | if not sollyaPrecSo is None: |
1567 | 56 | storres | initialPrecSo = sollya_lib_get_prec(None)
|
1568 | 56 | storres | sollya_lib_set_prec(sollyaPrecSo) |
1569 | 162 | storres | #
|
1570 | 85 | storres | # Error type stuff: default to absolute.
|
1571 | 85 | storres | if errorTypeSo is None: |
1572 | 85 | storres | errorTypeIsNone = True
|
1573 | 85 | storres | errorTypeSo = sollya_lib_absolute(None)
|
1574 | 85 | storres | else:
|
1575 | 85 | storres | errorTypeIsNone = False
|
1576 | 162 | storres | intervalCenterSo = sollya_lib_mid(rangeSo) |
1577 | 162 | storres | taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \ |
1578 | 162 | storres | intervalCenterSo, \ |
1579 | 56 | storres | rangeSo, errorTypeSo, None)
|
1580 | 116 | storres | # taylorFormListSaSo is a Python list of Sollya objects references that
|
1581 | 116 | storres | # are copies of the elements of taylorFormSo.
|
1582 | 116 | storres | # pobyso_get_list_elements_so_so clears taylorFormSo.
|
1583 | 162 | storres | (taylorFormListSo, numElements, isEndElliptic) = \ |
1584 | 56 | storres | pobyso_get_list_elements_so_so(taylorFormSo) |
1585 | 162 | storres | polySo = taylorFormListSo[0]
|
1586 | 162 | storres | errorRangeSo = taylorFormListSo[2]
|
1587 | 181 | storres | maxErrorSo = sollya_lib_sup(errorRangeSo) |
1588 | 181 | storres | minErrorSo = sollya_lib_inf(errorRangeSo) |
1589 | 181 | storres | absMaxErrorSo = sollya_lib_abs(maxErrorSo) |
1590 | 181 | storres | absMinErrorSo = sollya_lib_abs(minErrorSo) |
1591 | 181 | storres | sollya_lib_clear_obj(maxErrorSo) |
1592 | 181 | storres | sollya_lib_clear_obj(minErrorSo) |
1593 | 181 | storres | absMaxErrorSa = pobyso_get_constant_as_rn_so_sa(absMaxErrorSo) |
1594 | 181 | storres | absMinErrorSa = pobyso_get_constant_as_rn_so_sa(absMinErrorSo) |
1595 | 162 | storres | changeVarExpSo = sollya_lib_build_function_sub(\ |
1596 | 162 | storres | sollya_lib_build_function_free_variable(),\ |
1597 | 162 | storres | sollya_lib_copy_obj(intervalCenterSo)) |
1598 | 181 | storres | polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo) |
1599 | 181 | storres | sollya_lib_clear_obj(polySo) |
1600 | 162 | storres | sollya_lib_clear_obj(changeVarExpSo) |
1601 | 56 | storres | # If changed, reset the Sollya working precision.
|
1602 | 56 | storres | if not sollyaPrecSo is None: |
1603 | 56 | storres | sollya_lib_set_prec(initialPrecSo) |
1604 | 63 | storres | sollya_lib_clear_obj(initialPrecSo) |
1605 | 85 | storres | if errorTypeIsNone:
|
1606 | 85 | storres | sollya_lib_clear_obj(errorTypeSo) |
1607 | 162 | storres | sollya_lib_clear_obj(taylorFormSo) |
1608 | 162 | storres | # Do not clear maxErrorSo.
|
1609 | 181 | storres | if absMaxErrorSa > absMinErrorSa:
|
1610 | 181 | storres | sollya_lib_clear_obj(absMinErrorSo) |
1611 | 181 | storres | return((polyVarChangedSo, intervalCenterSo, absMaxErrorSo))
|
1612 | 181 | storres | else:
|
1613 | 181 | storres | sollya_lib_clear_obj(absMaxErrorSo) |
1614 | 181 | storres | return((polyVarChangedSo, intervalCenterSo, absMinErrorSo))
|
1615 | 162 | storres | # end pobyso_taylor_expansion_with_change_var_so_so
|
1616 | 56 | storres | |
1617 | 5 | storres | def pobyso_taylor(function, degree, point): |
1618 | 38 | storres | """ Legacy function. See pobysoTaylor_so_so. """
|
1619 | 38 | storres | return(pobyso_taylor_so_so(function, degree, point))
|
1620 | 38 | storres | |
1621 | 56 | storres | def pobyso_taylor_so_so(functionSo, degreeSo, pointSo): |
1622 | 56 | storres | return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
|
1623 | 5 | storres | |
1624 | 85 | storres | def pobyso_taylorform(function, degree, point = None, |
1625 | 85 | storres | interval = None, errorType=None): |
1626 | 85 | storres | """ Legacy function. See pobyso_taylorform_sa_sa;"""
|
1627 | 38 | storres | |
1628 | 38 | storres | def pobyso_taylorform_sa_sa(functionSa, \ |
1629 | 84 | storres | degreeSa, \ |
1630 | 84 | storres | pointSa, \ |
1631 | 84 | storres | intervalSa=None, \
|
1632 | 84 | storres | errorTypeSa=None, \
|
1633 | 84 | storres | precisionSa=None):
|
1634 | 37 | storres | """
|
1635 | 85 | storres | Compute the Taylor form of 'degreeSa' for 'functionSa' at 'pointSa'
|
1636 | 85 | storres | for 'intervalSa' with 'errorTypeSa' (a string) using 'precisionSa'.
|
1637 | 37 | storres | point: must be a Real or a Real interval.
|
1638 | 37 | storres | return the Taylor form as an array
|
1639 | 83 | storres | TODO: take care of the interval and of the point when it is an interval;
|
1640 | 38 | storres | when errorType is not None;
|
1641 | 83 | storres | take care of the other elements of the Taylor form (coefficients
|
1642 | 83 | storres | errors and delta.
|
1643 | 37 | storres | """
|
1644 | 37 | storres | # Absolute as the default error.
|
1645 | 84 | storres | if errorTypeSa is None: |
1646 | 37 | storres | errorTypeSo = sollya_lib_absolute() |
1647 | 84 | storres | elif errorTypeSa == "relative": |
1648 | 84 | storres | errorTypeSo = sollya_lib_relative() |
1649 | 84 | storres | elif errortypeSa == "absolute": |
1650 | 84 | storres | errorTypeSo = sollya_lib_absolute() |
1651 | 37 | storres | else:
|
1652 | 84 | storres | # No clean up needed.
|
1653 | 84 | storres | return None |
1654 | 84 | storres | # Global precision stuff
|
1655 | 84 | storres | precisionChangedSa = False
|
1656 | 84 | storres | currentSollyaPrecSo = pobyso_get_prec_so() |
1657 | 84 | storres | currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo) |
1658 | 84 | storres | if not precisionSa is None: |
1659 | 84 | storres | if precisionSa > currentSollyaPrecSa:
|
1660 | 84 | storres | pobyso_set_prec_sa_so(precisionSa) |
1661 | 84 | storres | precisionChangedSa = True
|
1662 | 84 | storres | |
1663 | 85 | storres | if len(functionSa.variables()) > 0: |
1664 | 85 | storres | varSa = functionSa.variables()[0]
|
1665 | 85 | storres | pobyso_name_free_variable_sa_so(str(varSa))
|
1666 | 84 | storres | # In any case (point or interval) the parent of pointSa has a precision
|
1667 | 84 | storres | # method.
|
1668 | 84 | storres | pointPrecSa = pointSa.parent().precision() |
1669 | 84 | storres | if precisionSa > pointPrecSa:
|
1670 | 84 | storres | pointPrecSa = precisionSa |
1671 | 84 | storres | # In any case (point or interval) pointSa has a base_ring() method.
|
1672 | 84 | storres | pointBaseRingString = str(pointSa.base_ring())
|
1673 | 84 | storres | if re.search('Interval', pointBaseRingString) is None: # Point |
1674 | 84 | storres | pointSo = pobyso_constant_sa_so(pointSa, pointPrecSa) |
1675 | 84 | storres | else: # Interval. |
1676 | 84 | storres | pointSo = pobyso_interval_to_range_sa_so(pointSa, pointPrecSa) |
1677 | 37 | storres | # Sollyafy the function.
|
1678 | 159 | storres | functionSo = pobyso_parse_string_sa_so(functionSa._assume_str().replace('_SAGE_VAR_', '')) |
1679 | 37 | storres | if sollya_lib_obj_is_error(functionSo):
|
1680 | 37 | storres | print "pobyso_tailorform: function string can't be parsed!" |
1681 | 37 | storres | return None |
1682 | 37 | storres | # Sollyafy the degree
|
1683 | 84 | storres | degreeSo = sollya_lib_constant_from_int(int(degreeSa))
|
1684 | 37 | storres | # Sollyafy the point
|
1685 | 37 | storres | # Call Sollya
|
1686 | 83 | storres | taylorFormSo = \ |
1687 | 83 | storres | sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\ |
1688 | 37 | storres | None)
|
1689 | 85 | storres | sollya_lib_clear_obj(functionSo) |
1690 | 85 | storres | sollya_lib_clear_obj(degreeSo) |
1691 | 85 | storres | sollya_lib_clear_obj(pointSo) |
1692 | 85 | storres | sollya_lib_clear_obj(errorTypeSo) |
1693 | 38 | storres | (tfsAsList, numElements, isEndElliptic) = \ |
1694 | 38 | storres | pobyso_get_list_elements_so_so(taylorFormSo) |
1695 | 37 | storres | polySo = tfsAsList[0]
|
1696 | 38 | storres | maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
1697 | 37 | storres | polyRealField = RealField(maxPrecision) |
1698 | 38 | storres | expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField) |
1699 | 84 | storres | if precisionChangedSa:
|
1700 | 84 | storres | sollya_lib_set_prec(currentSollyaPrecSo) |
1701 | 84 | storres | sollya_lib_clear_obj(currentSollyaPrecSo) |
1702 | 37 | storres | polynomialRing = polyRealField[str(varSa)]
|
1703 | 37 | storres | polySa = polynomial(expSa, polynomialRing) |
1704 | 37 | storres | taylorFormSa = [polySa] |
1705 | 85 | storres | # Final clean-up.
|
1706 | 85 | storres | sollya_lib_clear_obj(taylorFormSo) |
1707 | 51 | storres | return(taylorFormSa)
|
1708 | 51 | storres | # End pobyso_taylor_form_sa_sa
|
1709 | 54 | storres | |
1710 | 54 | storres | def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \ |
1711 | 54 | storres | errorTypeSo=None):
|
1712 | 54 | storres | createdErrorType = False
|
1713 | 51 | storres | if errorTypeSo is None: |
1714 | 51 | storres | errorTypeSo = sollya_lib_absolute() |
1715 | 54 | storres | createdErrorType = True
|
1716 | 51 | storres | else:
|
1717 | 51 | storres | #TODO: deal with the other case.
|
1718 | 51 | storres | pass
|
1719 | 51 | storres | if intervalSo is None: |
1720 | 54 | storres | resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \ |
1721 | 54 | storres | errorTypeSo, None)
|
1722 | 51 | storres | else:
|
1723 | 54 | storres | resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \ |
1724 | 54 | storres | intervalSo, errorTypeSo, None)
|
1725 | 54 | storres | if createdErrorType:
|
1726 | 54 | storres | sollya_lib_clear_obj(errorTypeSo) |
1727 | 215 | storres | return resultSo
|
1728 | 51 | storres | |
1729 | 37 | storres | |
1730 | 37 | storres | def pobyso_univar_polynomial_print_reverse(polySa): |
1731 | 51 | storres | """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
|
1732 | 51 | storres | return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
|
1733 | 38 | storres | |
1734 | 51 | storres | def pobyso_univar_polynomial_print_reverse_sa_sa(polySa): |
1735 | 37 | storres | """
|
1736 | 37 | storres | Return the string representation of a univariate polynomial with
|
1737 | 38 | storres | monomials ordered in the x^0..x^n order of the monomials.
|
1738 | 37 | storres | Remember: Sage
|
1739 | 37 | storres | """
|
1740 | 37 | storres | polynomialRing = polySa.base_ring() |
1741 | 37 | storres | # A very expensive solution:
|
1742 | 37 | storres | # -create a fake multivariate polynomial field with only one variable,
|
1743 | 37 | storres | # specifying a negative lexicographical order;
|
1744 | 37 | storres | mpolynomialRing = PolynomialRing(polynomialRing.base(), \ |
1745 | 37 | storres | polynomialRing.variable_name(), \ |
1746 | 37 | storres | 1, order='neglex') |
1747 | 37 | storres | # - convert the univariate argument polynomial into a multivariate
|
1748 | 37 | storres | # version;
|
1749 | 37 | storres | p = mpolynomialRing(polySa) |
1750 | 37 | storres | # - return the string representation of the converted form.
|
1751 | 37 | storres | # There is no simple str() method defined for p's class.
|
1752 | 37 | storres | return(p.__str__())
|
1753 | 5 | storres | #
|
1754 | 5 | storres | print pobyso_get_prec()
|
1755 | 5 | storres | pobyso_set_prec(165)
|
1756 | 5 | storres | print pobyso_get_prec()
|
1757 | 5 | storres | a=100
|
1758 | 5 | storres | print type(a) |
1759 | 5 | storres | id(a)
|
1760 | 5 | storres | print "Max arity: ", pobyso_max_arity |
1761 | 5 | storres | print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43) |
1762 | 56 | storres | print "Function None (44) as a string: ", pobyso_function_type_as_string(44) |
1763 | 56 | storres | print "...Pobyso check done" |