Anyway, you should also return the size of the allocated block. It might
be more standard to use
slong my_function(mpfr_ptr * answer, mpfr_ptr coeffs, slong length)
where the output would be the size of the allocated block at "*answer".
Don't you have a reasonable bound on the size of the result? Having
function managing memory allocation is often much slower. For example,
you might want to reuse the same allocated array for several calls of
"my_function". If this is the case, the following is cleaner
slong my_function(mpfr_ptr answer, mpfr_ptr coeffs, slong length)
Vincent
Le 05/02/2017 à 15:13, mmarco a écrit :
I see.
One problem here is that I don't know in advance the size of the returned
list, so the allocation should happen in the c++ library side. So I am
guessing the way around it is to return a pointer to the mpfr_ptr. Right?
El domingo, 5 de febrero de 2017, 11:43:54 (UTC+1), vdelecroix escribió:
If you want to be complient with GMP/MPFR/FLINT you should change the
signature of
mpfr_t* my_function (mpfr_t *_coef)
to
void my_function(mfpr_ptr result, mpfr_ptr input, slong len)
And such function should not do any allocation at all.
Le 05/02/2017 à 11:40, Vincent Delecroix a écrit :
If you had a look at the code I provided you can check that what you did
is very wrong.
mpfr_t is already an array. The type "mpfr_t * x" make no sense at all.
Le 05/02/2017 à 11:38, mmarco a écrit :
I am afraid I didn't explain my problem well enough.
My problem is not to write c++ code with mpfr. That is solved
It is also not to call c++ libraries from cython code that involves
plain
mpfr numbers. That is also solved.
My problem is to do so with functions that involve *arrays *(or lists,
or
vectors) of mpfr numbers.
In particular, assume that i have writen a c++ library that provides a
function my_function that takes an array of mpfr numbers and returns
another array of mpfr numbers. So I try to write some cython code to
interface in from sage:
#clang C++
#clib my_library
include 'cysignals/signals.pxi'
from sage.libs.mpfr cimport *
from sage.rings.real_mpfr cimport RealNumber
from sage.rings.real_mpfr import RealField
cdef extern from "my_library.h":
mpfr_t* my_function (mpfr_t *_coef)
def my_cython_function(values):
cdef mpfr_t* cvalues = <mpfr_t*>
sage_malloc(sizeof(mpfr_t)*len(values))
cdef mpfr_t tempnum
cdef mpfr_t* rop
for i,v in enumerate(values):
tempnum = values[i].value
mpfr_init(cvalues[i])
mpfr_set(cvalues[i], tempnum, MPFR_RNDN)
sig_on
rop = my_function(cvalues)
sig_off
This code is suppopsed to just create an array of mpfr objects to be
passed
to my_function, and another array to get the result of that function.
When
I try to compile it I get the following error:
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call
last)
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/
interactiveshell.pyc in run_code(self, code_obj, result)
2896 if result is not None:
2897 result.error_in_exec = sys.exc_info()[1]
-> 2898 self.showtraceback()
2899 else:
2900 outflag = 0
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/
interactiveshell.pyc in showtraceback(self, exc_tuple, filename,
tb_offset,
exception_only)
1822 except Exception:
1823 stb = self.InteractiveTB.
structured_traceback(etype,
-> 1824 value, tb,
tb_offset=
tb_offset)
1825
1826 self._showtraceback(etype, value, stb)
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/ultratb.pyc
in structured_traceback(self, etype, value, tb, tb_offset,
number_of_lines_of_context)
1404 self.tb = tb
1405 return FormattedTB.structured_traceback(
-> 1406 self, etype, value, tb, tb_offset,
number_of_lines_of_context)
1407
1408
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/ultratb.pyc
in structured_traceback(self, etype, value, tb, tb_offset,
number_of_lines_of_context)
1312 # Verbose modes need a full traceback
1313 return VerboseTB.structured_traceback(
-> 1314 self, etype, value, tb, tb_offset,
number_of_lines_of_context
1315 )
1316 else:
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/ultratb.pyc
in structured_traceback(self, etype, evalue, etb, tb_offset,
number_of_lines_of_context)
1162
1163 formatted_exception =
self.format_exception_as_a_whole(etype
, evalue, etb, number_of_lines_of_context,
-> 1164
tb_offset)
1165
1166 colors = self.Colors # just a shorthand + quicker name
lookup
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/ultratb.pyc
in format_exception_as_a_whole(self, etype, evalue, etb,
number_of_lines_of_context, tb_offset)
1111 return ""
1112
-> 1113 last_unique, recursion_repeat =
find_recursion(orig_etype,
evalue, records)
1114
1115 frames = self.format_records(records, last_unique,
recursion_repeat)
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/ultratb.pyc
in find_recursion(etype, value, records)
453 # quarter of the traceback (250 frames by default) is
repeats,
and find the
454 # first frame (from in to out) that looks different.
--> 455 if not is_recursion_error(etype, value, records):
456 return len(records), 0
457
/home/mmarco/sage/local/lib/python2.7/site-packages/IPython/core/ultratb.pyc
in is_recursion_error(etype, value, records)
439 # a recursion error.
440 return (etype is recursion_error_type) \
--> 441 and "recursion" in str(value).lower() \
442 and len(records) > 500
443
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
1158:
ordinal not in range(128)
I tried to circunvent this problem by using c++ vectors instead of
malloc'ed arrays, but then it is the c++ compilet which complains abput
creating vectors of mpfr_t objects.
Thoughts?
--
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.