Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 200

Historique | Voir | Annoter | Télécharger (55,4 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 85 storres
def pobyso_absolute_so_so():
89 85 storres
    return(sollya_lib_absolute(None))
90 85 storres
91 5 storres
def pobyso_autoprint(arg):
92 5 storres
    sollya_lib_autoprint(arg,None)
93 5 storres
94 38 storres
def pobyso_autoprint_so_so(arg):
95 38 storres
    sollya_lib_autoprint(arg,None)
96 54 storres
97 84 storres
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa, \
98 84 storres
                                 precisionSa=None):
99 84 storres
    """
100 84 storres
    Return a Sollya range from to 2 RealField Sage elements.
101 84 storres
    The Sollya range element has a sufficient precision to hold all
102 162 storres
    the digits of the widest of the Sage bounds.
103 84 storres
    """
104 84 storres
    # Sanity check.
105 84 storres
    if rnLowerBoundSa > rnUpperBoundSa:
106 84 storres
        return None
107 115 storres
    # Precision stuff.
108 85 storres
    if precisionSa is None:
109 84 storres
        # Check for the largest precision.
110 84 storres
        lbPrecSa = rnLowerBoundSa.parent().precision()
111 84 storres
        ubPrecSa = rnLowerBoundSa.parent().precision()
112 84 storres
        maxPrecSa = max(lbPrecSa, ubPrecSa)
113 84 storres
    else:
114 84 storres
        maxPrecSa = precisionSa
115 115 storres
    # From Sage to Sollya bounds.
116 162 storres
#    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa),
117 162 storres
#                                       maxPrecSa)
118 162 storres
    lowerBoundSo = pobyso_constant_sa_so(rnLowerBoundSa,
119 162 storres
                                         maxPrecSa)
120 162 storres
    upperBoundSo = pobyso_constant_sa_so(rnUpperBoundSa,
121 154 storres
                                       maxPrecSa)
122 162 storres
123 115 storres
    # From Sollya bounds to range.
124 84 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
125 84 storres
    # Back to original precision.
126 84 storres
    # Clean up
127 84 storres
    sollya_lib_clear_obj(lowerBoundSo)
128 84 storres
    sollya_lib_clear_obj(upperBoundSo)
129 154 storres
    return rangeSo
130 84 storres
# End pobyso_bounds_to_range_sa_so
131 84 storres
132 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
133 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
134 54 storres
135 85 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
136 54 storres
    """
137 85 storres
    Variable change in a function.
138 85 storres
    """
139 85 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
140 85 storres
# End pobyso_change_var_in_function_so_so
141 85 storres
142 85 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
143 85 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
144 85 storres
    return(resultSo)
145 85 storres
# End pobyso_chebyshevform_so_so.
146 85 storres
147 117 storres
def pobyso_clear_taylorform_sa_so(taylorFormSaSo):
148 117 storres
    """
149 117 storres
    This method is necessary to correctly clean up the memory from Taylor forms.
150 117 storres
    These are made of a Sollya object, a Sollya object list, a Sollya object.
151 117 storres
    For no clearly understood reason, sollya_lib_clear_object_list crashed
152 117 storres
    when applied to the object list.
153 117 storres
    Here, we decompose it into Sage list of Sollya objects references and we
154 117 storres
     clear them one by one.
155 117 storres
    """
156 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[0])
157 117 storres
    (coefficientsErrorsListSaSo, numElementsSa, isEndEllipticSa) = \
158 117 storres
        pobyso_get_list_elements_so_so(taylorFormSaSo[1])
159 117 storres
    for element in coefficientsErrorsListSaSo:
160 117 storres
        sollya_lib_clear_obj(element)
161 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[1])
162 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[2])
163 117 storres
# End pobyso_clear_taylorform_sa_so
164 117 storres
165 85 storres
def pobyso_cmp(rnArgSa, cteSo):
166 85 storres
    """
167 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
168 54 storres

169 54 storres
    Get the value of the Sollya constant into a RealNumber and compare
170 54 storres
    using MPFR. Could be optimized by working directly with a mpfr_t
171 54 storres
    for the intermediate number.
172 54 storres
    """
173 115 storres
    # Get the precision of the Sollya constant to build a Sage RealNumber
174 115 storres
    # with enough precision.to hold it.
175 5 storres
    precisionOfCte = c_int(0)
176 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
177 85 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, cteSo)
178 5 storres
    #print "Precision of constant: ", precisionOfCte
179 5 storres
    RRRR = RealField(precisionOfCte.value)
180 85 storres
    rnLocalSa = RRRR(0)
181 85 storres
    sollya_lib_get_constant(get_rn_value(rnLocalSa), cteSo)
182 115 storres
    #
183 115 storres
    ## Compare the Sage RealNumber version of the Sollya constant with rnArg.
184 85 storres
    return(cmp_rn_value(rnArgSa, rnLocal))
185 83 storres
# End pobyso_smp
186 5 storres
187 54 storres
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
188 54 storres
                                                     upperBoundSa):
189 54 storres
    """
190 85 storres
    TODO: completely rework and test.
191 54 storres
    """
192 85 storres
    pobyso = pobyso_name_free_variable_sa_so(funcSa.variables()[0])
193 159 storres
    funcSo = pobyso_parse_string(funcSa._assume_str().replace('_SAGE_VAR_', ''))
194 54 storres
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
195 54 storres
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
196 83 storres
    # Sollya return the infnorm as an interval.
197 54 storres
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
198 54 storres
    # Get the top bound and compute the binade top limit.
199 54 storres
    fMaxUpperBoundSa = fMaxSa.upper()
200 54 storres
    binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
201 54 storres
    # Put up together the function to use to compute the lower bound.
202 54 storres
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
203 159 storres
                                    '-(' + f._assume_str().replace('_SAGE_VAR_', '') + ')')
204 54 storres
    pobyso_autoprint(funcAuxSo)
205 83 storres
    # Clear the Sollya range before a new call to infnorm and issue the call.
206 54 storres
    sollya_lib_clear_obj(infnormSo)
207 54 storres
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
208 54 storres
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
209 54 storres
    sollya_lib_clear_obj(infnormSo)
210 85 storres
    fMinLowerBoundSa = binadeTopLimitSa - fMinSa.lower()
211 54 storres
    # Compute the maximum of the precisions of the different bounds.
212 54 storres
    maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
213 54 storres
                     fMaxUpperBoundSa.parent().precision()])
214 54 storres
    # Create a RealIntervalField and create an interval with the "good" bounds.
215 54 storres
    RRRI = RealIntervalField(maxPrecSa)
216 54 storres
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
217 83 storres
    # Free the unneeded Sollya objects
218 54 storres
    sollya_lib_clear_obj(funcSo)
219 54 storres
    sollya_lib_clear_obj(funcAuxSo)
220 54 storres
    sollya_lib_clear_obj(rangeSo)
221 54 storres
    return(imageIntervalSa)
222 83 storres
# End pobyso_compute_pos_function_abs_val_bounds_sa_sa
223 54 storres
224 5 storres
def pobyso_constant(rnArg):
225 38 storres
    """ Legacy function. See pobyso_constant_sa_so. """
226 38 storres
    return(pobyso_constant_sa_so(rnArg))
227 5 storres
228 84 storres
def pobyso_constant_sa_so(rnArgSa, precisionSa=None):
229 52 storres
    """
230 115 storres
    Create a Sollya constant from a Sage RealNumber.
231 52 storres
    """
232 84 storres
    # Precision stuff
233 84 storres
    if precisionSa is None:
234 84 storres
        precisionSa = rnArgSa.parent().precision()
235 85 storres
    currentSollyaPrecisionSo = sollya_lib_get_prec()
236 85 storres
    currentSollyaPrecisionSa = \
237 85 storres
        pobyso_constant_from_int(currentSollyaPrecisionSo)
238 115 storres
    # Sollya constant creation takes place here.
239 84 storres
    if precisionSa > currentSollyaPrecisionSa:
240 84 storres
        pobyso_set_prec_sa_so(precisionSa)
241 84 storres
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
242 154 storres
        pobyso_set_prec_so_so(currentSollyaPrecisionSo)
243 84 storres
    else:
244 84 storres
        constantSo = sollya_lib_constant(get_rn_value(rnArgSa))
245 85 storres
    sollya_lib_clear_obj(currentSollyaPrecisionSo)
246 154 storres
    return constantSo
