On Mon, Aug 18, 2008 at 7:08 PM, Bill Page <[EMAIL PROTECTED]> wrote:
>
> On Mon, Aug 18, 2008 at 12:48 PM, Ondrej Certik wrote:
>>
>> On Mon, Aug 18, 2008 at 6:24 PM, Bill Page wrote:
>>>
>>> ... It is not so easy to call Python code from a C mainline
>>> program either, is it?
>>
>> Thanks to Cython, it is very easy to call my Python implementation
>> of something from pure C. And it's fast, for example:
>>
>> http://freehg.sympy.org/u/certik/csympy/file/991e40db913e/basic.c
>>
>> line 841:
>>
>>      841             import_csympy();
>>      842             coeff = multi(m, n, &len);
>>
>> multi is implemented here
>> http://freehg.sympy.org/u/certik/csympy/file/991e40db913e/csympy.pyx:
>>
>>      146 from multinomial import multinomial_coefficients
>>      147
>>      148
>>      149 cdef api sympyint *multi(int m, int n, int *list_len):
>>      150     r = multinomial_coefficients(m, n)
>>      151     cdef int len2 = len(r)
>>      152     list_len[0] = len2
>>      153     l = []
>>      154     for k, v in r.iteritems():
>>      155         l.extend(k)
>>      156         l.append(v)
>>      157     cdef sympyint *cl = <sympyint*> malloc(len(l)*sizeof(sympyint))
>>      158     for i in range(len(l)):
>>      159         try:
>>      160             cl[i] = l[i]
>>      161         except OverflowError:
>>      162             print "Overflow occured, using 'cl[i] = 2'."
>>      163             print l[i]
>>      164             cl[i] = 2
>>      165     return cl
>>
>> So you see it calls pure Python function and just converts the
>> dictionary it returns to a C array of integers.
>>
>
> Ok, thanks for the example. Yes I admit that using Cython this way is
> quite nice. It makes me think that maybe it would be interesting to
> write such Cython wrappers for calling larger parts of Sage from some
> external program (such as FriCAS) this way.

Yes, exactly. I always wanted to call Sage like a library.

> But these sort of conversions are exactly the kind of thing to which I
> was referring. In your example, the conversion is quite simple but it
> even looks a little clumsy in this case. My main point in these
> comments, is that when converting between two different high-level
> representations of some more complex mathematical object, e.g. a
> matrix of p-adic polynomials, etc. these conversions can potentially
> dominate the interface.

Agree. So what's the other option?

Ondrej

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to