Statistiques
| Révision :

root / pobysoPythonSage / src / pobyso.py @ 55

Historique | Voir | Annoter | Télécharger (30,53 ko)

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

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

8
pobyso functions come in five flavors: 
9
- the _so_so (arguments and returned objects are pointers to Sollya objects, includes
10
  the void function and the no arguments function that return a pointer to a Sollya
11
  object);
12
- the _so_sa (argument are pointers to Sollya objects, returned objects are
13
  Sage/Python objects or, more generally, information is transfered from the Sollya
14
  world to Sage/Python world);
15
- the _sa_so (arguments are Sage/Python objects, returned objects are 
16
  pointers to Sollya objects);
17
- the sa_sa (arguments and returned objects are all Sage/Python objects);
18
- a catch all flavor, without any suffix, (e. g. functions that have no argument 
19
  nor return value).
20
NOTES:
21
Reported errors in Eclipse come from the calls to
22
the Sollya library
23

24
ToDo (among other things): 
25
 -memory management.
26
"""
27
from ctypes import *
28
import re
29
from sage.symbolic.expression_conversions import polynomial
30
"""
31
Create the equivalent to an enum for the Sollya function types.
32
"""
33
(SOLLYA_BASE_FUNC_ABS,
34
SOLLYA_BASE_FUNC_ACOS,
35
    SOLLYA_BASE_FUNC_ACOSH,
36
    SOLLYA_BASE_FUNC_ADD,
37
    SOLLYA_BASE_FUNC_ASIN,
38
    SOLLYA_BASE_FUNC_ASINH,
39
    SOLLYA_BASE_FUNC_ATAN,
40
    SOLLYA_BASE_FUNC_ATANH,
41
    SOLLYA_BASE_FUNC_CEIL,
42
    SOLLYA_BASE_FUNC_CONSTANT,
43
    SOLLYA_BASE_FUNC_COS,
44
    SOLLYA_BASE_FUNC_COSH,
45
    SOLLYA_BASE_FUNC_DIV,
46
    SOLLYA_BASE_FUNC_DOUBLE,
47
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
48
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
49
    SOLLYA_BASE_FUNC_ERF,
50
    SOLLYA_BASE_FUNC_ERFC,
51
    SOLLYA_BASE_FUNC_EXP,
52
    SOLLYA_BASE_FUNC_EXP_M1,
53
    SOLLYA_BASE_FUNC_FLOOR,
54
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
55
    SOLLYA_BASE_FUNC_HALFPRECISION,
56
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
57
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
58
    SOLLYA_BASE_FUNC_LOG,
59
    SOLLYA_BASE_FUNC_LOG_10,
60
    SOLLYA_BASE_FUNC_LOG_1P,
61
    SOLLYA_BASE_FUNC_LOG_2,
62
    SOLLYA_BASE_FUNC_MUL,
63
    SOLLYA_BASE_FUNC_NEARESTINT,
64
    SOLLYA_BASE_FUNC_NEG,
65
    SOLLYA_BASE_FUNC_PI,
66
    SOLLYA_BASE_FUNC_POW,
67
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
68
    SOLLYA_BASE_FUNC_QUAD,
69
    SOLLYA_BASE_FUNC_SIN,
70
    SOLLYA_BASE_FUNC_SINGLE,
71
    SOLLYA_BASE_FUNC_SINH,
72
    SOLLYA_BASE_FUNC_SQRT,
73
    SOLLYA_BASE_FUNC_SUB,
74
    SOLLYA_BASE_FUNC_TAN,
75
    SOLLYA_BASE_FUNC_TANH,
76
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
77
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
78
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
79

    
80
pobyso_max_arity = 9
81

    
82
def pobyso_autoprint(arg):
83
    sollya_lib_autoprint(arg,None)
84

    
85
def pobyso_autoprint_so_so(arg):
86
    sollya_lib_autoprint(arg,None)
87
    
88
def pobyso_absolute_so_so():
89
    return(sollya_lib_absolute(None))
90

    
91
def pobyso_build_function_sub_so_so(exp1So, exp2So):
92
    return(sollya_lib_build_function_sub(exp1So, exp2So))
93

    
94
def pobyso_cmp(rnArg, soCte):
95
    """
96
    Compare the MPFR value a RealNumber with that of a Sollya constant.
