Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 213

Historique | Voir | Annoter | Télécharger (58,22 ko)

1 5 storres
"""
2 209 storres
@file pobyso.py
3 5 storres
Actual functions to use in Sage
4 5 storres
ST 2012-11-13
5 5 storres

6 5 storres
Command line syntax:
7 5 storres
  use from Sage (via the "load" or the "attach" commands)
8 5 storres

9 38 storres
pobyso functions come in five flavors:
10 57 storres
- the _so_so (arguments and returned objects are pointers to Sollya objects,
11 57 storres
  includes the void function and the no arguments function that return a
12 57 storres
  pointer to a Sollya object);
13 38 storres
- the _so_sa (argument are pointers to Sollya objects, returned objects are
14 57 storres
  Sage/Python objects or, more generally, information is transfered from the
15 57 storres
  Sollya world to Sage/Python world; e.g. functions without arguments that
16 57 storres
  return a Sage/Python object);
17 38 storres
- the _sa_so (arguments are Sage/Python objects, returned objects are
18 38 storres
  pointers to Sollya objects);
19 38 storres
- the sa_sa (arguments and returned objects are all Sage/Python objects);
20 51 storres
- a catch all flavor, without any suffix, (e. g. functions that have no argument
21 51 storres
  nor return value).
22 57 storres
This classification is not always very strict. Conversion functions from Sollya
23 57 storres
to Sage/Python are sometimes decorated with Sage/Python arguments to set
24 57 storres
the precision. These functions remain in the so_sa category.
25 5 storres
NOTES:
26 5 storres
Reported errors in Eclipse come from the calls to
27 5 storres
the Sollya library
28 5 storres

29 10 storres
ToDo (among other things):
30 10 storres
 -memory management.
31 5 storres
"""
32 5 storres
from ctypes import *
33 37 storres
import re
34 37 storres
from sage.symbolic.expression_conversions import polynomial
35 59 storres
from sage.symbolic.expression_conversions import PolynomialConverter
36 38 storres
"""
37 38 storres
Create the equivalent to an enum for the Sollya function types.
38 38 storres
"""
39 5 storres
(SOLLYA_BASE_FUNC_ABS,
40 5 storres
SOLLYA_BASE_FUNC_ACOS,
41 5 storres
    SOLLYA_BASE_FUNC_ACOSH,
42 5 storres
    SOLLYA_BASE_FUNC_ADD,
43 5 storres
    SOLLYA_BASE_FUNC_ASIN,
44 5 storres
    SOLLYA_BASE_FUNC_ASINH,
45 5 storres
    SOLLYA_BASE_FUNC_ATAN,
46 5 storres
    SOLLYA_BASE_FUNC_ATANH,
47 5 storres
    SOLLYA_BASE_FUNC_CEIL,
48 5 storres
    SOLLYA_BASE_FUNC_CONSTANT,
49 5 storres
    SOLLYA_BASE_FUNC_COS,
50 5 storres
    SOLLYA_BASE_FUNC_COSH,
51 5 storres
    SOLLYA_BASE_FUNC_DIV,
52 5 storres
    SOLLYA_BASE_FUNC_DOUBLE,
53 5 storres
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
54 5 storres
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
55 5 storres
    SOLLYA_BASE_FUNC_ERF,
56 5 storres
    SOLLYA_BASE_FUNC_ERFC,
57 5 storres
    SOLLYA_BASE_FUNC_EXP,
58 5 storres
    SOLLYA_BASE_FUNC_EXP_M1,
59 5 storres
    SOLLYA_BASE_FUNC_FLOOR,
60 5 storres
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
61 5 storres
    SOLLYA_BASE_FUNC_HALFPRECISION,
62 5 storres
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
63 5 storres
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
64 5 storres
    SOLLYA_BASE_FUNC_LOG,
65 5 storres
    SOLLYA_BASE_FUNC_LOG_10,
66 5 storres
    SOLLYA_BASE_FUNC_LOG_1P,
67 5 storres
    SOLLYA_BASE_FUNC_LOG_2,
68 5 storres
    SOLLYA_BASE_FUNC_MUL,
69 5 storres
    SOLLYA_BASE_FUNC_NEARESTINT,
70 5 storres
    SOLLYA_BASE_FUNC_NEG,
71 5 storres
    SOLLYA_BASE_FUNC_PI,
72 5 storres
    SOLLYA_BASE_FUNC_POW,
73 5 storres
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
74 5 storres
    SOLLYA_BASE_FUNC_QUAD,
75 5 storres
    SOLLYA_BASE_FUNC_SIN,
76 5 storres
    SOLLYA_BASE_FUNC_SINGLE,
77 5 storres
    SOLLYA_BASE_FUNC_SINH,
78 5 storres
    SOLLYA_BASE_FUNC_SQRT,
79 5 storres
    SOLLYA_BASE_FUNC_SUB,
80 5 storres
    SOLLYA_BASE_FUNC_TAN,
81 5 storres
    SOLLYA_BASE_FUNC_TANH,
82 5 storres
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
83 56 storres
print "\nSuperficial pobyso check..."
84 5 storres
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
85 5 storres
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
86 5 storres
87 5 storres
pobyso_max_arity = 9
88 5 storres
89 85 storres
def pobyso_absolute_so_so():
90 85 storres
    return(sollya_lib_absolute(None))
91 85 storres
92 5 storres
def pobyso_autoprint(arg):
93 5 storres
    sollya_lib_autoprint(arg,None)
94 5 storres
95 38 storres
def pobyso_autoprint_so_so(arg):
96 38 storres
    sollya_lib_autoprint(arg,None)
97 54 storres
98 84 storres
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa, \
99 84 storres
                                 precisionSa=None):
