root / pobysoPythonSage / src / pobyso.py @ 83
Historique | Voir | Annoter | Télécharger (39,01 ko)
1 | 5 | storres | """
|
---|---|---|---|
2 | 5 | storres | Actual functions to use in Sage
|
3 | 5 | storres | ST 2012-11-13
|
4 | 5 | storres |
|
5 | 5 | storres | Command line syntax:
|
6 | 5 | storres | use from Sage (via the "load" or the "attach" commands)
|
7 | 5 | storres |
|
8 | 38 | storres | pobyso functions come in five flavors:
|
9 | 57 | storres | - the _so_so (arguments and returned objects are pointers to Sollya objects,
|
10 | 57 | storres | includes the void function and the no arguments function that return a
|
11 | 57 | storres | pointer to a Sollya object);
|
12 | 38 | storres | - the _so_sa (argument are pointers to Sollya objects, returned objects are
|
13 | 57 | storres | Sage/Python objects or, more generally, information is transfered from the
|
14 | 57 | storres | Sollya world to Sage/Python world; e.g. functions without arguments that
|
15 | 57 | storres | return a Sage/Python object);
|
16 | 38 | storres | - the _sa_so (arguments are Sage/Python objects, returned objects are
|
17 | 38 | storres | pointers to Sollya objects);
|
18 | 38 | storres | - the sa_sa (arguments and returned objects are all Sage/Python objects);
|
19 | 51 | storres | - a catch all flavor, without any suffix, (e. g. functions that have no argument
|
20 | 51 | storres | nor return value).
|
21 | 57 | storres | This classification is not always very strict. Conversion functions from Sollya
|
22 | 57 | storres | to Sage/Python are sometimes decorated with Sage/Python arguments to set
|
23 | 57 | storres | the precision. These functions remain in the so_sa category.
|
24 | 5 | storres | NOTES:
|
25 | 5 | storres | Reported errors in Eclipse come from the calls to
|
26 | 5 | storres | the Sollya library
|
27 | 5 | storres |
|
28 | 10 | storres | ToDo (among other things):
|
29 | 10 | storres | -memory management.
|
30 | 5 | storres | """
|
31 | 5 | storres | from ctypes import * |
32 | 37 | storres | import re |
33 | 37 | storres | from sage.symbolic.expression_conversions import polynomial |
34 | 59 | storres | from sage.symbolic.expression_conversions import PolynomialConverter |
35 | 38 | storres | """
|
36 | 38 | storres | Create the equivalent to an enum for the Sollya function types.
|
37 | 38 | storres | """
|
38 | 5 | storres | (SOLLYA_BASE_FUNC_ABS, |
39 | 5 | storres | SOLLYA_BASE_FUNC_ACOS, |
40 | 5 | storres | SOLLYA_BASE_FUNC_ACOSH, |
41 | 5 | storres | SOLLYA_BASE_FUNC_ADD, |
42 | 5 | storres | SOLLYA_BASE_FUNC_ASIN, |
43 | 5 | storres | SOLLYA_BASE_FUNC_ASINH, |
44 | 5 | storres | SOLLYA_BASE_FUNC_ATAN, |
45 | 5 | storres | SOLLYA_BASE_FUNC_ATANH, |
46 | 5 | storres | SOLLYA_BASE_FUNC_CEIL, |
47 | 5 | storres | SOLLYA_BASE_FUNC_CONSTANT, |
48 | 5 | storres | SOLLYA_BASE_FUNC_COS, |
49 | 5 | storres | SOLLYA_BASE_FUNC_COSH, |
50 | 5 | storres | SOLLYA_BASE_FUNC_DIV, |
51 | 5 | storres | SOLLYA_BASE_FUNC_DOUBLE, |
52 | 5 | storres | SOLLYA_BASE_FUNC_DOUBLEDOUBLE, |
53 | 5 | storres | SOLLYA_BASE_FUNC_DOUBLEEXTENDED, |
54 | 5 | storres | SOLLYA_BASE_FUNC_ERF, |
55 | 5 | storres | SOLLYA_BASE_FUNC_ERFC, |
56 | 5 | storres | SOLLYA_BASE_FUNC_EXP, |
57 | 5 | storres | SOLLYA_BASE_FUNC_EXP_M1, |
58 | 5 | storres | SOLLYA_BASE_FUNC_FLOOR, |
59 | 5 | storres | SOLLYA_BASE_FUNC_FREE_VARIABLE, |
60 | 5 | storres | SOLLYA_BASE_FUNC_HALFPRECISION, |
61 | 5 | storres | SOLLYA_BASE_FUNC_LIBRARYCONSTANT, |
62 | 5 | storres | SOLLYA_BASE_FUNC_LIBRARYFUNCTION, |
63 | 5 | storres | SOLLYA_BASE_FUNC_LOG, |
64 | 5 | storres | SOLLYA_BASE_FUNC_LOG_10, |
65 | 5 | storres | SOLLYA_BASE_FUNC_LOG_1P, |
66 | 5 | storres | SOLLYA_BASE_FUNC_LOG_2, |
67 | 5 | storres | SOLLYA_BASE_FUNC_MUL, |
68 | 5 | storres | SOLLYA_BASE_FUNC_NEARESTINT, |
69 | 5 | storres | SOLLYA_BASE_FUNC_NEG, |
70 | 5 | storres | SOLLYA_BASE_FUNC_PI, |
71 | 5 | storres | SOLLYA_BASE_FUNC_POW, |
72 | 5 | storres | SOLLYA_BASE_FUNC_PROCEDUREFUNCTION, |
73 | 5 | storres | SOLLYA_BASE_FUNC_QUAD, |
74 | 5 | storres | SOLLYA_BASE_FUNC_SIN, |
75 | 5 | storres | SOLLYA_BASE_FUNC_SINGLE, |
76 | 5 | storres | SOLLYA_BASE_FUNC_SINH, |
77 | 5 | storres | SOLLYA_BASE_FUNC_SQRT, |
78 | 5 | storres | SOLLYA_BASE_FUNC_SUB, |
79 | 5 | storres | SOLLYA_BASE_FUNC_TAN, |
80 | 5 | storres | SOLLYA_BASE_FUNC_TANH, |
81 | 5 | storres | SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44)) |
82 | 56 | storres | print "\nSuperficial pobyso check..." |
83 | 5 | storres | print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS |
84 | 5 | storres | print "Last constant - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE |
85 | 5 | storres | |
86 | 5 | storres | pobyso_max_arity = 9
|
87 | 5 | storres | |
88 | 5 | storres | def pobyso_autoprint(arg): |
89 | 5 | storres | sollya_lib_autoprint(arg,None)
|
90 | 5 | storres | |
91 | 38 | storres | def pobyso_autoprint_so_so(arg): |
92 | 38 | storres | sollya_lib_autoprint(arg,None)
|
93 | 54 | storres | |
94 | 54 | storres | def pobyso_absolute_so_so(): |
95 | 54 | storres | return(sollya_lib_absolute(None)) |
96 | 38 | storres | |
97 | 54 | storres | def pobyso_build_function_sub_so_so(exp1So, exp2So): |
98 | 54 | storres | return(sollya_lib_build_function_sub(exp1So, exp2So))
|
99 | 54 | storres | |
100 | 5 | storres | def pobyso_cmp(rnArg, soCte): |
101 | 54 | storres | """
|
102 | 54 | storres | Compare the MPFR value a RealNumber with that of a Sollya constant.
|
103 | 54 | storres |
|
104 | 54 | storres | Get the value of the Sollya constant into a RealNumber and compare
|
105 | 54 | storres | using MPFR. Could be optimized by working directly with a mpfr_t
|
106 | 54 | storres | for the intermediate number.
|
107 | 54 | storres | """
|
108 | 5 | storres | precisionOfCte = c_int(0)
|
109 | 5 | storres | # From the Sollya constant, create a local Sage RealNumber.
|
110 | 5 | storres | sollya_lib_get_prec_of_constant(precisionOfCte, soCte) |
111 | 5 | storres | #print "Precision of constant: ", precisionOfCte
|
112 | 5 | storres | RRRR = RealField(precisionOfCte.value) |
113 | 5 | storres | rnLocal = RRRR(0)
|
114 | 5 | storres | sollya_lib_get_constant(get_rn_value(rnLocal), soCte) |
115 | 5 | storres | #print "rnDummy: ", rnDummy
|
116 | 5 | storres | # Compare the local Sage RealNumber with rnArg.
|
117 | 5 | storres | return(cmp_rn_value(rnArg, rnLocal))
|
118 | 83 | storres | # End pobyso_smp
|
119 | 5 | storres | |
120 | 55 | storres | def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo): |
121 | 83 | storres | """
|
122 | 83 | storres | Variable change in a function.
|
123 | 83 | storres | """
|
124 | 55 | storres | return(sollya_lib_evaluate(funcSo,chvarExpSo))
|
125 | 83 | storres | # End pobyso_change_var_in_function_so_so
|
126 | 55 | storres | |
127 | 55 | storres | def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo): |
128 | 55 | storres | resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo) |
129 | 55 | storres | return(resultSo)
|
130 | 83 | storres | # End pobyso_chebyshevform_so_so.
|
131 | 55 | storres | |
132 | 54 | storres | def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \ |
133 | 54 | storres | upperBoundSa): |
134 | 54 | storres | """
|
135 | 54 | storres | TODO: set the variable name in Sollya.
|
136 | 54 | storres | """
|
137 | 54 | storres | funcSo = pobyso_parse_string(funcSa._assume_str()) |
138 | 54 | storres | rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa) |
139 | 54 | storres | infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo) |
140 | 83 | storres | # Sollya return the infnorm as an interval.
|
141 | 54 | storres | fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo) |
142 | 54 | storres | # Get the top bound and compute the binade top limit.
|
143 | 54 | storres | fMaxUpperBoundSa = fMaxSa.upper() |
144 | 54 | storres | binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
|
145 | 54 | storres | # Put up together the function to use to compute the lower bound.
|
146 | 54 | storres | funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) + \
|
147 | 54 | storres | '-(' + f._assume_str() + ')') |
148 | 54 | storres | pobyso_autoprint(funcAuxSo) |
149 | 83 | storres | # Clear the Sollya range before a new call to infnorm and issue the call.
|
150 | 54 | storres | sollya_lib_clear_obj(infnormSo) |
151 | 54 | storres | infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo) |
152 | 54 | storres | fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo) |
153 | 54 | storres | sollya_lib_clear_obj(infnormSo) |
154 | 54 | storres | fMinLowerBoundSa = topBinadeLimit - fMinSa.lower() |
155 | 54 | storres | # Compute the maximum of the precisions of the different bounds.
|
156 | 54 | storres | maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
|
157 | 54 | storres | fMaxUpperBoundSa.parent().precision()]) |
158 | 54 | storres | # Create a RealIntervalField and create an interval with the "good" bounds.
|
159 | 54 | storres | RRRI = RealIntervalField(maxPrecSa) |
160 | 54 | storres | imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa) |
161 | 83 | storres | # Free the unneeded Sollya objects
|
162 | 54 | storres | sollya_lib_clear_obj(funcSo) |
163 | 54 | storres | sollya_lib_clear_obj(funcAuxSo) |
164 | 54 | storres | sollya_lib_clear_obj(rangeSo) |
165 | 54 | storres | return(imageIntervalSa)
|
166 | 83 | storres | # End pobyso_compute_pos_function_abs_val_bounds_sa_sa
|
167 | 54 | storres | |
168 | 5 | storres | def pobyso_constant(rnArg): |
169 | 38 | storres | """ Legacy function. See pobyso_constant_sa_so. """
|
170 | 38 | storres | return(pobyso_constant_sa_so(rnArg))
|
171 | 5 | storres | |
172 | 38 | storres | def pobyso_constant_sa_so(rnArg): |
173 | 52 | storres | """
|
174 | 52 | storres | Create a Sollya constant from a RealNumber.
|
175 | 52 | storres | """
|
176 | 38 | storres | return(sollya_lib_constant(get_rn_value(rnArg)))
|
177 | 38 | storres | |
178 | 55 | storres | def pobyso_constant_0_sa_so(): |
179 | 55 | storres | return(pobyso_constant_from_int_sa_so(0)) |
180 | 55 | storres | |
181 | 5 | storres | def pobyso_constant_1(): |
182 | 38 | storres | """ Legacy function. See pobyso_constant_so_so. """
|
183 | 52 | storres | return(pobyso_constant_1_sa_so())
|
184 | 5 | storres | |
185 | 52 | storres | def pobyso_constant_1_sa_so(): |
186 | 38 | storres | return(pobyso_constant_from_int_sa_so(1)) |
187 | 38 | storres | |
188 | 5 | storres | def pobyso_constant_from_int(anInt): |
189 | 38 | storres | """ Legacy function. See pobyso_constant_from_int_sa_so. """
|
190 | 38 | storres | return(pobyso_constant_from_int_sa_so(anInt))
|
191 | 38 | storres | |
192 | 38 | storres | def pobyso_constant_from_int_sa_so(anInt): |
193 | 5 | storres | return(sollya_lib_constant_from_int(int(anInt))) |
194 | 5 | storres | |
195 | 5 | storres | def pobyso_function_type_as_string(funcType): |
196 | 38 | storres | """ Legacy function. See pobyso_function_type_as_string_so_sa. """
|
197 | 38 | storres | return(pobyso_function_type_as_string_so_sa(funcType))
|
198 | 38 | storres | |
199 | 38 | storres | def pobyso_function_type_as_string_so_sa(funcType): |
200 | 38 | storres | """
|
201 | 38 | storres | Numeric Sollya function codes -> Sage mathematical function names.
|
202 | 38 | storres | Notice that pow -> ^ (a la Sage, not a la Python).
|
203 | 38 | storres | """
|
204 | 5 | storres | if funcType == SOLLYA_BASE_FUNC_ABS:
|
205 | 5 | storres | return "abs" |
206 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ACOS:
|
207 | 5 | storres | return "arccos" |
208 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ACOSH:
|
209 | 5 | storres | return "arccosh" |
210 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ADD:
|
211 | 5 | storres | return "+" |
212 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ASIN:
|
213 | 5 | storres | return "arcsin" |
214 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ASINH:
|
215 | 5 | storres | return "arcsinh" |
216 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ATAN:
|
217 | 5 | storres | return "arctan" |
218 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ATANH:
|
219 | 5 | storres | return "arctanh" |
220 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_CEIL:
|
221 | 5 | storres | return "ceil" |
222 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
|
223 | 5 | storres | return "cte" |
224 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_COS:
|
225 | 5 | storres | return "cos" |
226 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_COSH:
|
227 | 5 | storres | return "cosh" |
228 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DIV:
|
229 | 5 | storres | return "/" |
230 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
|
231 | 5 | storres | return "double" |
232 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
|
233 | 5 | storres | return "doubleDouble" |
234 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
|
235 | 5 | storres | return "doubleDxtended" |
236 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ERF:
|
237 | 5 | storres | return "erf" |
238 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_ERFC:
|
239 | 5 | storres | return "erfc" |
240 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_EXP:
|
241 | 5 | storres | return "exp" |
242 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
|
243 | 5 | storres | return "expm1" |
244 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_FLOOR:
|
245 | 5 | storres | return "floor" |
246 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
247 | 5 | storres | return "freeVariable" |
248 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
|
249 | 5 | storres | return "halfPrecision" |
250 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
|
251 | 5 | storres | return "libraryConstant" |
252 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
|
253 | 5 | storres | return "libraryFunction" |
254 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG:
|
255 | 5 | storres | return "log" |
256 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG_10:
|
257 | 5 | storres | return "log10" |
258 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
|
259 | 5 | storres | return "log1p" |
260 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_LOG_2:
|
261 | 5 | storres | return "log2" |
262 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_MUL:
|
263 | 5 | storres | return "*" |
264 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
|
265 | 5 | storres | return "round" |
266 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_NEG:
|
267 | 5 | storres | return "__neg__" |
268 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_PI:
|
269 | 5 | storres | return "pi" |
270 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_POW:
|
271 | 5 | storres | return "^" |
272 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
|
273 | 5 | storres | return "procedureFunction" |
274 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_QUAD:
|
275 | 5 | storres | return "quad" |
276 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SIN:
|
277 | 5 | storres | return "sin" |
278 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SINGLE:
|
279 | 5 | storres | return "single" |
280 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SINH:
|
281 | 5 | storres | return "sinh" |
282 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SQRT:
|
283 | 5 | storres | return "sqrt" |
284 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_SUB:
|
285 | 5 | storres | return "-" |
286 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_TAN:
|
287 | 5 | storres | return "tan" |
288 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_TANH:
|
289 | 5 | storres | return "tanh" |
290 | 5 | storres | elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
|
291 | 5 | storres | return "tripleDouble" |
292 | 5 | storres | else:
|
293 | 5 | storres | return None |
294 | 5 | storres | |
295 | 5 | storres | def pobyso_get_constant(rnArg, soConst): |
296 | 38 | storres | """ Legacy function. See pobyso_get_constant_so_sa. """
|
297 | 53 | storres | return(pobyso_get_constant_so_sa(rnArg, soConst))
|
298 | 38 | storres | |
299 | 38 | storres | def pobyso_get_constant_so_sa(rnArg, soConst): |
300 | 52 | storres | """
|
301 | 52 | storres | Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
|
302 | 52 | storres | rnArg must already exist and belong to some RealField.
|
303 | 52 | storres | We assume that soConst points to a Sollya constant.
|
304 | 52 | storres | """
|
305 | 53 | storres | return(sollya_lib_get_constant(get_rn_value(rnArg), soConst))
|
306 | 5 | storres | |
307 | 57 | storres | def pobyso_get_constant_as_rn(ctExpSo): |
308 | 83 | storres | """
|
309 | 83 | storres | Legacy function. See pobyso_get_constant_as_rn_so_sa.
|
310 | 83 | storres | """
|
311 | 57 | storres | return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
|
312 | 38 | storres | |
313 | 56 | storres | def pobyso_get_constant_as_rn_so_sa(constExpSo): |
314 | 83 | storres | """
|
315 | 83 | storres | Get a Sollya constant as a Sage "real number".
|
316 | 83 | storres | The precision of the floating-point number returned is that of the Sollya
|
317 | 83 | storres | constant.
|
318 | 83 | storres | """
|
319 | 57 | storres | precisionSa = pobyso_get_prec_of_constant_so_sa(constExpSo) |
320 | 56 | storres | RRRR = RealField(precisionSa) |
321 | 56 | storres | rnSa = RRRR(0)
|
322 | 56 | storres | sollya_lib_get_constant(get_rn_value(rnSa), constExpSo) |
323 | 56 | storres | return(rnSa)
|
324 | 83 | storres | # End pobyso_get_constant_as_rn_so_sa
|
325 | 38 | storres | |
326 | 38 | storres | def pobyso_get_constant_as_rn_with_rf(ctExp, realField): |
327 | 83 | storres | """
|
328 | 83 | storres | Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
|
329 | 83 | storres | """
|
330 | 38 | storres | return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
|
331 | 5 | storres | |
332 | 56 | storres | def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None): |
333 | 83 | storres | """
|
334 | 83 | storres | Get a Sollya constant as a Sage "real number".
|
335 | 83 | storres | If no real field is specified, the precision of the floating-point number
|
336 | 83 | storres | returned is that of the Solly constant.
|
337 | 83 | storres | Otherwise is is that of the real field. Hence rounding may happen.
|
338 | 83 | storres | """
|
339 | 56 | storres | if realFieldSa is None: |
340 | 56 | storres | sollyaPrecSa = pobyso_get_prec_so_sa() |
341 | 56 | storres | realFieldSa = RealField(sollyaPrecSa) |
342 | 56 | storres | rnSa = realFieldSa(0)
|
343 | 56 | storres | sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo) |
344 | 56 | storres | return(rnSa)
|
345 | 83 | storres | # End pobyso_get_constant_as_rn_with_rf_so_sa
|
346 | 38 | storres | |
347 | 5 | storres | def pobyso_get_free_variable_name(): |
348 | 83 | storres | """
|
349 | 83 | storres | Legacy function. See pobyso_get_free_variable_name_so_sa.
|
350 | 83 | storres | """
|
351 | 38 | storres | return(pobyso_get_free_variable_name_so_sa())
|
352 | 38 | storres | |
353 | 38 | storres | def pobyso_get_free_variable_name_so_sa(): |
354 | 5 | storres | return(sollya_lib_get_free_variable_name())
|
355 | 5 | storres | |
356 | 38 | storres | def pobyso_get_function_arity(expressionSo): |
357 | 83 | storres | """
|
358 | 83 | storres | Legacy function. See pobyso_get_function_arity_so_sa.
|
359 | 83 | storres | """
|
360 | 38 | storres | return(pobyso_get_function_arity_so_sa(expressionSo))
|
361 | 38 | storres | |
362 | 38 | storres | def pobyso_get_function_arity_so_sa(expressionSo): |
363 | 5 | storres | arity = c_int(0)
|
364 | 38 | storres | sollya_lib_get_function_arity(byref(arity),expressionSo) |
365 | 5 | storres | return(int(arity.value)) |
366 | 5 | storres | |
367 | 38 | storres | def pobyso_get_head_function(expressionSo): |
368 | 83 | storres | """
|
369 | 83 | storres | Legacy function. See pobyso_get_head_function_so_sa.
|
370 | 83 | storres | """
|
371 | 38 | storres | return(pobyso_get_head_function_so_sa(expressionSo))
|
372 | 38 | storres | |
373 | 38 | storres | def pobyso_get_head_function_so_sa(expressionSo): |
374 | 5 | storres | functionType = c_int(0)
|
375 | 38 | storres | sollya_lib_get_head_function(byref(functionType), expressionSo, None)
|
376 | 5 | storres | return(int(functionType.value)) |
377 | 5 | storres | |
378 | 56 | storres | def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ): |
379 | 53 | storres | """
|
380 | 53 | storres | Return the Sage interval corresponding to the Sollya range argument.
|
381 | 83 | storres | If no reaIntervalField is passed as an argument, the interval bounds are not
|
382 | 56 | storres | rounded: they are elements of RealIntervalField of the "right" precision
|
383 | 56 | storres | to hold all the digits.
|
384 | 53 | storres | """
|
385 | 53 | storres | prec = c_int(0)
|
386 | 56 | storres | if realIntervalFieldSa is None: |
387 | 56 | storres | retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
|
388 | 56 | storres | if retval == 0: |
389 | 56 | storres | return(None) |
390 | 56 | storres | realIntervalFieldSa = RealIntervalField(prec.value) |
391 | 56 | storres | intervalSa = realIntervalFieldSa(0,0) |
392 | 53 | storres | retval = \ |
393 | 53 | storres | sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\ |
394 | 53 | storres | soRange) |
395 | 53 | storres | if retval == 0: |
396 | 53 | storres | return(None) |
397 | 53 | storres | return(intervalSa)
|
398 | 56 | storres | # End pobyso_get_interval_from_range_so_sa
|
399 | 56 | storres | |
400 | 5 | storres | def pobyso_get_list_elements(soObj): |
401 | 38 | storres | """ Legacy function. See pobyso_get_list_elements_so_so. """
|
402 | 38 | storres | return(pobyso_get_list_elements_so_so(soObj))
|
403 | 38 | storres | |
404 | 38 | storres | def pobyso_get_list_elements_so_so(soObj): |
405 | 51 | storres | """
|
406 | 51 | storres | Get the list elements as a Sage/Python array of Sollya objects.
|
407 | 51 | storres | The other data returned are also Sage/Python objects.
|
408 | 51 | storres | """
|
409 | 5 | storres | listAddress = POINTER(c_longlong)() |
410 | 5 | storres | numElements = c_int(0)
|
411 | 5 | storres | isEndElliptic = c_int(0)
|
412 | 5 | storres | listAsList = [] |
413 | 5 | storres | result = sollya_lib_get_list_elements(byref(listAddress),\ |
414 | 54 | storres | byref(numElements),\ |
415 | 54 | storres | byref(isEndElliptic),\ |
416 | 54 | storres | soObj) |
417 | 5 | storres | if result == 0 : |
418 | 5 | storres | return None |
419 | 5 | storres | for i in xrange(0, numElements.value, 1): |
420 | 5 | storres | listAsList.append(listAddress[i]) |
421 | 5 | storres | return(listAsList, numElements.value, isEndElliptic.value)
|
422 | 5 | storres | |
423 | 38 | storres | def pobyso_get_max_prec_of_exp(soExp): |
424 | 38 | storres | """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
|
425 | 38 | storres | return(pobyso_get_max_prec_of_exp_so_sa(soExp))
|
426 | 5 | storres | |
427 | 38 | storres | def pobyso_get_max_prec_of_exp_so_sa(soExp): |
428 | 38 | storres | """
|
429 | 38 | storres | Get the maximum precision used for the numbers in a Sollya expression.
|
430 | 52 | storres |
|
431 | 52 | storres | Arguments:
|
432 | 52 | storres | soExp -- a Sollya expression pointer
|
433 | 52 | storres | Return value:
|
434 | 52 | storres | A Python integer
|
435 | 38 | storres | TODO:
|
436 | 38 | storres | - error management;
|
437 | 38 | storres | - correctly deal with numerical type such as DOUBLEEXTENDED.
|
438 | 38 | storres | """
|
439 | 5 | storres | maxPrecision = 0
|
440 | 52 | storres | minConstPrec = 0
|
441 | 52 | storres | currentConstPrec = 0
|
442 | 38 | storres | operator = pobyso_get_head_function_so_sa(soExp) |
443 | 5 | storres | if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \ |
444 | 5 | storres | (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
445 | 38 | storres | (arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp) |
446 | 5 | storres | for i in xrange(arity): |
447 | 5 | storres | maxPrecisionCandidate = \ |
448 | 38 | storres | pobyso_get_max_prec_of_exp_so_sa(subexpressions[i]) |
449 | 5 | storres | if maxPrecisionCandidate > maxPrecision:
|
450 | 5 | storres | maxPrecision = maxPrecisionCandidate |
451 | 5 | storres | return(maxPrecision)
|
452 | 5 | storres | elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
453 | 52 | storres | minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp) |
454 | 52 | storres | #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
|
455 | 52 | storres | #print minConstPrec, " - ", currentConstPrec
|
456 | 52 | storres | return(pobyso_get_min_prec_of_constant_so_sa(soExp))
|
457 | 52 | storres | |
458 | 5 | storres | elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
459 | 5 | storres | return(0) |
460 | 5 | storres | else:
|
461 | 38 | storres | print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator." |
462 | 5 | storres | return(0) |
463 | 5 | storres | |
464 | 52 | storres | def pobyso_get_min_prec_of_constant_so_sa(soConstExp): |
465 | 52 | storres | """
|
466 | 52 | storres | Get the minimum precision necessary to represent the value of a Sollya
|
467 | 52 | storres | constant.
|
468 | 52 | storres | MPFR_MIN_PREC and powers of 2 are taken into account.
|
469 | 52 | storres | We assume that soCteExp is a point
|
470 | 52 | storres | """
|
471 | 52 | storres | constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp) |
472 | 52 | storres | return(min_mpfr_size(get_rn_value(constExpAsRn)))
|
473 | 52 | storres | |
474 | 5 | storres | def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR): |
475 | 38 | storres | """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
|
476 | 38 | storres | return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
|
477 | 38 | storres | |
478 | 38 | storres | def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR): |
479 | 5 | storres | """
|
480 | 38 | storres | Get a Sage expression from a Sollya expression.
|
481 | 38 | storres | Currently only tested with polynomials with floating-point coefficients.
|
482 | 5 | storres | Notice that, in the returned polynomial, the exponents are RealNumbers.
|
483 | 5 | storres | """
|
484 | 5 | storres | #pobyso_autoprint(sollyaExp)
|
485 | 55 | storres | operator = pobyso_get_head_function_so_sa(sollyaExp) |
486 | 83 | storres | sollyaLibFreeVariableName = sollya_lib_get_free_variable_name() |
487 | 5 | storres | # Constants and the free variable are special cases.
|
488 | 5 | storres | # All other operator are dealt with in the same way.
|
489 | 5 | storres | if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \ |
490 | 5 | storres | (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE): |
491 | 38 | storres | (arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp) |
492 | 5 | storres | if arity == 1: |
493 | 38 | storres | sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
|
494 | 52 | storres | "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \ |
495 | 52 | storres | realField) + ")")
|
496 | 5 | storres | elif arity == 2: |
497 | 63 | storres | # We do not get through the preprocessor.
|
498 | 63 | storres | # The "^" operator is then a special case.
|
499 | 5 | storres | if operator == SOLLYA_BASE_FUNC_POW:
|
500 | 5 | storres | operatorAsString = "**"
|
501 | 5 | storres | else:
|
502 | 52 | storres | operatorAsString = \ |
503 | 52 | storres | pobyso_function_type_as_string_so_sa(operator) |
504 | 5 | storres | sageExp = \ |
505 | 38 | storres | eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\ |
506 | 5 | storres | + " " + operatorAsString + " " + \ |
507 | 38 | storres | "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
|
508 | 63 | storres | # We do not know yet how to deal with arity >= 3
|
509 | 63 | storres | # (is there any in Sollya anyway?).
|
510 | 5 | storres | else:
|
511 | 5 | storres | sageExp = eval('None') |
512 | 5 | storres | return(sageExp)
|
513 | 5 | storres | elif operator == SOLLYA_BASE_FUNC_CONSTANT:
|
514 | 5 | storres | #print "This is a constant"
|
515 | 38 | storres | return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
|
516 | 5 | storres | elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
|
517 | 5 | storres | #print "This is free variable"
|
518 | 83 | storres | return(eval(sollyaLibFreeVariableName)) |
519 | 5 | storres | else:
|
520 | 5 | storres | print "Unexpected" |
521 | 5 | storres | return eval('None') |
522 | 5 | storres | # End pobyso_get_sage_poly_from_sollya_poly
|
523 | 73 | storres | |
524 | 73 | storres | def pobyso_get_poly_sa_so(polySo, realFieldSa=None): |
525 | 83 | storres | """
|
526 | 83 | storres | Create a Sollya polynomial from a Sage polynomial.
|
527 | 83 | storres | """
|
528 | 73 | storres | pass
|
529 | 73 | storres | # pobyso_get_poly_sa_so
|
530 | 73 | storres | |
531 | 62 | storres | def pobyso_get_poly_so_sa(polySo, realFieldSa=None): |
532 | 57 | storres | """
|
533 | 57 | storres | Convert a Sollya polynomial into a Sage polynomial.
|
534 | 62 | storres | We assume that the polynomial is in canonical form.
|
535 | 57 | storres | If no realField is given, a RealField corresponding to the maximum precision
|
536 | 57 | storres | of the coefficients is internally computed.
|
537 | 57 | storres | It is not returned but can be easily retrieved from the polynomial itself.
|
538 | 57 | storres | Main steps:
|
539 | 57 | storres | - (optional) compute the RealField of the coefficients;
|
540 | 57 | storres | - convert the Sollya expression into a Sage expression;
|
541 | 57 | storres | - convert the Sage expression into a Sage polynomial
|
542 | 59 | storres | TODO: the canonical thing for the polynomial.
|
543 | 57 | storres | """
|
544 | 57 | storres | if realFieldSa is None: |
545 | 57 | storres | expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo) |
546 | 57 | storres | realFieldSa = RealField(expressionPrecSa) |
547 | 63 | storres | #print "Sollya expression before...",
|
548 | 63 | storres | #pobyso_autoprint(polySo)
|
549 | 63 | storres | |
550 | 57 | storres | expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \ |
551 | 57 | storres | realFieldSa) |
552 | 63 | storres | #print "...Sollya expression after.",
|
553 | 63 | storres | #pobyso_autoprint(polySo)
|
554 | 57 | storres | polyVariableSa = expressionSa.variables()[0]
|
555 | 59 | storres | polyRingSa = realFieldSa[str(polyVariableSa)]
|
556 | 81 | storres | #print polyRingSa
|
557 | 62 | storres | # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
|
558 | 62 | storres | polynomialSa = polyRingSa(expressionSa) |
559 | 57 | storres | return(polynomialSa)
|
560 | 57 | storres | # End pobyso_get_sage_poly_from_sollya_poly
|
561 | 57 | storres | |
562 | 38 | storres | def pobyso_get_subfunctions(expressionSo): |
563 | 38 | storres | """ Legacy function. See pobyso_get_subfunctions_so_sa. """
|
564 | 38 | storres | return(pobyso_get_subfunctions_so_sa(expressionSo))
|
565 | 38 | storres | |
566 | 38 | storres | def pobyso_get_subfunctions_so_sa(expressionSo): |
567 | 38 | storres | """
|
568 | 38 | storres | Get the subfunctions of an expression.
|
569 | 38 | storres | Return the number of subfunctions and the list of subfunctions addresses.
|
570 | 55 | storres | S.T.: Could not figure out another way than that ugly list of declarations
|
571 | 83 | storres | to recover the addresses of the subfunctions.
|
572 | 83 | storres | We limit ourselves to arity 8 functions.
|
573 | 38 | storres | """
|
574 | 5 | storres | subf0 = c_int(0)
|
575 | 5 | storres | subf1 = c_int(0)
|
576 | 5 | storres | subf2 = c_int(0)
|
577 | 5 | storres | subf3 = c_int(0)
|
578 | 5 | storres | subf4 = c_int(0)
|
579 | 5 | storres | subf5 = c_int(0)
|
580 | 5 | storres | subf6 = c_int(0)
|
581 | 5 | storres | subf7 = c_int(0)
|
582 | 5 | storres | subf8 = c_int(0)
|
583 | 5 | storres | arity = c_int(0)
|
584 | 5 | storres | nullPtr = POINTER(c_int)() |
585 | 38 | storres | sollya_lib_get_subfunctions(expressionSo, byref(arity), \ |
586 | 83 | storres | byref(subf0), byref(subf1), byref(subf2), byref(subf3), \ |
587 | 83 | storres | byref(subf4), byref(subf5),\ |
588 | 83 | storres | byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
|
589 | 83 | storres | # byref(cast(subfunctions[0], POINTER(c_int))), \
|
590 | 83 | storres | # byref(cast(subfunctions[0], POINTER(c_int))), \
|
591 | 83 | storres | # byref(cast(subfunctions[2], POINTER(c_int))), \
|
592 | 83 | storres | # byref(cast(subfunctions[3], POINTER(c_int))), \
|
593 | 83 | storres | # byref(cast(subfunctions[4], POINTER(c_int))), \
|
594 | 83 | storres | # byref(cast(subfunctions[5], POINTER(c_int))), \
|
595 | 83 | storres | # byref(cast(subfunctions[6], POINTER(c_int))), \
|
596 | 83 | storres | # byref(cast(subfunctions[7], POINTER(c_int))), \
|
597 | 5 | storres | # byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
|
598 | 83 | storres | subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \ |
599 | 83 | storres | subf8] |
600 | 5 | storres | subs = [] |
601 | 5 | storres | if arity.value > pobyso_max_arity:
|
602 | 38 | storres | return(0,[]) |
603 | 5 | storres | for i in xrange(arity.value): |
604 | 5 | storres | subs.append(int(subfunctions[i].value))
|
605 | 5 | storres | #print subs[i]
|
606 | 5 | storres | return(int(arity.value), subs) |
607 | 5 | storres | |
608 | 5 | storres | def pobyso_get_prec(): |
609 | 38 | storres | """ Legacy function. See pobyso_get_prec_so_sa(). """
|
610 | 38 | storres | return(pobyso_get_prec_so_sa())
|
611 | 38 | storres | |
612 | 38 | storres | def pobyso_get_prec_so_sa(): |
613 | 38 | storres | """
|
614 | 38 | storres | Get the current default precision in Sollya.
|
615 | 38 | storres | The return value is Sage/Python int.
|
616 | 38 | storres | """
|
617 | 56 | storres | precSo = sollya_lib_get_prec(None)
|
618 | 56 | storres | precSa = c_int(0)
|
619 | 56 | storres | sollya_lib_get_constant_as_int(byref(precSa), precSo) |
620 | 56 | storres | sollya_lib_clear_obj(precSo) |
621 | 56 | storres | return(int(precSa.value)) |
622 | 83 | storres | # End pobyso_get_prec_so_sa.
|
623 | 5 | storres | |
624 | 38 | storres | def pobyso_get_prec_of_constant(ctExpSo): |
625 | 38 | storres | """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
|
626 | 38 | storres | return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
|
627 | 38 | storres | |
628 | 56 | storres | def pobyso_get_prec_of_constant_so_sa(ctExpSo): |
629 | 56 | storres | prec = c_int(0)
|
630 | 56 | storres | retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
|
631 | 56 | storres | if retc == 0: |
632 | 56 | storres | return(None) |
633 | 56 | storres | return(int(prec.value)) |
634 | 56 | storres | |
635 | 53 | storres | def pobyso_get_prec_of_range_so_sa(rangeSo): |
636 | 5 | storres | prec = c_int(0)
|
637 | 53 | storres | retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
|
638 | 56 | storres | if retc == 0: |
639 | 56 | storres | return(None) |
640 | 5 | storres | return(int(prec.value)) |
641 | 5 | storres | |
642 | 53 | storres | def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None): |
643 | 54 | storres | print "Do not use this function. User pobyso_supnorm_so_so instead." |
644 | 54 | storres | return(None) |
645 | 53 | storres | |
646 | 37 | storres | def pobyso_lib_init(): |
647 | 37 | storres | sollya_lib_init(None)
|
648 | 37 | storres | |
649 | 37 | storres | def pobyso_name_free_variable(freeVariableName): |
650 | 38 | storres | """ Legacy function. See pobyso_name_free_variable_sa_so. """
|
651 | 38 | storres | pobyso_name_free_variable_sa_so(freeVariableName) |
652 | 38 | storres | |
653 | 38 | storres | def pobyso_name_free_variable_sa_so(freeVariableName): |
654 | 83 | storres | """
|
655 | 83 | storres | Set the free variable name in Sollya from a Sage string.
|
656 | 83 | storres | """
|
657 | 37 | storres | sollya_lib_name_free_variable(freeVariableName) |
658 | 37 | storres | |
659 | 5 | storres | def pobyso_parse_string(string): |
660 | 38 | storres | """ Legacy function. See pobyso_parse_string_sa_so. """
|
661 | 38 | storres | return(pobyso_parse_string_sa_so(string))
|
662 | 38 | storres | |
663 | 38 | storres | def pobyso_parse_string_sa_so(string): |
664 | 83 | storres | """
|
665 | 83 | storres | Get the Sollya expression computed from a Sage string.
|
666 | 83 | storres | """
|
667 | 5 | storres | return(sollya_lib_parse_string(string))
|
668 | 5 | storres | |
669 | 5 | storres | def pobyso_range(rnLowerBound, rnUpperBound): |
670 | 38 | storres | """ Legacy function. See pobyso_range_sa_so. """
|
671 | 51 | storres | return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
|
672 | 38 | storres | |
673 | 62 | storres | def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa): |
674 | 56 | storres | """
|
675 | 83 | storres | Return a Sollya range from to 2 RealField Sage elements.
|
676 | 56 | storres | The Sollya range element has a sufficient precision to hold all
|
677 | 83 | storres | the digits of the Sage bounds.
|
678 | 56 | storres | """
|
679 | 83 | storres | # Sanity check.
|
680 | 62 | storres | if rnLowerBoundSa > rnUpperBoundSa:
|
681 | 62 | storres | return None |
682 | 83 | storres | # Check for the largest precision.
|
683 | 56 | storres | lbPrec = rnLowerBoundSa.parent().precision() |
684 | 56 | storres | ubPrec = rnLowerBoundSa.parent().precision() |
685 | 56 | storres | currentSollyaPrecSa = pobyso_get_prec_so_sa() |
686 | 56 | storres | maxPrecSa = max(lbPrec, ubPrec, currentSollyaPrecSa)
|
687 | 56 | storres | # Change the current Sollya precision only if necessary.
|
688 | 56 | storres | if maxPrecSa > currentSollyaPrecSa:
|
689 | 56 | storres | currentPrecSo = sollya_lib_get_prec(None)
|
690 | 56 | storres | newPrecSo = solly_lib_constant_from_uint64(maxPrecSa) |
691 | 56 | storres | sollya_lib_set_prec(newPrecSo) |
692 | 56 | storres | lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa)) |
693 | 56 | storres | upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa)) |
694 | 5 | storres | rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo) |
695 | 56 | storres | if maxPrecSa > currentSollyaPrecSa:
|
696 | 56 | storres | sollya_lib_set_prec(currentPrecSo) |
697 | 56 | storres | sollya_lib_clear_obj(currentPrecSo) |
698 | 56 | storres | sollya_lib_clear_obj(newPrecSo) |
699 | 56 | storres | sollya_lib_clear_obj(lowerBoundSo) |
700 | 56 | storres | sollya_lib_clear_obj(upperBoundSo) |
701 | 5 | storres | return(rangeSo)
|
702 | 5 | storres | |
703 | 62 | storres | def pobyso_range_to_interval_so_sa(rangeSo, realIntervalField = None): |
704 | 83 | storres | """
|
705 | 83 | storres | Get a Sage interval from a Sollya range.
|
706 | 83 | storres | If no realIntervalField is given as a parameter, the Sage interval
|
707 | 83 | storres | precision is that of the Sollya range.
|
708 | 83 | storres | Otherwise, the precision is that of the realIntervalField. Rounding
|
709 | 83 | storres | may happen.
|
710 | 83 | storres | """
|
711 | 56 | storres | if realIntervalField is None: |
712 | 56 | storres | precSa = pobyso_get_prec_of_range_so_sa(rangeSo) |
713 | 56 | storres | realIntervalField = RealIntervalField(precSa) |
714 | 56 | storres | intervalSa = \ |
715 | 56 | storres | pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalField) |
716 | 56 | storres | return(intervalSa)
|
717 | 56 | storres | |
718 | 52 | storres | def pobyso_remez_canonical_sa_sa(func, \ |
719 | 52 | storres | degree, \ |
720 | 52 | storres | lowerBound, \ |
721 | 52 | storres | upperBound, \ |
722 | 52 | storres | weight = None, \
|
723 | 52 | storres | quality = None):
|
724 | 52 | storres | """
|
725 | 52 | storres | All arguments are Sage/Python.
|
726 | 52 | storres | The functions (func and weight) must be passed as expressions or strings.
|
727 | 52 | storres | Otherwise the function fails.
|
728 | 83 | storres | The return value is a Sage polynomial.
|
729 | 52 | storres | """
|
730 | 83 | storres | var('zorglub') # Dummy variable name for type check only. Type of |
731 | 83 | storres | # zorglub is "symbolic expression".
|
732 | 52 | storres | polySo = pobyso_remez_canonical_sa_so(func, \ |
733 | 52 | storres | degree, \ |
734 | 52 | storres | lowerBound, \ |
735 | 52 | storres | upperBound, \ |
736 | 52 | storres | weight = None, \
|
737 | 52 | storres | quality = None)
|
738 | 83 | storres | # String test
|
739 | 52 | storres | if parent(func) == parent("string"): |
740 | 52 | storres | functionSa = eval(func)
|
741 | 52 | storres | # Expression test.
|
742 | 52 | storres | elif type(func) == type(zorglub): |
743 | 52 | storres | functionSa = func |
744 | 83 | storres | else:
|
745 | 83 | storres | return None |
746 | 83 | storres | #
|
747 | 52 | storres | maxPrecision = 0
|
748 | 52 | storres | if polySo is None: |
749 | 52 | storres | return(None) |
750 | 52 | storres | maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
751 | 52 | storres | RRRR = RealField(maxPrecision) |
752 | 52 | storres | polynomialRing = RRRR[functionSa.variables()[0]]
|
753 | 52 | storres | expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR) |
754 | 52 | storres | polySa = polynomial(expSa, polynomialRing) |
755 | 83 | storres | sollya_lib_clear_obj(polySo) |
756 | 52 | storres | return(polySa)
|
757 | 52 | storres | |
758 | 38 | storres | def pobyso_remez_canonical(func, \ |
759 | 5 | storres | degree, \ |
760 | 5 | storres | lowerBound, \ |
761 | 5 | storres | upperBound, \ |
762 | 38 | storres | weight = "1", \
|
763 | 5 | storres | quality = None):
|
764 | 38 | storres | """ Legacy function. See pobyso_remez_canonical_sa_so. """
|
765 | 51 | storres | return(pobyso_remez_canonical_sa_so(func, \
|
766 | 51 | storres | degree, \ |
767 | 51 | storres | lowerBound, \ |
768 | 51 | storres | upperBound, \ |
769 | 51 | storres | weight, \ |
770 | 51 | storres | quality)) |
771 | 38 | storres | def pobyso_remez_canonical_sa_so(func, \ |
772 | 38 | storres | degree, \ |
773 | 38 | storres | lowerBound, \ |
774 | 38 | storres | upperBound, \ |
775 | 52 | storres | weight = None, \
|
776 | 38 | storres | quality = None):
|
777 | 38 | storres | """
|
778 | 38 | storres | All arguments are Sage/Python.
|
779 | 51 | storres | The functions (func and weight) must be passed as expressions or strings.
|
780 | 51 | storres | Otherwise the function fails.
|
781 | 38 | storres | The return value is a pointer to a Sollya function.
|
782 | 38 | storres | """
|
783 | 83 | storres | var('zorglub') # Dummy variable name for type check only. Type of |
784 | 83 | storres | # zorglub is "symbolic expression".
|
785 | 52 | storres | currentVariableName = None
|
786 | 52 | storres | # The func argument can be of different types (string,
|
787 | 52 | storres | # symbolic expression...)
|
788 | 38 | storres | if parent(func) == parent("string"): |
789 | 38 | storres | functionSo = sollya_lib_parse_string(func) |
790 | 51 | storres | # Expression test.
|
791 | 52 | storres | elif type(func) == type(zorglub): |
792 | 52 | storres | # Until we are able to translate Sage expressions into Sollya
|
793 | 52 | storres | # expressions : parse the string version.
|
794 | 52 | storres | currentVariableName = func.variables()[0]
|
795 | 52 | storres | sollya_lib_name_free_variable(str(currentVariableName))
|
796 | 52 | storres | functionSo = sollya_lib_parse_string(func._assume_str()) |
797 | 38 | storres | else:
|
798 | 38 | storres | return(None) |
799 | 52 | storres | if weight is None: |
800 | 52 | storres | weightSo = pobyso_constant_1_sa_so() |
801 | 52 | storres | elif parent(weight) == parent("string"): |
802 | 51 | storres | weightSo = sollya_lib_parse_string(func) |
803 | 51 | storres | elif type(weight) == type(zorglub): |
804 | 51 | storres | functionSo = sollya_lib_parse_string_sa_so(weight._assume_str()) |
805 | 51 | storres | else:
|
806 | 51 | storres | return(None) |
807 | 5 | storres | degreeSo = pobyso_constant_from_int(degree) |
808 | 38 | storres | rangeSo = pobyso_range_sa_so(lowerBound, upperBound) |
809 | 38 | storres | if not quality is None: |
810 | 38 | storres | qualitySo= pobyso_constant_sa_so(quality) |
811 | 52 | storres | else:
|
812 | 52 | storres | qualitySo = None
|
813 | 83 | storres | |
814 | 83 | storres | remezPolySo = sollya_lib_remez(functionSo, \ |
815 | 83 | storres | degreeSo, \ |
816 | 83 | storres | rangeSo, \ |
817 | 83 | storres | weightSo, \ |
818 | 83 | storres | qualitySo, \ |
819 | 83 | storres | None)
|
820 | 83 | storres | sollya_lib_clear_obj(functionSo) |
821 | 83 | storres | sollya_lib_clear_obj(degreeSo) |
822 | 83 | storres | sollya_lib_clear_obj(rangeSo) |
823 | 83 | storres | sollya_lib_clear_obj(weightSo) |
824 | 83 | storres | if not qualitySo is None: |
825 | 83 | storres | sollya_lib_clear_obj(qualtiySo) |
826 | 83 | storres | return(remezPolySo)
|
827 | 83 | storres | # End pobyso_remez_canonical_sa_so
|
828 | 83 | storres | |
829 | 38 | storres | def pobyso_remez_canonical_so_so(funcSo, \ |
830 | 38 | storres | degreeSo, \ |
831 | 38 | storres | rangeSo, \ |
832 | 52 | storres | weightSo = pobyso_constant_1_sa_so(),\ |
833 | 38 | storres | qualitySo = None):
|
834 | 38 | storres | """
|
835 | 38 | storres | All arguments are pointers to Sollya objects.
|
836 | 38 | storres | The return value is a pointer to a Sollya function.
|
837 | 38 | storres | """
|
838 | 38 | storres | if not sollya_lib_obj_is_function(funcSo): |
839 | 38 | storres | return(None) |
840 | 38 | storres | return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None)) |
841 | 38 | storres | |
842 | 5 | storres | def pobyso_set_canonical_off(): |
843 | 5 | storres | sollya_lib_set_canonical(sollya_lib_off()) |
844 | 5 | storres | |
845 | 5 | storres | def pobyso_set_canonical_on(): |
846 | 5 | storres | sollya_lib_set_canonical(sollya_lib_on()) |
847 | 5 | storres | |
848 | 5 | storres | def pobyso_set_prec(p): |
849 | 38 | storres | """ Legacy function. See pobyso_set_prec_sa_so. """
|
850 | 38 | storres | return( pobyso_set_prec_sa_so(p))
|
851 | 38 | storres | |
852 | 38 | storres | def pobyso_set_prec_sa_so(p): |
853 | 5 | storres | a = c_int(p) |
854 | 5 | storres | precSo = c_void_p(sollya_lib_constant_from_int(a)) |
855 | 5 | storres | sollya_lib_set_prec(precSo) |
856 | 5 | storres | |
857 | 54 | storres | def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo, accuracySo): |
858 | 54 | storres | return(sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
|
859 | 54 | storres | accuracySo)) |
860 | 54 | storres | |
861 | 58 | storres | def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, rangeSo, \ |
862 | 58 | storres | errorTypeSo, \ |
863 | 58 | storres | sollyaPrecSo=None):
|
864 | 58 | storres | """
|
865 | 58 | storres | Compute the Taylor expansion with the variable change
|
866 | 58 | storres | x -> (x-intervalCenter) included.
|
867 | 58 | storres | """
|
868 | 58 | storres | # No global change of the working precision.
|
869 | 58 | storres | if not sollyaPrecSo is None: |
870 | 58 | storres | initialPrecSo = sollya_lib_get_prec(None)
|
871 | 58 | storres | sollya_lib_set_prec(sollyaPrecSo) |
872 | 58 | storres | #
|
873 | 58 | storres | intervalCenterSo = sollya_lib_mid(rangeSo) |
874 | 58 | storres | taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \ |
875 | 58 | storres | intervalCenterSo, \ |
876 | 58 | storres | rangeSo, errorTypeSo, None)
|
877 | 58 | storres | (taylorFormListSo, numElements, isEndElliptic) = \ |
878 | 58 | storres | pobyso_get_list_elements_so_so(taylorFormSo) |
879 | 58 | storres | polySo = taylorFormListSo[0]
|
880 | 58 | storres | errorRangeSo = taylorFormListSo[2]
|
881 | 58 | storres | maxErrorSo = sollya_lib_sup(errorRangeSo) |
882 | 58 | storres | changeVarExpSo = sollya_lib_build_function_sub(\ |
883 | 58 | storres | sollya_lib_build_function_free_variable(),\ |
884 | 58 | storres | sollya_lib_copy_obj(intervalCenterSo)) |
885 | 58 | storres | polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo) |
886 | 58 | storres | # If changed, reset the Sollya working precision.
|
887 | 58 | storres | if not sollyaPrecSo is None: |
888 | 58 | storres | sollya_lib_set_prec(initialPrecSo) |
889 | 83 | storres | sollya_lib_clear_obj(initialPrecSo) |
890 | 63 | storres | return((polyVarChangedSo, intervalCenterSo, maxErrorSo))
|
891 | 58 | storres | # end pobyso_taylor_expansion_with_change_var_so_so
|
892 | 58 | storres | |
893 | 56 | storres | def pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, rangeSo, \ |
894 | 56 | storres | errorTypeSo, \ |
895 | 56 | storres | sollyaPrecSo=None):
|
896 | 58 | storres | """
|
897 | 58 | storres | Compute the Taylor expansion without the variable change
|
898 | 58 | storres | x -> x-intervalCenter.
|
899 | 58 | storres | """
|
900 | 56 | storres | # No global change of the working precision.
|
901 | 56 | storres | if not sollyaPrecSo is None: |
902 | 56 | storres | initialPrecSo = sollya_lib_get_prec(None)
|
903 | 56 | storres | sollya_lib_set_prec(sollyaPrecSo) |
904 | 56 | storres | #
|
905 | 56 | storres | intervalCenterSo = sollya_lib_mid(rangeSo) |
906 | 56 | storres | taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \ |
907 | 56 | storres | intervalCenterSo, \ |
908 | 56 | storres | rangeSo, errorTypeSo, None)
|
909 | 56 | storres | (taylorFormListSo, numElements, isEndElliptic) = \ |
910 | 56 | storres | pobyso_get_list_elements_so_so(taylorFormSo) |
911 | 56 | storres | polySo = taylorFormListSo[0]
|
912 | 56 | storres | errorRangeSo = taylorFormListSo[2]
|
913 | 56 | storres | maxErrorSo = sollya_lib_sup(errorRangeSo) |
914 | 56 | storres | # If changed, reset the Sollya working precision.
|
915 | 56 | storres | if not sollyaPrecSo is None: |
916 | 56 | storres | sollya_lib_set_prec(initialPrecSo) |
917 | 63 | storres | sollya_lib_clear_obj(initialPrecSo) |
918 | 63 | storres | return((polySo, intervalCenterSo, maxErrorSo))
|
919 | 56 | storres | # end pobyso_taylor_expansion_no_change_var_so_so
|
920 | 56 | storres | |
921 | 5 | storres | def pobyso_taylor(function, degree, point): |
922 | 38 | storres | """ Legacy function. See pobysoTaylor_so_so. """
|
923 | 38 | storres | return(pobyso_taylor_so_so(function, degree, point))
|
924 | 38 | storres | |
925 | 56 | storres | def pobyso_taylor_so_so(functionSo, degreeSo, pointSo): |
926 | 56 | storres | return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
|
927 | 5 | storres | |
928 | 5 | storres | def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None): |
929 | 38 | storres | """ Legacy function. See ;"""
|
930 | 38 | storres | |
931 | 38 | storres | def pobyso_taylorform_sa_sa(functionSa, \ |
932 | 38 | storres | degree, \ |
933 | 43 | storres | point, \ |
934 | 43 | storres | precision, \ |
935 | 54 | storres | interval=None, \
|
936 | 38 | storres | errorType=None):
|
937 | 37 | storres | """
|
938 | 38 | storres | Compute the Taylor form of 'degree' for 'functionSa' at 'point'
|
939 | 37 | storres | for 'interval' with 'errorType'.
|
940 | 37 | storres | point: must be a Real or a Real interval.
|
941 | 37 | storres | return the Taylor form as an array
|
942 | 83 | storres | TODO: take care of the interval and of the point when it is an interval;
|
943 | 38 | storres | when errorType is not None;
|
944 | 83 | storres | take care of the other elements of the Taylor form (coefficients
|
945 | 83 | storres | errors and delta.
|
946 | 37 | storres | """
|
947 | 37 | storres | # Absolute as the default error.
|
948 | 5 | storres | if errorType is None: |
949 | 37 | storres | errorTypeSo = sollya_lib_absolute() |
950 | 37 | storres | else:
|
951 | 37 | storres | #TODO: deal with the other case.
|
952 | 37 | storres | pass
|
953 | 38 | storres | varSa = functionSa.variables()[0]
|
954 | 37 | storres | pointBaseRingString = str(point.base_ring())
|
955 | 37 | storres | if not re.search('Real', pointBaseRingString): |
956 | 37 | storres | return None |
957 | 37 | storres | # Call Sollya but first "sollyafy" the arguments.
|
958 | 38 | storres | pobyso_name_free_variable_sa_so(str(varSa))
|
959 | 38 | storres | #pobyso_set_prec_sa_so(300)
|
960 | 37 | storres | # Sollyafy the function.
|
961 | 38 | storres | functionSo = pobyso_parse_string_sa_so(functionSa._assume_str()) |
962 | 37 | storres | if sollya_lib_obj_is_error(functionSo):
|
963 | 37 | storres | print "pobyso_tailorform: function string can't be parsed!" |
964 | 37 | storres | return None |
965 | 37 | storres | # Sollyafy the degree
|
966 | 37 | storres | degreeSo = sollya_lib_constant_from_int(int(degree))
|
967 | 37 | storres | # Sollyafy the point
|
968 | 37 | storres | if not re.search('Interval', pointBaseRingString): |
969 | 38 | storres | pointSo = pobyso_constant_sa_so(point) |
970 | 37 | storres | else:
|
971 | 37 | storres | # TODO: deal with the interval case.
|
972 | 37 | storres | pass
|
973 | 37 | storres | # Call Sollya
|
974 | 83 | storres | taylorFormSo = \ |
975 | 83 | storres | sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\ |
976 | 37 | storres | None)
|
977 | 38 | storres | (tfsAsList, numElements, isEndElliptic) = \ |
978 | 38 | storres | pobyso_get_list_elements_so_so(taylorFormSo) |
979 | 37 | storres | polySo = tfsAsList[0]
|
980 | 38 | storres | maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo) |
981 | 37 | storres | polyRealField = RealField(maxPrecision) |
982 | 38 | storres | expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField) |
983 | 37 | storres | sollya_lib_close() |
984 | 37 | storres | polynomialRing = polyRealField[str(varSa)]
|
985 | 37 | storres | polySa = polynomial(expSa, polynomialRing) |
986 | 37 | storres | taylorFormSa = [polySa] |
987 | 51 | storres | return(taylorFormSa)
|
988 | 51 | storres | # End pobyso_taylor_form_sa_sa
|
989 | 54 | storres | |
990 | 54 | storres | def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \ |
991 | 54 | storres | errorTypeSo=None):
|
992 | 54 | storres | createdErrorType = False
|
993 | 51 | storres | if errorTypeSo is None: |
994 | 51 | storres | errorTypeSo = sollya_lib_absolute() |
995 | 54 | storres | createdErrorType = True
|
996 | 51 | storres | else:
|
997 | 51 | storres | #TODO: deal with the other case.
|
998 | 51 | storres | pass
|
999 | 51 | storres | if intervalSo is None: |
1000 | 54 | storres | resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \ |
1001 | 54 | storres | errorTypeSo, None)
|
1002 | 51 | storres | else:
|
1003 | 54 | storres | resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \ |
1004 | 54 | storres | intervalSo, errorTypeSo, None)
|
1005 | 54 | storres | if createdErrorType:
|
1006 | 54 | storres | sollya_lib_clear_obj(errorTypeSo) |
1007 | 51 | storres | return(resultSo)
|
1008 | 51 | storres | |
1009 | 37 | storres | |
1010 | 37 | storres | def pobyso_univar_polynomial_print_reverse(polySa): |
1011 | 51 | storres | """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
|
1012 | 51 | storres | return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
|
1013 | 38 | storres | |
1014 | 51 | storres | def pobyso_univar_polynomial_print_reverse_sa_sa(polySa): |
1015 | 37 | storres | """
|
1016 | 37 | storres | Return the string representation of a univariate polynomial with
|
1017 | 38 | storres | monomials ordered in the x^0..x^n order of the monomials.
|
1018 | 37 | storres | Remember: Sage
|
1019 | 37 | storres | """
|
1020 | 37 | storres | polynomialRing = polySa.base_ring() |
1021 | 37 | storres | # A very expensive solution:
|
1022 | 37 | storres | # -create a fake multivariate polynomial field with only one variable,
|
1023 | 37 | storres | # specifying a negative lexicographical order;
|
1024 | 37 | storres | mpolynomialRing = PolynomialRing(polynomialRing.base(), \ |
1025 | 37 | storres | polynomialRing.variable_name(), \ |
1026 | 37 | storres | 1, order='neglex') |
1027 | 37 | storres | # - convert the univariate argument polynomial into a multivariate
|
1028 | 37 | storres | # version;
|
1029 | 37 | storres | p = mpolynomialRing(polySa) |
1030 | 37 | storres | # - return the string representation of the converted form.
|
1031 | 37 | storres | # There is no simple str() method defined for p's class.
|
1032 | 37 | storres | return(p.__str__())
|
1033 | 5 | storres | #
|
1034 | 5 | storres | print pobyso_get_prec()
|
1035 | 5 | storres | pobyso_set_prec(165)
|
1036 | 5 | storres | print pobyso_get_prec()
|
1037 | 5 | storres | a=100
|
1038 | 5 | storres | print type(a) |
1039 | 5 | storres | id(a)
|
1040 | 5 | storres | print "Max arity: ", pobyso_max_arity |
1041 | 5 | storres | print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43) |
1042 | 56 | storres | print "Function None (44) as a string: ", pobyso_function_type_as_string(44) |
1043 | 56 | storres | print "...Pobyso check done" |