247 115 storres
# End pobyso_constant_sa_so
248 115 storres
249 55 storres
def pobyso_constant_0_sa_so():
250 115 storres
    """
251 115 storres
    Obvious.
252 115 storres
    """
253 55 storres
    return(pobyso_constant_from_int_sa_so(0))
254 55 storres
255 5 storres
def pobyso_constant_1():
256 115 storres
    """
257 115 storres
    Obvious.
258 115 storres
    Legacy function. See pobyso_constant_so_so.
259 115 storres
    """
260 52 storres
    return(pobyso_constant_1_sa_so())
261 5 storres
262 52 storres
def pobyso_constant_1_sa_so():
263 115 storres
    """
264 115 storres
    Obvious.
265 115 storres
    """
266 38 storres
    return(pobyso_constant_from_int_sa_so(1))
267 38 storres
268 5 storres
def pobyso_constant_from_int(anInt):
269 38 storres
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
270 38 storres
    return(pobyso_constant_from_int_sa_so(anInt))
271 38 storres
272 38 storres
def pobyso_constant_from_int_sa_so(anInt):
273 115 storres
    """
274 115 storres
    Get a Sollya constant from a Sage int.
275 115 storres
    """
276 200 storres
    return(sollya_lib_constant_from_int64(long(anInt)))
277 5 storres
278 84 storres
def pobyso_constant_from_int_so_sa(constSo):
279 84 storres
    """
280 117 storres
    Get a Sage int from a Sollya int constant.
281 115 storres
    Usefull for precision or powers in polynomials.
282 84 storres
    """
283 200 storres
    constSa = c_long(0)
284 200 storres
    sollya_lib_get_constant_as_int64(byref(constSa), constSo)
285 84 storres
    return(constSa.value)
286 84 storres
# End pobyso_constant_from_int_so_sa
287 84 storres
288 200 storres
def pobyso_constant_from_mpq_sa_so(rationalSa, precision = None):
289 200 storres
    """
290 200 storres
    Make a Sollya constant from Sage rational.
291 200 storres
    A bit convoluted to take into account precision and the fact
292 200 storres
    that mpq constants in Sollya a non-evaluated expressions.
293 200 storres
    Function building and evaluation is needed to make it a "real"
294 200 storres
    evaluated constant.
295 200 storres
    """
296 200 storres
    ## Deal with precision stuff.
297 200 storres
    curPrecSa = None
298 200 storres
    curPrecSo = None
299 200 storres
    if not precision is None:
300 200 storres
        curPrecSo = pobyso_get_prec_so()
301 200 storres
        curPrecSaSa = c_int(0)
302 200 storres
        sollya_lib_get_constant_as_int(byref(curPrecSaSa), curPrecSo)
303 200 storres
        curPrecSa = int(curPrecSaSa.value)
304 200 storres
        if curPrecSa != precision:
305 200 storres
            pobyso_set_prec_sa_so(precision)
306 200 storres
    ## In Sollya mpq constants are non-evaluated expressions.
307 200 storres
    #  We must force evaluation.
308 200 storres
    zeroSo    = pobyso_constant_0_sa_so()
309 200 storres
    oneSo     = pobyso_constant_1_sa_so()
310 200 storres
    ratExprSo = \
311 200 storres
        sollya_lib_constant_from_mpq(sgmp_get_rational_value(rationalSa))
312 200 storres
    addExprSo = sollya_lib_build_function_add(zeroSo, ratExprSo)
313 200 storres
    constSo   = sollya_lib_evaluate(addExprSo,oneSo)
314 200 storres
    ## Clears expression and arguments, as the former was created with a
315 200 storres
    #  "build" variant.
316 200 storres
    sollya_lib_clear_obj(addExprSo)
317 200 storres
    sollya_lib_clear_obj(oneSo)
318 200 storres
    if curPrecSa != precision:
319 200 storres
        pobyso_set_prec_so_so(curPrecSo)
320 200 storres
        sollya_lib_clear_obj(curPrecSo)
321 200 storres
    return constSo
322 200 storres
# End pobyso_constant_from_mpq_sa_so.
323 200 storres
324 155 storres
def pobyso_error_so():
325 155 storres
    return sollya_lib_error(None)
326 155 storres
# End pobyso_error().
327 155 storres
328 5 storres
def pobyso_function_type_as_string(funcType):
329 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
330 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
331 38 storres
332 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
333 38 storres
    """
334 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
335 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
336 38 storres
    """
337 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
338 5 storres
        return "abs"
339 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
340 5 storres
        return "arccos"
341 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
342 5 storres
        return "arccosh"
343 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
344 5 storres
        return "+"
345 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
346 5 storres
        return "arcsin"
347 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
348 5 storres
        return "arcsinh"
349 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
350 5 storres
        return "arctan"
351 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
352 5 storres
        return "arctanh"
353 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
354 5 storres
        return "ceil"
355 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
356 5 storres
        return "cte"
357 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
358 5 storres
        return "cos"
359 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
360 5 storres
        return "cosh"
361 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
362 5 storres
        return "/"
363 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
364 5 storres
        return "double"
365 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
366 5 storres
        return "doubleDouble"
367 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
368 5 storres
        return "doubleDxtended"
369 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
370 5 storres
        return "erf"
371 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
372 5 storres
        return "erfc"
373 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
374 5 storres
        return "exp"
375 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
376 5 storres
        return "expm1"
377 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
378 5 storres
        return "floor"
379 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
380 5 storres
        return "freeVariable"
381 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
382 5 storres
        return "halfPrecision"
383 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
384 5 storres
        return "libraryConstant"
385 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
386 5 storres
        return "libraryFunction"
387 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
388 5 storres
        return "log"
389 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
390 5 storres
        return "log10"
391 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
392 5 storres
        return "log1p"
393 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
394 5 storres
        return "log2"
395 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
396 5 storres
        return "*"
397 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
398 5 storres
        return "round"
399 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
400 5 storres
        return "__neg__"
401 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
402 5 storres
        return "pi"
403 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
404 5 storres
        return "^"
405 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
406 5 storres
        return "procedureFunction"
407 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
408 5 storres
        return "quad"
409 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
410 5 storres
        return "sin"
411 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
412 5 storres
        return "single"
413 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
414 5 storres
        return "sinh"
415 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
416 5 storres
        return "sqrt"
417 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
418 5 storres
        return "-"
419 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
420 5 storres
        return "tan"
421 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
422 5 storres
        return "tanh"
423 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
424 5 storres
        return "tripleDouble"
425 5 storres
    else:
426 5 storres
        return None
427 5 storres
428 85 storres
def pobyso_get_constant(rnArgSa, constSo):
429 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
430 85 storres
    return(pobyso_get_constant_so_sa(rnArgSa, constSo))
431 38 storres
432 84 storres
def pobyso_get_constant_so_sa(rnArgSa, constSo):
433 52 storres
    """
434 85 storres
    Set the value of rnArgSo to the value of constSo in MPFR_RNDN mode.
435 52 storres
    rnArg must already exist and belong to some RealField.
436 85 storres
    We assume that constSo points to a Sollya constant.
437 52 storres
    """
438 84 storres
    return(sollya_lib_get_constant(get_rn_value(rnArgSa), constSo))
439 5 storres
440 57 storres
def pobyso_get_constant_as_rn(ctExpSo):
441 83 storres
    """
442 83 storres
    Legacy function. See pobyso_get_constant_as_rn_so_sa.
443 83 storres
    """
444 57 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
445 38 storres
446 56 storres
def pobyso_get_constant_as_rn_so_sa(constExpSo):
447 83 storres
    """
448 83 storres
    Get a Sollya constant as a Sage "real number".
449 83 storres
    The precision of the floating-point number returned is that of the Sollya
450 83 storres
    constant.
451 83 storres
    """
452 57 storres
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
453 56 storres
    RRRR = RealField(precisionSa)
454 56 storres
    rnSa = RRRR(0)
455 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), constExpSo)
456 56 storres
    return(rnSa)
457 83 storres
# End pobyso_get_constant_as_rn_so_sa
458 38 storres
459 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
460 83 storres
    """
461 83 storres
    Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
462 83 storres
    """
463 38 storres
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
464 5 storres
465 56 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None):
466 83 storres
    """
467 83 storres
    Get a Sollya constant as a Sage "real number".
468 83 storres
    If no real field is specified, the precision of the floating-point number
469 85 storres
    returned is that of the Sollya constant.
470 83 storres
    Otherwise is is that of the real field. Hence rounding may happen.
471 83 storres
    """
