Révision 5

pobysoPythonSage/pobyso.py (revision 5)
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
NOTES:
9
Reported errors in Eclipse come from the calls to
10
the Sollya library
11

  
12
ToDo (among other things): memory management.
13
"""
14
from ctypes import *
15

  
16
(SOLLYA_BASE_FUNC_ABS,
17
SOLLYA_BASE_FUNC_ACOS,
18
    SOLLYA_BASE_FUNC_ACOSH,
19
    SOLLYA_BASE_FUNC_ADD,
20
    SOLLYA_BASE_FUNC_ASIN,
21
    SOLLYA_BASE_FUNC_ASINH,
22
    SOLLYA_BASE_FUNC_ATAN,
23
    SOLLYA_BASE_FUNC_ATANH,
24
    SOLLYA_BASE_FUNC_CEIL,
25
    SOLLYA_BASE_FUNC_CONSTANT,
26
    SOLLYA_BASE_FUNC_COS,
27
    SOLLYA_BASE_FUNC_COSH,
28
    SOLLYA_BASE_FUNC_DIV,
29
    SOLLYA_BASE_FUNC_DOUBLE,
30
    SOLLYA_BASE_FUNC_DOUBLEDOUBLE,
31
    SOLLYA_BASE_FUNC_DOUBLEEXTENDED,
32
    SOLLYA_BASE_FUNC_ERF,
33
    SOLLYA_BASE_FUNC_ERFC,
34
    SOLLYA_BASE_FUNC_EXP,
35
    SOLLYA_BASE_FUNC_EXP_M1,
36
    SOLLYA_BASE_FUNC_FLOOR,
37
    SOLLYA_BASE_FUNC_FREE_VARIABLE,
38
    SOLLYA_BASE_FUNC_HALFPRECISION,
39
    SOLLYA_BASE_FUNC_LIBRARYCONSTANT,
40
    SOLLYA_BASE_FUNC_LIBRARYFUNCTION,
41
    SOLLYA_BASE_FUNC_LOG,
42
    SOLLYA_BASE_FUNC_LOG_10,
43
    SOLLYA_BASE_FUNC_LOG_1P,
44
    SOLLYA_BASE_FUNC_LOG_2,
45
    SOLLYA_BASE_FUNC_MUL,
46
    SOLLYA_BASE_FUNC_NEARESTINT,
47
    SOLLYA_BASE_FUNC_NEG,
48
    SOLLYA_BASE_FUNC_PI,
49
    SOLLYA_BASE_FUNC_POW,
50
    SOLLYA_BASE_FUNC_PROCEDUREFUNCTION,
51
    SOLLYA_BASE_FUNC_QUAD,
52
    SOLLYA_BASE_FUNC_SIN,
53
    SOLLYA_BASE_FUNC_SINGLE,
54
    SOLLYA_BASE_FUNC_SINH,
55
    SOLLYA_BASE_FUNC_SQRT,
56
    SOLLYA_BASE_FUNC_SUB,
57
    SOLLYA_BASE_FUNC_TAN,
58
    SOLLYA_BASE_FUNC_TANH,
59
SOLLYA_BASE_FUNC_TRIPLEDOUBLE) = map(int,xrange(44))
60
print "First constant - SOLLYA_BASE_FUNC_ABS: ", SOLLYA_BASE_FUNC_ABS
61
print "Last constant  - SOLLYA_BASE_FUNC_TRIPLEDOUBLE: ", SOLLYA_BASE_FUNC_TRIPLEDOUBLE
62

  
63
pobyso_max_arity = 9
64

  
65
def pobyso_autoprint(arg):
66
    sollya_lib_autoprint(arg,None)
67

  
68
def pobyso_cmp(rnArg, soCte):
69
    precisionOfCte = c_int(0)
70
    # From the Sollya constant, create a local Sage RealNumber.
71
    sollya_lib_get_prec_of_constant(precisionOfCte, soCte) 
72
    #print "Precision of constant: ", precisionOfCte
73
    RRRR = RealField(precisionOfCte.value)
74
    rnLocal = RRRR(0)
75
    sollya_lib_get_constant(get_rn_value(rnLocal), soCte)
76
    #print "rnDummy: ", rnDummy
77
    # Compare the local Sage RealNumber with rnArg.
78
    return(cmp_rn_value(rnArg, rnLocal))
79

  
80
def pobyso_constant(rnArg):
81
    return (sollya_lib_constant(get_rn_value(rnArg)))
82
    
83
def pobyso_constant_1():
84
    return(pobyso_constant_from_int(1))
85

  
86
def pobyso_constant_from_int(anInt):
87
    return(sollya_lib_constant_from_int(int(anInt)))
88

  
89
# Numeric Sollya function codes -> Sage mathematical function names
90
def pobyso_function_type_as_string(funcType):
91
    if funcType == SOLLYA_BASE_FUNC_ABS:
92
        return "abs"
93
    elif funcType == SOLLYA_BASE_FUNC_ACOS:
94
        return "arccos"
95
    elif funcType == SOLLYA_BASE_FUNC_ACOSH:
96
        return "arccosh"
97
    elif funcType == SOLLYA_BASE_FUNC_ADD:
98
        return "+"
99
    elif funcType == SOLLYA_BASE_FUNC_ASIN:
100
        return "arcsin"
101
    elif funcType == SOLLYA_BASE_FUNC_ASINH:
102
        return "arcsinh"
103
    elif funcType == SOLLYA_BASE_FUNC_ATAN:
104
        return "arctan"
105
    elif funcType == SOLLYA_BASE_FUNC_ATANH:
106
        return "arctanh"
107
    elif funcType == SOLLYA_BASE_FUNC_CEIL:
108
        return "ceil"
109
    elif funcType == SOLLYA_BASE_FUNC_CONSTANT:
110
        return "cte"
111
    elif funcType == SOLLYA_BASE_FUNC_COS:
112
        return "cos"
113
    elif funcType == SOLLYA_BASE_FUNC_COSH:
114
        return "cosh"
115
    elif funcType == SOLLYA_BASE_FUNC_DIV:
116
        return "/"
117
    elif funcType == SOLLYA_BASE_FUNC_DOUBLE:
118
        return "double"
119
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEDOUBLE:
120
        return "doubleDouble"
121
    elif funcType == SOLLYA_BASE_FUNC_DOUBLEEXTENDED:
122
        return "doubleDxtended"
123
    elif funcType == SOLLYA_BASE_FUNC_ERF:
124
        return "erf"
125
    elif funcType == SOLLYA_BASE_FUNC_ERFC:
126
        return "erfc"
127
    elif funcType == SOLLYA_BASE_FUNC_EXP:
128
        return "exp"
129
    elif funcType == SOLLYA_BASE_FUNC_EXP_M1:
130
        return "expm1"
131
    elif funcType == SOLLYA_BASE_FUNC_FLOOR:
132
        return "floor"
133
    elif funcType == SOLLYA_BASE_FUNC_FREE_VARIABLE:
134
        return "freeVariable"
135
    elif funcType == SOLLYA_BASE_FUNC_HALFPRECISION:
136
        return "halfPrecision"
137
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYCONSTANT:
138
        return "libraryConstant"
139
    elif funcType == SOLLYA_BASE_FUNC_LIBRARYFUNCTION:
140
        return "libraryFunction"
141
    elif funcType == SOLLYA_BASE_FUNC_LOG:
142
        return "log"
143
    elif funcType == SOLLYA_BASE_FUNC_LOG_10:
144
        return "log10"
145
    elif funcType == SOLLYA_BASE_FUNC_LOG_1P:
146
        return "log1p"
147
    elif funcType == SOLLYA_BASE_FUNC_LOG_2:
148
        return "log2"
149
    elif funcType == SOLLYA_BASE_FUNC_MUL:
150
        return "*"
151
    elif funcType == SOLLYA_BASE_FUNC_NEARESTINT:
152
        return "round"
153
    elif funcType == SOLLYA_BASE_FUNC_NEG:
154
        return "__neg__"
155
    elif funcType == SOLLYA_BASE_FUNC_PI:
156
        return "pi"
157
    elif funcType == SOLLYA_BASE_FUNC_POW:
158
        return "^"
159
    elif funcType == SOLLYA_BASE_FUNC_PROCEDUREFUNCTION:
160
        return "procedureFunction"
161
    elif funcType == SOLLYA_BASE_FUNC_QUAD:
162
        return "quad"
163
    elif funcType == SOLLYA_BASE_FUNC_SIN:
164
        return "sin"
165
    elif funcType == SOLLYA_BASE_FUNC_SINGLE:
166
        return "single"
167
    elif funcType == SOLLYA_BASE_FUNC_SINH:
168
        return "sinh"
169
    elif funcType == SOLLYA_BASE_FUNC_SQRT:
170
        return "sqrt"
171
    elif funcType == SOLLYA_BASE_FUNC_SUB:
172
        return "-"
173
    elif funcType == SOLLYA_BASE_FUNC_TAN:
174
        return "tan"
175
    elif funcType == SOLLYA_BASE_FUNC_TANH:
176
        return "tanh"
177
    elif funcType == SOLLYA_BASE_FUNC_TRIPLEDOUBLE:
178
        return "tripleDouble"
179
    else:
180
        return None
181

  
182
def pobyso_get_constant(rnArg, soConst):
183
    set_rn_value(rnArg, soConst)
184
    
185
def pobyso_get_constant_as_rn(ctExp):
186
    precision  = pobyso_get_prec_of_constant(ctExp) 
187
    RRRR = RealField(precision)
188
    rn = RRRR(0)
189
    sollya_lib_get_constant(get_rn_value(rn), ctExp)
190
    return(rn)
191
    
192
def pobyso_get_constant_as_rn_with_rf(ctExp, realField):
193
    rn = realField(0)
194
    sollya_lib_get_constant(get_rn_value(rn), ctExp)
195
    return(rn)
196
def pobyso_get_free_variable_name():
197
    return(sollya_lib_get_free_variable_name())
198
    
199
def pobyso_get_function_arity(expression):
200
    arity = c_int(0)
201
    sollya_lib_get_function_arity(byref(arity),expression)
202
    return(int(arity.value))
203

  
204
def pobyso_get_head_function(expression):
205
    functionType = c_int(0)
206
    sollya_lib_get_head_function(byref(functionType), expression, None)
207
    return(int(functionType.value))
208

  
209
def pobyso_get_list_elements(soObj):
210
    # Type for array of pointers to sollya_obj_t
211
    listAddress = POINTER(c_longlong)()
212
    numElements = c_int(0)
213
    isEndElliptic = c_int(0)
214
    listAsList = []
215
    result = sollya_lib_get_list_elements(byref(listAddress),\
216
                                        byref(numElements),\
217
                                        byref(isEndElliptic),\
218
                                        soObj)
219
    if result == 0 :
220
        return None
221
    for i in xrange(0, numElements.value, 1):
222
        print "address ", i, " ->", listAddress[i]
223
        listAsList.append(listAddress[i])
224
    return(listAsList, numElements.value, isEndElliptic.value)
225

  
226

  
227
# Get the maximum precision used for the numbers in a 
228
# Sollya expression.
229
# ToDo: 
230
# - error management;
231
# - correctly deal with numerical type such as DOUBLEEXTENDED.
232
def pobyso_get_max_prec_of_exp(soExp):
233
    maxPrecision = 0
234
    operator = pobyso_get_head_function(soExp)
235
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
236
    (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
237
        (arity, subexpressions) = pobyso_get_subfunctions(soExp)
238
        for i in xrange(arity):
239
            maxPrecisionCandidate = \
240
            pobyso_get_max_prec_of_exp(subexpressions[i])
241
            if maxPrecisionCandidate > maxPrecision:
242
                maxPrecision = maxPrecisionCandidate
243
        return(maxPrecision)
244
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
245
        #print pobyso_get_prec_of_constant(soExp)
246
        return(pobyso_get_prec_of_constant(soExp))
247
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
248
        return(0)
249
    else:
250
        print "pobyso_get_max_prec_of_exp: unexepected operator."
251
        return(0)
252

  
253
def pobyso_get_sage_exp_from_sollya_exp(sollyaExp, realField = RR):
254
    """