100 84 storres
    """
101 84 storres
    Return a Sollya range from to 2 RealField Sage elements.
102 84 storres
    The Sollya range element has a sufficient precision to hold all
103 162 storres
    the digits of the widest of the Sage bounds.
104 84 storres
    """
105 84 storres
    # Sanity check.
106 84 storres
    if rnLowerBoundSa > rnUpperBoundSa:
107 84 storres
        return None
108 115 storres
    # Precision stuff.
109 85 storres
    if precisionSa is None:
110 84 storres
        # Check for the largest precision.
111 84 storres
        lbPrecSa = rnLowerBoundSa.parent().precision()
112 84 storres
        ubPrecSa = rnLowerBoundSa.parent().precision()
113 84 storres
        maxPrecSa = max(lbPrecSa, ubPrecSa)
114 84 storres
    else:
115 84 storres
        maxPrecSa = precisionSa
116 115 storres
    # From Sage to Sollya bounds.
117 162 storres
#    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa),
118 162 storres
#                                       maxPrecSa)
119 162 storres
    lowerBoundSo = pobyso_constant_sa_so(rnLowerBoundSa,
120 162 storres
                                         maxPrecSa)
121 162 storres
    upperBoundSo = pobyso_constant_sa_so(rnUpperBoundSa,
122 154 storres
                                       maxPrecSa)
123 162 storres
124 115 storres
    # From Sollya bounds to range.
125 84 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
126 84 storres
    # Back to original precision.
127 84 storres
    # Clean up
128 84 storres
    sollya_lib_clear_obj(lowerBoundSo)
129 84 storres
    sollya_lib_clear_obj(upperBoundSo)
130 154 storres
    return rangeSo
131 84 storres
# End pobyso_bounds_to_range_sa_so
132 84 storres
133 54 storres
def pobyso_build_function_sub_so_so(exp1So, exp2So):
134 54 storres
    return(sollya_lib_build_function_sub(exp1So, exp2So))
135 54 storres
136 85 storres
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
137 54 storres
    """
138 85 storres
    Variable change in a function.
139 85 storres
    """
