Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 64

Historique | Voir | Annoter | Télécharger (37,03 ko)

1 5 storres
"""
2 5 storres
Actual functions to use in Sage
3 5 storres
ST 2012-11-13
4 5 storres

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

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

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

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

404 52 storres
    Arguments:
405 52 storres
    soExp -- a Sollya expression pointer
406 52 storres
    Return value:
407 52 storres
    A Python integer
408 38 storres
    TODO:
409 38 storres
    - error management;
410 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
411 38 storres
    """
412 5 storres
    maxPrecision = 0
413 52 storres
    minConstPrec = 0
414 52 storres
    currentConstPrec = 0
415 38 storres
    operator = pobyso_get_head_function_so_sa(soExp)
416 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
417 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
418 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp)
419 5 storres
        for i in xrange(arity):
420 5 storres
            maxPrecisionCandidate = \
421 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
422 5 storres
            if maxPrecisionCandidate > maxPrecision:
423 5 storres
                maxPrecision = maxPrecisionCandidate
424 5 storres
        return(maxPrecision)
425 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
426 52 storres
        minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
427 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
428 52 storres
        #print minConstPrec, " - ", currentConstPrec
429 52 storres
        return(pobyso_get_min_prec_of_constant_so_sa(soExp))
430 52 storres
431 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
432 5 storres
        return(0)
433 5 storres
    else:
434 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
435 5 storres
        return(0)
436 5 storres
437 52 storres
def pobyso_get_min_prec_of_constant_so_sa(soConstExp):
438 52 storres
    """
439 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
440 52 storres
    constant.
441 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
442 52 storres
    We assume that soCteExp is a point
443 52 storres
    """
444 52 storres
    constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp)
445 52 storres
    return(min_mpfr_size(get_rn_value(constExpAsRn)))
446 52 storres
447 5 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
448 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
449 38 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
450 38 storres
451 38 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR):
452 5 storres
    """
453 38 storres
    Get a Sage expression from a Sollya expression.
454 38 storres
    Currently only tested with polynomials with floating-point coefficients.
455 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
456 5 storres
    """
457 5 storres
    #pobyso_autoprint(sollyaExp)
458 55 storres
    operator = pobyso_get_head_function_so_sa(sollyaExp)
459 5 storres
    # Constants and the free variable are special cases.
460 5 storres
    # All other operator are dealt with in the same way.
461 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
462 5 storres
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
463 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp)
464 5 storres
        if arity == 1:
465 38 storres
            sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
466 52 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \
467 52 storres
            realField) + ")")
468 5 storres
        elif arity == 2:
469 63 storres
            # We do not get through the preprocessor.
470 63 storres
            # The "^" operator is then a special case.
471 5 storres
            if operator == SOLLYA_BASE_FUNC_POW:
472 5 storres
                operatorAsString = "**"
473 5 storres
            else:
474 52 storres
                operatorAsString = \
475 52 storres
                    pobyso_function_type_as_string_so_sa(operator)
476 5 storres
            sageExp = \
477 38 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\
478 5 storres
              + " " + operatorAsString + " " + \
479 38 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
480 63 storres
        # We do not know yet how to deal with arity >= 3
481 63 storres
        # (is there any in Sollya anyway?).
482 5 storres
        else:
483 5 storres
            sageExp = eval('None')
484 5 storres
        return(sageExp)
485 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
486 5 storres
        #print "This is a constant"
487 38 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
488 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
489 5 storres
        #print "This is free variable"
490 5 storres
        return(eval(sollya_lib_get_free_variable_name()))
491 5 storres
    else:
492 5 storres
        print "Unexpected"
493 5 storres
        return eval('None')
494 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
495 62 storres
def pobyso_get_poly_so_sa(polySo, realFieldSa=None):
496 57 storres
    """
497 57 storres
    Convert a Sollya polynomial into a Sage polynomial.
498 62 storres
    We assume that the polynomial is in canonical form.
499 57 storres
    If no realField is given, a RealField corresponding to the maximum precision
500 57 storres
    of the coefficients is internally computed.
501 57 storres
    It is not returned but can be easily retrieved from the polynomial itself.
502 57 storres
    Main steps:
503 57 storres
    - (optional) compute the RealField of the coefficients;
504 57 storres
    - convert the Sollya expression into a Sage expression;
505 57 storres
    - convert the Sage expression into a Sage polynomial
506 59 storres
    TODO: the canonical thing for the polynomial.
507 57 storres
    """
