Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 52

Historique | Voir | Annoter | Télécharger (26,81 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 38 storres
    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 52 storres
    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 38 storres
    """ Legacy function. See ."""
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 5 storres
def pobyso_get_list_elements(soObj):
282 38 storres
    """ Legacy function. See pobyso_get_list_elements_so_so. """
283 38 storres
    return(pobyso_get_list_elements_so_so(soObj))
284 38 storres
285 38 storres
def pobyso_get_list_elements_so_so(soObj):
286 51 storres
    """
287 51 storres
    Get the list elements as a Sage/Python array of Sollya objects.
288 51 storres
    The other data returned are also Sage/Python objects.
289 51 storres
    """
290 5 storres
    listAddress = POINTER(c_longlong)()
291 5 storres
    numElements = c_int(0)
292 5 storres
    isEndElliptic = c_int(0)
293 5 storres
    listAsList = []
294 5 storres
    result = sollya_lib_get_list_elements(byref(listAddress),\
295 5 storres
                                        byref(numElements),\
296 5 storres
                                        byref(isEndElliptic),\
297 5 storres
                                        soObj)
298 5 storres
    if result == 0 :
299 5 storres
        return None
300 5 storres
    for i in xrange(0, numElements.value, 1):
301 5 storres
        listAsList.append(listAddress[i])
302 5 storres
    return(listAsList, numElements.value, isEndElliptic.value)
303 5 storres
304 38 storres
def pobyso_get_max_prec_of_exp(soExp):
305 38 storres
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
306 38 storres
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
307 5 storres
308 38 storres
def pobyso_get_max_prec_of_exp_so_sa(soExp):
309 38 storres
    """
310 38 storres
    Get the maximum precision used for the numbers in a Sollya expression.
311 52 storres

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