472 56 storres
    if realFieldSa is None:
473 85 storres
        sollyaPrecSa = pobyso_get_prec_of_constant_so_sa(ctExpSo)
474 56 storres
        realFieldSa = RealField(sollyaPrecSa)
475 56 storres
    rnSa = realFieldSa(0)
476 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo)
477 56 storres
    return(rnSa)
478 83 storres
# End pobyso_get_constant_as_rn_with_rf_so_sa
479 38 storres
480 200 storres
def pobyso_get_float_poly_sa_so(polySa, realFieldSa=None):
481 200 storres
    """
482 200 storres
    Create a Sollya polynomial from a Sage polynomial.
483 200 storres
    """
484 200 storres
    pass
485 200 storres
# End get_float_poly_sa_so.
486 200 storres
487 5 storres
def pobyso_get_free_variable_name():
488 83 storres
    """
489 83 storres
    Legacy function. See pobyso_get_free_variable_name_so_sa.
490 83 storres
    """
491 38 storres
    return(pobyso_get_free_variable_name_so_sa())
492 38 storres
493 38 storres
def pobyso_get_free_variable_name_so_sa():
494 5 storres
    return(sollya_lib_get_free_variable_name())
495 5 storres
496 38 storres
def pobyso_get_function_arity(expressionSo):
497 83 storres
    """
498 83 storres
    Legacy function. See pobyso_get_function_arity_so_sa.
499 83 storres
    """
500 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
501 38 storres
502 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
503 5 storres
    arity = c_int(0)
504 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
505 5 storres
    return(int(arity.value))
506 5 storres
507 38 storres
def pobyso_get_head_function(expressionSo):
508 83 storres
    """
509 83 storres
    Legacy function. See pobyso_get_head_function_so_sa.
510 83 storres
    """
511 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
512 38 storres
513 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
514 5 storres
    functionType = c_int(0)
515 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
516 5 storres
    return(int(functionType.value))
517 5 storres
518 56 storres
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
519 53 storres
    """
520 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
521 83 storres
    If no reaIntervalField is passed as an argument, the interval bounds are not
522 56 storres
    rounded: they are elements of RealIntervalField of the "right" precision
523 56 storres
    to hold all the digits.
524 53 storres
    """
525 53 storres
    prec = c_int(0)
526 56 storres
    if realIntervalFieldSa is None:
527 56 storres
        retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
528 56 storres
        if retval == 0:
529 56 storres
            return(None)
530 56 storres
        realIntervalFieldSa = RealIntervalField(prec.value)
531 56 storres
    intervalSa = realIntervalFieldSa(0,0)
532 53 storres
    retval = \
533 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
534 53 storres
                                           soRange)
535 53 storres
    if retval == 0:
536 53 storres
        return(None)
537 53 storres
    return(intervalSa)
538 56 storres
# End pobyso_get_interval_from_range_so_sa
539 56 storres
540 5 storres
def pobyso_get_list_elements(soObj):
541 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
542 38 storres
    return(pobyso_get_list_elements_so_so(soObj))
543 38 storres
544 117 storres
def pobyso_get_list_elements_so_so(objectListSo):
545 51 storres
    """
546 118 storres
    Get the Sollya list elements as a Sage/Python array of Sollya objects.
547 118 storres

548 118 storres
    INPUT:
549 118 storres
    - objectListSo: a Sollya list of Sollya objects.
550 118 storres

551 118 storres
    OUTPUT:
552 118 storres
    - a Sage/Python tuple made of:
553 118 storres
      - a Sage/Python list of Sollya objects,
554 118 storres
      - a Sage/Python int holding the number of elements,
555 118 storres
      - a Sage/Python int stating (!= 0) that the list is end-elliptic.
556 118 storres
    NOTE::
557 118 storres
        We recover the addresses of the Sollya object from the list of pointers
558 118 storres
        returned by sollya_lib_get_list_elements. The list itself is freed.
559 118 storres
    TODO::
560 118 storres
        Figure out what to do with numElements since the number of elements
561 118 storres
        can easily be recovered from the list itself.
562 118 storres
        Ditto for isEndElliptic.
563 51 storres
    """
564 5 storres
    listAddress = POINTER(c_longlong)()
565 5 storres
    numElements = c_int(0)
566 5 storres
    isEndElliptic = c_int(0)
567 117 storres
    listAsSageList = []
568 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
569 54 storres
                                          byref(numElements),\
570 54 storres
                                          byref(isEndElliptic),\
571 117 storres
                                          objectListSo)
572 5 storres
    if result == 0 :
573 5 storres
        return None
574 5 storres
    for i in xrange(0, numElements.value, 1):
575 118 storres
       #listAsSageList.append(sollya_lib_copy_obj(listAddress[i]))
576 118 storres
       listAsSageList.append(listAddress[i])
577 117 storres
       # Clear each of the elements returned by Sollya.
578 118 storres
       #sollya_lib_clear_obj(listAddress[i])
579 117 storres
    # Free the list itself.
580 117 storres
    sollya_lib_free(listAddress)
581 117 storres
    return(listAsSageList, numElements.value, isEndElliptic.value)
582 5 storres
583 38 storres
def pobyso_get_max_prec_of_exp(soExp):
584 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
585 38 storres
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
586 5 storres
587 85 storres
def pobyso_get_max_prec_of_exp_so_sa(expSo):
588 38 storres
    """
589 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
590 52 storres

591 52 storres
    Arguments:
592 52 storres
    soExp -- a Sollya expression pointer
593 52 storres
    Return value:
594 52 storres
    A Python integer
595 38 storres
    TODO:
596 38 storres
    - error management;
597 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
598 38 storres
    """
599 5 storres
    maxPrecision = 0
600 52 storres
    minConstPrec = 0
601 52 storres
    currentConstPrec = 0
602 85 storres
    operator = pobyso_get_head_function_so_sa(expSo)
603 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
604 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
605 85 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(expSo)
606 5 storres
        for i in xrange(arity):
607 5 storres
            maxPrecisionCandidate = \
608 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
609 5 storres
            if maxPrecisionCandidate > maxPrecision:
610 5 storres
                maxPrecision = maxPrecisionCandidate
611 5 storres
        return(maxPrecision)
612 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
613 85 storres
        #minConstPrec = pobyso_get_min_prec_of_constant_so_sa(expSo)
614 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
615 52 storres
        #print minConstPrec, " - ", currentConstPrec
616 85 storres
        return(pobyso_get_min_prec_of_constant_so_sa(expSo))
617 52 storres
618 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
619 5 storres
        return(0)
620 5 storres
    else:
621 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
622 5 storres
        return(0)
623 5 storres
624 85 storres
def pobyso_get_min_prec_of_constant_so_sa(constExpSo):
625 52 storres
    """
626 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
627 52 storres
    constant.
628 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
629 85 storres
    We assume that constExpSo is a point
630 52 storres
    """
631 85 storres
    constExpAsRnSa = pobyso_get_constant_as_rn_so_sa(constExpSo)
632 85 storres
    return(min_mpfr_size(get_rn_value(constExpAsRnSa)))
633 52 storres
634 200 storres
def pobyso_get_poly_so_sa(polySo, realFieldSa=None):
635 200 storres
    """
636 200 storres
    Convert a Sollya polynomial into a Sage polynomial.
637 200 storres
    We assume that the polynomial is in canonical form.
638 200 storres
    If no realField is given, a RealField corresponding to the maximum
639 200 storres
    precision of the coefficients is internally computed.
640 200 storres
    The real field is not returned but can be easily retrieved from
641 200 storres
    the polynomial itself.
642 200 storres
    ALGORITHM:
643 200 storres
    - (optional) compute the RealField of the coefficients;
644 200 storres
    - convert the Sollya expression into a Sage expression;
645 200 storres
    - convert the Sage expression into a Sage polynomial
646 200 storres
    TODO: the canonical thing for the polynomial.
647 200 storres
    """
648 200 storres
    if realFieldSa is None:
649 200 storres
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
650 200 storres
        realFieldSa = RealField(expressionPrecSa)
651 200 storres
    #print "Sollya expression before...",
652 200 storres
    #pobyso_autoprint(polySo)
653 200 storres
654 200 storres
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \
655 200 storres
                                                             realFieldSa)