97
    
98
    Get the value of the Sollya constant into a RealNumber and compare
99
    using MPFR. Could be optimized by working directly with a mpfr_t
100
    for the intermediate number. 
101
    """
102
    precisionOfCte = c_int(0)
103
    # From the Sollya constant, create a local Sage RealNumber.
104
    sollya_lib_get_prec_of_constant(precisionOfCte, soCte) 
105
    #print "Precision of constant: ", precisionOfCte
106
    RRRR = RealField(precisionOfCte.value)
107
    rnLocal = RRRR(0)
108
    sollya_lib_get_constant(get_rn_value(rnLocal), soCte)
109
    #print "rnDummy: ", rnDummy
110
    # Compare the local Sage RealNumber with rnArg.
111
    return(cmp_rn_value(rnArg, rnLocal))
112

    
113
def pobyso_change_var_in_function_so_so(funcSo, chvarExpSo):
114
    return(sollya_lib_evaluate(funcSo,chvarExpSo))
115
    
116

    
117
def pobyso_chebyshevform_so_so(functionSo, degreeSo, intervalSo):
118
    resultSo = sollya_lib_chebyshevform(functionSo, degreeSo, intervalSo)
119
    return(resultSo)
120
        
121

    
122

    
123
def pobyso_compute_pos_function_abs_val_bounds_sa_sa(funcSa, lowerBoundSa, \
124
                                                     upperBoundSa):
125
    """
126
    TODO: set the variable name in Sollya.
127
    """
128
    funcSo = pobyso_parse_string(funcSa._assume_str())
129
    rangeSo = pobyso_range_sa_so(lowerBoundSa, upperBoundSa)
130
    infnormSo = pobyso_infnorm_so_so(funcSo,rangeSo)
131
    fMaxSa = pobyso_get_interval_from_range_so_sa(infnormSo)
132
    # Get the top bound and compute the binade top limit.
133
    fMaxUpperBoundSa = fMaxSa.upper()
134
    binadeTopLimitSa = 2**ceil(fMaxUpperBoundSa.log2())
135
    # Put up together the function to use to compute the lower bound.
136
    funcAuxSo = pobyso_parse_string(str(binadeTopLimitSa) +  \
137
                                    '-(' + f._assume_str() + ')')
138
    pobyso_autoprint(funcAuxSo)
139
    # Clear the Sollay range before a new call to infnorm and issue the call.
140
    sollya_lib_clear_obj(infnormSo)
141
    infnormSo = pobyso_infnorm_so_so(funcAuxSo,rangeSo)
142
    fMinSa = pobyso_get_interval_from_range_so_sa(infnormSo)
143
    sollya_lib_clear_obj(infnormSo)
144
    fMinLowerBoundSa = topBinadeLimit - fMinSa.lower()
145
    # Compute the maximum of the precisions of the different bounds.
146
    maxPrecSa = max([fMinLowerBoundSa.parent().precision(), \
147
                     fMaxUpperBoundSa.parent().precision()])
148
    # Create a RealIntervalField and create an interval with the "good" bounds.
149
    RRRI = RealIntervalField(maxPrecSa)
150
    imageIntervalSa = RRRI(fMinLowerBoundSa, fMaxUpperBoundSa)
151
    # Free the uneeded Sollya objects
152
    sollya_lib_clear_obj(funcSo)
153
    sollya_lib_clear_obj(funcAuxSo)
154
    sollya_lib_clear_obj(rangeSo)
155
    return(imageIntervalSa)
156
    # End pobyso_compute_function_abs_val_bounds_sa_sa
157

    
158
def pobyso_constant(rnArg):
159
    """ Legacy function. See pobyso_constant_sa_so. """
160
    return(pobyso_constant_sa_so(rnArg))
161
    
162
def pobyso_constant_sa_so(rnArg):
163
    """
164
    Create a Sollya constant from a RealNumber.