508 57 storres
    if realFieldSa is None:
509 57 storres
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
510 57 storres
        realFieldSa = RealField(expressionPrecSa)
511 63 storres
    #print "Sollya expression before...",
512 63 storres
    #pobyso_autoprint(polySo)
513 63 storres
514 57 storres
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \
515 57 storres
                                                             realFieldSa)
516 63 storres
    #print "...Sollya expression after.",
517 63 storres
    #pobyso_autoprint(polySo)
518 57 storres
    polyVariableSa = expressionSa.variables()[0]
519 59 storres
    polyRingSa = realFieldSa[str(polyVariableSa)]
520 59 storres
    print polyRingSa
521 62 storres
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
522 62 storres
    polynomialSa = polyRingSa(expressionSa)
523 57 storres
    return(polynomialSa)
524 57 storres
# End pobyso_get_sage_poly_from_sollya_poly
525 57 storres
526 38 storres
def pobyso_get_subfunctions(expressionSo):
527 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
528 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
529 38 storres
530 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
531 38 storres
    """
532 38 storres
    Get the subfunctions of an expression.
533 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
534 55 storres
    S.T.: Could not figure out another way than that ugly list of declarations
535 55 storres
    to recover the addresses of the subfunctions.
536 38 storres
    """
537 5 storres
    subf0 = c_int(0)
538 5 storres
    subf1 = c_int(0)
539 5 storres
    subf2 = c_int(0)
540 5 storres
    subf3 = c_int(0)
541 5 storres
    subf4 = c_int(0)
542 5 storres
    subf5 = c_int(0)
543 5 storres
    subf6 = c_int(0)
544 5 storres
    subf7 = c_int(0)
545 5 storres
    subf8 = c_int(0)
546 5 storres
    arity = c_int(0)
547 5 storres
    nullPtr = POINTER(c_int)()
548 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
549 5 storres
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
550 5 storres
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
551 5 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
552 5 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
553 5 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
554 5 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
555 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
556 5 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
557 5 storres
    subs = []
558 5 storres
    if arity.value > pobyso_max_arity:
559 38 storres
        return(0,[])
560 5 storres
    for i in xrange(arity.value):
561 5 storres
        subs.append(int(subfunctions[i].value))
562 5 storres
        #print subs[i]
563 5 storres
    return(int(arity.value), subs)
564 5 storres
565 5 storres
def pobyso_get_prec():
566 38 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
567 38 storres
    return(pobyso_get_prec_so_sa())
568 38 storres
569 38 storres
def pobyso_get_prec_so_sa():
570 38 storres
    """
571 38 storres
    Get the current default precision in Sollya.
572 38 storres
    The return value is Sage/Python int.
573 38 storres
    """
574 56 storres
    precSo = sollya_lib_get_prec(None)
575 56 storres
    precSa = c_int(0)
576 56 storres
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
577 56 storres
    sollya_lib_clear_obj(precSo)
578 56 storres
    return(int(precSa.value))
579 5 storres
580 38 storres
def pobyso_get_prec_of_constant(ctExpSo):
581 38 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
582 38 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
583 38 storres
584 56 storres
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
585 56 storres
    prec = c_int(0)
586 56 storres
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
587 56 storres
    if retc == 0:
588 56 storres
        return(None)
589 56 storres
    return(int(prec.value))
590 56 storres
591 53 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
592 5 storres
    prec = c_int(0)
593 53 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
594 56 storres
    if retc == 0:
595 56 storres
        return(None)
596 5 storres
    return(int(prec.value))
597 5 storres
598 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
599 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
600 54 storres
    return(None)
601 53 storres
602 37 storres
def pobyso_lib_init():
603 37 storres
    sollya_lib_init(None)
604 37 storres
605 37 storres
def pobyso_name_free_variable(freeVariableName):
606 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
607 38 storres
    pobyso_name_free_variable_sa_so(freeVariableName)