656 200 storres
    #print "...Sollya expression after.",
657 200 storres
    #pobyso_autoprint(polySo)
658 200 storres
    polyVariableSa = expressionSa.variables()[0]
659 200 storres
    polyRingSa = realFieldSa[str(polyVariableSa)]
660 200 storres
    #print polyRingSa
661 200 storres
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
662 200 storres
    polynomialSa = polyRingSa(expressionSa)
663 200 storres
    return(polynomialSa)
664 200 storres
# End pobyso_get_poly_so_sa
665 200 storres
666 200 storres
def pobyso_get_prec():
667 200 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
668 200 storres
    return(pobyso_get_prec_so_sa())
669 200 storres
670 200 storres
def pobyso_get_prec_so():
671 200 storres
    """
672 200 storres
    Get the current default precision in Sollya.
673 200 storres
    The return value is a Sollya object.
674 200 storres
    Usefull when modifying the precision back and forth by avoiding
675 200 storres
    extra conversions.
676 200 storres
    """
677 200 storres
    return(sollya_lib_get_prec(None))
678 200 storres
679 200 storres
def pobyso_get_prec_so_sa():
680 200 storres
    """
681 200 storres
    Get the current default precision in Sollya.
682 200 storres
    The return value is Sage/Python int.
683 200 storres
    """
684 200 storres
    precSo = sollya_lib_get_prec(None)
685 200 storres
    precSa = c_int(0)
686 200 storres
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
687 200 storres
    sollya_lib_clear_obj(precSo)
688 200 storres
    return int(precSa.value)
689 200 storres
# End pobyso_get_prec_so_sa.
690 200 storres
691 200 storres
692 200 storres
def pobyso_get_prec_of_constant(ctExpSo):
693 200 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
694 200 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
695 200 storres
696 200 storres
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
697 200 storres
    """
698 200 storres
    Tries to find a precision to represent ctExpSo without rounding.
699 200 storres
    If not possible, returns None.
700 200 storres
    """
701 200 storres
    prec = c_int(0)
702 200 storres
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
703 200 storres
    if retc == 0:
704 200 storres
        return(None)
705 200 storres
    return(int(prec.value))
706 200 storres
707 200 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
708 200 storres
    """
709 200 storres
    Returns the number of bits elements of a range are coded with.
710 200 storres
    """
711 200 storres
    prec = c_int(0)
712 200 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
713 200 storres
    if retc == 0:
714 200 storres
        return(None)
715 200 storres
    return(int(prec.value))
716 200 storres
# End pobyso_get_prec_of_range_so_sa()
717 200 storres
718 200 storres
def pobyso_get_rat_poly_sa_so(polySa, precSa = None):
719 200 storres
    """
720 200 storres
    Create a Sollya polynomial from a Sage rational polynomial.
721 200 storres
    """
722 200 storres
    ## TODO: filter arguments.
723 200 storres
    ##
724 200 storres
    if not precSa is None:
725 200 storres
        initialPrecSo = pobyso_get_prec_so()
726 200 storres
    coefficients = polySa.coefficients()
727 200 storres
    exponents    = polySa.exponents()
728 200 storres
    ## TODO: deal with variables names.
729 200 storres
730 200 storres
    ## If necessary, return Sollya to its initial default precision.
731 200 storres
    if not precSa is None:
732 200 storres
        pobyso_set_prec_so_so(initialPrecSo)
733 200 storres
# End pobyso_get_rat_poly_sa_so
734 200 storres
735 85 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExpSo, realField = RR):
736 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
737 85 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, \
738 85 storres
                                                     realField = RR))
739 38 storres
740 85 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, realFieldSa = RR):
741 5 storres
    """
742 38 storres
    Get a Sage expression from a Sollya expression.
743 38 storres
    Currently only tested with polynomials with floating-point coefficients.
744 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
745 5 storres
    """
746 5 storres
    #pobyso_autoprint(sollyaExp)
747 85 storres
    operatorSa = pobyso_get_head_function_so_sa(sollyaExpSo)
748 83 storres
    sollyaLibFreeVariableName = sollya_lib_get_free_variable_name()
749 5 storres
    # Constants and the free variable are special cases.
750 5 storres
    # All other operator are dealt with in the same way.
751 85 storres
    if (operatorSa != SOLLYA_BASE_FUNC_CONSTANT) and \
752 85 storres
       (operatorSa != SOLLYA_BASE_FUNC_FREE_VARIABLE):
753 85 storres
        (aritySa, subexpressionsSa) = pobyso_get_subfunctions_so_sa(sollyaExpSo)
754 85 storres
        if aritySa == 1:
755 85 storres
            sageExpSa = eval(pobyso_function_type_as_string_so_sa(operatorSa) + \
756 85 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], \
757 85 storres
            realFieldSa) + ")")
758 85 storres
        elif aritySa == 2:
759 63 storres
            # We do not get through the preprocessor.
760 63 storres
            # The "^" operator is then a special case.
761 85 storres
            if operatorSa == SOLLYA_BASE_FUNC_POW:
762 85 storres
                operatorAsStringSa = "**"
763 5 storres
            else:
764 85 storres
                operatorAsStringSa = \
765 85 storres
                    pobyso_function_type_as_string_so_sa(operatorSa)
766 85 storres
            sageExpSa = \
767 85 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], realFieldSa)"\
768 85 storres
              + " " + operatorAsStringSa + " " + \
769 85 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[1], realFieldSa)")
770 63 storres
        # We do not know yet how to deal with arity >= 3
771 63 storres
        # (is there any in Sollya anyway?).
772 5 storres
        else:
773 85 storres
            sageExpSa = eval('None')
774 85 storres
        return(sageExpSa)
775 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_CONSTANT:
776 5 storres
        #print "This is a constant"
777 85 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExpSo, realFieldSa)
778 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_FREE_VARIABLE:
779 5 storres
        #print "This is free variable"
780 83 storres
        return(eval(sollyaLibFreeVariableName))
781 5 storres
    else:
782 5 storres
        print "Unexpected"
783 5 storres
        return eval('None')
784 185 storres
# End pobyso_get_sage_exp_from_sollya_exp_so_sa
785 73 storres
786 185 storres
787 38 storres
def pobyso_get_subfunctions(expressionSo):
788 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
789 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
790 200 storres
# End pobyso_get_subfunctions.
791 200 storres
792 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
793 38 storres
    """
794 38 storres
    Get the subfunctions of an expression.
795 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
796 55 storres
    S.T.: Could not figure out another way than that ugly list of declarations
797 83 storres
    to recover the addresses of the subfunctions.
798 83 storres
    We limit ourselves to arity 8 functions.
799 38 storres
    """
800 5 storres
    subf0 = c_int(0)
801 5 storres
    subf1 = c_int(0)
802 5 storres
    subf2 = c_int(0)
803 5 storres
    subf3 = c_int(0)
804 5 storres
    subf4 = c_int(0)
805 5 storres
    subf5 = c_int(0)
806 5 storres
    subf6 = c_int(0)
807 5 storres
    subf7 = c_int(0)
808 5 storres
    subf8 = c_int(0)
809 5 storres
    arity = c_int(0)
810 5 storres
    nullPtr = POINTER(c_int)()
811 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
812 83 storres
      byref(subf0), byref(subf1), byref(subf2), byref(subf3), \
813 83 storres
      byref(subf4), byref(subf5),\
814 83 storres
      byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
815 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
816 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
817 83 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), \
818 83 storres
#    byref(cast(subfunctions[3], POINTER(c_int))), \
819 83 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), \
820 83 storres
#    byref(cast(subfunctions[5], POINTER(c_int))), \
821 83 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), \
822 83 storres
#    byref(cast(subfunctions[7], POINTER(c_int))), \
823 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
824 83 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \
825 83 storres
                    subf8]
826 5 storres
    subs = []
827 5 storres
    if arity.value > pobyso_max_arity:
828 38 storres
        return(0,[])
829 5 storres
    for i in xrange(arity.value):
830 5 storres
        subs.append(int(subfunctions[i].value))
831 5 storres
        #print subs[i]
832 5 storres
    return(int(arity.value), subs)
833 200 storres
# End pobyso_get_subfunctions_so_sa
834 5 storres
835 155 storres
def pobyso_guess_degree_sa_sa(functionSa, intervalSa, approxErrorSa,
836 155 storres
                              weightSa=None, degreeBoundSa=None):
