On 2013-09-27, Volker Braun <vbraun.n...@gmail.com> wrote:
> The simpler C++ stuff is now well supported in Cython. Esoteric features 
> will always cause troubles, but just using the std library is easy. Have a 
> look at sage/tests/stl_vector.pyx for an example.

there is the line
       mpz_add_ui(accumulator.value, accumulator.value, <int>(i[0]))
in the loop:
 cdef Integer accumulator = Integer(0)
 cdef vector[int].iterator i = self.data.begin()
 while i != self.data.end():
     mpz_add_ui(accumulator.value, accumulator.value, <int>(i[0]))
     i += 1

which is not clear to me:
1) why can't one just use '+' instead? Is it a cython limitation?
2) why i[0] ?

Dima
>
>
>
>
>
> On Friday, September 27, 2013 5:00:07 PM UTC+1, mmarco wrote:
>>
>> I thought that C was easyer to interface from cython than C++
>>
>> Anyways,  the solution of passing the length of the array and then copying 
>> the values one by one doesn't sound bad to me.
>>
>> El jueves, 26 de septiembre de 2013 15:28:37 UTC+2, Volker Braun escribió:
>>>
>>> Numpy only does machine datatypes and python objects. You can do a numpy 
>>> array of python wrappers of mpfr reals but that would not be a good 
>>> solution here.
>>>
>>> If the library is under your own control, I would recommend to use C++ 
>>> instead of C and then design the interface around std::vector<double>. This 
>>> is easier than C arrays, and more easily wrapped in Cython as well.
>>>
>>>
>>> On Thursday, September 26, 2013 2:14:29 PM UTC+1, Dima Pasechnik wrote:
>>>>
>>>> On 2013-09-25, Volker Braun <vbrau...@gmail.com> wrote: 
>>>> > 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. 
>>>>
>>>> I read somewhere that it is convenient to use numpy arrays in such 
>>>> a situation (they are stored intrenally as C arrays, and there are extra 
>>>> Python hooks kept by numpy). 
>>>> Not sure I can reproduce details here. 
>>>>
>>>> > 
>>>> > 
>>>> > 
>>>> > 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.

Reply via email to