On Wed, Jul 8, 2009 at 2:30 AM, Nathann Cohen<[email protected]> wrote:
>
> Hello !!!
>
> Would any of you know how to convert a Python list to a C array of
> double or int ?
>
> I saw on Google plenty of people had the same problem but was not able
> to find a clear answer... Thank you !!
>
Here is a complete example of how to do this in the Sage notebook:
{{{
%cython
def f(v):
cdef int* w = <int*> sage_malloc(len(v)*sizeof(int))
cdef unsigned long i, n
n = len(v)
for i in range(n):
w[i] = int(v[i])
# Now w is a C array of ints. Do with it what you will, e.g., sum
its entries
cdef int s=0
for i in range(n):
s += w[i]
# Do not forget to de-allocate it:
sage_free(w)
return s
///
}}}
{{{
f([1..100])
///
5050
}}}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---