Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/mpmath/ext_libmp.pyx
4045 views
1
"""
2
Faster versions of some key functions in mpmath.libmp.
3
"""
4
5
include "../../ext/stdsage.pxi"
6
from ext_impl cimport *
7
from sage.libs.gmp.all cimport *
8
from sage.rings.integer cimport Integer
9
10
from ext_impl import exp_fixed, cos_sin_fixed, log_int_fixed
11
12
# Note: not thread-safe
13
cdef MPF tmp1
14
cdef MPF tmp2
15
MPF_init(&tmp1)
16
MPF_init(&tmp2)
17
18
def mpf_add(tuple x, tuple y, int prec=0, str rnd='d'):
19
cdef MPopts opts
20
MPF_set_tuple(&tmp1, x)
21
MPF_set_tuple(&tmp2, y)
22
opts.rounding = rndmode_from_python(rnd)
23
opts.prec = prec
24
MPF_add(&tmp1, &tmp1, &tmp2, opts)
25
return MPF_to_tuple(&tmp1)
26
27
def mpf_sub(tuple x, tuple y, int prec=0, str rnd='d'):
28
cdef MPopts opts
29
MPF_set_tuple(&tmp1, x)
30
MPF_set_tuple(&tmp2, y)
31
opts.rounding = rndmode_from_python(rnd)
32
opts.prec = prec
33
MPF_sub(&tmp1, &tmp1, &tmp2, opts)
34
return MPF_to_tuple(&tmp1)
35
36
def mpf_mul(tuple x, tuple y, int prec=0, str rnd='d'):
37
cdef MPopts opts
38
MPF_set_tuple(&tmp1, x)
39
MPF_set_tuple(&tmp2, y)
40
opts.rounding = rndmode_from_python(rnd)
41
opts.prec = prec
42
MPF_mul(&tmp1, &tmp1, &tmp2, opts)
43
return MPF_to_tuple(&tmp1)
44
45
def mpf_div(tuple x, tuple y, int prec, str rnd='d'):
46
cdef MPopts opts
47
MPF_set_tuple(&tmp1, x)
48
MPF_set_tuple(&tmp2, y)
49
opts.rounding = rndmode_from_python(rnd)
50
opts.prec = prec
51
MPF_div(&tmp1, &tmp1, &tmp2, opts)
52
return MPF_to_tuple(&tmp1)
53
54
def mpf_sqrt(tuple x, int prec, str rnd='d'):
55
"""
56
Computes sqrt(x) with mpf value tuples.
57
58
EXAMPLES::
59
60
sage: from mpmath.libmp import mpf_sqrt, from_float, to_float
61
sage: x = from_float(2)
62
sage: y = mpf_sqrt(x, 53, 'n')
63
sage: print to_float(y)
64
1.41421356237
65
66
"""
67
if x[0]:
68
import mpmath.libmp as libmp
69
raise libmp.ComplexResult("square root of a negative number")
70
cdef MPopts opts
71
MPF_set_tuple(&tmp1, x)
72
opts.rounding = rndmode_from_python(rnd)
73
opts.prec = prec
74
MPF_sqrt(&tmp1, &tmp1, opts)
75
return MPF_to_tuple(&tmp1)
76
77
def mpf_log(tuple x, int prec, str rnd='d'):
78
"""
79
Computes log(x) with mpf value tuples.
80
81
EXAMPLES::
82
83
sage: from mpmath.libmp import mpf_log, from_float, to_float
84
sage: x = from_float(2)
85
sage: y = mpf_log(x, 53, 'n')
86
sage: print to_float(y)
87
0.69314718056
88
89
"""
90
if x[0]:
91
import mpmath.libmp as libmp
92
raise libmp.ComplexResult("logarithm of a negative number")
93
cdef MPopts opts
94
MPF_set_tuple(&tmp1, x)
95
opts.rounding = rndmode_from_python(rnd)
96
opts.prec = prec
97
MPF_log(&tmp1, &tmp1, opts)
98
return MPF_to_tuple(&tmp1)
99
100
def mpf_exp(tuple x, int prec, str rnd='d'):
101
"""
102
Computes exp(x) with mpf value tuples.
103
104
EXAMPLES::
105
106
sage: from mpmath.libmp import mpf_exp, from_float, to_float
107
sage: x = from_float(2)
108
sage: z = mpf_exp(x, 53, 'n')
109
sage: print to_float(z)
110
7.38905609893
111
112
"""
113
cdef MPopts opts
114
MPF_set_tuple(&tmp1, x)
115
opts.rounding = rndmode_from_python(rnd)
116
opts.prec = prec
117
MPF_exp(&tmp1, &tmp1, opts)
118
return MPF_to_tuple(&tmp1)
119
120
def mpf_cos(tuple x, int prec, str rnd='d'):
121
"""
122
Computes cos(x) with mpf value tuples.
123
124
EXAMPLES::
125
126
sage: from mpmath.libmp import mpf_cos, from_float, to_float
127
sage: x = from_float(1)
128
sage: y = mpf_cos(x, 53, 'n')
129
sage: print to_float(y)
130
0.540302305868
131
132
"""
133
cdef MPopts opts
134
MPF_set_tuple(&tmp1, x)
135
opts.rounding = rndmode_from_python(rnd)
136
opts.prec = prec
137
MPF_cos(&tmp1, &tmp1, opts)
138
return MPF_to_tuple(&tmp1)
139
140
def mpf_sin(tuple x, int prec, str rnd='d'):
141
"""
142
Computes sin(x) with mpf value tuples.
143
144
EXAMPLES::
145
146
sage: from mpmath.libmp import mpf_sin, from_float, to_float
147
sage: x = from_float(1)
148
sage: y = mpf_sin(x, 53, 'n')
149
sage: print to_float(y)
150
0.841470984808
151
152
"""
153
cdef MPopts opts
154
MPF_set_tuple(&tmp1, x)
155
opts.rounding = rndmode_from_python(rnd)
156
opts.prec = prec
157
MPF_sin(&tmp1, &tmp1, opts)
158
return MPF_to_tuple(&tmp1)
159
160
def mpc_sqrt(tuple z, int prec, str rnd='d'):
161
"""
162
Computes sqrt(z) with mpc value tuples.
163
164
EXAMPLES::
165
166
sage: from mpmath.libmp import mpc_sqrt, from_float, to_float
167
sage: z = from_float(-2), from_float(0)
168
sage: re, im = mpc_sqrt(z, 53, 'n')
169
sage: print to_float(re), to_float(im)
170
0.0 1.41421356237
171
172
"""
173
cdef tuple a, b
174
cdef MPopts opts
175
a, b = z
176
MPF_set_tuple(&tmp1, a)
177
MPF_set_tuple(&tmp2, b)
178
opts.rounding = rndmode_from_python(rnd)
179
opts.prec = prec
180
MPF_complex_sqrt(&tmp1, &tmp2, &tmp1, &tmp2, opts)
181
return MPF_to_tuple(&tmp1), MPF_to_tuple(&tmp2)
182
183
def mpc_exp(tuple z, int prec, str rnd='d'):
184
"""
185
Computes exp(z) with mpc value tuples.
186
187
EXAMPLES::
188
189
sage: from mpmath.libmp import mpc_exp, from_float, to_float
190
sage: z = from_float(0), from_float(1)
191
sage: re, im = mpc_exp(z, 53, 'n')
192
sage: print to_float(re), to_float(im)
193
0.540302305868 0.841470984808
194
195
"""
196
cdef tuple a, b
197
cdef MPopts opts
198
a, b = z
199
MPF_set_tuple(&tmp1, a)
200
MPF_set_tuple(&tmp2, b)
201
opts.rounding = rndmode_from_python(rnd)
202
opts.prec = prec
203
MPF_complex_exp(&tmp1, &tmp2, &tmp1, &tmp2, opts)
204
return MPF_to_tuple(&tmp1), MPF_to_tuple(&tmp2)
205
206
def mpf_pow(tuple x, tuple y, int prec, str rnd='d'):
207
"""
208
Computes x ^ y with mpf value tuples.
209
210
EXAMPLES::
211
212
sage: from mpmath.libmp import mpf_pow, from_float, to_float
213
sage: x = from_float(2)
214
sage: y = from_float(3)
215
sage: z = mpf_pow(x, y, 53, 'n')
216
sage: print to_float(z)
217
8.0
218
219
"""
220
cdef MPopts opts
221
MPF_set_tuple(&tmp1, x)
222
MPF_set_tuple(&tmp2, y)
223
opts.rounding = rndmode_from_python(rnd)
224
opts.prec = prec
225
if MPF_pow(&tmp1, &tmp1, &tmp2, opts):
226
import mpmath.libmp as libmp
227
raise libmp.ComplexResult("negative number raised to a fractional power")
228
return MPF_to_tuple(&tmp1)
229
230