140 85 storres
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
141 85 storres
# End pobyso_change_var_in_function_so_so
142 85 storres
143 85 storres
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
144 85 storres
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
145 85 storres
    return(resultSo)
146 85 storres
# End pobyso_chebyshevform_so_so.
147 85 storres
148 117 storres
def pobyso_clear_taylorform_sa_so(taylorFormSaSo):
149 117 storres
    """
150 117 storres
    This method is necessary to correctly clean up the memory from Taylor forms.
151 117 storres
    These are made of a Sollya object, a Sollya object list, a Sollya object.
152 117 storres
    For no clearly understood reason, sollya_lib_clear_object_list crashed
153 117 storres
    when applied to the object list.
154 117 storres
    Here, we decompose it into Sage list of Sollya objects references and we
155 117 storres
     clear them one by one.
156 117 storres
    """
157 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[0])
158 117 storres
    (coefficientsErrorsListSaSo, numElementsSa, isEndEllipticSa) = \
159 117 storres
        pobyso_get_list_elements_so_so(taylorFormSaSo[1])
160 117 storres
    for element in coefficientsErrorsListSaSo:
161 117 storres
        sollya_lib_clear_obj(element)
162 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[1])
163 117 storres
    sollya_lib_clear_obj(taylorFormSaSo[2])
164 117 storres
# End pobyso_clear_taylorform_sa_so
165 117 storres
166 85 storres
def pobyso_cmp(rnArgSa, cteSo):
167 85 storres
    """
168 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
169 54 storres

170 54 storres
    Get the value of the Sollya constant into a RealNumber and compare
171 54 storres
    using MPFR. Could be optimized by working directly with a mpfr_t
172 54 storres
    for the intermediate number.
173 54 storres
    """
174 115 storres
    # Get the precision of the Sollya constant to build a Sage RealNumber
175 115 storres
    # with enough precision.to hold it.
176 5 storres
    precisionOfCte = c_int(0)
177 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
178 85 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, cteSo)
179 5 storres
    #print "Precision of constant: ", precisionOfCte
180 5 storres
    RRRR = RealField(precisionOfCte.value)
181 85 storres
    rnLocalSa = RRRR(0)
182 85 storres
    sollya_lib_get_constant(get_rn_value(rnLocalSa), cteSo)
183 115 storres
    #
184 115 storres
    ## Compare the Sage RealNumber version of the Sollya constant with rnArg.
185 85 storres
    return(cmp_rn_value(rnArgSa, rnLocal))
186 83 storres
# End pobyso_smp
187 5 storres
188 54 storres
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
189 54 storres
                                                     upperBoundSa):
190 54 storres
    """
191 85 storres
    TODO: completely rework and test.
192 54 storres
    """
193 85 storres
    pobyso = pobyso_name_free_variable_sa_so(funcSa.variables()[0])
194 159 storres
    funcSo = pobyso_parse_string(funcSa._assume_str().replace('_SAGE_VAR_', ''))
195 54 storres
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
196 54 storres
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
197 83 storres
    # Sollya return the infnorm as an interval.
198 54 storres
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
199 54 storres
    # Get the top bound and compute the binade top limit.
200 54 storres
    fMaxUpperBoundSa = fMaxSa.upper()
201 54 storres
    binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
202 54 storres
    # Put up together the function to use to compute the lower bound.
203 54 storres
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
204 159 storres
                                    '-(' + f._assume_str().replace('_SAGE_VAR_', '') + ')')
205 54 storres
    pobyso_autoprint(funcAuxSo)
206 83 storres
    # Clear the Sollya range before a new call to infnorm and issue the call.
207 54 storres
    sollya_lib_clear_obj(infnormSo)
208 54 storres
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
209 54 storres
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
210 54 storres
    sollya_lib_clear_obj(infnormSo)
211 85 storres
    fMinLowerBoundSa = binadeTopLimitSa - fMinSa.lower()
212 54 storres
    # Compute the maximum of the precisions of the different bounds.
213 54 storres
    maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
