Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 199

Historique | Voir | Annoter | Télécharger (52,88 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 5 storres
    return(sollya_lib_constant_from_int(int(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 84 storres
    constSa = c_int(0)
284 84 storres
    sollya_lib_get_constant_as_int(byref(constSa), constSo)
285 84 storres
    return(constSa.value)
286 84 storres
# End pobyso_constant_from_int_so_sa
287 84 storres
288 155 storres
def pobyso_error_so():
289 155 storres
    return sollya_lib_error(None)
290 155 storres
# End pobyso_error().
291 155 storres
292 5 storres
def pobyso_function_type_as_string(funcType):
293 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
294 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
295 38 storres
296 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
297 38 storres
    """
298 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
299 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
300 38 storres
    """
301 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
302 5 storres
        return "abs"
303 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
304 5 storres
        return "arccos"
305 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
306 5 storres
        return "arccosh"
307 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
308 5 storres
        return "+"
309 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
310 5 storres
        return "arcsin"
311 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
312 5 storres
        return "arcsinh"
313 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
314 5 storres
        return "arctan"
315 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
316 5 storres
        return "arctanh"
317 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
318 5 storres
        return "ceil"
319 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
320 5 storres
        return "cte"
321 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
322 5 storres
        return "cos"
323 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
324 5 storres
        return "cosh"
325 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
326 5 storres
        return "/"
327 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
328 5 storres
        return "double"
329 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
330 5 storres
        return "doubleDouble"
331 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
332 5 storres
        return "doubleDxtended"
333 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
334 5 storres
        return "erf"
335 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
336 5 storres
        return "erfc"
337 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
338 5 storres
        return "exp"
339 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
340 5 storres
        return "expm1"
341 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
342 5 storres
        return "floor"
343 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
344 5 storres
        return "freeVariable"
345 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
346 5 storres
        return "halfPrecision"
347 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
348 5 storres
        return "libraryConstant"
349 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
350 5 storres
        return "libraryFunction"
351 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
352 5 storres
        return "log"
353 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
354 5 storres
        return "log10"
355 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
356 5 storres
        return "log1p"
357 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
358 5 storres
        return "log2"
359 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
360 5 storres
        return "*"
361 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
362 5 storres
        return "round"
363 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
364 5 storres
        return "__neg__"
365 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
366 5 storres
        return "pi"
367 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
368 5 storres
        return "^"
369 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
370 5 storres
        return "procedureFunction"
371 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
372 5 storres
        return "quad"
373 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
374 5 storres
        return "sin"
375 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
376 5 storres
        return "single"
377 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
378 5 storres
        return "sinh"
379 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
380 5 storres
        return "sqrt"
381 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
382 5 storres
        return "-"
383 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
384 5 storres
        return "tan"
385 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
386 5 storres
        return "tanh"
387 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
388 5 storres
        return "tripleDouble"
389 5 storres
    else:
390 5 storres
        return None
391 5 storres
392 85 storres
def pobyso_get_constant(rnArgSa, constSo):
393 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
394 85 storres
    return(pobyso_get_constant_so_sa(rnArgSa, constSo))
395 38 storres
396 84 storres
def pobyso_get_constant_so_sa(rnArgSa, constSo):
397 52 storres
    """
398 85 storres
    Set the value of rnArgSo to the value of constSo in MPFR_RNDN mode.
399 52 storres
    rnArg must already exist and belong to some RealField.
400 85 storres
    We assume that constSo points to a Sollya constant.
401 52 storres
    """
402 84 storres
    return(sollya_lib_get_constant(get_rn_value(rnArgSa), constSo))
403 5 storres
404 57 storres
def pobyso_get_constant_as_rn(ctExpSo):
405 83 storres
    """
406 83 storres
    Legacy function. See pobyso_get_constant_as_rn_so_sa.
407 83 storres
    """
408 57 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExpSo))
409 38 storres
410 56 storres
def pobyso_get_constant_as_rn_so_sa(constExpSo):
411 83 storres
    """
412 83 storres
    Get a Sollya constant as a Sage "real number".
413 83 storres
    The precision of the floating-point number returned is that of the Sollya
414 83 storres
    constant.
415 83 storres
    """
416 57 storres
    precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
417 56 storres
    RRRR = RealField(precisionSa)
418 56 storres
    rnSa = RRRR(0)
419 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), constExpSo)
420 56 storres
    return(rnSa)
421 83 storres
# End pobyso_get_constant_as_rn_so_sa
422 38 storres
423 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
424 83 storres
    """
425 83 storres
    Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa.
426 83 storres
    """
427 38 storres
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
428 5 storres
429 56 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExpSo, realFieldSa = None):
430 83 storres
    """
431 83 storres
    Get a Sollya constant as a Sage "real number".
432 83 storres
    If no real field is specified, the precision of the floating-point number
433 85 storres
    returned is that of the Sollya constant.
434 83 storres
    Otherwise is is that of the real field. Hence rounding may happen.
435 83 storres
    """
436 56 storres
    if realFieldSa is None:
437 85 storres
        sollyaPrecSa = pobyso_get_prec_of_constant_so_sa(ctExpSo)
438 56 storres
        realFieldSa = RealField(sollyaPrecSa)
439 56 storres
    rnSa = realFieldSa(0)
440 56 storres
    sollya_lib_get_constant(get_rn_value(rnSa), ctExpSo)
441 56 storres
    return(rnSa)
442 83 storres
# End pobyso_get_constant_as_rn_with_rf_so_sa
443 38 storres
444 5 storres
def pobyso_get_free_variable_name():
445 83 storres
    """
446 83 storres
    Legacy function. See pobyso_get_free_variable_name_so_sa.
447 83 storres
    """
448 38 storres
    return(pobyso_get_free_variable_name_so_sa())
449 38 storres
450 38 storres
def pobyso_get_free_variable_name_so_sa():
451 5 storres
    return(sollya_lib_get_free_variable_name())
452 5 storres
453 38 storres
def pobyso_get_function_arity(expressionSo):
454 83 storres
    """
455 83 storres
    Legacy function. See pobyso_get_function_arity_so_sa.
456 83 storres
    """
457 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
458 38 storres
459 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
460 5 storres
    arity = c_int(0)
461 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
462 5 storres
    return(int(arity.value))
463 5 storres
464 38 storres
def pobyso_get_head_function(expressionSo):
465 83 storres
    """
466 83 storres
    Legacy function. See pobyso_get_head_function_so_sa.
467 83 storres
    """
468 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
469 38 storres
470 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
471 5 storres
    functionType = c_int(0)
472 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
473 5 storres
    return(int(functionType.value))
474 5 storres
475 56 storres
def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
476 53 storres
    """
477 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
478 83 storres
    If no reaIntervalField is passed as an argument, the interval bounds are not
479 56 storres
    rounded: they are elements of RealIntervalField of the "right" precision
480 56 storres
    to hold all the digits.
481 53 storres
    """
482 53 storres
    prec = c_int(0)
483 56 storres
    if realIntervalFieldSa is None:
484 56 storres
        retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
485 56 storres
        if retval == 0:
486 56 storres
            return(None)
487 56 storres
        realIntervalFieldSa = RealIntervalField(prec.value)
488 56 storres
    intervalSa = realIntervalFieldSa(0,0)
489 53 storres
    retval = \
490 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
491 53 storres
                                           soRange)
492 53 storres
    if retval == 0:
493 53 storres
        return(None)
494 53 storres
    return(intervalSa)
495 56 storres
# End pobyso_get_interval_from_range_so_sa
496 56 storres
497 5 storres
def pobyso_get_list_elements(soObj):
498 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
499 38 storres
    return(pobyso_get_list_elements_so_so(soObj))
500 38 storres
501 117 storres
def pobyso_get_list_elements_so_so(objectListSo):
502 51 storres
    """
503 118 storres
    Get the Sollya list elements as a Sage/Python array of Sollya objects.
504 118 storres

505 118 storres
    INPUT:
506 118 storres
    - objectListSo: a Sollya list of Sollya objects.
507 118 storres

508 118 storres
    OUTPUT:
509 118 storres
    - a Sage/Python tuple made of:
510 118 storres
      - a Sage/Python list of Sollya objects,
511 118 storres
      - a Sage/Python int holding the number of elements,
512 118 storres
      - a Sage/Python int stating (!= 0) that the list is end-elliptic.
513 118 storres
    NOTE::
514 118 storres
        We recover the addresses of the Sollya object from the list of pointers
515 118 storres
        returned by sollya_lib_get_list_elements. The list itself is freed.
516 118 storres
    TODO::
517 118 storres
        Figure out what to do with numElements since the number of elements
518 118 storres
        can easily be recovered from the list itself.
519 118 storres
        Ditto for isEndElliptic.
520 51 storres
    """
521 5 storres
    listAddress = POINTER(c_longlong)()
522 5 storres
    numElements = c_int(0)
523 5 storres
    isEndElliptic = c_int(0)
524 117 storres
    listAsSageList = []
525 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
526 54 storres
                                          byref(numElements),\
527 54 storres
                                          byref(isEndElliptic),\
528 117 storres
                                          objectListSo)
529 5 storres
    if result == 0 :
530 5 storres
        return None
531 5 storres
    for i in xrange(0, numElements.value, 1):
532 118 storres
       #listAsSageList.append(sollya_lib_copy_obj(listAddress[i]))
533 118 storres
       listAsSageList.append(listAddress[i])
534 117 storres
       # Clear each of the elements returned by Sollya.
535 118 storres
       #sollya_lib_clear_obj(listAddress[i])
536 117 storres
    # Free the list itself.
537 117 storres
    sollya_lib_free(listAddress)
538 117 storres
    return(listAsSageList, numElements.value, isEndElliptic.value)
539 5 storres
540 38 storres
def pobyso_get_max_prec_of_exp(soExp):
541 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
542 38 storres
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
543 5 storres
544 85 storres
def pobyso_get_max_prec_of_exp_so_sa(expSo):
545 38 storres
    """
546 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
547 52 storres

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