608 38 storres
609 38 storres
def pobyso_name_free_variable_sa_so(freeVariableName):
610 37 storres
    sollya_lib_name_free_variable(freeVariableName)
611 37 storres
612 5 storres
def pobyso_parse_string(string):
613 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
614 38 storres
    return(pobyso_parse_string_sa_so(string))
615 38 storres
616 38 storres
def pobyso_parse_string_sa_so(string):
617 5 storres
    return(sollya_lib_parse_string(string))
618 5 storres
619 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
620 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
621 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
622 38 storres
623 62 storres
def pobyso_bounds_to_range_sa_so(rnLowerBoundSa, rnUpperBoundSa):
624 56 storres
    """
625 56 storres
    Return a Sollya range from to 2 RealField elements.
626 56 storres
    The Sollya range element has a sufficient precision to hold all
627 56 storres
    the digits of the bounds.
628 56 storres
    """
629 62 storres
    if rnLowerBoundSa > rnUpperBoundSa:
630 62 storres
        return None
631 56 storres
    lbPrec = rnLowerBoundSa.parent().precision()
632 56 storres
    ubPrec = rnLowerBoundSa.parent().precision()
633 56 storres
    currentSollyaPrecSa = pobyso_get_prec_so_sa()
634 56 storres
    maxPrecSa = max(lbPrec, ubPrec, currentSollyaPrecSa)
635 56 storres
    # Change the current Sollya precision only if necessary.
636 56 storres
    if maxPrecSa > currentSollyaPrecSa:
637 56 storres
        currentPrecSo = sollya_lib_get_prec(None)
638 56 storres
        newPrecSo = solly_lib_constant_from_uint64(maxPrecSa)
639 56 storres
        sollya_lib_set_prec(newPrecSo)
640 56 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
641 56 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
642 5 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
643 56 storres
    currentPrecSo = sollya_lib_get_prec(None)
644 56 storres
    if maxPrecSa > currentSollyaPrecSa:
645 56 storres
        sollya_lib_set_prec(currentPrecSo)
646 56 storres
        sollya_lib_clear_obj(currentPrecSo)
647 56 storres
        sollya_lib_clear_obj(newPrecSo)
648 56 storres
    sollya_lib_clear_obj(lowerBoundSo)
649 56 storres
    sollya_lib_clear_obj(upperBoundSo)
650 5 storres
    return(rangeSo)
651 5 storres
652 62 storres
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalField = None):
653 56 storres
    if realIntervalField is None:
654 56 storres
        precSa = pobyso_get_prec_of_range_so_sa(rangeSo)
655 56 storres
        realIntervalField = RealIntervalField(precSa)
656 56 storres
    intervalSa = \
657 56 storres
        pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalField)
658 56 storres
    return(intervalSa)
659 56 storres
660 52 storres
def pobyso_remez_canonical_sa_sa(func, \
661 52 storres
                                 degree, \
662 52 storres
                                 lowerBound, \
663 52 storres
                                 upperBound, \
664 52 storres
                                 weight = None, \
665 52 storres
                                 quality = None):
666 52 storres
    """
667 52 storres
    All arguments are Sage/Python.
668 52 storres
    The functions (func and weight) must be passed as expressions or strings.
669 52 storres
    Otherwise the function fails.
670 52 storres
    The return value is a pointer is a Sage polynomial.
671 52 storres
    """
672 52 storres
    var('zorglub')    # Dummy variable name for type check only.
673 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
674 52 storres
                                 degree, \
675 52 storres
                                 lowerBound, \
676 52 storres
                                 upperBound, \
677 52 storres
                                 weight = None, \
678 52 storres
                                 quality = None)
679 52 storres
    if parent(func) == parent("string"):
680 52 storres
        functionSa = eval(func)
681 52 storres
    # Expression test.
682 52 storres
    elif type(func) == type(zorglub):
683 52 storres
        functionSa = func
684 52 storres
    maxPrecision = 0
685 52 storres
    if polySo is None:
686 52 storres
        return(None)
687 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
688 52 storres
    RRRR = RealField(maxPrecision)