214 54 storres
                     fMaxUpperBoundSa.parent().precision()])
215 54 storres
    # Create a RealIntervalField and create an interval with the "good" bounds.
216 54 storres
    RRRI = RealIntervalField(maxPrecSa)
217 54 storres
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
218 83 storres
    # Free the unneeded Sollya objects
219 54 storres
    sollya_lib_clear_obj(funcSo)
220 54 storres
    sollya_lib_clear_obj(funcAuxSo)
221 54 storres
    sollya_lib_clear_obj(rangeSo)
222 54 storres
    return(imageIntervalSa)
223 83 storres
# End pobyso_compute_pos_function_abs_val_bounds_sa_sa
224 54 storres
225 5 storres
def pobyso_constant(rnArg):
226 38 storres
    """ Legacy function. See pobyso_constant_sa_so. """
227 38 storres
    return(pobyso_constant_sa_so(rnArg))
228 5 storres
229 84 storres
def pobyso_constant_sa_so(rnArgSa, precisionSa=None):
230 52 storres
    """
231 115 storres
    Create a Sollya constant from a Sage RealNumber.
232 209 storres
    The sollya_lib_constant() function creates a constant
233 209 storres
    with the same precision as the source.
234 52 storres
    """
235 209 storres
    ## Precision stuff. If one wants to change precisions,
236 209 storres
    #  everything takes place in Sage. That only makes
237 209 storres
    #  sense if one wants to reduce the precision.
238 209 storres
    if not precisionSa is None:
239 209 storres
        RRR = RealField(precisionSa)
240 209 storres
        rnArgSa = RRR(rnArgSa)
241 209 storres
    #print rnArgSa, rnArgSa.precision()
242 115 storres
    # Sollya constant creation takes place here.
243 209 storres
    return sollya_lib_constant(get_rn_value(rnArgSa))
244 115 storres
# End pobyso_constant_sa_so
245 115 storres
246 55 storres
def pobyso_constant_0_sa_so():
247 115 storres
    """
248 115 storres
    Obvious.
249 115 storres
    """
250 55 storres
    return(pobyso_constant_from_int_sa_so(0))
251 55 storres
252 5 storres
def pobyso_constant_1():
253 115 storres
    """
254 115 storres
    Obvious.
255 115 storres
    Legacy function. See pobyso_constant_so_so.
256 115 storres
    """
257 52 storres
    return(pobyso_constant_1_sa_so())
258 5 storres
259 52 storres
def pobyso_constant_1_sa_so():
260 115 storres
    """
261 115 storres
    Obvious.
262 115 storres
    """
263 38 storres
    return(pobyso_constant_from_int_sa_so(1))
264 38 storres
265 5 storres
def pobyso_constant_from_int(anInt):
266 38 storres
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
267 38 storres
    return(pobyso_constant_from_int_sa_so(anInt))
268 38 storres
269 38 storres
def pobyso_constant_from_int_sa_so(anInt):
270 115 storres
    """
271 115 storres
    Get a Sollya constant from a Sage int.
272 115 storres
    """
273 200 storres
    return(sollya_lib_constant_from_int64(long(anInt)))
274 5 storres
275 84 storres
def pobyso_constant_from_int_so_sa(constSo):
276 84 storres
    """
277 117 storres
    Get a Sage int from a Sollya int constant.
278 115 storres
    Usefull for precision or powers in polynomials.
279 84 storres
    """
280 200 storres
    constSa = c_long(0)
281 200 storres
    sollya_lib_get_constant_as_int64(byref(constSa), constSo)
282 84 storres
    return(constSa.value)
283 84 storres
# End pobyso_constant_from_int_so_sa
284 84 storres
285 209 storres
def pobyso_constant_from_mpq_sa_so(rationalSa):
286 200 storres
    """
287 200 storres
    Make a Sollya constant from Sage rational.
288 209 storres
    The Sollya constant is an unevaluated expression.
289 209 storres
    Hence no precision argument is needed.
290 209 storres
    It is better to leave this way since Sollya has its own
291 209 storres
    optimized evaluation mecanism that tries very hard to
292 209 storres
    return exact values or at least faithful ones.
293 200 storres
    """
