ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 15 May 2013 21:04:07 -0500static typing of mpf variables in cythonhttps://ask.sagemath.org/question/10120/static-typing-of-mpf-variables-in-cython/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,
Wed, 15 May 2013 18:02:42 -0500https://ask.sagemath.org/question/10120/static-typing-of-mpf-variables-in-cython/Answer by vdelecroix for <p>Hello,</p>
<p>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.</p>
<p>For example:</p>
<p>python: N=10
static typing in cython: cdef int N=10</p>
<p>Now I'm looking for the equivalent for mpf variables:</p>
<p>python: from mpmath import mpf , N = mpf(10)</p>
<p>cython: ???
maybe sth like: 'cdef mpf_t N=10' but this doesn't work.</p>
<p>Thank you,</p>
https://ask.sagemath.org/question/10120/static-typing-of-mpf-variables-in-cython/?answer=14932#post-id-14932Hi,
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`Wed, 15 May 2013 21:04:07 -0500https://ask.sagemath.org/question/10120/static-typing-of-mpf-variables-in-cython/?answer=14932#post-id-14932