689 52 storres
    polynomialRing = RRRR[functionSa.variables()[0]]
690 52 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR)
691 52 storres
    polySa = polynomial(expSa, polynomialRing)
692 52 storres
    return(polySa)
693 52 storres
694 38 storres
def pobyso_remez_canonical(func, \
695 5 storres
                           degree, \
696 5 storres
                           lowerBound, \
697 5 storres
                           upperBound, \
698 38 storres
                           weight = "1", \
699 5 storres
                           quality = None):
700 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
701 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
702 51 storres
                                        degree, \
703 51 storres
                                        lowerBound, \
704 51 storres
                                        upperBound, \
705 51 storres
                                        weight, \
706 51 storres
                                        quality))
707 38 storres
def pobyso_remez_canonical_sa_so(func, \
708 38 storres
                                 degree, \
709 38 storres
                                 lowerBound, \
710 38 storres
                                 upperBound, \
711 52 storres
                                 weight = None, \
712 38 storres
                                 quality = None):
713 38 storres
    """
714 38 storres
    All arguments are Sage/Python.
715 51 storres
    The functions (func and weight) must be passed as expressions or strings.
716 51 storres
    Otherwise the function fails.
717 38 storres
    The return value is a pointer to a Sollya function.
718 38 storres
    """
719 52 storres
    var('zorglub')    # Dummy variable name for type check only.
720 52 storres
    currentVariableName = None
721 52 storres
    # The func argument can be of different types (string,
722 52 storres
    # symbolic expression...)
723 38 storres
    if parent(func) == parent("string"):
724 38 storres
        functionSo = sollya_lib_parse_string(func)
725 51 storres
    # Expression test.
726 52 storres
    elif type(func) == type(zorglub):
727 52 storres
        # Until we are able to translate Sage expressions into Sollya
728 52 storres
        # expressions : parse the string version.
729 52 storres
        currentVariableName = func.variables()[0]
730 52 storres
        sollya_lib_name_free_variable(str(currentVariableName))
731 52 storres
        functionSo = sollya_lib_parse_string(func._assume_str())
732 38 storres
    else:
733 38 storres
        return(None)
734 52 storres
    if weight is None:
735 52 storres
        weightSo = pobyso_constant_1_sa_so()
736 52 storres
    elif parent(weight) == parent("string"):
737 51 storres
        weightSo = sollya_lib_parse_string(func)
738 51 storres
    elif type(weight) == type(zorglub):
739 51 storres
        functionSo = sollya_lib_parse_string_sa_so(weight._assume_str())
740 51 storres
    else:
741 51 storres
        return(None)
742 5 storres
    degreeSo = pobyso_constant_from_int(degree)
743 38 storres
    rangeSo = pobyso_range_sa_so(lowerBound, upperBound)
744 38 storres
    if not quality is None:
745 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
746 52 storres
    else:
747 52 storres
        qualitySo = None
748 52 storres
    return(sollya_lib_remez(functionSo, \
749 52 storres
                            degreeSo, \
750 52 storres
                            rangeSo, \
751 52 storres
                            weightSo, \
752 52 storres
                            qualitySo, \
753 52 storres
                            None))
754 5 storres
755 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
756 38 storres
                                 degreeSo, \
757 38 storres
                                 rangeSo, \
758 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
759 38 storres
                                 qualitySo = None):
760 38 storres
    """
761 38 storres
    All arguments are pointers to Sollya objects.
762 38 storres
    The return value is a pointer to a Sollya function.
763 38 storres
    """
764 38 storres
    if not sollya_lib_obj_is_function(funcSo):
765 38 storres
        return(None)
766 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
767 38 storres
768 5 storres
def pobyso_set_canonical_off():
769 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
770 5 storres
771 5 storres
def pobyso_set_canonical_on():
772 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
773 5 storres
774 5 storres
def pobyso_set_prec(p):
775 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
776 38 storres
    return( pobyso_set_prec_sa_so(p))
777 38 storres
778 38 storres
def pobyso_set_prec_sa_so(p):
779 5 storres
    a = c_int(p)
780 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
781 5 storres
    sollya_lib_set_prec(precSo)
