A pointer to a dynamically malloc'ed array doesn't know how long the array is. Your C library function must returns its size, too. You can then read it out one-by-one but you can't call list(c_array) since the latter doesn't even know how long it is.
On Wednesday, September 25, 2013 5:02:55 PM UTC+1, mmarco wrote: > > I see, thanks. > > So, if i understand it correctly, i import my_c_function and then, to call > it, i create the memory space for the array, copy the data into it and pass > the array to the function. > > I guess the result will be another c array that i can access from python > in a transparent way, right? > > I mean, if i write: > > res=my_c_function(c_values) > > Then i can just use > > list(res) > > to get a list of floats? > > > El miércoles, 25 de septiembre de 2013 13:22:51 UTC+2, Volker Braun > escribió: >> >> Definitely use Cython. >> >> For array of doubles, say, you just need a sage/libs/my_library.pyx with >> >> include "stdsage.pxi" >> >> cdef extern from "my_library.h" >> my_c_function(double*) >> >> def my_python_function(values): >> cdef double * c_values = <double*> >> sage_malloc(sizeof(double)*len(values)) >> for i,v in enumerate(values): >> c_values[i] = values[i] >> my_c_function(c_values) >> >> >> >> >> >> >> On Wednesday, September 25, 2013 10:08:24 AM UTC+1, mmarco wrote: >>> >>> We are working on a c library to do homotoy continuation of polynomial >>> roots using interval arithmetic. Our idea is to make a spkg with it, and >>> write some functions in the sage library that would use it (in particular, >>> to compute the fundamental group of the complement of an algebraic curve). >>> so i have a question: >>> >>> how should we pass the data to the library, and retrieve it back? Both >>> the input and output can be seen as an array of mpfr reals (or, depending >>> on the version, floats or doubles). The length of the arrays is not known a >>> priori. >>> >>> Which should be the best way to go? Write our interface in cython? or >>> use ctypes? And in any case, is there some easy tutorial that we could >>> follow? >>> >>> Thanks in advance. >>> >> -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/groups/opt_out.