Statistiques
| Révision :

root / pobysoPythonSage / src / sageMpfr.spyx @ 80

Historique | Voir | Annoter | Télécharger (3,65 ko)

1 5 storres
from sage.rings.real_mpfr cimport *
2 53 storres
from sage.rings.real_mpfi cimport *
3 5 storres
#
4 51 storres
# Functions to manipulate the real RealNumber values.
5 5 storres
#
6 51 storres
# Import constants and declarations that are not exported
7 51 storres
# from the Sage real_mpfr.pyx file.
8 51 storres
# Notes:
9 51 storres
# - notice how #define constants are imported.
10 52 storres
# - mpfr_min_prec is declared with an unsigned int return value, not
11 51 storres
#   a mpfr_prec_t since it is not recognized as type in Cython.
12 51 storres
cdef extern from "mpfr.h":
13 51 storres
    cdef int MPFR_PREC_MIN "MPFR_PREC_MIN"
14 52 storres
    cdef unsigned int mpfr_min_prec(mpfr_t x)
15 51 storres
16 53 storres
cpdef int get_interval_value(RealIntervalFieldElement x):
17 53 storres
    """
18 53 storres
    Get the address of the value of a RealIntervalField Element.
19 53 storres
    This address can be used to to set the value as well.
20 53 storres
    """
21 53 storres
    return(<int>&(x.value))
22 53 storres
23 5 storres
cpdef int get_rn_value(RealNumber x):
24 51 storres
    """
25 51 storres
    Get the address of the value of a RealNumber. This address can be
26 51 storres
    used to to set the value as well.
27 51 storres
    """
28 5 storres
    return(<int>&(x.value))
29 53 storres
30 53 storres
cpdef set_interval_value(RealIntervalFieldElement x, int p):
31 53 storres
    """
32 53 storres
    Set the value of x from MPFI interval p.
33 53 storres
    """
34 53 storres
    # We use the "array trick" to workaround the pointer dereferencing
35 53 storres
    # issue (in Cython there is no unary operator "*").
36 53 storres
    #
37 53 storres
    mpfi_set(x.value, (<mpfi_t *>p)[0])
38 51 storres
39 5 storres
cpdef set_rn_value(RealNumber x, int p, mp_rnd_t rnd=GMP_RNDN):
40 51 storres
    """
41 51 storres
    Set the value of x from MPFR number p.
42 51 storres
    """
43 51 storres
    # We use the "array trick" to workaround the pointer dereferencing
44 51 storres
    # issue (in Cython there is no unary operator "*").
45 51 storres
    #
46 5 storres
    mpfr_set(x.value, (<mpfr_t *>p)[0], rnd)
47 5 storres
#
48 51 storres
cpdef min_rn_size(RealNumber x):
49 51 storres
    """
50 51 storres
    Compute the minimum number of bits necessary to represent
51 52 storres
    the x RealNumber's value.
52 51 storres
    If x is a power of 2 (only 1 bit needed) return the MPFR
53 51 storres
    MPFR_PREC_MIN value.
54 51 storres
    """
55 51 storres
    cdef int min_prec = mpfr_min_prec(x.value)
56 51 storres
    if min_prec < MPFR_PREC_MIN:
57 51 storres
        return(MPFR_PREC_MIN)
58 51 storres
    else:
59 51 storres
        return(min_prec)
60 51 storres
61 52 storres
cpdef min_mpfr_size(int p):
62 52 storres
    """
63 52 storres
    Compute the minimum number of bits necessary to represent
64 52 storres
    the MPFR value pointed by p.
65 52 storres
    If x is a power of 2 (only 1 bit needed) return the MPFR
66 52 storres
    MPFR_PREC_MIN value.
67 52 storres
    """
68 52 storres
    cdef int min_prec = mpfr_min_prec((<mpfr_t *>p)[0])
69 52 storres
    if min_prec < MPFR_PREC_MIN:
70 52 storres
        return(MPFR_PREC_MIN)
71 52 storres
    else:
72 52 storres
        return(min_prec)
73 52 storres
74 53 storres
cpdef free_mpfi_num(int mpfiNum):
75 53 storres
    mpfi_clear((<mpfi_t*>mpfiNum)[0])
76 53 storres
77 53 storres
cpdef free_mpfr_num(int mpfrNum):
78 53 storres
    mpfr_clear((<mpfr_t*>mpfrNum)[0])
79 53 storres
80 53 storres
cpdef new_interval_num(unsigned int prec):
81 53 storres
    cdef mpfi_t *newValue = NULL
82 53 storres
    print "Precision : ", prec
83 53 storres
    newValue = <mpfi_t*>malloc(sizeof(mpfi_t))
84 53 storres
    mpfi_init2(newValue[0], prec)
85 53 storres
    print "Creation done!"
86 53 storres
    return(<int>newValue)
87 53 storres
88 52 storres
cpdef new_mpfr_num(unsigned int prec):
89 52 storres
    cdef mpfr_t *newValue = NULL
90 52 storres
    print "Precision : ", prec
91 52 storres
    newValue = <mpfr_t*>malloc(sizeof(mpfr_t))
92 52 storres
    mpfr_init2(newValue[0], prec)
93 52 storres
    print "Creation done!"
94 52 storres
    return(<int>newValue)
95 52 storres
96 52 storres
#
97 52 storres
98 52 storres
cpdef cmp_rn_value(RealNumber x, RealNumber y):
99 52 storres
    """
100 52 storres
    Compare two RN with the mpfr_cmp function
101 52 storres
    """
102 52 storres
    return(mpfr_cmp(x.value, y.value))
103 52 storres
104 5 storres
# Do not use this function!
105 5 storres
# It changes the precision of the instance behind the curtain, but precision is
106 5 storres
# class attribute. If the precision is increased, memory space is wasted and
107 5 storres
# other problems may surface in subsequent operations.
108 5 storres
# If the precision is decreased the semantics
109 5 storres
# are broken since the expected precision (from the class attribute) is not
110 5 storres
# the actual precision.
111 5 storres
#
112 5 storres
cpdef s_rn_value(RealNumber x, int p):
113 5 storres
    mpfr_clear(x.value)
114 5 storres
    mpfr_init2(x.value, mpfr_get_prec((<mpfr_t *>p)[0]))
115 5 storres
    #x.__prec = mpfr_get_prec(x.value)
116 5 storres
    printf("x.value prec: %d\n", mpfr_get_prec(x.value))
117 5 storres
    mpfr_set(x.value, (<mpfr_t *>p)[0], GMP_RNDN)