782 5 storres
783 54 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo, accuracySo):
784 54 storres
    return(sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
785 54 storres
                              accuracySo))
786 54 storres
787 58 storres
def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, rangeSo, \
788 58 storres
                                                errorTypeSo, \
789 58 storres
                                                sollyaPrecSo=None):
790 58 storres
    """
791 58 storres
    Compute the Taylor expansion with the variable change
792 58 storres
    x -> (x-intervalCenter) included.
793 58 storres
    """
794 58 storres
    # No global change of the working precision.
795 58 storres
    if not sollyaPrecSo is None:
796 58 storres
        initialPrecSo = sollya_lib_get_prec(None)
797 58 storres
        sollya_lib_set_prec(sollyaPrecSo)
798 58 storres
    #
799 58 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
800 58 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
801 58 storres
                                         intervalCenterSo, \
802 58 storres
                                         rangeSo, errorTypeSo, None)
803 58 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
804 58 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
805 58 storres
    polySo = taylorFormListSo[0]
806 58 storres
    errorRangeSo = taylorFormListSo[2]
807 58 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
808 58 storres
    changeVarExpSo = sollya_lib_build_function_sub(\
809 58 storres
                       sollya_lib_build_function_free_variable(),\
810 58 storres
                       sollya_lib_copy_obj(intervalCenterSo))
811 58 storres
    polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo)
812 58 storres
    # If changed, reset the Sollya working precision.
813 58 storres
    if not sollyaPrecSo is None:
814 58 storres
        sollya_lib_set_prec(initialPrecSo)
815 58 storres
        sollya_lib_clear_obj(initailPrecSo)
816 63 storres
    return((polyVarChangedSo, intervalCenterSo, maxErrorSo))
817 58 storres
# end pobyso_taylor_expansion_with_change_var_so_so
818 58 storres
819 56 storres
def pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, rangeSo, \
820 56 storres
                                                errorTypeSo, \
821 56 storres
                                                sollyaPrecSo=None):
822 58 storres
    """
823 58 storres
    Compute the Taylor expansion without the variable change
824 58 storres
    x -> x-intervalCenter.
825 58 storres
    """
826 56 storres
    # No global change of the working precision.
827 56 storres
    if not sollyaPrecSo is None:
828 56 storres
        initialPrecSo = sollya_lib_get_prec(None)
829 56 storres
        sollya_lib_set_prec(sollyaPrecSo)
830 56 storres
    #
831 56 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
832 56 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
833 56 storres
                                         intervalCenterSo, \
834 56 storres
                                         rangeSo, errorTypeSo, None)
835 56 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
836 56 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
837 56 storres
    polySo = taylorFormListSo[0]
838 56 storres
    errorRangeSo = taylorFormListSo[2]
839 56 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
840 56 storres
    # If changed, reset the Sollya working precision.
841 56 storres
    if not sollyaPrecSo is None:
842 56 storres
        sollya_lib_set_prec(initialPrecSo)
843 63 storres
        sollya_lib_clear_obj(initialPrecSo)
844 63 storres
    return((polySo, intervalCenterSo, maxErrorSo))
845 56 storres
# end pobyso_taylor_expansion_no_change_var_so_so
846 56 storres
847 5 storres
def pobyso_taylor(function, degree, point):
848 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
849 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
850 38 storres
851 56 storres
def pobyso_taylor_so_so(functionSo, degreeSo, pointSo):
852 56 storres
    return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
853 5 storres
854 5 storres
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
855 38 storres
    """ Legacy function. See ;"""
856 38 storres
857 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
858 38 storres
                            degree, \
859 43 storres
                            point, \
860 43 storres
                            precision, \
861 54 storres
                            interval=None, \
862 38 storres
                            errorType=None):
863 37 storres
    """
864 38 storres
    Compute the Taylor form of 'degree' for 'functionSa' at 'point'
865 37 storres
    for 'interval' with 'errorType'.
866 37 storres
    point: must be a Real or a Real interval.
867 37 storres
    return the Taylor form as an array
868 38 storres
    TODO: take care of the interval and of point when it is an interval;
869 38 storres
          when errorType is not None;
870 38 storres
          take care of the other elements of the Taylor form (coefficients errors and
871 38 storres
          delta.
872 37 storres
    """