165
    """
166
    return(sollya_lib_constant(get_rn_value(rnArg)))
167
    
168
def pobyso_constant_0_sa_so():
169
    return(pobyso_constant_from_int_sa_so(0))
170

    
171
def pobyso_constant_1():
172
    """ Legacy function. See pobyso_constant_so_so. """
173
    return(pobyso_constant_1_sa_so())
174

    
175
def pobyso_constant_1_sa_so():
176
    return(pobyso_constant_from_int_sa_so(1))
177

    
178
def pobyso_constant_from_int(anInt):
179
    """ Legacy function. See pobyso_constant_from_int_sa_so. """
180
    return(pobyso_constant_from_int_sa_so(anInt))
181

    
182
def pobyso_constant_from_int_sa_so(anInt):
183
    return(sollya_lib_constant_from_int(int(anInt)))
184

    
185
def pobyso_function_type_as_string(funcType):
186
    """ Legacy function. See pobyso_function_type_as_string_so_sa. """
187
    return(pobyso_function_type_as_string_so_sa(funcType))
188

    
189
def pobyso_function_type_as_string_so_sa(funcType):
190
    """
191
    Numeric Sollya function codes -> Sage mathematical function names.
192
    Notice that pow -> ^ (a la Sage, not a la Python).
193
    """
194
    if funcType == SOLLYA_BASE_FUNC_ABS:
195
        return "abs"
196
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
197
        return "arccos"
198
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
199
        return "arccosh"
200
    elif funcType == SOLLYA_BASE_FUNC_ADD:
201
        return "+"
202
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
203
        return "arcsin"
204
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
205
        return "arcsinh"
206
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
207
        return "arctan"
208
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
209
        return "arctanh"
210
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
211
        return "ceil"
212
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
213
        return "cte"
214
    elif funcType == SOLLYA_BASE_FUNC_COS:
215
        return "cos"
216
    elif funcType == SOLLYA_BASE_FUNC_COSH:
217
        return "cosh"
218
    elif funcType == SOLLYA_BASE_FUNC_DIV:
219
        return "/"
220
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
221
        return "double"
222
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
223
        return "doubleDouble"
224
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
225
        return "doubleDxtended"
226
    elif funcType == SOLLYA_BASE_FUNC_ERF:
227
        return "erf"
228
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
229
        return "erfc"
230
    elif funcType == SOLLYA_BASE_FUNC_EXP:
231
        return "exp"
232
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
233
        return "expm1"
234
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
235
        return "floor"
236
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
237
        return "freeVariable"
238
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
239
        return "halfPrecision"
240
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
241
        return "libraryConstant"
242
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
243
        return "libraryFunction"
244
    elif funcType == SOLLYA_BASE_FUNC_LOG:
245
        return "log"
246
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
247
        return "log10"
248
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
249
        return "log1p"
250
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
251
        return "log2"
252
    elif funcType == SOLLYA_BASE_FUNC_MUL:
253
        return "*"
254
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
255
        return "round"
256
    elif funcType == SOLLYA_BASE_FUNC_NEG:
257
        return "__neg__"
258
    elif funcType == SOLLYA_BASE_FUNC_PI:
259
        return "pi"
260
    elif funcType == SOLLYA_BASE_FUNC_POW:
261
        return "^"
262
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
263
        return "procedureFunction"
264
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
265
        return "quad"
266
    elif funcType == SOLLYA_BASE_FUNC_SIN:
267
        return "sin"
268
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
269
        return "single"
270
    elif funcType == SOLLYA_BASE_FUNC_SINH:
271
        return "sinh"
272
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
273
        return "sqrt"
274
    elif funcType == SOLLYA_BASE_FUNC_SUB:
275
        return "-"
276
    elif funcType == SOLLYA_BASE_FUNC_TAN:
277
        return "tan"
278
    elif funcType == SOLLYA_BASE_FUNC_TANH:
279
        return "tanh"
280
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
281
        return "tripleDouble"
282
    else:
283
        return None
284

    
285
def pobyso_get_constant(rnArg, soConst):
286
    """ Legacy function. See pobyso_get_constant_so_sa. """
287
    return(pobyso_get_constant_so_sa(rnArg, soConst))
288

    
289
def pobyso_get_constant_so_sa(rnArg, soConst):
290
    """
291
    Set the value of rnArg to the value of soConst in MPFR_RNDN mode.
292
    rnArg must already exist and belong to some RealField.
293
    We assume that soConst points to a Sollya constant.