837 155 storres
    """
838 155 storres
    Sa_sa variant of the solly_guessdegree function.
839 155 storres
    Return 0 if something goes wrong.
840 155 storres
    """
841 159 storres
    functionAsStringSa = functionSa._assume_str().replace('_SAGE_VAR_', '')
842 154 storres
    functionSo = pobyso_parse_string_sa_so(functionAsStringSa)
843 155 storres
    if pobyso_is_error_so_sa(functionSo):
844 155 storres
        sollya_lib_clear_obj(functionSo)
845 155 storres
        return 0
846 154 storres
    rangeSo = pobyso_interval_to_range_sa_so(intervalSa)
847 155 storres
    # The approximation error is expected to be a floating point number.
848 155 storres
    if pobyso_is_floating_point_number_sa_sa(approxErrorSa):
849 155 storres
        approxErrorSo = pobyso_constant_sa_so(approxErrorSa)
850 155 storres
    else:
851 155 storres
        approxErrorSo = pobyso_constant_sa_so(RR(approxErrorSa))
852 154 storres
    if not weightSa is None:
853 159 storres
        weightAsStringSa = weightSa._assume_str().replace('_SAGE_VAR_', '')
854 154 storres
        weightSo = pobyso_parse_string_sa_so(weightAsStringSa)
855 166 storres
        if pobyso_is_error_so_sa(weightSo):
856 155 storres
            sollya_lib_clear_obj(functionSo)
857 155 storres
            sollya_lib_clear_obj(rangeSo)
858 155 storres
            sollya_lib_clear_obj(approxErrorSo)
859 155 storres
            sollya_lib_clear_obj(weightSo)
860 155 storres
            return 0
861 154 storres
    else:
862 154 storres
        weightSo = None
863 154 storres
    if not degreeBoundSa is None:
864 154 storres
        degreeBoundSo = pobyso_constant_from_int_sa_so(degreeBoundSa)
865 154 storres
    else:
866 154 storres
        degreeBoundSo = None
867 154 storres
    guessedDegreeSa = pobyso_guess_degree_so_sa(functionSo,
868 162 storres
                                                rangeSo,
869 162 storres
                                                approxErrorSo,
870 162 storres
                                                weightSo,
871 162 storres
                                                degreeBoundSo)
872 154 storres
    sollya_lib_clear_obj(functionSo)
873 154 storres
    sollya_lib_clear_obj(rangeSo)
874 155 storres
    sollya_lib_clear_obj(approxErrorSo)
875 154 storres
    if not weightSo is None:
876 154 storres
        sollya_lib_clear_obj(weightSo)
877 154 storres
    if not degreeBoundSo is None:
878 154 storres
        sollya_lib_clear_obj(degreeBoundSo)
879 154 storres
    return guessedDegreeSa
880 154 storres
# End poyso_guess_degree_sa_sa
881 154 storres
882 153 storres
def pobyso_guess_degree_so_sa(functionSo, rangeSo, errorSo, weightSo=None, \
883 154 storres
                              degreeBoundSo=None):
884 154 storres
    """
885 154 storres
    Thin wrapper around the guessdegree function.
886 154 storres
    Nevertheless, some precision control stuff has been appended.
887 154 storres
    """
888 154 storres
    # Deal with Sollya internal precision issues: if it is too small,
889 154 storres
    # compared with the error, increases it to about twice -log2(error).
890 154 storres
    errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(errorSo)
891 154 storres
    log2ErrorSa = errorSa.log2()
892 154 storres
    if log2ErrorSa < 0:
893 154 storres
        neededPrecisionSa = int(2 * int(-log2ErrorSa) / 64) * 64
894 154 storres
    else:
895 154 storres
        neededPrecisionSa = int(2 * int(log2ErrorSa) / 64) * 64
896 154 storres
    #print "Needed precision:", neededPrecisionSa
897 154 storres
    currentPrecSa = pobyso_get_prec_so_sa()
898 154 storres
    if neededPrecisionSa > currentPrecSa:
899 154 storres
        currentPrecSo = pobyso_get_prec_so()
900 154 storres
        pobyso_set_prec_sa_so(neededPrecisionSa)
901 166 storres
    #print "Guessing degree..."
902 153 storres
    # weightSo and degreeBoundsSo are optional arguments.
903 162 storres
    # As declared, sollya_lib_guessdegree must take 5 arguments.
904 153 storres
    if weightSo is None:
905 162 storres
        degreeRangeSo = sollya_lib_guessdegree(functionSo, rangeSo, errorSo,
906 162 storres
                                               0, 0, None)
907 154 storres
    elif degreeBoundSo is None:
908 153 storres
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, \
909 162 storres
                                                errorSo, weightSo, 0, None)
910 153 storres
    else:
911 153 storres
        degreeRangeSo =  sollya_lib_guessdegree(functionSo, rangeSo, errorSo, \
912 154 storres
                                                weightSo, degreeBoundSo, None)
913 166 storres
    #print "...degree guess done."
914 154 storres
    # Restore internal precision, if applicable.
915 154 storres
    if neededPrecisionSa > currentPrecSa:
916 154 storres
        pobyso_set_prec_so_so(currentPrecSo)
917 154 storres
        sollya_lib_clear_obj(currentPrecSo)
918 154 storres
    degreeIntervalSa = pobyso_range_to_interval_so_sa(degreeRangeSo)
919 154 storres
    sollya_lib_clear_obj(degreeRangeSo)
920 154 storres
    # When ok, both bounds match.
921 154 storres
    # When the degree bound is too low, the upper bound is the degree
922 154 storres
    # for which the error can be honored.
923 154 storres
    # When it really goes wrong, the upper bound is infinity.
924 154 storres
    if degreeIntervalSa.lower() == degreeIntervalSa.upper():
925 154 storres
        return int(degreeIntervalSa.lower())
926 154 storres
    else:
927 154 storres
        if degreeIntervalSa.upper().is_infinity():
928 154 storres
            return None
929 154 storres
        else:
930 154 storres
            return int(degreeIntervalSa.upper())
931 154 storres
    # End pobyso_guess_degree_so_sa
932 153 storres
933 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
934 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
935 54 storres
    return(None)
936 53 storres
937 84 storres
def pobyso_interval_to_range_sa_so(intervalSa, precisionSa=None):
938 84 storres
    if precisionSa is None:
939 84 storres
        precisionSa = intervalSa.parent().precision()
940 84 storres
    intervalSo = pobyso_bounds_to_range_sa_so(intervalSa.lower(),\
941 84 storres
                                              intervalSa.upper(),\
942 84 storres
                                              precisionSa)
943 84 storres
    return(intervalSo)
944 84 storres
# End pobyso_interval_to_range_sa_so
945 84 storres
946 155 storres
def pobyso_is_error_so_sa(objSo):
947 155 storres
    """
948 155 storres
    Thin wrapper around the sollya_lib_obj_is_error() function.
949 155 storres
    """
950 155 storres
    if sollya_lib_obj_is_error(objSo) != 0:
951 155 storres
        return True
952 155 storres
    else:
953 155 storres
        return False
954 155 storres
# End pobyso_is_error-so_sa
955 155 storres
956 155 storres
def pobyso_is_floating_point_number_sa_sa(numberSa):
957 155 storres
    """
958 155 storres
    Check whether a Sage number is floating point
959 155 storres
    """
960 155 storres
    return numberSa.parent().__class__ is RR.__class__
961 155 storres
962 37 storres
def pobyso_lib_init():
963 37 storres
    sollya_lib_init(None)
964 116 storres
965 116 storres
def pobyso_lib_close():
966 116 storres
    sollya_lib_close(None)
967 37 storres
968 85 storres
def pobyso_name_free_variable(freeVariableNameSa):
969 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
970 85 storres
    pobyso_name_free_variable_sa_so(freeVariableNameSa)
971 38 storres
972 85 storres
def pobyso_name_free_variable_sa_so(freeVariableNameSa):
973 83 storres
    """
974 83 storres
    Set the free variable name in Sollya from a Sage string.
975 83 storres
    """
976 85 storres
    sollya_lib_name_free_variable(freeVariableNameSa)
977 37 storres
978 5 storres
def pobyso_parse_string(string):
979 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
980 38 storres
    return(pobyso_parse_string_sa_so(string))
981 38 storres
982 38 storres
def pobyso_parse_string_sa_so(string):
983 83 storres
    """
984 155 storres
    Get the Sollya expression computed from a Sage string or
985 155 storres
    a Sollya error object if parsing failed.
986 83 storres
    """