294 200 storres
    ratExprSo = \
295 200 storres
        sollya_lib_constant_from_mpq(sgmp_get_rational_value(rationalSa))
296 209 storres
    return ratExprSo
297 200 storres
# End pobyso_constant_from_mpq_sa_so.
298 200 storres
299 209 storres
def pobyso_constant_sollya_prec_sa_so(rnArgSa):
300 209 storres
    """
301 209 storres
    Create a Sollya constant from a Sage RealNumber at the
302 209 storres
    current precision in Sollya.
303 209 storres
    """
304 209 storres
    currentSollyaPrecSa = pobyso_get_prec_so_sa()
305 209 storres
    return pobyso_constant_sa_so(rnArgSa, currentSollyaPrecSa)
306 209 storres
# End pobyso_constant_sollya_prec_sa_so
307 209 storres
308 155 storres
def pobyso_error_so():
309 155 storres
    return sollya_lib_error(None)
310 155 storres
# End pobyso_error().
311 155 storres
312 209 storres
def pobyso_float_poly_sa_so(polySa, precSa = None):
313 209 storres
    """
314 209 storres
    Create a Sollya polynomial from a Sage RealField polynomial.
315 209 storres
    """
316 209 storres
    ## TODO: filter arguments.
317 209 storres
    ## Precision. If a precision is given, convert the polynomial
318 209 storres
    #  into the right polynomial field. If not convert it straight
319 209 storres
    #  to Sollya.
320 209 storres
    if not precSa is None:
321 209 storres
        RRR = RealField(precSa)
322 209 storres
        ## Create a Sage polynomial in the "right" precision.
323 209 storres
        P_RRR = RRR[polySa.variables()[0]]
324 209 storres
        polyFloatSa = P_RRR(polySa)
325 209 storres
    else:
326 209 storres
        polyFloatSa = polySa
327 209 storres
        precSa = polySa.parent().base_ring().precision()
328 209 storres
    ## Get exponents and coefficients.
329 213 storres
    exponentsSa = polyFloatSa.exponents()
330 209 storres
    coefficientsSa = polyFloatSa.coefficients()
331 209 storres
    ## Build the polynomial.
332 209 storres
    polySo = None
333 213 storres
    for coefficientSa, exponentSa in zip(coefficientsSa, exponentsSa):
334 209 storres
        #print coefficientSa.n(prec=precSa), exponentSa
335 209 storres
        coefficientSo = \
336 209 storres
            pobyso_constant_sa_so(coefficientSa)
337 209 storres
        #pobyso_autoprint(coefficientSo)
338 209 storres
        exponentSo = \
339 209 storres
            pobyso_constant_from_int_sa_so(exponentSa)
340 209 storres
        #pobyso_autoprint(exponentSo)
341 209 storres
        monomialSo = sollya_lib_build_function_pow(
342 209 storres
                       sollya_lib_build_function_free_variable(),
343 209 storres
                       exponentSo)
344 209 storres
        if polySo is None:
345 209 storres
            polySo = sollya_lib_build_function_mul(coefficientSo,
346 209 storres
                                                   monomialSo)
347 209 storres
        else:
348 209 storres
            polyTermSo = sollya_lib_build_function_mul(coefficientSo,
349 209 storres
                                                       monomialSo)
350 209 storres
            polySo = sollya_lib_build_function_add(polySo, polyTermSo)
351 209 storres
    return polySo
