Ask Your Question
0

static typing of mpf variables in cython

asked 11 years ago

anonymous user

Anonymous

Hello,

I am a new user of cython. I was looking for the static typing of python's mpf type variables in cython for speed up.

For example:

python: N=10 static typing in cython: cdef int N=10

Now I'm looking for the equivalent for mpf variables:

python: from mpmath import mpf , N = mpf(10)

cython: ??? maybe sth like: 'cdef mpf_t N=10' but this doesn't work.

Thank you,

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 11 years ago

vdelecroix gravatar image

updated 11 years ago

Hi,

It depends. You have the choice of using a Cython variable as follows

from sage.rings.real_mpfr cimport RealNumber
from sage.rings.real_mpfr import RR

cdef RealNumber my_number = RR(5.634)
...

Or you can use pure C as follows

cdef mpfr_t value
mpfr_init2(value, 53)
mpfr_set_flt(value, 5.364, MPFR_RNDN)
...

Anyway, in the latter case, it is not possible to write value = 5 because there is no cast from int to mpfr_t (those are C variables)!

It might help to have a look at how the real number are implemented in Sage in the files

  • $SAGE_ROOT/devel/sage-main/sage/rings/real_mpfr.pyx
  • $SAGE_ROOT/devel/sage-main/sage/rings/real_mpfr.pxd
Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 10,208 times

Last updated: May 16 '13