294
    """
295
    return(sollya_lib_get_constant(get_rn_value(rnArg), soConst))
296
    
297
def pobyso_get_constant_as_rn(ctExp):
298
    """ Legacy function. See pobyso_get_constant_as_rn_so_sa. """ 
299
    return(pobyso_get_constant_as_rn_so_sa(ctExp))
300
    
301
def pobyso_get_constant_as_rn_so_sa(constExp):
302
    precision  = pobyso_get_prec_of_constant(constExp) 
303
    RRRR = RealField(precision)
304
    rn = RRRR(0)
305
    sollya_lib_get_constant(get_rn_value(rn), constExp)
306
    return(rn)
307

    
308
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
309
    """ Legacy function. See pobyso_get_constant_as_rn_with_rf_so_sa."""
310
    return(pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField))
311
    
312
def pobyso_get_constant_as_rn_with_rf_so_sa(ctExp, realField):
313
    rn = realField(0)
314
    sollya_lib_get_constant(get_rn_value(rn), ctExp)
315
    return(rn)
316

    
317
def pobyso_get_free_variable_name():
318
    """ Legacy function. See pobyso_get_free_variable_name_so_sa."""
319
    return(pobyso_get_free_variable_name_so_sa())
320

    
321
def pobyso_get_free_variable_name_so_sa():
322
    return(sollya_lib_get_free_variable_name())
323
    
324
def pobyso_get_function_arity(expressionSo):
325
    """ Legacy function. See pobyso_get_function_arity_so_sa."""
326
    return(pobyso_get_function_arity_so_sa(expressionSo))
327

    
328
def pobyso_get_function_arity_so_sa(expressionSo):
329
    arity = c_int(0)
330
    sollya_lib_get_function_arity(byref(arity),expressionSo)
331
    return(int(arity.value))
332

    
333
def pobyso_get_head_function(expressionSo):
334
    """ Legacy function. See pobyso_get_head_function_so_sa. """
335
    return(pobyso_get_head_function_so_sa(expressionSo)) 
336

    
337
def pobyso_get_head_function_so_sa(expressionSo):
338
    functionType = c_int(0)
339
    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
340
    return(int(functionType.value))
341

    
342
def pobyso_get_interval_from_range_so_sa(soRange):
343
    """
344
    Return the Sage interval corresponding to the Sollya range argument.
345
    The interval bounds are not rounded: they are elements of RealIntervalField
346
    of the "right" precision.
347
    """
348
    prec = c_int(0)
349
    retval = sollya_lib_get_prec_of_range(byref(prec), soRange, None)
350
    if retval == 0:
351
        return(None)
352
    RRRI = RealIntervalField(prec.value)
353
    intervalSa = RRRI(0,0)
354
    retval = \
355
        sollya_lib_get_interval_from_range(get_interval_value(intervalSa),\
356
                                           soRange)
357
    if retval == 0:
358
        return(None)
359
    return(intervalSa)
360
    
361
def pobyso_get_list_elements(soObj):
362
    """ Legacy function. See pobyso_get_list_elements_so_so. """
363
    return(pobyso_get_list_elements_so_so(soObj))
364
 
365
def pobyso_get_list_elements_so_so(soObj):
366
    """
367
    Get the list elements as a Sage/Python array of Sollya objects.
368
    The other data returned are also Sage/Python objects.
369
    """
370
    listAddress = POINTER(c_longlong)()
371
    numElements = c_int(0)
372
    isEndElliptic = c_int(0)
373
    listAsList = []
374
    result = sollya_lib_get_list_elements(byref(listAddress),\
375
                                          byref(numElements),\
376
                                          byref(isEndElliptic),\
377
                                          soObj)
378
    if result == 0 :
379
        return None
380
    for i in xrange(0, numElements.value, 1):
381
        listAsList.append(listAddress[i])
382
    return(listAsList, numElements.value, isEndElliptic.value)
383

    
384
def pobyso_get_max_prec_of_exp(soExp):
385
    """ Legacy function. See pobyso_get_max_prec_of_exp_so_sa. """
386
    return(pobyso_get_max_prec_of_exp_so_sa(soExp))
387

    
388
def pobyso_get_max_prec_of_exp_so_sa(soExp):
389
    """
390
    Get the maximum precision used for the numbers in a Sollya expression.
391
    
392
    Arguments:
393
    soExp -- a Sollya expression pointer
394
    Return value:
395
    A Python integer
396
    TODO: 
397
    - error management;
398
    - correctly deal with numerical type such as DOUBLEEXTENDED.
