Dear Marco,

Solution one

http://www.holoborodko.com/pavel/mpfr/

Second solution

see attachment

Vincent

Le 05/02/2017 à 02:20, mmarco a écrit :
I am working in a C++ library that provides some certified homotopy
continuations. It provides a function that, takes as input a list of
floating point numbres and returns another such list.

I am having a lot of trouble trying to write a sage-cython interface for
mpfr floats (the version for doubles works ok).

I have tried to use vectors, lists and malloc'ed arrays, but none of them
seems to work. mpfr_t' are so tricky.

Whis is the right way to write such a cython interface?


--
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 https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.
from libc.stdlib cimport malloc, free
from sage.libs.mpfr cimport *
from sage.rings.real_mpfr cimport RealNumber, RealField_class, RealField

cdef class RealVector(object):
    cdef RealField_class _real_field
    cdef size_t _length
    cdef mpfr_ptr _data

    def __cinit__(self, data, length, prec):
        cdef int i
        self._data = <mpfr_ptr> malloc(length * sizeof(__mpfr_struct))
        self._length = length
        for i in range(self._length):
            mpfr_init2(self._data + i, prec)

    def __dealloc__(self):
        cdef int i
        for i in range(self._length):
            mpfr_clear(self._data + i)
        free(self._data)
    
    def __init__(self, data, length, prec):
        self._real_field = RealField(prec)

        cdef int i
        if data:
            for i in range(self._length):
                self[i] = data[i]

    def __repr__(self):
        cdef int i
        return "[" + ", ".join(self[i].str() for i in range(self._length)) + "]"

    def __len__(self):
        return self._length

    def __setitem__(self, Py_ssize_t i, RealNumber x):
        if not 0 <= i < self._length:
            raise IndexError
        mpfr_set(self._data + i, x.value, self._real_field.rnd)

    def __getitem__(self, Py_ssize_t i):
        if not 0 <= i < self._length:
            raise IndexError
        cdef RealNumber x = self._real_field._new()
        mpfr_set(x.value, self._data + i, self._real_field.rnd)
        return x

Reply via email to