Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 106

Historique | Voir | Annoter | Télécharger (44,23 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 84 storres
    the digits of the Sage bounds.
103 84 storres
    """
104 84 storres
    # Sanity check.
105 84 storres
    if rnLowerBoundSa > rnUpperBoundSa:
106 84 storres
        return None
107 85 storres
    if precisionSa is None:
108 84 storres
        # Check for the largest precision.
109 84 storres
        lbPrecSa = rnLowerBoundSa.parent().precision()
110 84 storres
        ubPrecSa = rnLowerBoundSa.parent().precision()
111 84 storres
        maxPrecSa = max(lbPrecSa, ubPrecSa)
112 84 storres
    else:
113 84 storres
        maxPrecSa = precisionSa
114 84 storres
    sollyaCurrentPrecSo = pobyso_get_prec_so()
115 84 storres
    sollyaCurrentPrecSa = pobyso_constant_from_int_so_sa(sollyaCurrentPrecSo)
116 84 storres
    # Change the current Sollya precision only if necessary.
117 84 storres
    if maxPrecSa > sollyaCurrentPrecSa:
118 84 storres
        pobyso_set_prec_sa_so(maxPrecSa)
119 84 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBoundSa))
120 84 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBoundSa))
121 84 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
122 84 storres
    # Back to original precision.
123 84 storres
    if maxPrecSa > sollyaCurrentPrecSa:
124 84 storres
        sollya_lib_set_prec(sollyaCurrentPrecSo)
125 84 storres
    # Clean up
126 84 storres
    sollya_lib_clear_obj(sollyaCurrentPrecSo)
127 84 storres
    sollya_lib_clear_obj(lowerBoundSo)
128 84 storres
    sollya_lib_clear_obj(upperBoundSo)
129 84 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 85 storres
def pobyso_cmp(rnArgSa, cteSo):
148 85 storres
    """
149 54 storres
    Compare the MPFR value a RealNumber with that of a Sollya constant.
150 54 storres

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

489 52 storres
    Arguments:
490 52 storres
    soExp -- a Sollya expression pointer
491 52 storres
    Return value:
492 52 storres
    A Python integer
493 38 storres
    TODO:
494 38 storres
    - error management;
495 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
496 38 storres
    """
497 5 storres
    maxPrecision = 0
498 52 storres
    minConstPrec = 0
499 52 storres
    currentConstPrec = 0
500 85 storres
    operator = pobyso_get_head_function_so_sa(expSo)
501 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
502 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
503 85 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(expSo)
504 5 storres
        for i in xrange(arity):
505 5 storres
            maxPrecisionCandidate = \
506 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
507 5 storres
            if maxPrecisionCandidate > maxPrecision:
508 5 storres
                maxPrecision = maxPrecisionCandidate
509 5 storres
        return(maxPrecision)
510 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
511 85 storres
        #minConstPrec = pobyso_get_min_prec_of_constant_so_sa(expSo)
512 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
513 52 storres
        #print minConstPrec, " - ", currentConstPrec
514 85 storres
        return(pobyso_get_min_prec_of_constant_so_sa(expSo))
515 52 storres
516 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
517 5 storres
        return(0)
518 5 storres
    else:
519 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
520 5 storres
        return(0)
521 5 storres
522 85 storres
def pobyso_get_min_prec_of_constant_so_sa(constExpSo):
523 52 storres
    """
524 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
525 52 storres
    constant.
526 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
527 85 storres
    We assume that constExpSo is a point
528 52 storres
    """
529 85 storres
    constExpAsRnSa = pobyso_get_constant_as_rn_so_sa(constExpSo)
530 85 storres
    return(min_mpfr_size(get_rn_value(constExpAsRnSa)))
531 52 storres
532 85 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExpSo, realField = RR):
533 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
534 85 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, \
535 85 storres
                                                     realField = RR))
536 38 storres
537 85 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExpSo, realFieldSa = RR):
538 5 storres
    """
539 38 storres
    Get a Sage expression from a Sollya expression.
540 38 storres
    Currently only tested with polynomials with floating-point coefficients.
541 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
542 5 storres
    """
543 5 storres
    #pobyso_autoprint(sollyaExp)
544 85 storres
    operatorSa = pobyso_get_head_function_so_sa(sollyaExpSo)
545 83 storres
    sollyaLibFreeVariableName = sollya_lib_get_free_variable_name()
546 5 storres
    # Constants and the free variable are special cases.
547 5 storres
    # All other operator are dealt with in the same way.
548 85 storres
    if (operatorSa != SOLLYA_BASE_FUNC_CONSTANT) and \
549 85 storres
       (operatorSa != SOLLYA_BASE_FUNC_FREE_VARIABLE):
550 85 storres
        (aritySa, subexpressionsSa) = pobyso_get_subfunctions_so_sa(sollyaExpSo)
551 85 storres
        if aritySa == 1:
552 85 storres
            sageExpSa = eval(pobyso_function_type_as_string_so_sa(operatorSa) + \
553 85 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], \
554 85 storres
            realFieldSa) + ")")
555 85 storres
        elif aritySa == 2:
556 63 storres
            # We do not get through the preprocessor.
557 63 storres
            # The "^" operator is then a special case.
558 85 storres
            if operatorSa == SOLLYA_BASE_FUNC_POW:
559 85 storres
                operatorAsStringSa = "**"
560 5 storres
            else:
561 85 storres
                operatorAsStringSa = \
562 85 storres
                    pobyso_function_type_as_string_so_sa(operatorSa)
563 85 storres
            sageExpSa = \
564 85 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[0], realFieldSa)"\
565 85 storres
              + " " + operatorAsStringSa + " " + \
566 85 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressionsSa[1], realFieldSa)")
567 63 storres
        # We do not know yet how to deal with arity >= 3
568 63 storres
        # (is there any in Sollya anyway?).
569 5 storres
        else:
570 85 storres
            sageExpSa = eval('None')
571 85 storres
        return(sageExpSa)
572 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_CONSTANT:
573 5 storres
        #print "This is a constant"
574 85 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExpSo, realFieldSa)
575 85 storres
    elif operatorSa == SOLLYA_BASE_FUNC_FREE_VARIABLE:
576 5 storres
        #print "This is free variable"
577 83 storres
        return(eval(sollyaLibFreeVariableName))
578 5 storres
    else:
579 5 storres
        print "Unexpected"
580 5 storres
        return eval('None')
581 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
582 73 storres
583 73 storres
def pobyso_get_poly_sa_so(polySo, realFieldSa=None):
584 83 storres
    """
585 83 storres
    Create a Sollya polynomial from a Sage polynomial.
586 83 storres
    """
587 73 storres
    pass
588 73 storres
# pobyso_get_poly_sa_so
589 73 storres
590 62 storres
def pobyso_get_poly_so_sa(polySo, realFieldSa=None):
591 57 storres
    """
592 57 storres
    Convert a Sollya polynomial into a Sage polynomial.
593 62 storres
    We assume that the polynomial is in canonical form.
594 57 storres
    If no realField is given, a RealField corresponding to the maximum precision
595 57 storres
    of the coefficients is internally computed.
596 57 storres
    It is not returned but can be easily retrieved from the polynomial itself.
597 57 storres
    Main steps:
598 57 storres
    - (optional) compute the RealField of the coefficients;
599 57 storres
    - convert the Sollya expression into a Sage expression;
600 57 storres
    - convert the Sage expression into a Sage polynomial
601 59 storres
    TODO: the canonical thing for the polynomial.
602 57 storres
    """
603 57 storres
    if realFieldSa is None:
604 57 storres
        expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
605 57 storres
        realFieldSa = RealField(expressionPrecSa)
606 63 storres
    #print "Sollya expression before...",
607 63 storres
    #pobyso_autoprint(polySo)
608 63 storres
609 57 storres
    expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, \
610 57 storres
                                                             realFieldSa)
611 63 storres
    #print "...Sollya expression after.",
612 63 storres
    #pobyso_autoprint(polySo)
613 57 storres
    polyVariableSa = expressionSa.variables()[0]
614 59 storres
    polyRingSa = realFieldSa[str(polyVariableSa)]
615 81 storres
    #print polyRingSa
616 62 storres
    # Do not use the polynomial(expressionSa, ring=polyRingSa) form!
617 62 storres
    polynomialSa = polyRingSa(expressionSa)
618 57 storres
    return(polynomialSa)
619 57 storres
# End pobyso_get_sage_poly_from_sollya_poly
620 57 storres
621 38 storres
def pobyso_get_subfunctions(expressionSo):
622 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
623 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
624 38 storres
625 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
626 38 storres
    """
627 38 storres
    Get the subfunctions of an expression.
628 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
629 55 storres
    S.T.: Could not figure out another way than that ugly list of declarations
630 83 storres
    to recover the addresses of the subfunctions.
631 83 storres
    We limit ourselves to arity 8 functions.
632 38 storres
    """
633 5 storres
    subf0 = c_int(0)
634 5 storres
    subf1 = c_int(0)
635 5 storres
    subf2 = c_int(0)
636 5 storres
    subf3 = c_int(0)
637 5 storres
    subf4 = c_int(0)
638 5 storres
    subf5 = c_int(0)
639 5 storres
    subf6 = c_int(0)
640 5 storres
    subf7 = c_int(0)
641 5 storres
    subf8 = c_int(0)
642 5 storres
    arity = c_int(0)
643 5 storres
    nullPtr = POINTER(c_int)()
644 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
645 83 storres
      byref(subf0), byref(subf1), byref(subf2), byref(subf3), \
646 83 storres
      byref(subf4), byref(subf5),\
647 83 storres
      byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
648 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
649 83 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), \
650 83 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), \
651 83 storres
#    byref(cast(subfunctions[3], POINTER(c_int))), \
652 83 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), \
653 83 storres
#    byref(cast(subfunctions[5], POINTER(c_int))), \
654 83 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), \
655 83 storres
#    byref(cast(subfunctions[7], POINTER(c_int))), \
656 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
657 83 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, \
658 83 storres
                    subf8]
659 5 storres
    subs = []
660 5 storres
    if arity.value > pobyso_max_arity:
661 38 storres
        return(0,[])
662 5 storres
    for i in xrange(arity.value):
663 5 storres
        subs.append(int(subfunctions[i].value))
664 5 storres
        #print subs[i]
665 5 storres
    return(int(arity.value), subs)
666 5 storres
667 5 storres
def pobyso_get_prec():
668 38 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
669 38 storres
    return(pobyso_get_prec_so_sa())
670 38 storres
671 84 storres
def pobyso_get_prec_so():
672 84 storres
    """
673 84 storres
    Get the current default precision in Sollya.
674 84 storres
    The return value is a Sollya object.
675 84 storres
    Usefull when modifying the precision back and forth by avoiding
676 84 storres
    extra conversions.
677 84 storres
    """
678 84 storres
    return(sollya_lib_get_prec(None))
679 84 storres
680 38 storres
def pobyso_get_prec_so_sa():
681 38 storres
    """
682 38 storres
    Get the current default precision in Sollya.
683 38 storres
    The return value is Sage/Python int.
684 38 storres
    """
685 56 storres
    precSo = sollya_lib_get_prec(None)
686 56 storres
    precSa = c_int(0)
687 56 storres
    sollya_lib_get_constant_as_int(byref(precSa), precSo)
688 56 storres
    sollya_lib_clear_obj(precSo)
689 56 storres
    return(int(precSa.value))
690 83 storres
# End pobyso_get_prec_so_sa.
691 5 storres
692 38 storres
def pobyso_get_prec_of_constant(ctExpSo):
693 38 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
694 38 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
695 38 storres
696 56 storres
def pobyso_get_prec_of_constant_so_sa(ctExpSo):
697 56 storres
    prec = c_int(0)
698 56 storres
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
699 56 storres
    if retc == 0:
700 56 storres
        return(None)
701 56 storres
    return(int(prec.value))
702 56 storres
703 53 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
704 5 storres
    prec = c_int(0)
705 53 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
706 56 storres
    if retc == 0:
707 56 storres
        return(None)
708 5 storres
    return(int(prec.value))
709 5 storres
710 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
711 54 storres
    print "Do not use this function. User pobyso_supnorm_so_so instead."
712 54 storres
    return(None)
713 53 storres
714 84 storres
def pobyso_interval_to_range_sa_so(intervalSa, precisionSa=None):
715 84 storres
    if precisionSa is None:
716 84 storres
        precisionSa = intervalSa.parent().precision()
717 84 storres
    intervalSo = pobyso_bounds_to_range_sa_so(intervalSa.lower(),\
718 84 storres
                                              intervalSa.upper(),\
719 84 storres
                                              precisionSa)
720 84 storres
    return(intervalSo)
721 84 storres
# End pobyso_interval_to_range_sa_so
722 84 storres
723 37 storres
def pobyso_lib_init():
724 37 storres
    sollya_lib_init(None)
725 37 storres
726 85 storres
def pobyso_name_free_variable(freeVariableNameSa):
727 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
728 85 storres
    pobyso_name_free_variable_sa_so(freeVariableNameSa)
729 38 storres
730 85 storres
def pobyso_name_free_variable_sa_so(freeVariableNameSa):
731 83 storres
    """
732 83 storres
    Set the free variable name in Sollya from a Sage string.
733 83 storres
    """
734 85 storres
    sollya_lib_name_free_variable(freeVariableNameSa)
735 37 storres
736 5 storres
def pobyso_parse_string(string):
737 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
738 38 storres
    return(pobyso_parse_string_sa_so(string))
739 38 storres
740 38 storres
def pobyso_parse_string_sa_so(string):
741 83 storres
    """
742 83 storres
    Get the Sollya expression computed from a Sage string.
743 83 storres
    """
744 5 storres
    return(sollya_lib_parse_string(string))
745 5 storres
746 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
747 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
748 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
749 38 storres
750 5 storres
751 85 storres
def pobyso_range_to_interval_so_sa(rangeSo, realIntervalFieldSa = None):
752 83 storres
    """
753 83 storres
    Get a Sage interval from a Sollya range.
754 83 storres
    If no realIntervalField is given as a parameter, the Sage interval
755 83 storres
    precision is that of the Sollya range.
756 85 storres
    Otherwise, the precision is that of the realIntervalField. In this case
757 85 storres
    rounding may happen.
758 83 storres
    """
759 85 storres
    if realIntervalFieldSa is None:
760 56 storres
        precSa = pobyso_get_prec_of_range_so_sa(rangeSo)
761 85 storres
        realIntervalFieldSa = RealIntervalField(precSa)
762 56 storres
    intervalSa = \
763 85 storres
        pobyso_get_interval_from_range_so_sa(rangeSo, realIntervalFieldSa)
764 56 storres
    return(intervalSa)
765 56 storres
766 52 storres
def pobyso_remez_canonical_sa_sa(func, \
767 52 storres
                                 degree, \
768 52 storres
                                 lowerBound, \
769 52 storres
                                 upperBound, \
770 52 storres
                                 weight = None, \
771 52 storres
                                 quality = None):
772 52 storres
    """
773 52 storres
    All arguments are Sage/Python.
774 52 storres
    The functions (func and weight) must be passed as expressions or strings.
775 52 storres
    Otherwise the function fails.
776 83 storres
    The return value is a Sage polynomial.
777 52 storres
    """
778 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
779 83 storres
    # zorglub is "symbolic expression".
780 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
781 52 storres
                                 degree, \
782 52 storres
                                 lowerBound, \
783 52 storres
                                 upperBound, \
784 85 storres
                                 weight, \
785 85 storres
                                 quality)
786 83 storres
    # String test
787 52 storres
    if parent(func) == parent("string"):
788 52 storres
        functionSa = eval(func)
789 52 storres
    # Expression test.
790 52 storres
    elif type(func) == type(zorglub):
791 52 storres
        functionSa = func
792 83 storres
    else:
793 83 storres
        return None
794 83 storres
    #
795 52 storres
    maxPrecision = 0
796 52 storres
    if polySo is None:
797 52 storres
        return(None)
798 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
799 85 storres
    RRRRSa = RealField(maxPrecision)
800 85 storres
    polynomialRingSa = RRRRSa[functionSa.variables()[0]]
801 85 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRRSa)
802 85 storres
    polySa = polynomial(expSa, polynomialRingSa)
803 83 storres
    sollya_lib_clear_obj(polySo)
804 52 storres
    return(polySa)
805 85 storres
# End pobyso_remez_canonical_sa_sa
806 52 storres
807 38 storres
def pobyso_remez_canonical(func, \
808 5 storres
                           degree, \
809 5 storres
                           lowerBound, \
810 5 storres
                           upperBound, \
811 38 storres
                           weight = "1", \
812 5 storres
                           quality = None):
813 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
814 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
815 51 storres
                                        degree, \
816 51 storres
                                        lowerBound, \
817 51 storres
                                        upperBound, \
818 51 storres
                                        weight, \
819 51 storres
                                        quality))
820 38 storres
def pobyso_remez_canonical_sa_so(func, \
821 38 storres
                                 degree, \
822 38 storres
                                 lowerBound, \
823 38 storres
                                 upperBound, \
824 52 storres
                                 weight = None, \
825 38 storres
                                 quality = None):
826 38 storres
    """
827 38 storres
    All arguments are Sage/Python.
828 51 storres
    The functions (func and weight) must be passed as expressions or strings.
829 51 storres
    Otherwise the function fails.
830 38 storres
    The return value is a pointer to a Sollya function.
831 38 storres
    """
832 83 storres
    var('zorglub')    # Dummy variable name for type check only. Type of
833 83 storres
    # zorglub is "symbolic expression".
834 85 storres
    currentVariableNameSa = None
835 52 storres
    # The func argument can be of different types (string,
836 52 storres
    # symbolic expression...)
837 38 storres
    if parent(func) == parent("string"):
838 85 storres
        localFuncSa = eval(func)
839 85 storres
        if len(localFuncSa.variables()) > 0:
840 85 storres
            currentVariableNameSa = localFuncSa.variables()[0]
841 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
842 85 storres
            functionSo = sollya_lib_parse_string(localFuncSa._assume_str())
843 51 storres
    # Expression test.
844 52 storres
    elif type(func) == type(zorglub):
845 52 storres
        # Until we are able to translate Sage expressions into Sollya
846 52 storres
        # expressions : parse the string version.
847 85 storres
        if len(func.variables()) > 0:
848 85 storres
            currentVariableNameSa = func.variables()[0]
849 85 storres
            sollya_lib_name_free_variable(str(currentVariableNameSa))
850 85 storres
            functionSo = sollya_lib_parse_string(func._assume_str())
851 38 storres
    else:
852 38 storres
        return(None)
853 85 storres
    if weight is None: # No weight given -> 1.
854 52 storres
        weightSo = pobyso_constant_1_sa_so()
855 85 storres
    elif parent(weight) == parent("string"): # Weight given as string: parse it.
856 51 storres
        weightSo = sollya_lib_parse_string(func)
857 85 storres
    elif type(weight) == type(zorglub): # Weight given as symbolice expression.
858 51 storres
        functionSo = sollya_lib_parse_string_sa_so(weight._assume_str())
859 51 storres
    else:
860 51 storres
        return(None)
861 5 storres
    degreeSo = pobyso_constant_from_int(degree)
862 85 storres
    rangeSo = pobyso_bounds_to_range_sa_so(lowerBound, upperBound)
863 38 storres
    if not quality is None:
864 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
865 52 storres
    else:
866 52 storres
        qualitySo = None
867 83 storres
868 83 storres
    remezPolySo = sollya_lib_remez(functionSo, \
869 83 storres
                                   degreeSo, \
870 83 storres
                                   rangeSo, \
871 83 storres
                                   weightSo, \
872 83 storres
                                   qualitySo, \
873 83 storres
                                   None)
874 83 storres
    sollya_lib_clear_obj(functionSo)
875 83 storres
    sollya_lib_clear_obj(degreeSo)
876 83 storres
    sollya_lib_clear_obj(rangeSo)
877 83 storres
    sollya_lib_clear_obj(weightSo)
878 83 storres
    if not qualitySo is None:
879 85 storres
        sollya_lib_clear_obj(qualitySo)
880 83 storres
    return(remezPolySo)
881 83 storres
# End pobyso_remez_canonical_sa_so
882 83 storres
883 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
884 38 storres
                                 degreeSo, \
885 38 storres
                                 rangeSo, \
886 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
887 38 storres
                                 qualitySo = None):
888 38 storres
    """
889 38 storres
    All arguments are pointers to Sollya objects.
890 38 storres
    The return value is a pointer to a Sollya function.
891 38 storres
    """
892 38 storres
    if not sollya_lib_obj_is_function(funcSo):
893 38 storres
        return(None)
894 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
895 38 storres
896 5 storres
def pobyso_set_canonical_off():
897 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
898 5 storres
899 5 storres
def pobyso_set_canonical_on():
900 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
901 5 storres
902 5 storres
def pobyso_set_prec(p):
903 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
904 85 storres
    pobyso_set_prec_sa_so(p)
905 38 storres
906 38 storres
def pobyso_set_prec_sa_so(p):
907 5 storres
    a = c_int(p)
908 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
909 85 storres
    sollya_lib_set_prec(precSo, None)
910 5 storres
911 85 storres
def pobyso_set_prec_so_so(newPrecSo):
912 85 storres
    sollya_lib_set_prec(newPrecSo, None)
913 54 storres
914 85 storres
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo = None,\
915 85 storres
                         accuracySo = None):
916 58 storres
    """
917 85 storres
    Computes the supnorm of the approximation error between the given
918 85 storres
    polynomial and function.
919 85 storres
    errorTypeSo defaults to "absolute".
920 85 storres
    accuracySo defaults to 2^(-40).
921 85 storres
    """
922 85 storres
    if errorTypeSo is None:
923 85 storres
        errorTypeSo = sollya_lib_absolute(None)
924 85 storres
        errorTypeIsNone = True
925 85 storres
    else:
926 85 storres
        errorTypeIsNone = False
927 85 storres
    #
928 85 storres
    if accuracySo is None:
929 85 storres
        # Notice the **!
930 85 storres
        accuracySo = pobyso_constant_sa_so(RR(2**(-40)))
931 85 storres
        accuracyIsNone = True
932 85 storres
    else:
933 85 storres
        accuracyIsNone = False
934 85 storres
    pobyso_autoprint(accuracySo)
935 85 storres
    resultSo = \
936 85 storres
        sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
937 85 storres
                              accuracySo)
938 85 storres
    if errorTypeIsNone:
939 85 storres
        sollya_lib_clear_obj(errorTypeSo)
940 85 storres
    if accuracyIsNone:
941 85 storres
        sollya_lib_clear_obj(accuracySo)
942 85 storres
    return resultSo
943 85 storres
# End pobyso_supnorm_so_so
944 85 storres
945 85 storres
def pobyso_taylor_expansion_with_change_var_so_so(functionSo, degreeSo, \
946 85 storres
                                                  rangeSo, \
947 85 storres
                                                  errorTypeSo=None, \
948 85 storres
                                                  sollyaPrecSo=None):
949 85 storres
    """
950 58 storres
    Compute the Taylor expansion with the variable change
951 58 storres
    x -> (x-intervalCenter) included.
952 58 storres
    """
953 58 storres
    # No global change of the working precision.
954 58 storres
    if not sollyaPrecSo is None:
955 58 storres
        initialPrecSo = sollya_lib_get_prec(None)
956 58 storres
        sollya_lib_set_prec(sollyaPrecSo)
957 58 storres
    #
958 85 storres
    # Error type stuff: default to absolute.
959 85 storres
    if errorTypeSo is None:
960 85 storres
        errorTypeIsNone = True
961 85 storres
        errorTypeSo = sollya_lib_absolute(None)
962 85 storres
    else:
963 85 storres
        errorTypeIsNone = False
964 58 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
965 58 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
966 58 storres
                                         intervalCenterSo, \
967 58 storres
                                         rangeSo, errorTypeSo, None)
968 58 storres
    (taylorFormListSo, numElements, isEndElliptic) = \
969 58 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
970 58 storres
    polySo = taylorFormListSo[0]
971 58 storres
    errorRangeSo = taylorFormListSo[2]
972 58 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
973 58 storres
    changeVarExpSo = sollya_lib_build_function_sub(\
974 58 storres
                       sollya_lib_build_function_free_variable(),\
975 58 storres
                       sollya_lib_copy_obj(intervalCenterSo))
976 58 storres
    polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpSo)
977 58 storres
    # If changed, reset the Sollya working precision.
978 58 storres
    if not sollyaPrecSo is None:
979 58 storres
        sollya_lib_set_prec(initialPrecSo)
980 83 storres
        sollya_lib_clear_obj(initialPrecSo)
981 85 storres
    if errorTypeIsNone:
982 85 storres
        sollya_lib_clear_obj(errorTypeSo)
983 85 storres
    sollya_lib_clear_obj(taylorFormSo)
984 85 storres
    # Do not clear maxErrorSo.
985 63 storres
    return((polyVarChangedSo, intervalCenterSo, maxErrorSo))
986 58 storres
# end pobyso_taylor_expansion_with_change_var_so_so
987 58 storres
988 56 storres
def pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, rangeSo, \
989 85 storres
                                                errorTypeSo=None, \
990 56 storres
                                                sollyaPrecSo=None):
991 58 storres
    """
992 58 storres
    Compute the Taylor expansion without the variable change
993 58 storres
    x -> x-intervalCenter.
994 58 storres
    """
995 56 storres
    # No global change of the working precision.
996 56 storres
    if not sollyaPrecSo is None:
997 56 storres
        initialPrecSo = sollya_lib_get_prec(None)
998 56 storres
        sollya_lib_set_prec(sollyaPrecSo)
999 85 storres
    # Error type stuff: default to absolute.
1000 85 storres
    if errorTypeSo is None:
1001 85 storres
        errorTypeIsNone = True
1002 85 storres
        errorTypeSo = sollya_lib_absolute(None)
1003 85 storres
    else:
1004 85 storres
        errorTypeIsNone = False
1005 56 storres
    intervalCenterSo = sollya_lib_mid(rangeSo)
1006 56 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, \
1007 56 storres
                                         intervalCenterSo, \
1008 56 storres
                                         rangeSo, errorTypeSo, None)
1009 85 storres
    (taylorFormListSo, numElementsSo, isEndEllipticSo) = \
1010 56 storres
        pobyso_get_list_elements_so_so(taylorFormSo)
1011 85 storres
    polySo = sollya_lib_copy_obj(taylorFormListSo[0])
1012 56 storres
    errorRangeSo = taylorFormListSo[2]
1013 103 storres
    # No copy_obj needed here: a new object is created.
1014 56 storres
    maxErrorSo = sollya_lib_sup(errorRangeSo)
1015 56 storres
    # If changed, reset the Sollya working precision.
1016 56 storres
    if not sollyaPrecSo is None:
1017 56 storres
        sollya_lib_set_prec(initialPrecSo)
1018 63 storres
        sollya_lib_clear_obj(initialPrecSo)
1019 85 storres
    if errorTypeIsNone:
1020 85 storres
        sollya_lib_clear_obj(errorTypeSo)
1021 85 storres
    sollya_lib_clear_obj(taylorFormSo)
1022 103 storres
    for element in taylorFormListSo:
1023 103 storres
        sollya_lib_clear_obj(element)
1024 103 storres
    # Those are cleared with taylorForSo.
1025 103 storres
    #sollya_lib_clear_obj(numElementsSo)
1026 103 storres
    #sollya_lib_clear_obj(isEndEllipticSo)
1027 63 storres
    return((polySo, intervalCenterSo, maxErrorSo))
1028 56 storres
# end pobyso_taylor_expansion_no_change_var_so_so
1029 56 storres
1030 5 storres
def pobyso_taylor(function, degree, point):
1031 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
1032 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
1033 38 storres
1034 56 storres
def pobyso_taylor_so_so(functionSo, degreeSo, pointSo):
1035 56 storres
    return(sollya_lib_taylor(functionSo, degreeSo, pointSo))
1036 5 storres
1037 85 storres
def pobyso_taylorform(function, degree, point = None,
1038 85 storres
                      interval = None, errorType=None):
1039 85 storres
    """ Legacy function. See pobyso_taylorform_sa_sa;"""
1040 38 storres
1041 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
1042 84 storres
                            degreeSa, \
1043 84 storres
                            pointSa, \
1044 84 storres
                            intervalSa=None, \
1045 84 storres
                            errorTypeSa=None, \
1046 84 storres
                            precisionSa=None):
1047 37 storres
    """
1048 85 storres
    Compute the Taylor form of 'degreeSa' for 'functionSa' at 'pointSa'
1049 85 storres
    for 'intervalSa' with 'errorTypeSa' (a string) using 'precisionSa'.
1050 37 storres
    point: must be a Real or a Real interval.
1051 37 storres
    return the Taylor form as an array
1052 83 storres
    TODO: take care of the interval and of the point when it is an interval;
1053 38 storres
          when errorType is not None;
1054 83 storres
          take care of the other elements of the Taylor form (coefficients
1055 83 storres
          errors and delta.
1056 37 storres
    """
1057 37 storres
    # Absolute as the default error.
1058 84 storres
    if errorTypeSa is None:
1059 37 storres
        errorTypeSo = sollya_lib_absolute()
1060 84 storres
    elif errorTypeSa == "relative":
1061 84 storres
        errorTypeSo = sollya_lib_relative()
1062 84 storres
    elif errortypeSa == "absolute":
1063 84 storres
        errorTypeSo = sollya_lib_absolute()
1064 37 storres
    else:
1065 84 storres
        # No clean up needed.
1066 84 storres
        return None
1067 84 storres
    # Global precision stuff
1068 84 storres
    precisionChangedSa = False
1069 84 storres
    currentSollyaPrecSo = pobyso_get_prec_so()
1070 84 storres
    currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo)
1071 84 storres
    if not precisionSa is None:
1072 84 storres
        if precisionSa > currentSollyaPrecSa:
1073 84 storres
            pobyso_set_prec_sa_so(precisionSa)
1074 84 storres
            precisionChangedSa = True
1075 84 storres
1076 85 storres
    if len(functionSa.variables()) > 0:
1077 85 storres
        varSa = functionSa.variables()[0]
1078 85 storres
        pobyso_name_free_variable_sa_so(str(varSa))
1079 84 storres
    # In any case (point or interval) the parent of pointSa has a precision
1080 84 storres
    # method.
1081 84 storres
    pointPrecSa = pointSa.parent().precision()
1082 84 storres
    if precisionSa > pointPrecSa:
1083 84 storres
        pointPrecSa = precisionSa
1084 84 storres
    # In any case (point or interval) pointSa has a base_ring() method.
1085 84 storres
    pointBaseRingString = str(pointSa.base_ring())
1086 84 storres
    if re.search('Interval', pointBaseRingString) is None: # Point
1087 84 storres
        pointSo = pobyso_constant_sa_so(pointSa, pointPrecSa)
1088 84 storres
    else: # Interval.
1089 84 storres
        pointSo = pobyso_interval_to_range_sa_so(pointSa, pointPrecSa)
1090 37 storres
    # Sollyafy the function.
1091 38 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
1092 37 storres
    if sollya_lib_obj_is_error(functionSo):
1093 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
1094 37 storres
        return None
1095 37 storres
    # Sollyafy the degree
1096 84 storres
    degreeSo = sollya_lib_constant_from_int(int(degreeSa))
1097 37 storres
    # Sollyafy the point
1098 37 storres
    # Call Sollya
1099 83 storres
    taylorFormSo = \
1100 83 storres
        sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
1101 37 storres
                                         None)
1102 85 storres
    sollya_lib_clear_obj(functionSo)
1103 85 storres
    sollya_lib_clear_obj(degreeSo)
1104 85 storres
    sollya_lib_clear_obj(pointSo)
1105 85 storres
    sollya_lib_clear_obj(errorTypeSo)
1106 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
1107 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
1108 37 storres
    polySo = tfsAsList[0]
1109 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
1110 37 storres
    polyRealField = RealField(maxPrecision)
1111 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
1112 84 storres
    if precisionChangedSa:
1113 84 storres
        sollya_lib_set_prec(currentSollyaPrecSo)
1114 84 storres
        sollya_lib_clear_obj(currentSollyaPrecSo)
1115 37 storres
    polynomialRing = polyRealField[str(varSa)]
1116 37 storres
    polySa = polynomial(expSa, polynomialRing)
1117 37 storres
    taylorFormSa = [polySa]
1118 85 storres
    # Final clean-up.
1119 85 storres
    sollya_lib_clear_obj(taylorFormSo)
1120 51 storres
    return(taylorFormSa)
1121 51 storres
# End pobyso_taylor_form_sa_sa
1122 54 storres
1123 54 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
1124 54 storres
                            errorTypeSo=None):
1125 54 storres
    createdErrorType = False
1126 51 storres
    if errorTypeSo is None:
1127 51 storres
        errorTypeSo = sollya_lib_absolute()
1128 54 storres
        createdErrorType = True
1129 51 storres
    else:
1130 51 storres
        #TODO: deal with the other case.
1131 51 storres
        pass
1132 51 storres
    if intervalSo is None:
1133 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1134 54 storres
                                         errorTypeSo, None)
1135 51 storres
    else:
1136 54 storres
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
1137 54 storres
                                         intervalSo, errorTypeSo, None)
1138 54 storres
    if createdErrorType:
1139 54 storres
        sollya_lib_clear_obj(errorTypeSo)
1140 51 storres
    return(resultSo)
1141 51 storres
1142 37 storres
1143 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
1144 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
1145 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
1146 38 storres
1147 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
1148 37 storres
    """
1149 37 storres
    Return the string representation of a univariate polynomial with
1150 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
1151 37 storres
    Remember: Sage
1152 37 storres
    """
1153 37 storres
    polynomialRing = polySa.base_ring()
1154 37 storres
    # A very expensive solution:
1155 37 storres
    # -create a fake multivariate polynomial field with only one variable,
1156 37 storres
    #   specifying a negative lexicographical order;
1157 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
1158 37 storres
                                     polynomialRing.variable_name(), \
1159 37 storres
                                     1, order='neglex')
1160 37 storres
    # - convert the univariate argument polynomial into a multivariate
1161 37 storres
    #   version;
1162 37 storres
    p = mpolynomialRing(polySa)
1163 37 storres
    # - return the string representation of the converted form.
1164 37 storres
    # There is no simple str() method defined for p's class.
1165 37 storres
    return(p.__str__())
1166 5 storres
#
1167 5 storres
print pobyso_get_prec()
1168 5 storres
pobyso_set_prec(165)
1169 5 storres
print pobyso_get_prec()
1170 5 storres
a=100
1171 5 storres
print type(a)
1172 5 storres
id(a)
1173 5 storres
print "Max arity: ", pobyso_max_arity
1174 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
1175 56 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
1176 56 storres
print "...Pobyso check done"