399
    """
400
    maxPrecision = 0
401
    minConstPrec = 0
402
    currentConstPrec = 0
403
    operator = pobyso_get_head_function_so_sa(soExp)
404
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
405
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
406
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(soExp)
407
        for i in xrange(arity):
408
            maxPrecisionCandidate = \
409
                pobyso_get_max_prec_of_exp_so_sa(subexpressions[i])
410
            if maxPrecisionCandidate > maxPrecision:
411
                maxPrecision = maxPrecisionCandidate
412
        return(maxPrecision)
413
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
414
        minConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
415
        #currentConstPrec = pobyso_get_min_prec_of_constant_so_sa(soExp)
416
        #print minConstPrec, " - ", currentConstPrec 
417
        return(pobyso_get_min_prec_of_constant_so_sa(soExp))
418
    
419
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
420
        return(0)
421
    else:
422
        print "pobyso_get_max_prec_of_exp_so_sa: unexepected operator."
423
        return(0)
424

    
425
def pobyso_get_min_prec_of_constant_so_sa(soConstExp):
426
    """
427
    Get the minimum precision necessary to represent the value of a Sollya
428
    constant.
429
    MPFR_MIN_PREC and powers of 2 are taken into account.
430
    We assume that soCteExp is a point
431
    """
432
    constExpAsRn = pobyso_get_constant_as_rn_so_sa(soConstExp)
433
    return(min_mpfr_size(get_rn_value(constExpAsRn)))
434

    
435
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
436
    """ Legacy function. See pobyso_get_sage_exp_from_sollya_exp_so_sa. """
437
    return(pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR))
438

    
439
def pobyso_get_sage_exp_from_sollya_exp_so_sa(sollyaExp, realField = RR):
440
    """
441
    Get a Sage expression from a Sollya expression. 
442
    Currently only tested with polynomials with floating-point coefficients.
443
    Notice that, in the returned polynomial, the exponents are RealNumbers.
444
    """
445
    #pobyso_autoprint(sollyaExp)
446
    operator = pobyso_get_head_function_so_sa(sollyaExp)
447
    # Constants and the free variable are special cases.
448
    # All other operator are dealt with in the same way.
449
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
450
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
451
        (arity, subexpressions) = pobyso_get_subfunctions_so_sa(sollyaExp)
452
        if arity == 1:
453
            sageExp = eval(pobyso_function_type_as_string_so_sa(operator) + \
454
            "(" + pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], \
455
            realField) + ")")
456
        elif arity == 2:
457
            if operator == SOLLYA_BASE_FUNC_POW:
458
                operatorAsString = "**"
459
            else:
460
                operatorAsString = \
461
                    pobyso_function_type_as_string_so_sa(operator)
462
            sageExp = \
463
              eval("pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[0], realField)"\
464
              + " " + operatorAsString + " " + \
465
                   "pobyso_get_sage_exp_from_sollya_exp_so_sa(subexpressions[1], realField)")
466
        # We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
467
        else:
468
            sageExp = eval('None')
469
        return(sageExp)
470
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
471
        #print "This is a constant"
472
        return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExp, realField)
473
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
474
        #print "This is free variable"
475
        return(eval(sollya_lib_get_free_variable_name()))
476
    else:
477
        print "Unexpected"
478
        return eval('None')
479
# End pobyso_get_sage_poly_from_sollya_poly
480
    
481
def pobyso_get_subfunctions(expressionSo):
482
    """ Legacy function. See pobyso_get_subfunctions_so_sa. """
483
    return(pobyso_get_subfunctions_so_sa(expressionSo)) 
484

    
485
def pobyso_get_subfunctions_so_sa(expressionSo):
486
    """
487
    Get the subfunctions of an expression.
488
    Return the number of subfunctions and the list of subfunctions addresses.
489
    S.T.: Could not figure out another way than that ugly list of declarations
490
    to recover the addresses of the subfunctions. 