987 5 storres
    return(sollya_lib_parse_string(string))
988 5 storres
989 200 storres
def pobyso_precision_so_sa(ctExpSo):
990 200 storres
    precisionSo = sollya_lib_precision(ctExpSo)
991 200 storres
    precisionSa = pobyso_constant_from_int_so_sa(precisionSo)
992 200 storres
    sollya_lib_clear_obj(precisionSo)
993 200 storres
    return precisionSa
994 200 storres
# End pobyso_precision_so_sa
995 200 storres
996 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
997 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
998 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
999 38 storres
1000 5 storres
1001 85 storres
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalFieldSa = None):
1002 83 storres
    """
1003 83 storres
    Get a Sage interval from a Sollya range.
1004 83 storres
    If no realIntervalField is given as a parameter, the Sage interval
1005 83 storres
    precision is that of the Sollya range.
1006 85 storres
    Otherwise, the precision is that of the realIntervalField. In this case
1007 85 storres
    rounding may happen.
1008 83 storres
    """
1009 85 storres
    if realIntervalFieldSa is None:
1010 56 storres
        precSa = pobyso_get_prec_of_range_so_sa(rangeSo)
1011 85 storres
        realIntervalFieldSa = RealIntervalField(precSa)
1012 56 storres
    intervalSa = \
1013 85 storres
        pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalFieldSa)
1014 56 storres
    return(intervalSa)
1015 56 storres
1016 52 storres
def pobyso_remez_canonical_sa_sa(func, \
1017 52 storres
                                 degree, \
1018 52 storres
                                 lowerBound, \
1019 52 storres
                                 upperBound, \
1020 52 storres
                                 weight = None, \
1021 52 storres
                                 quality = None):
1022 52 storres
    """
1023 52 storres
    All arguments are Sage/Python.
1024 52 storres
    The functions (func and weight) must be passed as expressions or strings.
1025 52 storres
    Otherwise the function fails.
1026 83 storres
    The return value is a Sage polynomial.
1027 52 storres
    """
1028 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
1029 83 storres
    # zorglub is "symbolic expression".
1030 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
1031 52 storres
                                 degree, \
1032 52 storres
                                 lowerBound, \
1033 52 storres
                                 upperBound, \
1034 85 storres
                                 weight, \
1035 85 storres
                                 quality)
1036 83 storres
    # String test
1037 52 storres
    if parent(func) == parent("string"):
1038 52 storres
        functionSa = eval(func)
1039 52 storres
    # Expression test.
1040 52 storres
    elif type(func) == type(zorglub):
1041 52 storres
        functionSa = func
1042 83 storres
    else:
1043 83 storres
        return None
1044 83 storres
    #
1045 52 storres
    maxPrecision = 0
1046 52 storres
    if polySo is None:
1047 52 storres
        return(None)
1048 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
1049 85 storres
    RRRRSa = RealField(maxPrecision)
1050 85 storres
    polynomialRingSa = RRRRSa[functionSa.variables()[0]]
1051 85 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRRSa)
1052 85 storres
    polySa = polynomial(expSa, polynomialRingSa)
1053 83 storres
    sollya_lib_clear_obj(polySo)
1054 52 storres
    return(polySa)
1055 85 storres
# End pobyso_remez_canonical_sa_sa
1056 52 storres
1057 38 storres
def pobyso_remez_canonical(func, \
1058 5 storres
                           degree, \
1059 5 storres
                           lowerBound, \
1060 5 storres
                           upperBound, \
1061 38 storres
                           weight = "1", \
1062 5 storres
                           quality = None):
1063 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
1064 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
1065 51 storres
                                        degree, \
1066 51 storres
                                        lowerBound, \
1067 51 storres
                                        upperBound, \
1068 51 storres
                                        weight, \
1069 51 storres
                                        quality))
1070 200 storres
# End pobyso_remez_canonical.
1071 200 storres
1072 38 storres
def pobyso_remez_canonical_sa_so(func, \
1073 38 storres
                                 degree, \
1074 38 storres
                                 lowerBound, \
1075 38 storres
                                 upperBound, \
1076 52 storres
                                 weight = None, \
1077 38 storres
                                 quality = None):
1078 38 storres
    """
1079 38 storres
    All arguments are Sage/Python.
1080 51 storres
    The functions (func and weight) must be passed as expressions or strings.
1081 51 storres
    Otherwise the function fails.
1082 38 storres
    The return value is a pointer to a Sollya function.
1083 38 storres
    """
1084 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
1085 83 storres
    # zorglub is "symbolic expression".
1086 85 storres
    currentVariableNameSa = None
1087 52 storres
    # The func argument can be of different types (string,
1088 52 storres
    # symbolic expression...)
1089 38 storres
    if parent(func) == parent("string"):
1090 85 storres
        localFuncSa = eval(func)
1091 85 storres
        if len(localFuncSa.variables()) > 0:
1092 85 storres
            currentVariableNameSa = localFuncSa.variables()[0]
1093 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
1094 159 storres
            functionSo = \
1095 159 storres
              sollya_lib_parse_string(localFuncSa._assume_str().replace('_SAGE_VAR_', ''))
1096 51 storres
    # Expression test.
1097 52 storres
    elif type(func) == type(zorglub):
1098 52 storres
        # Until we are able to translate Sage expressions into Sollya
1099 52 storres
        # expressions : parse the string version.
1100 85 storres
        if len(func.variables()) > 0:
1101 85 storres
            currentVariableNameSa = func.variables()[0]
1102 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
1103 159 storres
            functionSo = \
1104 159 storres
              sollya_lib_parse_string(func._assume_str().replace('_SAGE_VAR_', ''))
1105 38 storres
    else:
1106 38 storres
        return(None)
1107 85 storres
    if weight is None: # No weight given -> 1.
1108 52 storres
        weightSo = pobyso_constant_1_sa_so()
1109 85 storres
    elif parent(weight) == parent("string"): # Weight given as string: parse it.
1110 51 storres
        weightSo = sollya_lib_parse_string(func)
1111 85 storres
    elif type(weight) == type(zorglub): # Weight given as symbolice expression.
1112 159 storres
        functionSo = \
1113 159 storres
          sollya_lib_parse_string_sa_so(weight._assume_str().replace('_SAGE_VAR_', ''))
1114 51 storres
    else:
1115 51 storres
        return(None)
1116 5 storres
    degreeSo = pobyso_constant_from_int(degree)
1117 85 storres
    rangeSo = pobyso_bounds_to_range_sa_so(lowerBound, upperBound)
1118 38 storres
    if not quality is None:
1119 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
1120 52 storres
    else:
1121 52 storres
        qualitySo = None
1122 83 storres
1123 83 storres
    remezPolySo = sollya_lib_remez(functionSo, \
1124 83 storres
                                   degreeSo, \
1125 83 storres
                                   rangeSo, \
1126 83 storres
                                   weightSo, \
1127 83 storres
                                   qualitySo, \
1128 83 storres
                                   None)
1129 83 storres
    sollya_lib_clear_obj(functionSo)
1130 83 storres
    sollya_lib_clear_obj(degreeSo)
1131 83 storres
    sollya_lib_clear_obj(rangeSo)
1132 83 storres
    sollya_lib_clear_obj(weightSo)
1133 83 storres
    if not qualitySo is None:
1134 85 storres
        sollya_lib_clear_obj(qualitySo)
1135 83 storres
    return(remezPolySo)
1136 83 storres
# End pobyso_remez_canonical_sa_so
1137 83 storres
1138 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
1139 38 storres
                                 degreeSo, \
1140 38 storres
                                 rangeSo, \
1141 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
1142 38 storres
                                 qualitySo = None):
1143 38 storres
    """
1144 38 storres
    All arguments are pointers to Sollya objects.
1145 38 storres
    The return value is a pointer to a Sollya function.
1146 38 storres
    """
1147 38 storres
    if not sollya_lib_obj_is_function(funcSo):
1148 38 storres
        return(None)
1149 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
1150 200 storres
# End pobyso_remez_canonical_so_so.
1151 200 storres
1152 5 storres
def pobyso_set_canonical_off():
1153 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
1154 5 storres
1155 5 storres
def pobyso_set_canonical_on():
1156 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
1157 5 storres
1158 5 storres
def pobyso_set_prec(p):
1159 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
1160 85 storres
    pobyso_set_prec_sa_so(p)