352 209 storres
# End pobyso_float_poly_sa_so
353 209 storres
354 209 storres
def pobyso_float_poly_so_sa(polySo, realFieldSa=None):
355 209 storres
    """
356 209 storres
    Convert a Sollya polynomial into a Sage floating-point polynomial.
357 209 storres
    We assume that the polynomial is in canonical form.
358 209 storres
    If no realField is given, a RealField corresponding to the maximum
359 209 storres
    precision of the coefficients is internally computed.
360 209 storres
    The real field is not returned but can be easily retrieved from
361 209 storres
    the polynomial itself.
362 209 storres
    ALGORITHM:
363 209 storres
    - (optional) compute the RealField of the coefficients;
364 209 storres
    - convert the Sollya expression into a Sage expression;
365 209 storres
    - convert the Sage expression into a Sage polynomial
366 209 storres
    TODO: the canonical thing for the polynomial.
367 209 storres
    """
368 209 storres
    if realFieldSa is None:
369 209 storres
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
370 209 storres
        realFieldSa      = RealField(expressionPrecSa)
371 209 storres
    #print "Sollya expression before...",
372 209 storres
    #pobyso_autoprint(polySo)
373 209 storres
374 209 storres
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo,
375 209 storres
                                                             realFieldSa)
376 209 storres
    #print "...Sollya expression after.",
377 209 storres
    #pobyso_autoprint(polySo)
378 209 storres
    polyVariableSa = expressionSa.variables()[0]
379 209 storres
    polyRingSa     = realFieldSa[str(polyVariableSa)]
380 209 storres
    #print polyRingSa
381 209 storres
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
382 209 storres
    polynomialSa = polyRingSa(expressionSa)
383 209 storres
    return polynomialSa
384 209 storres
# End pobyso_float_poly_so_sa
385 209 storres
386 209 storres
387 5 storres
def pobyso_function_type_as_string(funcType):
388 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
389 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
390 38 storres
391 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
392 38 storres
    """
393 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
394 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
395 38 storres
    """
396 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
397 5 storres
        return "abs"
398 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
399 5 storres
        return "arccos"
400 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
401 5 storres
        return "arccosh"
402 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
403 5 storres
        return "+"
404 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
405 5 storres
        return "arcsin"
406 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
407 5 storres
        return "arcsinh"
408 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
409 5 storres
        return "arctan"
410 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
411 5 storres
        return "arctanh"
412 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
413 5 storres
        return "ceil"
414 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
415 5 storres
        return "cte"
416 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
417 5 storres
        return "cos"
418 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
419 5 storres
        return "cosh"
420 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
421 5 storres
        return "/"
422 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
423 5 storres
        return "double"
424 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
425 5 storres
        return "doubleDouble"
426 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
427 5 storres
        return "doubleDxtended"
428 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
429 5 storres
        return "erf"
430 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
431 5 storres
        return "erfc"
432 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
433 5 storres
        return "exp"
434 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
435 5 storres
        return "expm1"
436 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
437 5 storres
        return "floor"
438 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
439 5 storres
        return "freeVariable"
440 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
441 5 storres
        return "halfPrecision"
442 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
443 5 storres
        return "libraryConstant"
444 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
445 5 storres
        return "libraryFunction"
446 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
447 5 storres
        return "log"
448 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
449 5 storres
        return "log10"
450 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
451 5 storres
        return "log1p"
452 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
453 5 storres
        return "log2"
454 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
455 5 storres
        return "*"
456 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
457 5 storres
        return "round"
458 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
459 5 storres
        return "__neg__"
460 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
461 5 storres
        return "pi"
462 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
463 5 storres
        return "^"
464 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
465 5 storres
        return "procedureFunction"
466 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
467 5 storres
        return "quad"
468 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
469 5 storres
        return "sin"
470 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
471 5 storres
        return "single"
472 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
473 5 storres
        return "sinh"
474 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
475 5 storres
        return "sqrt"
476 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
477 5 storres
        return "-"
478 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
479 5 storres
        return "tan"
480 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
481 5 storres
        return "tanh"
482 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
483 5 storres
        return "tripleDouble"
484 5 storres
    else:
485 5 storres
        return None
486 5 storres
487 85 storres
def pobyso_get_constant(rnArgSa, constSo):
488 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
489 209 storres
    return pobyso_get_constant_so_sa(rnArgSa, constSo)