491
    """
492
    subf0 = c_int(0)
493
    subf1 = c_int(0)
494
    subf2 = c_int(0)
495
    subf3 = c_int(0)
496
    subf4 = c_int(0)
497
    subf5 = c_int(0)
498
    subf6 = c_int(0)
499
    subf7 = c_int(0)
500
    subf8 = c_int(0)
501
    arity = c_int(0)
502
    nullPtr = POINTER(c_int)()
503
    sollya_lib_get_subfunctions(expressionSo, byref(arity), \
504
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
505
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None) 
506
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
507
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
508
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
509
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
510
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
511
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
512
    subs = []
513
    if arity.value > pobyso_max_arity:
514
        return(0,[])
515
    for i in xrange(arity.value):
516
        subs.append(int(subfunctions[i].value))
517
        #print subs[i]
518
    return(int(arity.value), subs)
519
    
520
def pobyso_get_prec():
521
    """ Legacy function. See pobyso_get_prec_so_sa(). """
522
    return(pobyso_get_prec_so_sa())
523

    
524
def pobyso_get_prec_so_sa():
525
    """
526
    Get the current default precision in Sollya.
527
    The return value is Sage/Python int.
528
    """
529
    retc = sollya_lib_get_prec(None)
530
    a = c_int(0)
531
    sollya_lib_get_constant_as_int(byref(a), retc)
532
    return(int(a.value))
533

    
534
def pobyso_get_prec_of_constant(ctExpSo):
535
    """ Legacy function. See pobyso_get_prec_of_constant_so_sa. """
536
    return(pobyso_get_prec_of_constant_so_sa(ctExpSo))
537

    
538
def pobyso_get_prec_of_range_so_sa(rangeSo):
539
    prec = c_int(0)
540
    retc = sollya_lib_get_prec_of_range(byref(prec), rangeSo, None)
541
    return(int(prec.value))
542

    
543
def pobyso_infnorm_so_so(func, interval, file = None, intervalList = None):
544
    print "Do not use this function. User pobyso_supnorm_so_so instead."
545
    return(None)
546

    
547
def pobyso_lib_init():
548
    sollya_lib_init(None)
549
    
550
def pobyso_name_free_variable(freeVariableName):
551
    """ Legacy function. See pobyso_name_free_variable_sa_so. """
552
    pobyso_name_free_variable_sa_so(freeVariableName)
553

    
554
def pobyso_name_free_variable_sa_so(freeVariableName):
555
    sollya_lib_name_free_variable(freeVariableName)
556

    
557
def pobyso_parse_string(string):
558
    """ Legacy function. See pobyso_parse_string_sa_so. """
559
    return(pobyso_parse_string_sa_so(string))
560
 
561
def pobyso_parse_string_sa_so(string):
562
    return(sollya_lib_parse_string(string))
563

    
564
def pobyso_range(rnLowerBound, rnUpperBound):
565
    """ Legacy function. See pobyso_range_sa_so. """
566
    return(pobyso_range_sa_so(rnLowerBound, rnUpperBound)) 
567

    
568
def pobyso_range_sa_so(rnLowerBound, rnUpperBound):
569
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound))
570
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound))
571
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
572
    return(rangeSo)
573

    
574
def pobyso_remez_canonical_sa_sa(func, \
575
                                 degree, \
576
                                 lowerBound, \
577
                                 upperBound, \
578
                                 weight = None, \
579
                                 quality = None):
580
    """
581
    All arguments are Sage/Python.
582
    The functions (func and weight) must be passed as expressions or strings.
583
    Otherwise the function fails. 
584
    The return value is a pointer is a Sage polynomial.
585
    """
586
    var('zorglub')    # Dummy variable name for type check only.
587
    polySo = pobyso_remez_canonical_sa_so(func, \
588
                                 degree, \
589
                                 lowerBound, \
590
                                 upperBound, \
591
                                 weight = None, \
592
                                 quality = None)
593
    if parent(func) == parent("string"):
594
        functionSa = eval(func)
595
    # Expression test.
596
    elif type(func) == type(zorglub):
597
        functionSa = func
598
    maxPrecision = 0
599
    if polySo is None:
600
        return(None)
601
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
602
    RRRR = RealField(maxPrecision)
603
    polynomialRing = RRRR[functionSa.variables()[0]]
604
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, RRRR)
605
    polySa = polynomial(expSa, polynomialRing)
606
    return(polySa)
607
    
608
def pobyso_remez_canonical(func, \
609
                           degree, \
610
                           lowerBound, \
611
                           upperBound, \
612
                           weight = "1", \
613
                           quality = None):
614
    """ Legacy function. See pobyso_remez_canonical_sa_so. """
615
    return(pobyso_remez_canonical_sa_so(func, \
616
                                        degree, \
617
                                        lowerBound, \
618
                                        upperBound, \
619
                                        weight, \
620
                                        quality))
621
def pobyso_remez_canonical_sa_so(func, \
622
                                 degree, \
623
                                 lowerBound, \
624
                                 upperBound, \
625
                                 weight = None, \
626
                                 quality = None):
627
    """