1161 38 storres
1162 38 storres
def pobyso_set_prec_sa_so(p):
1163 5 storres
    a = c_int(p)
1164 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
1165 85 storres
    sollya_lib_set_prec(precSo, None)
1166 5 storres
1167 85 storres
def pobyso_set_prec_so_so(newPrecSo):
1168 85 storres
    sollya_lib_set_prec(newPrecSo, None)
1169 54 storres
1170 85 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo = None,\
1171 85 storres
                         accuracySo = None):
1172 58 storres
    """
1173 85 storres
    Computes the supnorm of the approximation error between the given
1174 85 storres
    polynomial and function.
1175 85 storres
    errorTypeSo defaults to "absolute".
1176 85 storres
    accuracySo defaults to 2^(-40).
1177 85 storres
    """
1178 85 storres
    if errorTypeSo is None:
1179 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1180 85 storres
        errorTypeIsNone = True
1181 85 storres
    else:
1182 85 storres
        errorTypeIsNone = False
1183 85 storres
    #
1184 85 storres
    if accuracySo is None:
1185 85 storres
        # Notice the **!
1186 85 storres
        accuracySo = pobyso_constant_sa_so(RR(2**(-40)))
1187 85 storres
        accuracyIsNone = True
1188 85 storres
    else:
1189 85 storres
        accuracyIsNone = False
1190 85 storres
    pobyso_autoprint(accuracySo)
1191 85 storres
    resultSo = \
1192 85 storres
        sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
1193 85 storres
                              accuracySo)
1194 85 storres
    if errorTypeIsNone:
1195 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1196 85 storres
    if accuracyIsNone:
1197 85 storres
        sollya_lib_clear_obj(accuracySo)
1198 85 storres
    return resultSo
1199 85 storres
# End pobyso_supnorm_so_so
1200 85 storres
1201 162 storres
def pobyso_taylor_expansion_no_change_var_so_so(functionSo,
1202 162 storres
                                                degreeSo,
1203 162 storres
                                                rangeSo,
1204 162 storres
                                                errorTypeSo=None,
1205 162 storres
                                                sollyaPrecSo=None):
1206 85 storres
    """
1207 162 storres
    Compute the Taylor expansion without the variable change
1208 162 storres
    x -> x-intervalCenter.
1209 58 storres
    """
1210 58 storres
    # No global change of the working precision.
1211 58 storres
    if not sollyaPrecSo is None:
1212 58 storres
        initialPrecSo = sollya_lib_get_prec(None)
1213 58 storres
        sollya_lib_set_prec(sollyaPrecSo)
1214 85 storres
    # Error type stuff: default to absolute.
1215 85 storres
    if errorTypeSo is None:
1216 85 storres
        errorTypeIsNone = True
1217 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1218 85 storres
    else:
1219 85 storres
        errorTypeIsNone = False
1220 162 storres
    intervalCenterSo = sollya_lib_mid(rangeSo, None)
1221 162 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo,
1222 162 storres
                                         intervalCenterSo,
1223 58 storres
                                         rangeSo, errorTypeSo, None)
1224 117 storres
    # taylorFormListSaSo is a Python list of Sollya objects references that
1225 117 storres
    # are copies of the elements of taylorFormSo.
1226 117 storres
    # pobyso_get_list_elements_so_so clears taylorFormSo.
1227 162 storres
    (taylorFormListSaSo, numElementsSa, isEndEllipticSa) = \
1228 58 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
1229 162 storres
    polySo = sollya_lib_copy_obj(taylorFormListSaSo[0])
1230 162 storres
    #print "Num elements:", numElementsSa
1231 162 storres
    sollya_lib_clear_obj(taylorFormSo)
1232 162 storres
    #polySo = taylorFormListSaSo[0]
1233 162 storres
    #errorRangeSo = sollya_lib_copy_obj(taylorFormListSaSo[2])
1234 162 storres
    errorRangeSo = taylorFormListSaSo[2]
1235 181 storres
    # No copy_obj needed here: a new objects are created.
1236 181 storres
    maxErrorSo    = sollya_lib_sup(errorRangeSo)
1237 181 storres
    minErrorSo    = sollya_lib_inf(errorRangeSo)
1238 181 storres
    absMaxErrorSo = sollya_lib_abs(maxErrorSo)
1239 181 storres
    absMinErrorSo = sollya_lib_abs(minErrorSo)
1240 181 storres
    sollya_lib_clear_obj(maxErrorSo)
1241 181 storres
    sollya_lib_clear_obj(minErrorSo)
1242 181 storres
    absMaxErrorSa = pobyso_get_constant_as_rn_so_sa(absMaxErrorSo)
1243 181 storres
    absMinErrorSa = pobyso_get_constant_as_rn_so_sa(absMinErrorSo)
1244 58 storres
    # If changed, reset the Sollya working precision.
1245 58 storres
    if not sollyaPrecSo is None:
1246 58 storres
        sollya_lib_set_prec(initialPrecSo)
1247 83 storres
        sollya_lib_clear_obj(initialPrecSo)
1248 85 storres
    if errorTypeIsNone:
1249 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1250 162 storres
    pobyso_clear_taylorform_sa_so(taylorFormListSaSo)
1251 181 storres
    if absMaxErrorSa > absMinErrorSa:
1252 181 storres
        sollya_lib_clear_obj(absMinErrorSo)
1253 181 storres
        return((polySo, intervalCenterSo, absMaxErrorSo))
1254 181 storres
    else:
1255 181 storres
        sollya_lib_clear_obj(absMaxErrorSo)
1256 181 storres
        return((polySo, intervalCenterSo, absMinErrorSo))
1257 162 storres
# end pobyso_taylor_expansion_no_change_var_so_so
1258 58 storres
1259 162 storres
def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, \
1260 162 storres
                                                  rangeSo, \
1261 162 storres
                                                  errorTypeSo=None, \
1262 162 storres
                                                  sollyaPrecSo=None):
1263 58 storres
    """
1264 162 storres
    Compute the Taylor expansion with the variable change
1265 162 storres
    x -> (x-intervalCenter) included.
1266 58 storres
    """
1267 56 storres
    # No global change of the working precision.
1268 56 storres
    if not sollyaPrecSo is None:
1269 56 storres
        initialPrecSo = sollya_lib_get_prec(None)
1270 56 storres
        sollya_lib_set_prec(sollyaPrecSo)
1271 162 storres
    #
1272 85 storres
    # Error type stuff: default to absolute.
1273 85 storres
    if errorTypeSo is None:
1274 85 storres
        errorTypeIsNone = True
1275 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1276 85 storres
    else:
1277 85 storres
        errorTypeIsNone = False
1278 162 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
1279 162 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
1280 162 storres
                                         intervalCenterSo, \
1281 56 storres
                                         rangeSo, errorTypeSo, None)
1282 116 storres
    # taylorFormListSaSo is a Python list of Sollya objects references that
1283 116 storres
    # are copies of the elements of taylorFormSo.
1284 116 storres
    # pobyso_get_list_elements_so_so clears taylorFormSo.
1285 162 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
1286 56 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
1287 162 storres
    polySo = taylorFormListSo[0]
1288 162 storres
    errorRangeSo = taylorFormListSo[2]
1289 181 storres
    maxErrorSo    = sollya_lib_sup(errorRangeSo)
1290 181 storres
    minErrorSo    = sollya_lib_inf(errorRangeSo)
1291 181 storres
    absMaxErrorSo = sollya_lib_abs(maxErrorSo)
1292 181 storres
    absMinErrorSo = sollya_lib_abs(minErrorSo)
1293 181 storres
    sollya_lib_clear_obj(maxErrorSo)
1294 181 storres
    sollya_lib_clear_obj(minErrorSo)
1295 181 storres
    absMaxErrorSa = pobyso_get_constant_as_rn_so_sa(absMaxErrorSo)
1296 181 storres
    absMinErrorSa = pobyso_get_constant_as_rn_so_sa(absMinErrorSo)
1297 162 storres
    changeVarExpSo = sollya_lib_build_function_sub(\
1298 162 storres
                       sollya_lib_build_function_free_variable(),\
1299 162 storres
                       sollya_lib_copy_obj(intervalCenterSo))
1300 181 storres
    polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo)
1301 181 storres
    sollya_lib_clear_obj(polySo)