255
    Get a Sage expression from a Sollya expression, currently only tested
256
    with polynomials with floating-point coefficients.
257
    Notice that, in the returned polynomial, the exponents are RealNumbers.
258
    """
259
    #pobyso_autoprint(sollyaExp)
260
    operator = pobyso_get_head_function(sollyaExp)
261
    # Constants and the free variable are special cases.
262
    # All other operator are dealt with in the same way.
263
    if (operator != SOLLYA_BASE_FUNC_CONSTANT) and \
264
       (operator != SOLLYA_BASE_FUNC_FREE_VARIABLE):
265
        (arity, subexpressions) = pobyso_get_subfunctions(sollyaExp)
266
        if arity == 1:
267
            sageExp = eval(pobyso_function_type_as_string(operator) + \
268
            "(" + pobyso_get_sage_exp_from_sollya_exp(subexpressions[0], realField)\
269
             + ")")
270
        elif arity == 2:
271
            if operator == SOLLYA_BASE_FUNC_POW:
272
                operatorAsString = "**"
273
            else:
274
                operatorAsString = pobyso_function_type_as_string(operator)
275
            sageExp = \
276
              eval("pobyso_get_sage_exp_from_sollya_exp(subexpressions[0], realField)"\
277
              + " " + operatorAsString + " " + \
278
                   "pobyso_get_sage_exp_from_sollya_exp(subexpressions[1], realField)")
279
        # We do not know yet how to deal with arity > 3 (is there any in Sollya anyway?).
280
        else:
281
            sageExp = eval('None')
282
        return(sageExp)
283
    elif operator == SOLLYA_BASE_FUNC_CONSTANT:
284
        #print "This is a constant"
285
        return pobyso_get_constant_as_rn_with_rf(sollyaExp, realField)
286
    elif operator == SOLLYA_BASE_FUNC_FREE_VARIABLE:
287
        #print "This is free variable"
288
        return(eval(sollya_lib_get_free_variable_name()))
289
    else:
290
        print "Unexpected"
291
        return eval('None')
292
# End pobyso_get_sage_poly_from_sollya_poly
293
    
294
def pobyso_get_subfunctions(expression):
295
    subf0 = c_int(0)
296
    subf1 = c_int(0)
297
    subf2 = c_int(0)
298
    subf3 = c_int(0)
299
    subf4 = c_int(0)
300
    subf5 = c_int(0)
301
    subf6 = c_int(0)
302
    subf7 = c_int(0)
303
    subf8 = c_int(0)
304
    arity = c_int(0)
305
    nullPtr = POINTER(c_int)()
306
    sollya_lib_get_subfunctions(expression, byref(arity), \
307
    byref(subf0), byref(subf1), byref(subf2), byref(subf3), byref(subf4), byref(subf5),\
308
     byref(subf6), byref(subf7), byref(subf8), nullPtr, None) 
309
#    byref(cast(subfunctions[0], POINTER(c_int))), byref(cast(subfunctions[0], POINTER(c_int))), \
310
#    byref(cast(subfunctions[2], POINTER(c_int))), byref(cast(subfunctions[3], POINTER(c_int))), \
311
#    byref(cast(subfunctions[4], POINTER(c_int))), byref(cast(subfunctions[5], POINTER(c_int))), \
312
#    byref(cast(subfunctions[6], POINTER(c_int))), byref(cast(subfunctions[7], POINTER(c_int))), \
313
#    byref(cast(subfunctions[8], POINTER(c_int))), nullPtr)
314
    subfunctions = [subf0, subf1, subf2, subf3, subf4, subf5, subf6, subf7, subf8]
315
    subs = []
316
    if arity.value > pobyso_max_arity:
317
        return(None,None)
318
    for i in xrange(arity.value):
319
        subs.append(int(subfunctions[i].value))
320
        #print subs[i]
321
    return(int(arity.value), subs)
322
    
323
def pobyso_get_prec():
324
    retc = sollya_lib_get_prec(None)
325
    a = c_int(0)
326
    sollya_lib_get_constant_as_int(byref(a), retc)
327
    return(int(a.value))
328

  
329
def pobyso_get_prec_of_constant(ctExp):
330
    prec = c_int(0)
331
    retc = sollya_lib_get_prec_of_constant(byref(prec), ctExp, None)
332
    return(int(prec.value))
333

  
334
def pobyso_parse_string(string):
335
    return(sollya_lib_parse_string(string))
336

  
337
def pobyso_univar_polynomial_print_reverse(polySa):
338
    """