490 209 storres
# End pobyso_get_constant
491 38 storres
492 84 storres
def pobyso_get_constant_so_sa(rnArgSa, constSo):
493 52 storres
    """
494 85 storres
    Set the value of rnArgSo to the value of constSo in MPFR_RNDN mode.
495 52 storres
    rnArg must already exist and belong to some RealField.
496 85 storres
    We assume that constSo points to a Sollya constant.
497 52 storres
    """
498 209 storres
    outcome = sollya_lib_get_constant(get_rn_value(rnArgSa), constSo)
499 209 storres
    if outcome == 0: # Failure because constSo is not a constant expression.
500 209 storres
        return None
501 209 storres
    else:
502 209 storres
        return outcome
503 209 storres
# End  pobyso_get_constant_so_sa
504 209 storres
505 57 storres
def pobyso_get_constant_as_rn(ctExpSo):
506 83 storres
    """
507 83 storres
    Legacy function. See pobyso_get_constant_as_rn_so_sa.
508 83 storres
    """
509 57 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
510 38 storres
511 56 storres
def pobyso_get_constant_as_rn_so_sa(constExpSo):
512 83 storres
    """
513 83 storres
    Get a Sollya constant as a Sage "real number".
514 83 storres
    The precision of the floating-point number returned is that of the Sollya
515 83 storres
    constant.
516 83 storres
    """
517 209 storres
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
518 209 storres
    ## If the expression can not be exactly converted, None is returned.
519 209 storres
    #  In this case opt for the Sollya current expression.
520 209 storres
    if precisionSa is None:
521 209 storres
        precisionSa = pobyso_get_prec_so_sa()
522 56 storres
    RRRR = RealField(precisionSa)
523 56 storres
    rnSa = RRRR(0)
524 209 storres
    outcome = sollya_lib_get_constant(get_rn_value(rnSa), constExpSo)
525 209 storres
    if outcome == 0:
526 209 storres
        return None
527 209 storres
    else:
528 209 storres
        return rnSa
529 83 storres
# End pobyso_get_constant_as_rn_so_sa
530 38 storres
531 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
532 83 storres
    """
533 83 storres
    Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
534 83 storres
    """
535 209 storres
    return pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField)
536 209 storres
# End pobyso_get_constant_as_rn_with_rf
537 5 storres
538 56 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None):
539 83 storres
    """
540 83 storres
    Get a Sollya constant as a Sage "real number".
541 83 storres
    If no real field is specified, the precision of the floating-point number
542 85 storres
    returned is that of the Sollya constant.
543 83 storres
    Otherwise is is that of the real field. Hence rounding may happen.
544 83 storres
    """
545 56 storres
    if realFieldSa is None:
546 209 storres
        return pobyso_get_constant_as_rn_so_sa(ctExpSo)
547 56 storres
    rnSa = realFieldSa(0)
548 209 storres
    outcome = sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo)
549 209 storres
    if outcome == 0:
550 209 storres
        return None
551 209 storres
    else:
552 209 storres
        return rnSa
553 83 storres
# End pobyso_get_constant_as_rn_with_rf_so_sa
554 38 storres
555 5 storres
def pobyso_get_free_variable_name():
556 83 storres
    """
557 83 storres
    Legacy function. See pobyso_get_free_variable_name_so_sa.
558 83 storres
    """
559 38 storres
    return(pobyso_get_free_variable_name_so_sa())
560 38 storres
561 38 storres
def pobyso_get_free_variable_name_so_sa():
562 209 storres
    return sollya_lib_get_free_variable_name()
563 5 storres
564 38 storres
def pobyso_get_function_arity(expressionSo):
565 83 storres
    """
566 83 storres
    Legacy function. See pobyso_get_function_arity_so_sa.
567 83 storres
    """
568 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
569 38 storres
570 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
571 5 storres
    arity = c_int(0)