1302 162 storres
    sollya_lib_clear_obj(changeVarExpSo)
1303 56 storres
    # If changed, reset the Sollya working precision.
1304 56 storres
    if not sollyaPrecSo is None:
1305 56 storres
        sollya_lib_set_prec(initialPrecSo)
1306 63 storres
        sollya_lib_clear_obj(initialPrecSo)
1307 85 storres
    if errorTypeIsNone:
1308 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1309 162 storres
    sollya_lib_clear_obj(taylorFormSo)
1310 162 storres
    # Do not clear maxErrorSo.
1311 181 storres
    if absMaxErrorSa > absMinErrorSa:
1312 181 storres
        sollya_lib_clear_obj(absMinErrorSo)
1313 181 storres
        return((polyVarChangedSo, intervalCenterSo, absMaxErrorSo))
1314 181 storres
    else:
1315 181 storres
        sollya_lib_clear_obj(absMaxErrorSo)
1316 181 storres
        return((polyVarChangedSo, intervalCenterSo, absMinErrorSo))
1317 162 storres
# end pobyso_taylor_expansion_with_change_var_so_so
1318 56 storres
1319 5 storres
def pobyso_taylor(function, degree, point):
1320 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
1321 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
1322 38 storres
1323 56 storres
def pobyso_taylor_so_so(functionSo, degreeSo, pointSo):
1324 56 storres
    return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
1325 5 storres
1326 85 storres
def pobyso_taylorform(function, degree, point = None,
1327 85 storres
                      interval = None, errorType=None):
1328 85 storres
    """ Legacy function. See pobyso_taylorform_sa_sa;"""
1329 38 storres
1330 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
1331 84 storres
                            degreeSa, \
1332 84 storres
                            pointSa, \
1333 84 storres
                            intervalSa=None, \
1334 84 storres
                            errorTypeSa=None, \
1335 84 storres
                            precisionSa=None):
1336 37 storres
    """
1337 85 storres
    Compute the Taylor form of 'degreeSa' for 'functionSa' at 'pointSa'
1338 85 storres
    for 'intervalSa' with 'errorTypeSa' (a string) using 'precisionSa'.
1339 37 storres
    point: must be a Real or a Real interval.
1340 37 storres
    return the Taylor form as an array
1341 83 storres
    TODO: take care of the interval and of the point when it is an interval;
1342 38 storres
          when errorType is not None;
1343 83 storres
          take care of the other elements of the Taylor form (coefficients
1344 83 storres
          errors and delta.
1345 37 storres
    """
1346 37 storres
    # Absolute as the default error.
1347 84 storres
    if errorTypeSa is None:
1348 37 storres
        errorTypeSo = sollya_lib_absolute()
1349 84 storres
    elif errorTypeSa == "relative":
1350 84 storres
        errorTypeSo = sollya_lib_relative()
1351 84 storres
    elif errortypeSa == "absolute":
1352 84 storres
        errorTypeSo = sollya_lib_absolute()
1353 37 storres
    else:
1354 84 storres
        # No clean up needed.
1355 84 storres
        return None
1356 84 storres
    # Global precision stuff
1357 84 storres
    precisionChangedSa = False
1358 84 storres
    currentSollyaPrecSo = pobyso_get_prec_so()
1359 84 storres
    currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo)
1360 84 storres
    if not precisionSa is None:
1361 84 storres
        if precisionSa > currentSollyaPrecSa:
1362 84 storres
            pobyso_set_prec_sa_so(precisionSa)
1363 84 storres
            precisionChangedSa = True
1364 84 storres
1365 85 storres
    if len(functionSa.variables()) > 0:
1366 85 storres
        varSa = functionSa.variables()[0]
1367 85 storres
        pobyso_name_free_variable_sa_so(str(varSa))
1368 84 storres
    # In any case (point or interval) the parent of pointSa has a precision
1369 84 storres
    # method.
1370 84 storres
    pointPrecSa = pointSa.parent().precision()
1371 84 storres
    if precisionSa > pointPrecSa:
1372 84 storres
        pointPrecSa = precisionSa
1373 84 storres
    # In any case (point or interval) pointSa has a base_ring() method.
1374 84 storres
    pointBaseRingString = str(pointSa.base_ring())
1375 84 storres
    if re.search('Interval', pointBaseRingString) is None: # Point
1376 84 storres
        pointSo = pobyso_constant_sa_so(pointSa, pointPrecSa)
1377 84 storres
    else: # Interval.
1378 84 storres
        pointSo = pobyso_interval_to_range_sa_so(pointSa, pointPrecSa)
1379 37 storres
    # Sollyafy the function.
1380 159 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str().replace('_SAGE_VAR_', ''))
1381 37 storres
    if sollya_lib_obj_is_error(functionSo):
1382 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
1383 37 storres
        return None
1384 37 storres
    # Sollyafy the degree
1385 84 storres
    degreeSo = sollya_lib_constant_from_int(int(degreeSa))
1386 37 storres
    # Sollyafy the point
1387 37 storres
    # Call Sollya
1388 83 storres
    taylorFormSo = \
1389 83 storres
        sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
1390 37 storres
                                         None)
1391 85 storres
    sollya_lib_clear_obj(functionSo)
1392 85 storres
    sollya_lib_clear_obj(degreeSo)
1393 85 storres
    sollya_lib_clear_obj(pointSo)
1394 85 storres
    sollya_lib_clear_obj(errorTypeSo)
1395 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
1396 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
1397 37 storres
    polySo = tfsAsList[0]
1398 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
1399 37 storres
    polyRealField = RealField(maxPrecision)
1400 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
1401 84 storres
    if precisionChangedSa:
1402 84 storres
        sollya_lib_set_prec(currentSollyaPrecSo)
1403 84 storres
        sollya_lib_clear_obj(currentSollyaPrecSo)
1404 37 storres
    polynomialRing = polyRealField[str(varSa)]
1405 37 storres
    polySa = polynomial(expSa, polynomialRing)
1406 37 storres
    taylorFormSa = [polySa]
1407 85 storres
    # Final clean-up.
1408 85 storres
    sollya_lib_clear_obj(taylorFormSo)
1409 51 storres
    return(taylorFormSa)
1410 51 storres
# End pobyso_taylor_form_sa_sa
1411 54 storres
1412 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
1413 54 storres
                            errorTypeSo=None):
1414 54 storres
    createdErrorType = False
1415 51 storres
    if errorTypeSo is None:
1416 51 storres
        errorTypeSo = sollya_lib_absolute()
1417 54 storres
        createdErrorType = True
1418 51 storres
    else:
1419 51 storres
        #TODO: deal with the other case.
1420 51 storres
        pass
1421 51 storres
    if intervalSo is None:
1422 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1423 54 storres
                                         errorTypeSo, None)
1424 51 storres
    else:
1425 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1426 54 storres
                                         intervalSo, errorTypeSo, None)
1427 54 storres
    if createdErrorType:
1428 54 storres
        sollya_lib_clear_obj(errorTypeSo)
1429 51 storres
    return(resultSo)
1430 51 storres
1431 37 storres
1432 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
1433 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
1434 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
1435 38 storres
1436 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
1437 37 storres
    """
1438 37 storres
    Return the string representation of a univariate polynomial with
1439 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
1440 37 storres
    Remember: Sage
1441 37 storres
    """
1442 37 storres
    polynomialRing = polySa.base_ring()
1443 37 storres
    # A very expensive solution:
1444 37 storres
    # -create a fake multivariate polynomial field with only one variable,
1445 37 storres
    #   specifying a negative lexicographical order;
1446 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
1447 37 storres
                                     polynomialRing.variable_name(), \
1448 37 storres
                                     1, order='neglex')
1449 37 storres
    # - convert the univariate argument polynomial into a multivariate
1450 37 storres
    #   version;
1451 37 storres
    p = mpolynomialRing(polySa)
1452 37 storres
    # - return the string representation of the converted form.
1453 37 storres
    # There is no simple str() method defined for p's class.
1454 37 storres
    return(p.__str__())
1455 5 storres
#
1456 5 storres
print pobyso_get_prec()
1457 5 storres
pobyso_set_prec(165)
1458 5 storres
print pobyso_get_prec()
1459 5 storres
a=100
1460 5 storres
print type(a)
1461 5 storres
id(a)
1462 5 storres
print "Max arity: ", pobyso_max_arity
1463 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
1464 56 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
1465 56 storres
print "...Pobyso check done"