628
    All arguments are Sage/Python.
629
    The functions (func and weight) must be passed as expressions or strings.
630
    Otherwise the function fails. 
631
    The return value is a pointer to a Sollya function.
632
    """
633
    var('zorglub')    # Dummy variable name for type check only.
634
    currentVariableName = None
635
    # The func argument can be of different types (string, 
636
    # symbolic expression...)
637
    if parent(func) == parent("string"):
638
        functionSo = sollya_lib_parse_string(func)
639
    # Expression test.
640
    elif type(func) == type(zorglub):
641
        # Until we are able to translate Sage expressions into Sollya 
642
        # expressions : parse the string version.
643
        currentVariableName = func.variables()[0]
644
        sollya_lib_name_free_variable(str(currentVariableName))
645
        functionSo = sollya_lib_parse_string(func._assume_str())
646
    else:
647
        return(None)
648
    if weight is None:
649
        weightSo = pobyso_constant_1_sa_so()
650
    elif parent(weight) == parent("string"):
651
        weightSo = sollya_lib_parse_string(func)
652
    elif type(weight) == type(zorglub): 
653
        functionSo = sollya_lib_parse_string_sa_so(weight._assume_str())
654
    else:
655
        return(None)
656
    degreeSo = pobyso_constant_from_int(degree)
657
    rangeSo = pobyso_range_sa_so(lowerBound, upperBound)
658
    if not quality is None:
659
        qualitySo= pobyso_constant_sa_so(quality)
660
    else:
661
        qualitySo = None
662
    return(sollya_lib_remez(functionSo, \
663
                            degreeSo, \
664
                            rangeSo, \
665
                            weightSo, \
666
                            qualitySo, \
667
                            None))
668
    
669
def pobyso_remez_canonical_so_so(funcSo, \
670
                                 degreeSo, \
671
                                 rangeSo, \
672
                                 weightSo = pobyso_constant_1_sa_so(),\
673
                                 qualitySo = None):
674
    """
675
    All arguments are pointers to Sollya objects.
676
    The return value is a pointer to a Sollya function.
677
    """
678
    if not sollya_lib_obj_is_function(funcSo):
679
        return(None)
680
    return(sollya_lib_remez(funcSo, degreeSo, rangeSo, weightSo, qualitySo, None))
681
    
682
def pobyso_set_canonical_off():
683
    sollya_lib_set_canonical(sollya_lib_off())
684

    
685
def pobyso_set_canonical_on():
686
    sollya_lib_set_canonical(sollya_lib_on())
687

    
688
def pobyso_set_prec(p):
689
    """ Legacy function. See pobyso_set_prec_sa_so. """
690
    return( pobyso_set_prec_sa_so(p))
691

    
692
def pobyso_set_prec_sa_so(p):
693
    a = c_int(p)
694
    precSo = c_void_p(sollya_lib_constant_from_int(a))
695
    sollya_lib_set_prec(precSo)
696

    
697
def pobyso_supnorm_so_so(polySo, funcSo, intervalSo, errorTypeSo, accuracySo):
698
    return(sollya_lib_supnorm(polySo, funcSo, intervalSo, errorTypeSo, \
699
                              accuracySo))
700

    
701
def pobyso_taylor(function, degree, point):
702
    """ Legacy function. See pobysoTaylor_so_so. """
703
    return(pobyso_taylor_so_so(function, degree, point))
704

    
705
def pobyso_taylor_so_so(function, degree, point):
706
    return(sollya_lib_taylor(function, degree, point))
707
    
708
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
709
    """ Legacy function. See ;"""
710
    
711
def pobyso_taylorform_sa_sa(functionSa, \
712
                            degree, \
713
                            point, \
714
                            precision, \
715
                            interval=None, \
716
                            errorType=None):
717
    """
718
    Compute the Taylor form of 'degree' for 'functionSa' at 'point' 
719
    for 'interval' with 'errorType'. 