873 37 storres
    # Absolute as the default error.
874 5 storres
    if errorType is None:
875 37 storres
        errorTypeSo = sollya_lib_absolute()
876 37 storres
    else:
877 37 storres
        #TODO: deal with the other case.
878 37 storres
        pass
879 38 storres
    varSa = functionSa.variables()[0]
880 37 storres
    pointBaseRingString = str(point.base_ring())
881 37 storres
    if not re.search('Real', pointBaseRingString):
882 37 storres
        return None
883 37 storres
    # Call Sollya but first "sollyafy" the arguments.
884 38 storres
    pobyso_name_free_variable_sa_so(str(varSa))
885 38 storres
    #pobyso_set_prec_sa_so(300)
886 37 storres
    # Sollyafy the function.
887 38 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
888 37 storres
    if sollya_lib_obj_is_error(functionSo):
889 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
890 37 storres
        return None
891 37 storres
    # Sollyafy the degree
892 37 storres
    degreeSo = sollya_lib_constant_from_int(int(degree))
893 37 storres
    # Sollyafy the point
894 37 storres
    if not re.search('Interval', pointBaseRingString):
895 38 storres
        pointSo  = pobyso_constant_sa_so(point)
896 37 storres
    else:
897 37 storres
        # TODO: deal with the interval case.
898 37 storres
        pass
899 37 storres
    # Call Sollya
900 37 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
901 37 storres
                                         None)
902 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
903 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
904 37 storres
    polySo = tfsAsList[0]
905 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
906 37 storres
    polyRealField = RealField(maxPrecision)
907 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
908 37 storres
    sollya_lib_close()
909 37 storres
    polynomialRing = polyRealField[str(varSa)]
910 37 storres
    polySa = polynomial(expSa, polynomialRing)
911 37 storres
    taylorFormSa = [polySa]
912 51 storres
    return(taylorFormSa)
913 51 storres
# End pobyso_taylor_form_sa_sa
914 54 storres
915 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
916 54 storres
                            errorTypeSo=None):
917 54 storres
    createdErrorType = False
918 51 storres
    if errorTypeSo is None:
919 51 storres
        errorTypeSo = sollya_lib_absolute()
920 54 storres
        createdErrorType = True
921 51 storres
    else:
922 51 storres
        #TODO: deal with the other case.
923 51 storres
        pass
924 51 storres
    if intervalSo is None:
925 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
926 54 storres
                                         errorTypeSo, None)
927 51 storres
    else:
928 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
929 54 storres
                                         intervalSo, errorTypeSo, None)
930 54 storres
    if createdErrorType:
931 54 storres
        sollya_lib_clear_obj(errorTypeSo)
932 51 storres
    return(resultSo)
933 51 storres
934 37 storres
935 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
936 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
937 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
938 38 storres
939 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
940 37 storres
    """
941 37 storres
    Return the string representation of a univariate polynomial with
942 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
943 37 storres
    Remember: Sage
944 37 storres
    """
945 37 storres
    polynomialRing = polySa.base_ring()
946 37 storres
    # A very expensive solution:
947 37 storres
    # -create a fake multivariate polynomial field with only one variable,
948 37 storres
    #   specifying a negative lexicographical order;
949 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
950 37 storres
                                     polynomialRing.variable_name(), \
951 37 storres
                                     1, order='neglex')
952 37 storres
    # - convert the univariate argument polynomial into a multivariate
953 37 storres
    #   version;
954 37 storres
    p = mpolynomialRing(polySa)
955 37 storres
    # - return the string representation of the converted form.
956 37 storres
    # There is no simple str() method defined for p's class.
957 37 storres
    return(p.__str__())
958 5 storres
#
959 5 storres
print pobyso_get_prec()
960 5 storres
pobyso_set_prec(165)
961 5 storres
print pobyso_get_prec()
962 5 storres
a=100
963 5 storres
print type(a)
964 5 storres
id(a)
965 5 storres
print "Max arity: ", pobyso_max_arity
966 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
967 56 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
968 56 storres
print "...Pobyso check done"