339
    Return the string representation of a univariate polynomial with
340
    monomial ordered in the x^0..x^n order of the monomials.
341
    Remember: Sage
342
    """
343
    polynomialRing = polySa.base_ring()
344
    # A very expensive solution:
345
    # -create a fake multivariate polynomial field with only one variable,
346
    #   specifying a negative lexicographical order;
347
    mpolynomialRing = PolynomialRing(polynomialRing.base(), \
348
                                     polynomialRing.variable_name(), \
349
                                     1, order='neglex')
350
    # - convert the univariate argument polynomial into a multivariate
351
    #   version;
352
    p = mpolynomialRing(polySa)
353
    # - return the string representation of the converted form.
354
    # There is no simple str() method defined for p's class.
355
    return(p.__str__())
356
    
357
def pobyso_range(rnLowerBound, rnUpperBound):
358
    lowerBoundSo = sollya_lib_constant(get_rn_value(rnLowerBound))
359
    upperBoundSo = sollya_lib_constant(get_rn_value(rnUpperBound))
360
    rangeSo = sollya_lib_range(lowerBoundSo, upperBoundSo)
361
    return(rangeSo)
362

  
363
def pobyso_remez_canonical(function, \
364
                           degree, \
365
                           lowerBound, \
366
                           upperBound, \
367
                           weightSo = pobyso_constant_1(),
368
                           quality = None):
369
    if parent(function) == parent("string"):
370
        functionSo = sollya_lib_parse_string(function)
371
    #    print "Is string!"
372
    elif sollya_lib_obj_is_function(function):
373
        functionSo = function
374
    #    print "Is Function!"
375
    degreeSo = pobyso_constant_from_int(degree)
376
    rangeSo = pobyso_range(lowerBound, upperBound)
377
    return(sollya_lib_remez(functionSo, degreeSo, rangeSo, quality, None))
378
    
379
def pobyso_set_canonical_off():
380
    sollya_lib_set_canonical(sollya_lib_off())
381

  
382
def pobyso_set_canonical_on():
383
    sollya_lib_set_canonical(sollya_lib_on())
384

  
385
def pobyso_set_prec(p):
386
    a = c_int(p)
387
    precSo = c_void_p(sollya_lib_constant_from_int(a))
388
    sollya_lib_set_prec(precSo)
389

  
390
def pobyso_taylor(function, degree, point):
391
    return(sollya_lib_taylor(function, degree, point))
392
    
393
def pobyso_taylorform(function, degree, point = None, interval = None, errorType=None):
394
    if errorType is None:
395
        errorType = sollya_lib_absolute()
396
    return(sollya_lib_taylorform(function, degree, point, errorType, None))
397
#
398
print "Superficial test of pobyso:"    
399
print pobyso_get_prec()  
400
pobyso_set_prec(165)
401
print pobyso_get_prec()  
402
a=100
403
print type(a)
404
id(a)
405
print "Max arity: ", pobyso_max_arity
406
print "Function tripleDouble (43) as a string: ", pobyso_function_type_as_string(43)
407
print "Function None (44) as a string: ", pobyso_function_type_as_string(44)
pobysoPythonSage/sageMpfr.spyx (revision 5)
1
from sage.rings.real_mpfr cimport *
2
#
3
# Two functions to manipulate the real RealNumber values.
4
#
5
# Get the address of the value of a RealNumber. This address can be
6
# used to to set the value as well.
7
#
8
cpdef int get_rn_value(RealNumber x):
9
    return(<int>&(x.value))
10
#
11
# We use the "array trick" to workaround the pointer dereferencing
12
# issue (in Cython there is no unary operator "*").
13
#
14
cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN):
15
    mpfr_set(x.value, (<mpfr_t *>p)[0], rnd)
16
#
17
# Do not use this function!
18
# It changes the precision of the instance behind the curtain, but precision is
19
# class attribute. If the precision is increased, memory space is wasted and
20
# other problems may surface in subsequent operations.
21
# If the precision is decreased the semantics
22
# are broken since the expected precision (from the class attribute) is not
23
# the actual precision.
24
#
25
cpdef s_rn_value(RealNumber x, int p):
26
    mpfr_clear(x.value)
27
    mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0]))
28
    #x.__prec = mpfr_get_prec(x.value)
29
    printf("x.value prec: %d\n", mpfr_get_prec(x.value))
30
    mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN)
31
#
32
# Compare two RN with the mpfr_cmp function
33
cpdef cmp_rn_value(RealNumber x, RealNumber y):
34
    return(mpfr_cmp(x.value, y.value))
pobysoPythonSage/sollya_lib.sage (revision 5)
1
import sys
2
from ctypes import *
3
from sage.rings.real_mpfr import *
4
try:
5
    sollya=CDLL("/warehouse/storres/root/lib/libsollya.so")
6
except :
7
    print "\nCould not find nor load the Sollya library.\n"
8
    sys.exit(1)
9

  
10
sollya
11
sollya.sollya_lib_init(None)
12
try:
13
    # Export the functions with their name in the library (so we can
14
    # use them without the "sollya." prefix).
15
        sollya_lib_absolute = sollya.sollya_lib_absolute
16
        sollya_lib_autoprint = sollya.sollya_lib_autoprint
17
        sollya_lib_build_function_exp = sollya.sollya_lib_build_function_exp
18
        sollya_lib_clear_obj = sollya.sollya_lib_clear_obj
19
        sollya_lib_clear_object_list = sollya.sollya_lib_clear_object_list
20
        sollya_lib_close = sollya.sollya_lib_close
21
        sollya_lib_constant = sollya.sollya_lib_constant
22
        sollya_lib_constant_from_int = sollya.sollya_lib_constant_from_int
23
        sollya_lib_constant_from_int64 = sollya.sollya_lib_constant_from_int64
24
        sollya_lib_constant_from_uint64 = \
25
            sollya.sollya_lib_constant_from_uint64
26
        sollya_lib_cos = sollya.sollya_lib_cos
27
        sollya_lib_evaluate = sollya.sollya_lib_evaluate
28
        sollya_lib_get_canonical = sollya.sollya_lib_get_canonical
29
        sollya_lib_get_constant = sollya.sollya_lib_get_constant
30
        sollya_lib_get_constant_as_int = \
31
            sollya.sollya_lib_get_constant_as_int
32
        sollya_lib_get_constant_as_uint64 = \
33
            sollya.sollya_lib_get_constant_as_uint64
34
        sollya_lib_get_free_variable_name = \
35
        sollya.sollya_lib_get_free_variable_name
36
        sollya_lib_get_function_arity = sollya.sollya_lib_get_function_arity
37
        sollya_lib_get_head_function = sollya.sollya_lib_get_head_function
38
        sollya_lib_get_list_elements = sollya.sollya_lib_get_list_elements
39
        sollya_lib_get_object_list_head = \
40
            sollya.sollya_lib_get_object_list_head
41
        sollya_lib_get_object_list_tail = \
42
            sollya.sollya_lib_get_object_list_tail
43
        sollya_lib_get_prec = sollya.sollya_lib_get_prec
44
        sollya_lib_get_prec_of_constant = sollya.sollya_lib_get_prec_of_constant
45
        sollya_lib_get_subfunctions = sollya.sollya_lib_get_subfunctions
46
        sollya_lib_head = sollya.sollya_lib_head
47
        sollya_lib_is_absolute = sollya.sollya_lib_is_absolute
48
        sollya_lib_obj_is_function = sollya.sollya_lib_obj_is_function
49
        sollya_lib_obj_is_list = sollya.sollya_lib_obj_is_list
50
        sollya_lib_obj_is_range = sollya.sollya_lib_obj_is_range
51
        sollya_lib_obj_is_structure = sollya.sollya_lib_obj_is_structure
52
        sollya_lib_off = sollya.sollya_lib_off
53
        sollya_lib_on = sollya.sollya_lib_on
54
        sollya_lib_parse_string = sollya.sollya_lib_parse_string
55
        sollya_lib_range = sollya.sollya_lib_range
56
        sollya_lib_relative = sollya.sollya_lib_relative
57
        sollya_lib_remez = sollya.sollya_lib_remez
58
        sollya_lib_set_canonical = sollya.sollya_lib_set_canonical
59
        sollya_lib_set_prec = sollya.sollya_lib_set_prec
60
        sollya_lib_taylor = sollya.sollya_lib_taylor
61
        sollya_lib_taylorform = sollya.sollya_lib_taylorform
62

  
63
except :
64
    print "\nCould not rename one of the functions.\n"
65
    sys.exit(1)
66

  
67
# Set the return type of several functions (those that have a return
68
# type different from c_int or void). We consider the all the functions
69
# returning a sollya_obj_t as returning an int.
70
try:
71
    sollya_lib_get_free_variable_name.restype = c_char_p
72
except :
73
    print "\nOne of the Python-Sollya return type setting command has \
74
          failed.\n"
75
    sys.exit(1)
76
#
77
# Set the argument type of several functions.
78
#
79
try:
80
    sollya_lib_get_constant_as_int.argtypes  = [POINTER(c_int), c_int]
81
    sollya_lib_get_function_arity.argtypes   = [POINTER(c_int), c_int]
82
    sollya_lib_get_head_function.argtypes    = [POINTER(c_int), c_int]
83
    sollya_lib_get_list_elements.argtypes    = [POINTER(POINTER(c_longlong)), POINTER(c_int),\
84
                                                POINTER(c_int), c_int]
85
    sollya_lib_get_prec_of_constant.argtypes = [POINTER(c_int), c_int]
86
    sollya_lib_get_subfunctions.argtypes     = [c_int, POINTER(c_int), \
87
    POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int), \
88
    POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int)]
89
except :
90
    print "\nOne of the Python-Sollya arguement type setting command has \
91
          failed.\n"
92
    sys.exit(1)
93
#
94
# Give it a try!
95
#
96
print "Sollya library tests"
97
print sollya.sollya_lib_get_free_variable_name()
98
sollyaExp = sollya_lib_parse_string("exp(x)")
99
retc = sollya_lib_autoprint(sollyaExp, None)
100
arg = sollya_lib_constant_from_int(int(1))
101
print "Type of sollya_obj_t: ",type(arg), "Value: ", arg
102
res = sollya_lib_evaluate(sollyaExp, arg)
103
retc = sollya_lib_autoprint(res, None)
104
retc = sollya_lib_get_prec(None)
105
a = c_int(0)
106
sollya_lib_get_constant_as_int(byref(a), retc)
107
print "Precision : ", a.value
108
print "Address of a.value ", addressof(a)
109
#b = RealNumber()
110
#print cast(retc, POINTER(c_int)).contents
111
#retc = cast(retc, POINTER(c_int)).contents
112
#arg = sollya_lib_constant_from_int(int(retc))
113
#retc = sollya_lib_autoprint(arg, None)
114
#sollya_lib_set_prec(c_int(100))

Formats disponibles : Unified diff