Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 55

Historique | Voir | Annoter | Télécharger (30,53 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 38 storres
- the _so_so (arguments and returned objects are pointers to Sollya objects, includes
10 38 storres
  the void function and the no arguments function that return a pointer to a Sollya
11 38 storres
  object);
12 38 storres
- the _so_sa (argument are pointers to Sollya objects, returned objects are
13 38 storres
  Sage/Python objects or, more generally, information is transfered from the Sollya
14 38 storres
  world to Sage/Python world);
15 38 storres
- the _sa_so (arguments are Sage/Python objects, returned objects are
16 38 storres
  pointers to Sollya objects);
17 38 storres
- the sa_sa (arguments and returned objects are all Sage/Python objects);
18 51 storres
- a catch all flavor, without any suffix, (e. g. functions that have no argument
19 51 storres
  nor return value).
20 5 storres
NOTES:
21 5 storres
Reported errors in Eclipse come from the calls to
22 5 storres
the Sollya library
23 5 storres

24 10 storres
ToDo (among other things):
25 10 storres
 -memory management.
26 5 storres
"""
27 5 storres
from ctypes import *
28 37 storres
import re
29 37 storres
from sage.symbolic.expression_conversions import polynomial
30 38 storres
"""
31 38 storres
Create the equivalent to an enum for the Sollya function types.
32 38 storres
"""
33 5 storres
(SOLLYA_BASE_FUNC_ABS,
34 5 storres
SOLLYA_BASE_FUNC_ACOS,
35 5 storres
    SOLLYA_BASE_FUNC_ACOSH,
36 5 storres
    SOLLYA_BASE_FUNC_ADD,
37 5 storres
    SOLLYA_BASE_FUNC_ASIN,
38 5 storres
    SOLLYA_BASE_FUNC_ASINH,
39 5 storres
    SOLLYA_BASE_FUNC_ATAN,
40 5 storres
    SOLLYA_BASE_FUNC_ATANH,
41 5 storres
    SOLLYA_BASE_FUNC_CEIL,
42 5 storres
    SOLLYA_BASE_FUNC_CONSTANT,
43 5 storres
    SOLLYA_BASE_FUNC_COS,
44 5 storres
    SOLLYA_BASE_FUNC_COSH,
45 5 storres
    SOLLYA_BASE_FUNC_DIV,
46 5 storres
    SOLLYA_BASE_FUNC_DOUBLE,
47 5 storres
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
48 5 storres
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
49 5 storres
    SOLLYA_BASE_FUNC_ERF,
50 5 storres
    SOLLYA_BASE_FUNC_ERFC,
51 5 storres
    SOLLYA_BASE_FUNC_EXP,
52 5 storres
    SOLLYA_BASE_FUNC_EXP_M1,
53 5 storres
    SOLLYA_BASE_FUNC_FLOOR,
54 5 storres
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
55 5 storres
    SOLLYA_BASE_FUNC_HALFPRECISION,
56 5 storres
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
57 5 storres
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
58 5 storres
    SOLLYA_BASE_FUNC_LOG,
59 5 storres
    SOLLYA_BASE_FUNC_LOG_10,
60 5 storres
    SOLLYA_BASE_FUNC_LOG_1P,
61 5 storres
    SOLLYA_BASE_FUNC_LOG_2,
62 5 storres
    SOLLYA_BASE_FUNC_MUL,
63 5 storres
    SOLLYA_BASE_FUNC_NEARESTINT,
64 5 storres
    SOLLYA_BASE_FUNC_NEG,
65 5 storres
    SOLLYA_BASE_FUNC_PI,
66 5 storres
    SOLLYA_BASE_FUNC_POW,
67 5 storres
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
68 5 storres
    SOLLYA_BASE_FUNC_QUAD,
69 5 storres
    SOLLYA_BASE_FUNC_SIN,
70 5 storres
    SOLLYA_BASE_FUNC_SINGLE,
71 5 storres
    SOLLYA_BASE_FUNC_SINH,
72 5 storres
    SOLLYA_BASE_FUNC_SQRT,
73 5 storres
    SOLLYA_BASE_FUNC_SUB,
74 5 storres
    SOLLYA_BASE_FUNC_TAN,
75 5 storres
    SOLLYA_BASE_FUNC_TANH,
76 5 storres
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
77 5 storres
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
78 5 storres
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
79 5 storres
80 5 storres
pobyso_max_arity = 9
81 5 storres
82 5 storres
def pobyso_autoprint(arg):
83 5 storres
    sollya_lib_autoprint(arg,None)
84 5 storres
85 38 storres
def pobyso_autoprint_so_so(arg):
86 38 storres
    sollya_lib_autoprint(arg,None)
87 54 storres
88 54 storres
def pobyso_absolute_so_so():
89 54 storres
    return(sollya_lib_absolute(None))
90 38 storres
91 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
92 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
93 54 storres
94 5 storres
def pobyso_cmp(rnArg, soCte):
95 54 storres
    """
96 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
97 54 storres

98 54 storres
    Get the value of the Sollya constant into a RealNumber and compare
99 54 storres
    using MPFR. Could be optimized by working directly with a mpfr_t
100 54 storres
    for the intermediate number.
101 54 storres
    """
102 5 storres
    precisionOfCte = c_int(0)
103 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
104 5 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, soCte)
105 5 storres
    #print "Precision of constant: ", precisionOfCte
106 5 storres
    RRRR = RealField(precisionOfCte.value)
107 5 storres
    rnLocal = RRRR(0)
108 5 storres
    sollya_lib_get_constant(get_rn_value(rnLocal), soCte)
109 5 storres
    #print "rnDummy: ", rnDummy
110 5 storres
    # Compare the local Sage RealNumber with rnArg.
111 5 storres
    return(cmp_rn_value(rnArg, rnLocal))
112 5 storres
113 55 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
114 55 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
115 55 storres
116 55 storres
117 55 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
118 55 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
119 55 storres
    return(resultSo)
120 55 storres
121 55 storres
122 55 storres
123 54 storres
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
124 54 storres
                                                     upperBoundSa):
125 54 storres
    """
126 54 storres
    TODO: set the variable name in Sollya.
127 54 storres
    """
128 54 storres
    funcSo = pobyso_parse_string(funcSa._assume_str())
129 54 storres
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
130 54 storres
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
131 54 storres
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
132 54 storres
    # Get the top bound and compute the binade top limit.
133 54 storres
    fMaxUpperBoundSa = fMaxSa.upper()
134 54 storres
    binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
135 54 storres
    # Put up together the function to use to compute the lower bound.
136 54 storres
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
137 54 storres
                                    '-(' + f._assume_str() + ')')
138 54 storres
    pobyso_autoprint(funcAuxSo)
139 54 storres
    # Clear the Sollay range before a new call to infnorm and issue the call.
140 54 storres
    sollya_lib_clear_obj(infnormSo)
141 54 storres
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
142 54 storres
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
143 54 storres
    sollya_lib_clear_obj(infnormSo)
144 54 storres
    fMinLowerBoundSa = topBinadeLimit - fMinSa.lower()
145 54 storres
    # Compute the maximum of the precisions of the different bounds.
146 54 storres
    maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
147 54 storres
                     fMaxUpperBoundSa.parent().precision()])
148 54 storres
    # Create a RealIntervalField and create an interval with the "good" bounds.
149 54 storres
    RRRI = RealIntervalField(maxPrecSa)
150 54 storres
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
151 54 storres
    # Free the uneeded Sollya objects
152 54 storres
    sollya_lib_clear_obj(funcSo)
153 54 storres
    sollya_lib_clear_obj(funcAuxSo)
154 54 storres
    sollya_lib_clear_obj(rangeSo)
155 54 storres
    return(imageIntervalSa)
156 54 storres
    # End pobyso_compute_function_abs_val_bounds_sa_sa
157 54 storres
158 5 storres
def pobyso_constant(rnArg):
159 38 storres
    """ Legacy function. See pobyso_constant_sa_so. """
160 38 storres
    return(pobyso_constant_sa_so(rnArg))
161 5 storres
162 38 storres
def pobyso_constant_sa_so(rnArg):
163 52 storres
    """
164 52 storres
    Create a Sollya constant from a RealNumber.
165 52 storres
    """
166 38 storres
    return(sollya_lib_constant(get_rn_value(rnArg)))
167 38 storres
168 55 storres
def pobyso_constant_0_sa_so():
169 55 storres
    return(pobyso_constant_from_int_sa_so(0))
170 55 storres
171 5 storres
def pobyso_constant_1():
172 38 storres
    """ Legacy function. See pobyso_constant_so_so. """
173 52 storres
    return(pobyso_constant_1_sa_so())
174 5 storres
175 52 storres
def pobyso_constant_1_sa_so():
176 38 storres
    return(pobyso_constant_from_int_sa_so(1))
177 38 storres
178 5 storres
def pobyso_constant_from_int(anInt):
179 38 storres
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
180 38 storres
    return(pobyso_constant_from_int_sa_so(anInt))
181 38 storres
182 38 storres
def pobyso_constant_from_int_sa_so(anInt):
183 5 storres
    return(sollya_lib_constant_from_int(int(anInt)))
184 5 storres
185 5 storres
def pobyso_function_type_as_string(funcType):
186 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
187 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
188 38 storres
189 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
190 38 storres
    """
191 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
192 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
193 38 storres
    """
194 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
195 5 storres
        return "abs"
196 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
197 5 storres
        return "arccos"
198 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
199 5 storres
        return "arccosh"
200 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
201 5 storres
        return "+"
202 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
203 5 storres
        return "arcsin"
204 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
205 5 storres
        return "arcsinh"
206 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
207 5 storres
        return "arctan"
208 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
209 5 storres
        return "arctanh"
210 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
211 5 storres
        return "ceil"
212 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
213 5 storres
        return "cte"
214 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
215 5 storres
        return "cos"
216 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
217 5 storres
        return "cosh"
218 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
219 5 storres
        return "/"
220 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
221 5 storres
        return "double"
222 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
223 5 storres
        return "doubleDouble"
224 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
225 5 storres
        return "doubleDxtended"
226 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
227 5 storres
        return "erf"
228 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
229 5 storres
        return "erfc"
230 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
231 5 storres
        return "exp"
232 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
233 5 storres
        return "expm1"
234 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
235 5 storres
        return "floor"
236 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
237 5 storres
        return "freeVariable"
238 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
239 5 storres
        return "halfPrecision"
240 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
241 5 storres
        return "libraryConstant"
242 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
243 5 storres
        return "libraryFunction"
244 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
245 5 storres
        return "log"
246 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
247 5 storres
        return "log10"
248 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
249 5 storres
        return "log1p"
250 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
251 5 storres
        return "log2"
252 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
253 5 storres
        return "*"
254 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
255 5 storres
        return "round"
256 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
257 5 storres
        return "__neg__"
258 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
259 5 storres
        return "pi"
260 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
261 5 storres
        return "^"
262 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
263 5 storres
        return "procedureFunction"
264 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
265 5 storres
        return "quad"
266 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
267 5 storres
        return "sin"
268 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
269 5 storres
        return "single"
270 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
271 5 storres
        return "sinh"
272 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
273 5 storres
        return "sqrt"
274 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
275 5 storres
        return "-"
276 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
277 5 storres
        return "tan"
278 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
279 5 storres
        return "tanh"
280 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
281 5 storres
        return "tripleDouble"
282 5 storres
    else:
283 5 storres
        return None
284 5 storres
285 5 storres
def pobyso_get_constant(rnArg, soConst):
286 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
287 53 storres
    return(pobyso_get_constant_so_sa(rnArg, soConst))
288 38 storres
289 38 storres
def pobyso_get_constant_so_sa(rnArg, soConst):
290 52 storres
    """
291 52 storres
    Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
292 52 storres
    rnArg must already exist and belong to some RealField.
293 52 storres
    We assume that soConst points to a Sollya constant.
294 52 storres
    """
295 53 storres
    return(sollya_lib_get_constant(get_rn_value(rnArg), soConst))
296 5 storres
297 5 storres
def pobyso_get_constant_as_rn(ctExp):
298 38 storres
    """ Legacy function. See pobyso_get_constant_as_rn_so_sa. """
299 38 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExp))
300 38 storres
301 52 storres
def pobyso_get_constant_as_rn_so_sa(constExp):
302 52 storres
    precision  = pobyso_get_prec_of_constant(constExp)
303 5 storres
    RRRR = RealField(precision)
304 5 storres
    rn = RRRR(0)
305 52 storres
    sollya_lib_get_constant(get_rn_value(rn), constExp)
306 5 storres
    return(rn)
307 38 storres
308 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
309 53 storres
    """ Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa."""
310 38 storres
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
311 5 storres
312 38 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField):
313 5 storres
    rn = realField(0)
314 5 storres
    sollya_lib_get_constant(get_rn_value(rn), ctExp)
315 5 storres
    return(rn)
316 38 storres
317 5 storres
def pobyso_get_free_variable_name():
318 38 storres
    """ Legacy function. See pobyso_get_free_variable_name_so_sa."""
319 38 storres
    return(pobyso_get_free_variable_name_so_sa())
320 38 storres
321 38 storres
def pobyso_get_free_variable_name_so_sa():
322 5 storres
    return(sollya_lib_get_free_variable_name())
323 5 storres
324 38 storres
def pobyso_get_function_arity(expressionSo):
325 38 storres
    """ Legacy function. See pobyso_get_function_arity_so_sa."""
326 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
327 38 storres
328 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
329 5 storres
    arity = c_int(0)
330 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
331 5 storres
    return(int(arity.value))
332 5 storres
333 38 storres
def pobyso_get_head_function(expressionSo):
334 38 storres
    """ Legacy function. See pobyso_get_head_function_so_sa. """
335 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
336 38 storres
337 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
338 5 storres
    functionType = c_int(0)
339 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
340 5 storres
    return(int(functionType.value))
341 5 storres
342 53 storres
def pobyso_get_interval_from_range_so_sa(soRange):
343 53 storres
    """
344 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
345 53 storres
    The interval bounds are not rounded: they are elements of RealIntervalField
346 53 storres
    of the "right" precision.
347 53 storres
    """
348 53 storres
    prec = c_int(0)
349 53 storres
    retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
350 53 storres
    if retval == 0:
351 53 storres
        return(None)
352 53 storres
    RRRI = RealIntervalField(prec.value)
353 53 storres
    intervalSa = RRRI(0,0)
354 53 storres
    retval = \
355 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
356 53 storres
                                           soRange)
357 53 storres
    if retval == 0:
358 53 storres
        return(None)
359 53 storres
    return(intervalSa)
360 53 storres
361 5 storres
def pobyso_get_list_elements(soObj):
362 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
363 38 storres
    return(pobyso_get_list_elements_so_so(soObj))
364 38 storres
365 38 storres
def pobyso_get_list_elements_so_so(soObj):
366 51 storres
    """
367 51 storres
    Get the list elements as a Sage/Python array of Sollya objects.
368 51 storres
    The other data returned are also Sage/Python objects.
369 51 storres
    """
370 5 storres
    listAddress = POINTER(c_longlong)()
371 5 storres
    numElements = c_int(0)
372 5 storres
    isEndElliptic = c_int(0)
373 5 storres
    listAsList = []
374 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
375 54 storres
                                          byref(numElements),\
376 54 storres
                                          byref(isEndElliptic),\
377 54 storres
                                          soObj)
378 5 storres
    if result == 0 :
379 5 storres
        return None
380 5 storres
    for i in xrange(0, numElements.value, 1):
381 5 storres
        listAsList.append(listAddress[i])
382 5 storres
    return(listAsList, numElements.value, isEndElliptic.value)
383 5 storres
384 38 storres
def pobyso_get_max_prec_of_exp(soExp):
385 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
386 38 storres
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
387 5 storres
388 38 storres
def pobyso_get_max_prec_of_exp_so_sa(soExp):
389 38 storres
    """
390 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
391 52 storres

392 52 storres
    Arguments:
393 52 storres
    soExp -- a Sollya expression pointer
394 52 storres
    Return value:
395 52 storres
    A Python integer
396 38 storres
    TODO:
397 38 storres
    - error management;
398 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
399 38 storres
    """
400 5 storres
    maxPrecision = 0
401 52 storres
    minConstPrec = 0
402 52 storres
    currentConstPrec = 0
403 38 storres
    operator = pobyso_get_head_function_so_sa(soExp)
404 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
405 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
406 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp)
407 5 storres
        for i in xrange(arity):
408 5 storres
            maxPrecisionCandidate = \
409 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
410 5 storres
            if maxPrecisionCandidate > maxPrecision:
411 5 storres
                maxPrecision = maxPrecisionCandidate
412 5 storres
        return(maxPrecision)
413 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
414 52 storres
        minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
415 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
416 52 storres
        #print minConstPrec, " - ", currentConstPrec
417 52 storres
        return(pobyso_get_min_prec_of_constant_so_sa(soExp))
418 52 storres
419 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
420 5 storres
        return(0)
421 5 storres
    else:
422 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
423 5 storres
        return(0)
424 5 storres
425 52 storres
def pobyso_get_min_prec_of_constant_so_sa(soConstExp):
426 52 storres
    """
427 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
428 52 storres
    constant.
429 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
430 52 storres
    We assume that soCteExp is a point
431 52 storres
    """
432 52 storres
    constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp)
433 52 storres
    return(min_mpfr_size(get_rn_value(constExpAsRn)))
434 52 storres
435 5 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
436 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
437 38 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
438 38 storres
439 38 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR):
440 5 storres
    """
441 38 storres
    Get a Sage expression from a Sollya expression.
442 38 storres
    Currently only tested with polynomials with floating-point coefficients.
443 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
444 5 storres
    """
445 5 storres
    #pobyso_autoprint(sollyaExp)
446 55 storres
    operator = pobyso_get_head_function_so_sa(sollyaExp)
447 5 storres
    # Constants and the free variable are special cases.
448 5 storres
    # All other operator are dealt with in the same way.
449 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
450 5 storres
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
451 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp)
452 5 storres
        if arity == 1:
453 38 storres
            sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
454 52 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \
455 52 storres
            realField) + ")")
456 5 storres
        elif arity == 2:
457 5 storres
            if operator == SOLLYA_BASE_FUNC_POW:
458 5 storres
                operatorAsString = "**"
459 5 storres
            else:
460 52 storres
                operatorAsString = \
461 52 storres
                    pobyso_function_type_as_string_so_sa(operator)
462 5 storres
            sageExp = \
463 38 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\
464 5 storres
              + " " + operatorAsString + " " + \
465 38 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
466 5 storres
        # We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
467 5 storres
        else:
468 5 storres
            sageExp = eval('None')
469 5 storres
        return(sageExp)
470 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
471 5 storres
        #print "This is a constant"
472 38 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
473 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
474 5 storres
        #print "This is free variable"
475 5 storres
        return(eval(sollya_lib_get_free_variable_name()))
476 5 storres
    else:
477 5 storres
        print "Unexpected"
478 5 storres
        return eval('None')
479 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
480 5 storres
481 38 storres
def pobyso_get_subfunctions(expressionSo):
482 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
483 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
484 38 storres
485 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
486 38 storres
    """
487 38 storres
    Get the subfunctions of an expression.
488 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
489 55 storres
    S.T.: Could not figure out another way than that ugly list of declarations
490 55 storres
    to recover the addresses of the subfunctions.
491 38 storres
    """
492 5 storres
    subf0 = c_int(0)
493 5 storres
    subf1 = c_int(0)
494 5 storres
    subf2 = c_int(0)
495 5 storres
    subf3 = c_int(0)
496 5 storres
    subf4 = c_int(0)
497 5 storres
    subf5 = c_int(0)
498 5 storres
    subf6 = c_int(0)
499 5 storres
    subf7 = c_int(0)
500 5 storres
    subf8 = c_int(0)
501 5 storres
    arity = c_int(0)
502 5 storres
    nullPtr = POINTER(c_int)()
503 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
504 5 storres
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
505 5 storres
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
506 5 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
507 5 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
508 5 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
509 5 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
510 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
511 5 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
512 5 storres
    subs = []
513 5 storres
    if arity.value > pobyso_max_arity:
514 38 storres
        return(0,[])
515 5 storres
    for i in xrange(arity.value):
516 5 storres
        subs.append(int(subfunctions[i].value))
517 5 storres
        #print subs[i]
518 5 storres
    return(int(arity.value), subs)
519 5 storres
520 5 storres
def pobyso_get_prec():
521 38 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
522 38 storres
    return(pobyso_get_prec_so_sa())
523 38 storres
524 38 storres
def pobyso_get_prec_so_sa():
525 38 storres
    """
526 38 storres
    Get the current default precision in Sollya.
527 38 storres
    The return value is Sage/Python int.
528 38 storres
    """
529 5 storres
    retc = sollya_lib_get_prec(None)
530 5 storres
    a = c_int(0)
531 5 storres
    sollya_lib_get_constant_as_int(byref(a), retc)
532 5 storres
    return(int(a.value))
533 5 storres
534 38 storres
def pobyso_get_prec_of_constant(ctExpSo):
535 38 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
536 38 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
537 38 storres
538 53 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
539 5 storres
    prec = c_int(0)
540 53 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
541 5 storres
    return(int(prec.value))
542 5 storres
543 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
544 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
545 54 storres
    return(None)
546 53 storres
547 37 storres
def pobyso_lib_init():
548 37 storres
    sollya_lib_init(None)
549 37 storres
550 37 storres
def pobyso_name_free_variable(freeVariableName):
551 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
552 38 storres
    pobyso_name_free_variable_sa_so(freeVariableName)
553 38 storres
554 38 storres
def pobyso_name_free_variable_sa_so(freeVariableName):
555 37 storres
    sollya_lib_name_free_variable(freeVariableName)
556 37 storres
557 5 storres
def pobyso_parse_string(string):
558 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
559 38 storres
    return(pobyso_parse_string_sa_so(string))
560 38 storres
561 38 storres
def pobyso_parse_string_sa_so(string):
562 5 storres
    return(sollya_lib_parse_string(string))
563 5 storres
564 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
565 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
566 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
567 38 storres
568 38 storres
def pobyso_range_sa_so(rnLowerBound, rnUpperBound):
569 5 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound))
570 5 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound))
571 5 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
572 5 storres
    return(rangeSo)
573 5 storres
574 52 storres
def pobyso_remez_canonical_sa_sa(func, \
575 52 storres
                                 degree, \
576 52 storres
                                 lowerBound, \
577 52 storres
                                 upperBound, \
578 52 storres
                                 weight = None, \
579 52 storres
                                 quality = None):
580 52 storres
    """
581 52 storres
    All arguments are Sage/Python.
582 52 storres
    The functions (func and weight) must be passed as expressions or strings.
583 52 storres
    Otherwise the function fails.
584 52 storres
    The return value is a pointer is a Sage polynomial.
585 52 storres
    """
586 52 storres
    var('zorglub')    # Dummy variable name for type check only.
587 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
588 52 storres
                                 degree, \
589 52 storres
                                 lowerBound, \
590 52 storres
                                 upperBound, \
591 52 storres
                                 weight = None, \
592 52 storres
                                 quality = None)
593 52 storres
    if parent(func) == parent("string"):
594 52 storres
        functionSa = eval(func)
595 52 storres
    # Expression test.
596 52 storres
    elif type(func) == type(zorglub):
597 52 storres
        functionSa = func
598 52 storres
    maxPrecision = 0
599 52 storres
    if polySo is None:
600 52 storres
        return(None)
601 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
602 52 storres
    RRRR = RealField(maxPrecision)
603 52 storres
    polynomialRing = RRRR[functionSa.variables()[0]]
604 52 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR)
605 52 storres
    polySa = polynomial(expSa, polynomialRing)
606 52 storres
    return(polySa)
607 52 storres
608 38 storres
def pobyso_remez_canonical(func, \
609 5 storres
                           degree, \
610 5 storres
                           lowerBound, \
611 5 storres
                           upperBound, \
612 38 storres
                           weight = "1", \
613 5 storres
                           quality = None):
614 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
615 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
616 51 storres
                                        degree, \
617 51 storres
                                        lowerBound, \
618 51 storres
                                        upperBound, \
619 51 storres
                                        weight, \
620 51 storres
                                        quality))
621 38 storres
def pobyso_remez_canonical_sa_so(func, \
622 38 storres
                                 degree, \
623 38 storres
                                 lowerBound, \
624 38 storres
                                 upperBound, \
625 52 storres
                                 weight = None, \
626 38 storres
                                 quality = None):
627 38 storres
    """
628 38 storres
    All arguments are Sage/Python.
629 51 storres
    The functions (func and weight) must be passed as expressions or strings.
630 51 storres
    Otherwise the function fails.
631 38 storres
    The return value is a pointer to a Sollya function.
632 38 storres
    """
633 52 storres
    var('zorglub')    # Dummy variable name for type check only.
634 52 storres
    currentVariableName = None
635 52 storres
    # The func argument can be of different types (string,
636 52 storres
    # symbolic expression...)
637 38 storres
    if parent(func) == parent("string"):
638 38 storres
        functionSo = sollya_lib_parse_string(func)
639 51 storres
    # Expression test.
640 52 storres
    elif type(func) == type(zorglub):
641 52 storres
        # Until we are able to translate Sage expressions into Sollya
642 52 storres
        # expressions : parse the string version.
643 52 storres
        currentVariableName = func.variables()[0]
644 52 storres
        sollya_lib_name_free_variable(str(currentVariableName))
645 52 storres
        functionSo = sollya_lib_parse_string(func._assume_str())
646 38 storres
    else:
647 38 storres
        return(None)
648 52 storres
    if weight is None:
649 52 storres
        weightSo = pobyso_constant_1_sa_so()
650 52 storres
    elif parent(weight) == parent("string"):
651 51 storres
        weightSo = sollya_lib_parse_string(func)
652 51 storres
    elif type(weight) == type(zorglub):
653 51 storres
        functionSo = sollya_lib_parse_string_sa_so(weight._assume_str())
654 51 storres
    else:
655 51 storres
        return(None)
656 5 storres
    degreeSo = pobyso_constant_from_int(degree)
657 38 storres
    rangeSo = pobyso_range_sa_so(lowerBound, upperBound)
658 38 storres
    if not quality is None:
659 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
660 52 storres
    else:
661 52 storres
        qualitySo = None
662 52 storres
    return(sollya_lib_remez(functionSo, \
663 52 storres
                            degreeSo, \
664 52 storres
                            rangeSo, \
665 52 storres
                            weightSo, \
666 52 storres
                            qualitySo, \
667 52 storres
                            None))
668 5 storres
669 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
670 38 storres
                                 degreeSo, \
671 38 storres
                                 rangeSo, \
672 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
673 38 storres
                                 qualitySo = None):
674 38 storres
    """
675 38 storres
    All arguments are pointers to Sollya objects.
676 38 storres
    The return value is a pointer to a Sollya function.
677 38 storres
    """
678 38 storres
    if not sollya_lib_obj_is_function(funcSo):
679 38 storres
        return(None)
680 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
681 38 storres
682 5 storres
def pobyso_set_canonical_off():
683 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
684 5 storres
685 5 storres
def pobyso_set_canonical_on():
686 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
687 5 storres
688 5 storres
def pobyso_set_prec(p):
689 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
690 38 storres
    return( pobyso_set_prec_sa_so(p))
691 38 storres
692 38 storres
def pobyso_set_prec_sa_so(p):
693 5 storres
    a = c_int(p)
694 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
695 5 storres
    sollya_lib_set_prec(precSo)
696 5 storres
697 54 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo, accuracySo):
698 54 storres
    return(sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
699 54 storres
                              accuracySo))
700 54 storres
701 5 storres
def pobyso_taylor(function, degree, point):
702 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
703 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
704 38 storres
705 38 storres
def pobyso_taylor_so_so(function, degree, point):
706 5 storres
    return(sollya_lib_taylor(function, degree, point))
707 5 storres
708 5 storres
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
709 38 storres
    """ Legacy function. See ;"""
710 38 storres
711 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
712 38 storres
                            degree, \
713 43 storres
                            point, \
714 43 storres
                            precision, \
715 54 storres
                            interval=None, \
716 38 storres
                            errorType=None):
717 37 storres
    """
718 38 storres
    Compute the Taylor form of 'degree' for 'functionSa' at 'point'
719 37 storres
    for 'interval' with 'errorType'.
720 37 storres
    point: must be a Real or a Real interval.
721 37 storres
    return the Taylor form as an array
722 38 storres
    TODO: take care of the interval and of point when it is an interval;
723 38 storres
          when errorType is not None;
724 38 storres
          take care of the other elements of the Taylor form (coefficients errors and
725 38 storres
          delta.
726 37 storres
    """
727 37 storres
    # Absolute as the default error.
728 5 storres
    if errorType is None:
729 37 storres
        errorTypeSo = sollya_lib_absolute()
730 37 storres
    else:
731 37 storres
        #TODO: deal with the other case.
732 37 storres
        pass
733 38 storres
    varSa = functionSa.variables()[0]
734 37 storres
    pointBaseRingString = str(point.base_ring())
735 37 storres
    if not re.search('Real', pointBaseRingString):
736 37 storres
        return None
737 37 storres
    # Call Sollya but first "sollyafy" the arguments.
738 38 storres
    pobyso_name_free_variable_sa_so(str(varSa))
739 38 storres
    #pobyso_set_prec_sa_so(300)
740 37 storres
    # Sollyafy the function.
741 38 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
742 37 storres
    if sollya_lib_obj_is_error(functionSo):
743 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
744 37 storres
        return None
745 37 storres
    # Sollyafy the degree
746 37 storres
    degreeSo = sollya_lib_constant_from_int(int(degree))
747 37 storres
    # Sollyafy the point
748 37 storres
    if not re.search('Interval', pointBaseRingString):
749 38 storres
        pointSo  = pobyso_constant_sa_so(point)
750 37 storres
    else:
751 37 storres
        # TODO: deal with the interval case.
752 37 storres
        pass
753 37 storres
    # Call Sollya
754 37 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
755 37 storres
                                         None)
756 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
757 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
758 37 storres
    polySo = tfsAsList[0]
759 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
760 37 storres
    polyRealField = RealField(maxPrecision)
761 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
762 37 storres
    sollya_lib_close()
763 37 storres
    polynomialRing = polyRealField[str(varSa)]
764 37 storres
    polySa = polynomial(expSa, polynomialRing)
765 37 storres
    taylorFormSa = [polySa]
766 51 storres
    return(taylorFormSa)
767 51 storres
# End pobyso_taylor_form_sa_sa
768 54 storres
769 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
770 54 storres
                            errorTypeSo=None):
771 54 storres
    createdErrorType = False
772 51 storres
    if errorTypeSo is None:
773 51 storres
        errorTypeSo = sollya_lib_absolute()
774 54 storres
        createdErrorType = True
775 51 storres
    else:
776 51 storres
        #TODO: deal with the other case.
777 51 storres
        pass
778 51 storres
    if intervalSo is None:
779 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
780 54 storres
                                         errorTypeSo, None)
781 51 storres
    else:
782 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
783 54 storres
                                         intervalSo, errorTypeSo, None)
784 54 storres
    if createdErrorType:
785 54 storres
        sollya_lib_clear_obj(errorTypeSo)
786 51 storres
    return(resultSo)
787 51 storres
788 37 storres
789 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
790 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
791 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
792 38 storres
793 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
794 37 storres
    """
795 37 storres
    Return the string representation of a univariate polynomial with
796 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
797 37 storres
    Remember: Sage
798 37 storres
    """
799 37 storres
    polynomialRing = polySa.base_ring()
800 37 storres
    # A very expensive solution:
801 37 storres
    # -create a fake multivariate polynomial field with only one variable,
802 37 storres
    #   specifying a negative lexicographical order;
803 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
804 37 storres
                                     polynomialRing.variable_name(), \
805 37 storres
                                     1, order='neglex')
806 37 storres
    # - convert the univariate argument polynomial into a multivariate
807 37 storres
    #   version;
808 37 storres
    p = mpolynomialRing(polySa)
809 37 storres
    # - return the string representation of the converted form.
810 37 storres
    # There is no simple str() method defined for p's class.
811 37 storres
    return(p.__str__())
812 5 storres
#
813 5 storres
print "Superficial test of pobyso:"
814 5 storres
print pobyso_get_prec()
815 5 storres
pobyso_set_prec(165)
816 5 storres
print pobyso_get_prec()
817 5 storres
a=100
818 5 storres
print type(a)
819 5 storres
id(a)
820 5 storres
print "Max arity: ", pobyso_max_arity
821 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
822 5 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)