572 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
573 209 storres
    return int(arity.value)
574 5 storres
575 38 storres
def pobyso_get_head_function(expressionSo):
576 83 storres
    """
577 83 storres
    Legacy function. See pobyso_get_head_function_so_sa.
578 83 storres
    """
579 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
580 38 storres
581 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
582 5 storres
    functionType = c_int(0)
583 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
584 209 storres
    return int(functionType.value)
585 5 storres
586 56 storres
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
587 53 storres
    """
588 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
589 83 storres
    If no reaIntervalField is passed as an argument, the interval bounds are not
590 56 storres
    rounded: they are elements of RealIntervalField of the "right" precision
591 56 storres
    to hold all the digits.
592 53 storres
    """
593 53 storres
    prec = c_int(0)
594 56 storres
    if realIntervalFieldSa is None:
595 56 storres
        retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
596 56 storres
        if retval == 0:
597 209 storres
            return None
598 56 storres
        realIntervalFieldSa = RealIntervalField(prec.value)
599 56 storres
    intervalSa = realIntervalFieldSa(0,0)
600 53 storres
    retval = \
601 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
602 53 storres
                                           soRange)
603 53 storres
    if retval == 0:
604 209 storres
        return None
605 209 storres
    return intervalSa
606 56 storres
# End pobyso_get_interval_from_range_so_sa
607 56 storres
608 5 storres
def pobyso_get_list_elements(soObj):
609 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
610 209 storres
    return pobyso_get_list_elements_so_so(soObj)
611 38 storres
612 117 storres
def pobyso_get_list_elements_so_so(objectListSo):
613 51 storres
    """
614 118 storres
    Get the Sollya list elements as a Sage/Python array of Sollya objects.
615 118 storres

616 118 storres
    INPUT:
617 118 storres
    - objectListSo: a Sollya list of Sollya objects.
618 118 storres

619 118 storres
    OUTPUT:
620 118 storres
    - a Sage/Python tuple made of:
621 118 storres
      - a Sage/Python list of Sollya objects,
622 118 storres
      - a Sage/Python int holding the number of elements,
623 118 storres
      - a Sage/Python int stating (!= 0) that the list is end-elliptic.
624 118 storres
    NOTE::
625 118 storres
        We recover the addresses of the Sollya object from the list of pointers
626 118 storres
        returned by sollya_lib_get_list_elements. The list itself is freed.
627 118 storres
    TODO::
628 118 storres
        Figure out what to do with numElements since the number of elements
629 118 storres
        can easily be recovered from the list itself.
630 118 storres
        Ditto for isEndElliptic.
631 51 storres
    """
632 5 storres
    listAddress = POINTER(c_longlong)()
633 5 storres
    numElements = c_int(0)
634 5 storres
    isEndElliptic = c_int(0)
635 117 storres
    listAsSageList = []
636 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
637 54 storres
                                          byref(numElements),\
638 54 storres
                                          byref(isEndElliptic),\
639 117 storres
                                          objectListSo)
640 5 storres
    if result == 0 :
641 5 storres
        return None
642 5 storres
    for i in xrange(0, numElements.value, 1):
643 118 storres
       #listAsSageList.append(sollya_lib_copy_obj(listAddress[i]))
644 118 storres
       listAsSageList.append(listAddress[i])
645 117 storres
       # Clear each of the elements returned by Sollya.
646 118 storres
       #sollya_lib_clear_obj(listAddress[i])
647 117 storres
    # Free the list itself.
648 117 storres
    sollya_lib_free(listAddress)
649 209 storres
    return (listAsSageList, numElements.value, isEndElliptic.value)
650 5 storres
651 38 storres
def pobyso_get_max_prec_of_exp(soExp):
652 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
653 209 storres
    return pobyso_get_max_prec_of_exp_so_sa(soExp)
654 5 storres
655 85 storres
def pobyso_get_max_prec_of_exp_so_sa(expSo):
656 38 storres
    """
657 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
658 52 storres

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