Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 53

Historique | Voir | Annoter | Télécharger (27,65 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 38 storres
- the _so_so (arguments and returned objects are pointers to Sollya objects, includes
10 38 storres
  the void function and the no arguments function that return a pointer to a Sollya
11 38 storres
  object);
12 38 storres
- the _so_sa (argument are pointers to Sollya objects, returned objects are
13 38 storres
  Sage/Python objects or, more generally, information is transfered from the Sollya
14 38 storres
  world to Sage/Python world);
15 38 storres
- the _sa_so (arguments are Sage/Python objects, returned objects are
16 38 storres
  pointers to Sollya objects);
17 38 storres
- the sa_sa (arguments and returned objects are all Sage/Python objects);
18 51 storres
- a catch all flavor, without any suffix, (e. g. functions that have no argument
19 51 storres
  nor return value).
20 5 storres
NOTES:
21 5 storres
Reported errors in Eclipse come from the calls to
22 5 storres
the Sollya library
23 5 storres

24 10 storres
ToDo (among other things):
25 10 storres
 -memory management.
26 5 storres
"""
27 5 storres
from ctypes import *
28 37 storres
import re
29 37 storres
from sage.symbolic.expression_conversions import polynomial
30 38 storres
"""
31 38 storres
Create the equivalent to an enum for the Sollya function types.
32 38 storres
"""
33 5 storres
(SOLLYA_BASE_FUNC_ABS,
34 5 storres
SOLLYA_BASE_FUNC_ACOS,
35 5 storres
    SOLLYA_BASE_FUNC_ACOSH,
36 5 storres
    SOLLYA_BASE_FUNC_ADD,
37 5 storres
    SOLLYA_BASE_FUNC_ASIN,
38 5 storres
    SOLLYA_BASE_FUNC_ASINH,
39 5 storres
    SOLLYA_BASE_FUNC_ATAN,
40 5 storres
    SOLLYA_BASE_FUNC_ATANH,
41 5 storres
    SOLLYA_BASE_FUNC_CEIL,
42 5 storres
    SOLLYA_BASE_FUNC_CONSTANT,
43 5 storres
    SOLLYA_BASE_FUNC_COS,
44 5 storres
    SOLLYA_BASE_FUNC_COSH,
45 5 storres
    SOLLYA_BASE_FUNC_DIV,
46 5 storres
    SOLLYA_BASE_FUNC_DOUBLE,
47 5 storres
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
48 5 storres
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
49 5 storres
    SOLLYA_BASE_FUNC_ERF,
50 5 storres
    SOLLYA_BASE_FUNC_ERFC,
51 5 storres
    SOLLYA_BASE_FUNC_EXP,
52 5 storres
    SOLLYA_BASE_FUNC_EXP_M1,
53 5 storres
    SOLLYA_BASE_FUNC_FLOOR,
54 5 storres
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
55 5 storres
    SOLLYA_BASE_FUNC_HALFPRECISION,
56 5 storres
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
57 5 storres
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
58 5 storres
    SOLLYA_BASE_FUNC_LOG,
59 5 storres
    SOLLYA_BASE_FUNC_LOG_10,
60 5 storres
    SOLLYA_BASE_FUNC_LOG_1P,
61 5 storres
    SOLLYA_BASE_FUNC_LOG_2,
62 5 storres
    SOLLYA_BASE_FUNC_MUL,
63 5 storres
    SOLLYA_BASE_FUNC_NEARESTINT,
64 5 storres
    SOLLYA_BASE_FUNC_NEG,
65 5 storres
    SOLLYA_BASE_FUNC_PI,
66 5 storres
    SOLLYA_BASE_FUNC_POW,
67 5 storres
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
68 5 storres
    SOLLYA_BASE_FUNC_QUAD,
69 5 storres
    SOLLYA_BASE_FUNC_SIN,
70 5 storres
    SOLLYA_BASE_FUNC_SINGLE,
71 5 storres
    SOLLYA_BASE_FUNC_SINH,
72 5 storres
    SOLLYA_BASE_FUNC_SQRT,
73 5 storres
    SOLLYA_BASE_FUNC_SUB,
74 5 storres
    SOLLYA_BASE_FUNC_TAN,
75 5 storres
    SOLLYA_BASE_FUNC_TANH,
76 5 storres
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
77 5 storres
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
78 5 storres
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
79 5 storres
80 5 storres
pobyso_max_arity = 9
81 5 storres
82 5 storres
def pobyso_autoprint(arg):
83 5 storres
    sollya_lib_autoprint(arg,None)
84 5 storres
85 38 storres
def pobyso_autoprint_so_so(arg):
86 38 storres
    sollya_lib_autoprint(arg,None)
87 38 storres
88 5 storres
def pobyso_cmp(rnArg, soCte):
89 5 storres
    precisionOfCte = c_int(0)
90 5 storres
    # From the Sollya constant, create a local Sage RealNumber.
91 5 storres
    sollya_lib_get_prec_of_constant(precisionOfCte, soCte)
92 5 storres
    #print "Precision of constant: ", precisionOfCte
93 5 storres
    RRRR = RealField(precisionOfCte.value)
94 5 storres
    rnLocal = RRRR(0)
95 5 storres
    sollya_lib_get_constant(get_rn_value(rnLocal), soCte)
96 5 storres
    #print "rnDummy: ", rnDummy
97 5 storres
    # Compare the local Sage RealNumber with rnArg.
98 5 storres
    return(cmp_rn_value(rnArg, rnLocal))
99 5 storres
100 5 storres
def pobyso_constant(rnArg):
101 38 storres
    """ Legacy function. See pobyso_constant_sa_so. """
102 38 storres
    return(pobyso_constant_sa_so(rnArg))
103 5 storres
104 38 storres
def pobyso_constant_sa_so(rnArg):
105 52 storres
    """
106 52 storres
    Create a Sollya constant from a RealNumber.
107 52 storres
    """
108 38 storres
    return(sollya_lib_constant(get_rn_value(rnArg)))
109 38 storres
110 5 storres
def pobyso_constant_1():
111 38 storres
    """ Legacy function. See pobyso_constant_so_so. """
112 52 storres
    return(pobyso_constant_1_sa_so())
113 5 storres
114 52 storres
def pobyso_constant_1_sa_so():
115 38 storres
    return(pobyso_constant_from_int_sa_so(1))
116 38 storres
117 5 storres
def pobyso_constant_from_int(anInt):
118 38 storres
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
119 38 storres
    return(pobyso_constant_from_int_sa_so(anInt))
120 38 storres
121 38 storres
def pobyso_constant_from_int_sa_so(anInt):
122 5 storres
    return(sollya_lib_constant_from_int(int(anInt)))
123 5 storres
124 5 storres
def pobyso_function_type_as_string(funcType):
125 38 storres
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
126 38 storres
    return(pobyso_function_type_as_string_so_sa(funcType))
127 38 storres
128 38 storres
def pobyso_function_type_as_string_so_sa(funcType):
129 38 storres
    """
130 38 storres
    Numeric Sollya function codes -> Sage mathematical function names.
131 38 storres
    Notice that pow -> ^ (a la Sage, not a la Python).
132 38 storres
    """
133 5 storres
    if funcType == SOLLYA_BASE_FUNC_ABS:
134 5 storres
        return "abs"
135 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
136 5 storres
        return "arccos"
137 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
138 5 storres
        return "arccosh"
139 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ADD:
140 5 storres
        return "+"
141 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
142 5 storres
        return "arcsin"
143 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
144 5 storres
        return "arcsinh"
145 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
146 5 storres
        return "arctan"
147 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
148 5 storres
        return "arctanh"
149 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
150 5 storres
        return "ceil"
151 5 storres
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
152 5 storres
        return "cte"
153 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COS:
154 5 storres
        return "cos"
155 5 storres
    elif funcType == SOLLYA_BASE_FUNC_COSH:
156 5 storres
        return "cosh"
157 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DIV:
158 5 storres
        return "/"
159 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
160 5 storres
        return "double"
161 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
162 5 storres
        return "doubleDouble"
163 5 storres
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
164 5 storres
        return "doubleDxtended"
165 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERF:
166 5 storres
        return "erf"
167 5 storres
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
168 5 storres
        return "erfc"
169 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP:
170 5 storres
        return "exp"
171 5 storres
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
172 5 storres
        return "expm1"
173 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
174 5 storres
        return "floor"
175 5 storres
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
176 5 storres
        return "freeVariable"
177 5 storres
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
178 5 storres
        return "halfPrecision"
179 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
180 5 storres
        return "libraryConstant"
181 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
182 5 storres
        return "libraryFunction"
183 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG:
184 5 storres
        return "log"
185 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
186 5 storres
        return "log10"
187 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
188 5 storres
        return "log1p"
189 5 storres
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
190 5 storres
        return "log2"
191 5 storres
    elif funcType == SOLLYA_BASE_FUNC_MUL:
192 5 storres
        return "*"
193 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
194 5 storres
        return "round"
195 5 storres
    elif funcType == SOLLYA_BASE_FUNC_NEG:
196 5 storres
        return "__neg__"
197 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PI:
198 5 storres
        return "pi"
199 5 storres
    elif funcType == SOLLYA_BASE_FUNC_POW:
200 5 storres
        return "^"
201 5 storres
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
202 5 storres
        return "procedureFunction"
203 5 storres
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
204 5 storres
        return "quad"
205 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SIN:
206 5 storres
        return "sin"
207 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
208 5 storres
        return "single"
209 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SINH:
210 5 storres
        return "sinh"
211 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
212 5 storres
        return "sqrt"
213 5 storres
    elif funcType == SOLLYA_BASE_FUNC_SUB:
214 5 storres
        return "-"
215 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TAN:
216 5 storres
        return "tan"
217 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TANH:
218 5 storres
        return "tanh"
219 5 storres
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
220 5 storres
        return "tripleDouble"
221 5 storres
    else:
222 5 storres
        return None
223 5 storres
224 5 storres
def pobyso_get_constant(rnArg, soConst):
225 38 storres
    """ Legacy function. See pobyso_get_constant_so_sa. """
226 53 storres
    return(pobyso_get_constant_so_sa(rnArg, soConst))
227 38 storres
228 38 storres
def pobyso_get_constant_so_sa(rnArg, soConst):
229 52 storres
    """
230 52 storres
    Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
231 52 storres
    rnArg must already exist and belong to some RealField.
232 52 storres
    We assume that soConst points to a Sollya constant.
233 52 storres
    """
234 53 storres
    return(sollya_lib_get_constant(get_rn_value(rnArg), soConst))
235 5 storres
236 5 storres
def pobyso_get_constant_as_rn(ctExp):
237 38 storres
    """ Legacy function. See pobyso_get_constant_as_rn_so_sa. """
238 38 storres
    return(pobyso_get_constant_as_rn_so_sa(ctExp))
239 38 storres
240 52 storres
def pobyso_get_constant_as_rn_so_sa(constExp):
241 52 storres
    precision  = pobyso_get_prec_of_constant(constExp)
242 5 storres
    RRRR = RealField(precision)
243 5 storres
    rn = RRRR(0)
244 52 storres
    sollya_lib_get_constant(get_rn_value(rn), constExp)
245 5 storres
    return(rn)
246 38 storres
247 38 storres
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
248 53 storres
    """ Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa."""
249 38 storres
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
250 5 storres
251 38 storres
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField):
252 5 storres
    rn = realField(0)
253 5 storres
    sollya_lib_get_constant(get_rn_value(rn), ctExp)
254 5 storres
    return(rn)
255 38 storres
256 5 storres
def pobyso_get_free_variable_name():
257 38 storres
    """ Legacy function. See pobyso_get_free_variable_name_so_sa."""
258 38 storres
    return(pobyso_get_free_variable_name_so_sa())
259 38 storres
260 38 storres
def pobyso_get_free_variable_name_so_sa():
261 5 storres
    return(sollya_lib_get_free_variable_name())
262 5 storres
263 38 storres
def pobyso_get_function_arity(expressionSo):
264 38 storres
    """ Legacy function. See pobyso_get_function_arity_so_sa."""
265 38 storres
    return(pobyso_get_function_arity_so_sa(expressionSo))
266 38 storres
267 38 storres
def pobyso_get_function_arity_so_sa(expressionSo):
268 5 storres
    arity = c_int(0)
269 38 storres
    sollya_lib_get_function_arity(byref(arity),expressionSo)
270 5 storres
    return(int(arity.value))
271 5 storres
272 38 storres
def pobyso_get_head_function(expressionSo):
273 38 storres
    """ Legacy function. See pobyso_get_head_function_so_sa. """
274 38 storres
    return(pobyso_get_head_function_so_sa(expressionSo))
275 38 storres
276 38 storres
def pobyso_get_head_function_so_sa(expressionSo):
277 5 storres
    functionType = c_int(0)
278 38 storres
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
279 5 storres
    return(int(functionType.value))
280 5 storres
281 53 storres
def pobyso_get_interval_from_range_so_sa(soRange):
282 53 storres
    """
283 53 storres
    Return the Sage interval corresponding to the Sollya range argument.
284 53 storres
    The interval bounds are not rounded: they are elements of RealIntervalField
285 53 storres
    of the "right" precision.
286 53 storres
    """
287 53 storres
    prec = c_int(0)
288 53 storres
    retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
289 53 storres
    if retval == 0:
290 53 storres
        return(None)
291 53 storres
    RRRI = RealIntervalField(prec.value)
292 53 storres
    intervalSa = RRRI(0,0)
293 53 storres
    retval = \
294 53 storres
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
295 53 storres
                                           soRange)
296 53 storres
    if retval == 0:
297 53 storres
        return(None)
298 53 storres
    return(intervalSa)
299 53 storres
300 5 storres
def pobyso_get_list_elements(soObj):
301 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
302 38 storres
    return(pobyso_get_list_elements_so_so(soObj))
303 38 storres
304 38 storres
def pobyso_get_list_elements_so_so(soObj):
305 51 storres
    """
306 51 storres
    Get the list elements as a Sage/Python array of Sollya objects.
307 51 storres
    The other data returned are also Sage/Python objects.
308 51 storres
    """
309 5 storres
    listAddress = POINTER(c_longlong)()
310 5 storres
    numElements = c_int(0)
311 5 storres
    isEndElliptic = c_int(0)
312 5 storres
    listAsList = []
313 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
314 5 storres
                                        byref(numElements),\
315 5 storres
                                        byref(isEndElliptic),\
316 5 storres
                                        soObj)
317 5 storres
    if result == 0 :
318 5 storres
        return None
319 5 storres
    for i in xrange(0, numElements.value, 1):
320 5 storres
        listAsList.append(listAddress[i])
321 5 storres
    return(listAsList, numElements.value, isEndElliptic.value)
322 5 storres
323 38 storres
def pobyso_get_max_prec_of_exp(soExp):
324 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
325 38 storres
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
326 5 storres
327 38 storres
def pobyso_get_max_prec_of_exp_so_sa(soExp):
328 38 storres
    """
329 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
330 52 storres

331 52 storres
    Arguments:
332 52 storres
    soExp -- a Sollya expression pointer
333 52 storres
    Return value:
334 52 storres
    A Python integer
335 38 storres
    TODO:
336 38 storres
    - error management;
337 38 storres
    - correctly deal with numerical type such as DOUBLEEXTENDED.
338 38 storres
    """
339 5 storres
    maxPrecision = 0
340 52 storres
    minConstPrec = 0
341 52 storres
    currentConstPrec = 0
342 38 storres
    operator = pobyso_get_head_function_so_sa(soExp)
343 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
344 5 storres
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
345 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp)
346 5 storres
        for i in xrange(arity):
347 5 storres
            maxPrecisionCandidate = \
348 38 storres
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
349 5 storres
            if maxPrecisionCandidate > maxPrecision:
350 5 storres
                maxPrecision = maxPrecisionCandidate
351 5 storres
        return(maxPrecision)
352 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
353 52 storres
        minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
354 52 storres
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
355 52 storres
        #print minConstPrec, " - ", currentConstPrec
356 52 storres
        return(pobyso_get_min_prec_of_constant_so_sa(soExp))
357 52 storres
358 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
359 5 storres
        return(0)
360 5 storres
    else:
361 38 storres
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
362 5 storres
        return(0)
363 5 storres
364 52 storres
def pobyso_get_min_prec_of_constant_so_sa(soConstExp):
365 52 storres
    """
366 52 storres
    Get the minimum precision necessary to represent the value of a Sollya
367 52 storres
    constant.
368 52 storres
    MPFR_MIN_PREC and powers of 2 are taken into account.
369 52 storres
    We assume that soCteExp is a point
370 52 storres
    """
371 52 storres
    constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp)
372 52 storres
    return(min_mpfr_size(get_rn_value(constExpAsRn)))
373 52 storres
374 5 storres
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
375 38 storres
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
376 38 storres
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
377 38 storres
378 38 storres
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR):
379 5 storres
    """
380 38 storres
    Get a Sage expression from a Sollya expression.
381 38 storres
    Currently only tested with polynomials with floating-point coefficients.
382 5 storres
    Notice that, in the returned polynomial, the exponents are RealNumbers.
383 5 storres
    """
384 5 storres
    #pobyso_autoprint(sollyaExp)
385 5 storres
    operator = pobyso_get_head_function(sollyaExp)
386 5 storres
    # Constants and the free variable are special cases.
387 5 storres
    # All other operator are dealt with in the same way.
388 5 storres
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
389 5 storres
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
390 38 storres
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp)
391 5 storres
        if arity == 1:
392 38 storres
            sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
393 52 storres
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \
394 52 storres
            realField) + ")")
395 5 storres
        elif arity == 2:
396 5 storres
            if operator == SOLLYA_BASE_FUNC_POW:
397 5 storres
                operatorAsString = "**"
398 5 storres
            else:
399 52 storres
                operatorAsString = \
400 52 storres
                    pobyso_function_type_as_string_so_sa(operator)
401 5 storres
            sageExp = \
402 38 storres
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\
403 5 storres
              + " " + operatorAsString + " " + \
404 38 storres
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
405 5 storres
        # We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
406 5 storres
        else:
407 5 storres
            sageExp = eval('None')
408 5 storres
        return(sageExp)
409 5 storres
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
410 5 storres
        #print "This is a constant"
411 38 storres
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
412 5 storres
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
413 5 storres
        #print "This is free variable"
414 5 storres
        return(eval(sollya_lib_get_free_variable_name()))
415 5 storres
    else:
416 5 storres
        print "Unexpected"
417 5 storres
        return eval('None')
418 5 storres
# End pobyso_get_sage_poly_from_sollya_poly
419 5 storres
420 38 storres
def pobyso_get_subfunctions(expressionSo):
421 38 storres
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
422 38 storres
    return(pobyso_get_subfunctions_so_sa(expressionSo))
423 38 storres
424 38 storres
def pobyso_get_subfunctions_so_sa(expressionSo):
425 38 storres
    """
426 38 storres
    Get the subfunctions of an expression.
427 38 storres
    Return the number of subfunctions and the list of subfunctions addresses.
428 38 storres
    Could not figure out another way than that ugly list of declarations
429 38 storres
    to recover the addresses of the subfunctions.
430 38 storres
    Arity is limited to 9.
431 38 storres
    """
432 5 storres
    subf0 = c_int(0)
433 5 storres
    subf1 = c_int(0)
434 5 storres
    subf2 = c_int(0)
435 5 storres
    subf3 = c_int(0)
436 5 storres
    subf4 = c_int(0)
437 5 storres
    subf5 = c_int(0)
438 5 storres
    subf6 = c_int(0)
439 5 storres
    subf7 = c_int(0)
440 5 storres
    subf8 = c_int(0)
441 5 storres
    arity = c_int(0)
442 5 storres
    nullPtr = POINTER(c_int)()
443 38 storres
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
444 5 storres
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
445 5 storres
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None)
446 5 storres
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
447 5 storres
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
448 5 storres
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
449 5 storres
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
450 5 storres
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
451 5 storres
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
452 5 storres
    subs = []
453 5 storres
    if arity.value > pobyso_max_arity:
454 38 storres
        return(0,[])
455 5 storres
    for i in xrange(arity.value):
456 5 storres
        subs.append(int(subfunctions[i].value))
457 5 storres
        #print subs[i]
458 5 storres
    return(int(arity.value), subs)
459 5 storres
460 5 storres
def pobyso_get_prec():
461 38 storres
    """ Legacy function. See pobyso_get_prec_so_sa(). """
462 38 storres
    return(pobyso_get_prec_so_sa())
463 38 storres
464 38 storres
def pobyso_get_prec_so_sa():
465 38 storres
    """
466 38 storres
    Get the current default precision in Sollya.
467 38 storres
    The return value is Sage/Python int.
468 38 storres
    """
469 5 storres
    retc = sollya_lib_get_prec(None)
470 5 storres
    a = c_int(0)
471 5 storres
    sollya_lib_get_constant_as_int(byref(a), retc)
472 5 storres
    return(int(a.value))
473 5 storres
474 38 storres
def pobyso_get_prec_of_constant(ctExpSo):
475 38 storres
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
476 38 storres
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
477 38 storres
478 53 storres
def pobyso_get_prec_of_range_so_sa(rangeSo):
479 5 storres
    prec = c_int(0)
480 53 storres
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
481 5 storres
    return(int(prec.value))
482 5 storres
483 53 storres
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
484 53 storres
    return(sollya_lib_infnorm(func, interval, file, intervalList, None))
485 53 storres
486 37 storres
def pobyso_lib_init():
487 37 storres
    sollya_lib_init(None)
488 37 storres
489 37 storres
def pobyso_name_free_variable(freeVariableName):
490 38 storres
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
491 38 storres
    pobyso_name_free_variable_sa_so(freeVariableName)
492 38 storres
493 38 storres
def pobyso_name_free_variable_sa_so(freeVariableName):
494 37 storres
    sollya_lib_name_free_variable(freeVariableName)
495 37 storres
496 5 storres
def pobyso_parse_string(string):
497 38 storres
    """ Legacy function. See pobyso_parse_string_sa_so. """
498 38 storres
    return(pobyso_parse_string_sa_so(string))
499 38 storres
500 38 storres
def pobyso_parse_string_sa_so(string):
501 5 storres
    return(sollya_lib_parse_string(string))
502 5 storres
503 5 storres
def pobyso_range(rnLowerBound, rnUpperBound):
504 38 storres
    """ Legacy function. See pobyso_range_sa_so. """
505 51 storres
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound))
506 38 storres
507 38 storres
def pobyso_range_sa_so(rnLowerBound, rnUpperBound):
508 5 storres
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound))
509 5 storres
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound))
510 5 storres
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
511 5 storres
    return(rangeSo)
512 5 storres
513 52 storres
def pobyso_remez_canonical_sa_sa(func, \
514 52 storres
                                 degree, \
515 52 storres
                                 lowerBound, \
516 52 storres
                                 upperBound, \
517 52 storres
                                 weight = None, \
518 52 storres
                                 quality = None):
519 52 storres
    """
520 52 storres
    All arguments are Sage/Python.
521 52 storres
    The functions (func and weight) must be passed as expressions or strings.
522 52 storres
    Otherwise the function fails.
523 52 storres
    The return value is a pointer is a Sage polynomial.
524 52 storres
    """
525 52 storres
    var('zorglub')    # Dummy variable name for type check only.
526 52 storres
    polySo = pobyso_remez_canonical_sa_so(func, \
527 52 storres
                                 degree, \
528 52 storres
                                 lowerBound, \
529 52 storres
                                 upperBound, \
530 52 storres
                                 weight = None, \
531 52 storres
                                 quality = None)
532 52 storres
    if parent(func) == parent("string"):
533 52 storres
        functionSa = eval(func)
534 52 storres
    # Expression test.
535 52 storres
    elif type(func) == type(zorglub):
536 52 storres
        functionSa = func
537 52 storres
    maxPrecision = 0
538 52 storres
    if polySo is None:
539 52 storres
        return(None)
540 52 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
541 52 storres
    RRRR = RealField(maxPrecision)
542 52 storres
    polynomialRing = RRRR[functionSa.variables()[0]]
543 52 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR)
544 52 storres
    polySa = polynomial(expSa, polynomialRing)
545 52 storres
    return(polySa)
546 52 storres
547 38 storres
def pobyso_remez_canonical(func, \
548 5 storres
                           degree, \
549 5 storres
                           lowerBound, \
550 5 storres
                           upperBound, \
551 38 storres
                           weight = "1", \
552 5 storres
                           quality = None):
553 38 storres
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
554 51 storres
    return(pobyso_remez_canonical_sa_so(func, \
555 51 storres
                                        degree, \
556 51 storres
                                        lowerBound, \
557 51 storres
                                        upperBound, \
558 51 storres
                                        weight, \
559 51 storres
                                        quality))
560 38 storres
def pobyso_remez_canonical_sa_so(func, \
561 38 storres
                                 degree, \
562 38 storres
                                 lowerBound, \
563 38 storres
                                 upperBound, \
564 52 storres
                                 weight = None, \
565 38 storres
                                 quality = None):
566 38 storres
    """
567 38 storres
    All arguments are Sage/Python.
568 51 storres
    The functions (func and weight) must be passed as expressions or strings.
569 51 storres
    Otherwise the function fails.
570 38 storres
    The return value is a pointer to a Sollya function.
571 38 storres
    """
572 52 storres
    var('zorglub')    # Dummy variable name for type check only.
573 52 storres
    currentVariableName = None
574 52 storres
    # The func argument can be of different types (string,
575 52 storres
    # symbolic expression...)
576 38 storres
    if parent(func) == parent("string"):
577 38 storres
        functionSo = sollya_lib_parse_string(func)
578 51 storres
    # Expression test.
579 52 storres
    elif type(func) == type(zorglub):
580 52 storres
        # Until we are able to translate Sage expressions into Sollya
581 52 storres
        # expressions : parse the string version.
582 52 storres
        currentVariableName = func.variables()[0]
583 52 storres
        sollya_lib_name_free_variable(str(currentVariableName))
584 52 storres
        functionSo = sollya_lib_parse_string(func._assume_str())
585 38 storres
    else:
586 38 storres
        return(None)
587 52 storres
    if weight is None:
588 52 storres
        weightSo = pobyso_constant_1_sa_so()
589 52 storres
    elif parent(weight) == parent("string"):
590 51 storres
        weightSo = sollya_lib_parse_string(func)
591 51 storres
    elif type(weight) == type(zorglub):
592 51 storres
        functionSo = sollya_lib_parse_string_sa_so(weight._assume_str())
593 51 storres
    else:
594 51 storres
        return(None)
595 5 storres
    degreeSo = pobyso_constant_from_int(degree)
596 38 storres
    rangeSo = pobyso_range_sa_so(lowerBound, upperBound)
597 38 storres
    if not quality is None:
598 38 storres
        qualitySo= pobyso_constant_sa_so(quality)
599 52 storres
    else:
600 52 storres
        qualitySo = None
601 52 storres
    return(sollya_lib_remez(functionSo, \
602 52 storres
                            degreeSo, \
603 52 storres
                            rangeSo, \
604 52 storres
                            weightSo, \
605 52 storres
                            qualitySo, \
606 52 storres
                            None))
607 5 storres
608 38 storres
def pobyso_remez_canonical_so_so(funcSo, \
609 38 storres
                                 degreeSo, \
610 38 storres
                                 rangeSo, \
611 52 storres
                                 weightSo = pobyso_constant_1_sa_so(),\
612 38 storres
                                 qualitySo = None):
613 38 storres
    """
614 38 storres
    All arguments are pointers to Sollya objects.
615 38 storres
    The return value is a pointer to a Sollya function.
616 38 storres
    """
617 38 storres
    if not sollya_lib_obj_is_function(funcSo):
618 38 storres
        return(None)
619 38 storres
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
620 38 storres
621 5 storres
def pobyso_set_canonical_off():
622 5 storres
    sollya_lib_set_canonical(sollya_lib_off())
623 5 storres
624 5 storres
def pobyso_set_canonical_on():
625 5 storres
    sollya_lib_set_canonical(sollya_lib_on())
626 5 storres
627 5 storres
def pobyso_set_prec(p):
628 38 storres
    """ Legacy function. See pobyso_set_prec_sa_so. """
629 38 storres
    return( pobyso_set_prec_sa_so(p))
630 38 storres
631 38 storres
def pobyso_set_prec_sa_so(p):
632 5 storres
    a = c_int(p)
633 5 storres
    precSo = c_void_p(sollya_lib_constant_from_int(a))
634 5 storres
    sollya_lib_set_prec(precSo)
635 5 storres
636 5 storres
def pobyso_taylor(function, degree, point):
637 38 storres
    """ Legacy function. See pobysoTaylor_so_so. """
638 38 storres
    return(pobyso_taylor_so_so(function, degree, point))
639 38 storres
640 38 storres
def pobyso_taylor_so_so(function, degree, point):
641 5 storres
    return(sollya_lib_taylor(function, degree, point))
642 5 storres
643 5 storres
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
644 38 storres
    """ Legacy function. See ;"""
645 38 storres
646 38 storres
def pobyso_taylorform_sa_sa(functionSa, \
647 38 storres
                            degree, \
648 43 storres
                            point, \
649 43 storres
                            precision, \
650 38 storres
                            interval = None, \
651 38 storres
                            errorType=None):
652 37 storres
    """
653 38 storres
    Compute the Taylor form of 'degree' for 'functionSa' at 'point'
654 37 storres
    for 'interval' with 'errorType'.
655 37 storres
    point: must be a Real or a Real interval.
656 37 storres
    return the Taylor form as an array
657 38 storres
    TODO: take care of the interval and of point when it is an interval;
658 38 storres
          when errorType is not None;
659 38 storres
          take care of the other elements of the Taylor form (coefficients errors and
660 38 storres
          delta.
661 37 storres
    """
662 37 storres
    # Absolute as the default error.
663 5 storres
    if errorType is None:
664 37 storres
        errorTypeSo = sollya_lib_absolute()
665 37 storres
    else:
666 37 storres
        #TODO: deal with the other case.
667 37 storres
        pass
668 38 storres
    varSa = functionSa.variables()[0]
669 37 storres
    pointBaseRingString = str(point.base_ring())
670 37 storres
    if not re.search('Real', pointBaseRingString):
671 37 storres
        return None
672 37 storres
    # Call Sollya but first "sollyafy" the arguments.
673 37 storres
    sollya_lib_init(None)
674 38 storres
    pobyso_name_free_variable_sa_so(str(varSa))
675 38 storres
    #pobyso_set_prec_sa_so(300)
676 37 storres
    # Sollyafy the function.
677 38 storres
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
678 37 storres
    if sollya_lib_obj_is_error(functionSo):
679 37 storres
        print "pobyso_tailorform: function string can't be parsed!"
680 37 storres
        return None
681 37 storres
    # Sollyafy the degree
682 37 storres
    degreeSo = sollya_lib_constant_from_int(int(degree))
683 37 storres
    # Sollyafy the point
684 37 storres
    if not re.search('Interval', pointBaseRingString):
685 38 storres
        pointSo  = pobyso_constant_sa_so(point)
686 37 storres
    else:
687 37 storres
        # TODO: deal with the interval case.
688 37 storres
        pass
689 37 storres
    # Call Sollya
690 37 storres
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
691 37 storres
                                         None)
692 38 storres
    (tfsAsList, numElements, isEndElliptic) = \
693 38 storres
            pobyso_get_list_elements_so_so(taylorFormSo)
694 37 storres
    polySo = tfsAsList[0]
695 38 storres
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
696 37 storres
    polyRealField = RealField(maxPrecision)
697 38 storres
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
698 37 storres
    sollya_lib_close()
699 37 storres
    polynomialRing = polyRealField[str(varSa)]
700 37 storres
    polySa = polynomial(expSa, polynomialRing)
701 37 storres
    taylorFormSa = [polySa]
702 51 storres
    return(taylorFormSa)
703 51 storres
# End pobyso_taylor_form_sa_sa
704 51 storres
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, errorTypeSo=None):
705 51 storres
    if errorTypeSo is None:
706 51 storres
        errorTypeSo = sollya_lib_absolute()
707 51 storres
    else:
708 51 storres
        #TODO: deal with the other case.
709 51 storres
        pass
710 51 storres
    if intervalSo is None:
711 51 storres
        resultSo = sollya_lib_taylorform(functionSo, degree, pointSo, errorTypeSo, None)
712 51 storres
    else:
713 51 storres
        resultSo = sollya_lib_taylorform(functionSo, degree, pointSo, intervalSo, errorTypeSo, None)
714 51 storres
    sollya_lib_clear_obj(errorTypeSo)
715 51 storres
    return(resultSo)
716 51 storres
717 37 storres
718 37 storres
def pobyso_univar_polynomial_print_reverse(polySa):
719 51 storres
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
720 51 storres
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
721 38 storres
722 51 storres
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
723 37 storres
    """
724 37 storres
    Return the string representation of a univariate polynomial with
725 38 storres
    monomials ordered in the x^0..x^n order of the monomials.
726 37 storres
    Remember: Sage
727 37 storres
    """
728 37 storres
    polynomialRing = polySa.base_ring()
729 37 storres
    # A very expensive solution:
730 37 storres
    # -create a fake multivariate polynomial field with only one variable,
731 37 storres
    #   specifying a negative lexicographical order;
732 37 storres
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
733 37 storres
                                     polynomialRing.variable_name(), \
734 37 storres
                                     1, order='neglex')
735 37 storres
    # - convert the univariate argument polynomial into a multivariate
736 37 storres
    #   version;
737 37 storres
    p = mpolynomialRing(polySa)
738 37 storres
    # - return the string representation of the converted form.
739 37 storres
    # There is no simple str() method defined for p's class.
740 37 storres
    return(p.__str__())
741 5 storres
#
742 5 storres
print "Superficial test of pobyso:"
743 5 storres
print pobyso_get_prec()
744 5 storres
pobyso_set_prec(165)
745 5 storres
print pobyso_get_prec()
746 5 storres
a=100
747 5 storres
print type(a)
748 5 storres
id(a)
749 5 storres
print "Max arity: ", pobyso_max_arity
750 5 storres
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
751 5 storres
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)