720
    point: must be a Real or a Real interval.
721
    return the Taylor form as an array
722
    TODO: take care of the interval and of point when it is an interval;
723
          when errorType is not None;
724
          take care of the other elements of the Taylor form (coefficients errors and
725
          delta.
726
    """
727
    # Absolute as the default error.
728
    if errorType is None:
729
        errorTypeSo = sollya_lib_absolute()
730
    else:
731
        #TODO: deal with the other case.
732
        pass
733
    varSa = functionSa.variables()[0]
734
    pointBaseRingString = str(point.base_ring())
735
    if not re.search('Real', pointBaseRingString):
736
        return None
737
    # Call Sollya but first "sollyafy" the arguments.
738
    pobyso_name_free_variable_sa_so(str(varSa))
739
    #pobyso_set_prec_sa_so(300)
740
    # Sollyafy the function.
741
    functionSo = pobyso_parse_string_sa_so(functionSa._assume_str())
742
    if sollya_lib_obj_is_error(functionSo):
743
        print "pobyso_tailorform: function string can't be parsed!"
744
        return None
745
    # Sollyafy the degree
746
    degreeSo = sollya_lib_constant_from_int(int(degree))
747
    # Sollyafy the point
748
    if not re.search('Interval', pointBaseRingString):
749
        pointSo  = pobyso_constant_sa_so(point)
750
    else:
751
        # TODO: deal with the interval case.
752
        pass
753
    # Call Sollya
754
    taylorFormSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, errorTypeSo,\
755
                                         None)
756
    (tfsAsList, numElements, isEndElliptic) = \
757
            pobyso_get_list_elements_so_so(taylorFormSo)
758
    polySo = tfsAsList[0]
759
    maxPrecision = pobyso_get_max_prec_of_exp_so_sa(polySo)
760
    polyRealField = RealField(maxPrecision)
761
    expSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo, polyRealField)
762
    sollya_lib_close()
763
    polynomialRing = polyRealField[str(varSa)]
764
    polySa = polynomial(expSa, polynomialRing)
765
    taylorFormSa = [polySa]
766
    return(taylorFormSa)
767
# End pobyso_taylor_form_sa_sa
768

    
769
def pobyso_taylorform_so_so(functionSo, degreeSo, pointSo, intervalSo=None, \
770
                            errorTypeSo=None):
771
    createdErrorType = False
772
    if errorTypeSo is None:
773
        errorTypeSo = sollya_lib_absolute()
774
        createdErrorType = True
775
    else:
776
        #TODO: deal with the other case.
777
        pass
778
    if intervalSo is None:
779
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
780
                                         errorTypeSo, None)
781
    else:
782
        resultSo = sollya_lib_taylorform(functionSo, degreeSo, pointSo, \
783
                                         intervalSo, errorTypeSo, None)
784
    if createdErrorType:
785
        sollya_lib_clear_obj(errorTypeSo)
786
    return(resultSo)
787
        
788

    
789
def pobyso_univar_polynomial_print_reverse(polySa):
790
    """ Legacy function. See pobyso_univar_polynomial_print_reverse_sa_sa. """
791
    return(pobyso_univar_polynomial_print_reverse_sa_sa(polySa))
792

    
793
def pobyso_univar_polynomial_print_reverse_sa_sa(polySa):
794
    """
795
    Return the string representation of a univariate polynomial with
796
    monomials ordered in the x^0..x^n order of the monomials.
797
    Remember: Sage
798
    """
799
    polynomialRing = polySa.base_ring()
800
    # A very expensive solution:
801
    # -create a fake multivariate polynomial field with only one variable,
802
    #   specifying a negative lexicographical order;
803
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
804
                                     polynomialRing.variable_name(), \
805
                                     1, order='neglex')
806
    # - convert the univariate argument polynomial into a multivariate
807
    #   version;
808
    p = mpolynomialRing(polySa)
809
    # - return the string representation of the converted form.
810
    # There is no simple str() method defined for p's class.
811
    return(p.__str__())
812
#
813
print "Superficial test of pobyso:"    
814
print pobyso_get_prec()  
815
pobyso_set_prec(165)
816
print pobyso_get_prec()  
817
a=100
818
print type(a)
819
id(a)
820
print "Max arity: ", pobyso_max_arity
821
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
822
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)