Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/libs/pari/gen_py.py
8817 views
1
from sage.rings.all import *
2
3
def pari(x):
4
"""
5
Return the pari object constructed from a Sage object.
6
7
The work is done by the __call__ method of the class PariInstance,
8
which in turn passes the work to any class which has its own
9
method _pari_().
10
11
EXAMPLES::
12
13
sage: pari([2,3,5])
14
[2, 3, 5]
15
sage: pari(Matrix(2,2,range(4)))
16
[0, 1; 2, 3]
17
sage: pari(x^2-3)
18
x^2 - 3
19
20
::
21
22
sage: a = pari(1); a, a.type()
23
(1, 't_INT')
24
sage: a = pari(1/2); a, a.type()
25
(1/2, 't_FRAC')
26
sage: a = pari(1/2); a, a.type()
27
(1/2, 't_FRAC')
28
29
Conversion from reals uses the real's own precision::
30
31
sage: a = pari(1.2); a, a.type(), a.precision()
32
(1.20000000000000, 't_REAL', 4) # 32-bit
33
(1.20000000000000, 't_REAL', 3) # 64-bit
34
35
Conversion from strings uses the current PARI real precision.
36
By default, this is 96 bits on 32-bit systems and 128 bits on
37
64-bit systems::
38
39
sage: a = pari('1.2'); a, a.type(), a.precision()
40
(1.20000000000000, 't_REAL', 5) # 32-bit
41
(1.20000000000000, 't_REAL', 4) # 64-bit
42
43
But we can change this precision::
44
45
sage: pari.set_real_precision(35) # precision in decimal digits
46
15
47
sage: a = pari('1.2'); a, a.type(), a.precision()
48
(1.2000000000000000000000000000000000, 't_REAL', 6) # 32-bit
49
(1.2000000000000000000000000000000000, 't_REAL', 4) # 64-bit
50
51
Set the precision to 15 digits for the remaining tests::
52
53
sage: pari.set_real_precision(15)
54
35
55
56
Conversion from matrices is supported, but not from vectors;
57
use lists or tuples instead::
58
59
sage: a = pari(matrix(2,3,[1,2,3,4,5,6])); a, a.type()
60
([1, 2, 3; 4, 5, 6], 't_MAT')
61
62
sage: v = vector([1.2,3.4,5.6])
63
sage: pari(v)
64
Traceback (most recent call last):
65
...
66
PariError: syntax error, unexpected ')', expecting )-> or ','
67
sage: b = pari(list(v)); b,b.type()
68
([1.20000000000000, 3.40000000000000, 5.60000000000000], 't_VEC')
69
sage: b = pari(tuple(v)); b, b.type()
70
([1.20000000000000, 3.40000000000000, 5.60000000000000], 't_VEC')
71
72
Some more exotic examples::
73
74
sage: K.<a> = NumberField(x^3 - 2)
75
sage: pari(K)
76
[y^3 - 2, [1, 1], -108, 1, [[1, 1.25992104989487, 1.58740105196820; 1, -0.629960524947437 - 1.09112363597172*I, -0.793700525984100 + 1.37472963699860*I], [1, 1.25992104989487, 1.58740105196820; 1, -1.72108416091916, 0.581029111014503; 1, 0.461163111024285, -2.16843016298270], [1, 1, 2; 1, -2, 1; 1, 0, -2], [3, 0, 0; 0, 0, 6; 0, 6, 0], [6, 0, 0; 0, 6, 0; 0, 0, 3], [2, 0, 0; 0, 0, 1; 0, 1, 0], [2, [0, 0, 2; 1, 0, 0; 0, 1, 0]]], [1.25992104989487, -0.629960524947437 - 1.09112363597172*I], [1, y, y^2], [1, 0, 0; 0, 1, 0; 0, 0, 1], [1, 0, 0, 0, 0, 2, 0, 2, 0; 0, 1, 0, 1, 0, 0, 0, 0, 2; 0, 0, 1, 0, 1, 0, 1, 0, 0]]
77
78
sage: E = EllipticCurve('37a1')
79
sage: pari(E)
80
[0, 0, 1, -1, 0, 0, -2, 1, -1, 48, -216, 37, 110592/37, [0.837565435283323, 0.269594436405445, -1.10715987168877]~, 2.99345864623196, -2.45138938198679*I, 0.942638555913623, 1.32703057887968*I, 7.33813274078958]
81
82
Conversion from basic Python types::
83
84
sage: pari(int(-5))
85
-5
86
sage: pari(long(2**150))
87
1427247692705959881058285969449495136382746624
88
sage: pari(float(pi))
89
3.14159265358979
90
sage: pari(complex(exp(pi*I/4)))
91
0.707106781186548 + 0.707106781186547*I
92
sage: pari(False)
93
0
94
sage: pari(True)
95
1
96
97
Some commands are just executed without returning a value::
98
99
sage: pari("dummy = 0; kill(dummy)")
100
sage: type(pari("dummy = 0; kill(dummy)"))
101
<type 'NoneType'>
102
"""
103
from sage.libs.pari.all import pari
104
return pari(x)
105
106
def python(z, locals=None):
107
"""
108
Return the closest Python/Sage equivalent of the given pari object.
109
110
INPUT:
111
112
- `z` -- pari object
113
114
- `locals` -- optional dictionary used in fallback cases that
115
involve sage_eval
116
117
The component parts of a t_COMPLEX may be t_INT, t_REAL, t_INTMOD,
118
t_FRAC, t_PADIC. The components need not have the same type
119
(e.g. if z=2+1.2*I then z.real() is t_INT while z.imag() is
120
t_REAL(). They are converted as follows:
121
122
t_INT: ZZ[i]
123
t_FRAC: QQ(i)
124
t_REAL: ComplexField(prec) for equivalent precision
125
t_INTMOD, t_PADIC: raise NotImplementedError
126
127
EXAMPLES::
128
129
sage: a = pari('(3+I)').python(); a
130
i + 3
131
sage: a.parent()
132
Maximal Order in Number Field in i with defining polynomial x^2 + 1
133
134
sage: a = pari('2^31-1').python(); a
135
2147483647
136
sage: a.parent()
137
Integer Ring
138
139
sage: a = pari('12/34').python(); a
140
6/17
141
sage: a.parent()
142
Rational Field
143
144
sage: a = pari('1.234').python(); a
145
1.23400000000000000
146
sage: a.parent()
147
Real Field with 64 bits of precision
148
149
sage: a = pari('(3+I)/2').python(); a
150
1/2*i + 3/2
151
sage: a.parent()
152
Number Field in i with defining polynomial x^2 + 1
153
154
Conversion of complex numbers: the next example is converting from
155
an element of the Symbolic Ring, which goes via the string
156
representation::
157
158
sage: I = SR(I)
159
sage: a = pari(1.0+2.0*I).python(); a
160
1.00000000000000000 + 2.00000000000000000*I
161
sage: type(a)
162
<type 'sage.rings.complex_number.ComplexNumber'>
163
sage: a.parent()
164
Complex Field with 64 bits of precision
165
166
For architecture-independent complex numbers, start from a
167
suitable ComplexField::
168
169
sage: z = pari(CC(1.0+2.0*I)); z
170
1.00000000000000 + 2.00000000000000*I
171
sage: a=z.python(); a
172
1.00000000000000000 + 2.00000000000000000*I
173
sage: a.parent()
174
Complex Field with 64 bits of precision
175
176
Vectors and matrices::
177
178
sage: a = pari('[1,2,3,4]')
179
sage: a
180
[1, 2, 3, 4]
181
sage: a.type()
182
't_VEC'
183
sage: b = a.python(); b
184
[1, 2, 3, 4]
185
sage: type(b)
186
<type 'list'>
187
188
sage: a = pari('[1,2;3,4]')
189
sage: a.type()
190
't_MAT'
191
sage: b = a.python(); b
192
[1 2]
193
[3 4]
194
sage: b.parent()
195
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
196
197
sage: a = pari('Vecsmall([1,2,3,4])')
198
sage: a.type()
199
't_VECSMALL'
200
sage: a.python()
201
[1, 2, 3, 4]
202
203
We use the locals dictionary::
204
205
sage: f = pari('(2/3)*x^3 + x - 5/7 + y')
206
sage: x,y=var('x,y')
207
sage: import sage.libs.pari.gen_py
208
sage: sage.libs.pari.gen_py.python(f, {'x':x, 'y':y})
209
2/3*x^3 + x + y - 5/7
210
sage: sage.libs.pari.gen_py.python(f)
211
Traceback (most recent call last):
212
...
213
NameError: name 'x' is not defined
214
215
Conversion of p-adics::
216
217
sage: K = Qp(11,5)
218
sage: x = K(11^-10 + 5*11^-7 + 11^-6); x
219
11^-10 + 5*11^-7 + 11^-6 + O(11^-5)
220
sage: y = pari(x); y
221
11^-10 + 5*11^-7 + 11^-6 + O(11^-5)
222
sage: y.sage()
223
11^-10 + 5*11^-7 + 11^-6 + O(11^-5)
224
sage: pari(K(11^-5)).sage()
225
11^-5 + O(11^0)
226
"""
227
from sage.libs.pari.pari_instance import prec_words_to_bits
228
229
t = z.type()
230
if t == "t_REAL":
231
return RealField(prec_words_to_bits(z.precision()))(z)
232
elif t == "t_FRAC":
233
Q = RationalField()
234
return Q(z)
235
elif t == "t_INT":
236
Z = IntegerRing()
237
return Z(z)
238
elif t == "t_COMPLEX":
239
tx = z.real().type()
240
ty = z.imag().type()
241
if tx in ["t_INTMOD", "t_PADIC"] or ty in ["t_INTMOD", "t_PADIC"]:
242
raise NotImplementedError, "No conversion to python available for t_COMPLEX with t_INTMOD or t_PADIC components"
243
if tx == "t_REAL" or ty == "t_REAL":
244
xprec = z.real().precision() # will be 0 if exact
245
yprec = z.imag().precision() # will be 0 if exact
246
if xprec == 0:
247
prec = prec_words_to_bits(yprec)
248
elif yprec == 0:
249
prec = prec_words_to_bits(xprec)
250
else:
251
prec = max(prec_words_to_bits(xprec), prec_words_to_bits(yprec))
252
R = RealField(prec)
253
C = ComplexField(prec)
254
return C(R(z.real()), R(z.imag()))
255
if tx == "t_FRAC" or ty == "t_FRAC":
256
return QuadraticField(-1,'i')([python(c) for c in list(z)])
257
if tx == "t_INT" or ty == "t_INT":
258
return QuadraticField(-1,'i').ring_of_integers()([python(c) for c in list(z)])
259
raise NotImplementedError, "No conversion to python available for t_COMPLEX with components %s"%(tx,ty)
260
elif t == "t_VEC":
261
return [python(x) for x in z.python_list()]
262
elif t == "t_VECSMALL":
263
return z.python_list_small()
264
elif t == "t_MAT":
265
from sage.matrix.constructor import matrix
266
return matrix(z.nrows(),z.ncols(),[python(z[i,j]) for i in range(z.nrows()) for j in range(z.ncols())])
267
elif t == "t_PADIC":
268
from sage.rings.padics.factory import Qp
269
Z = IntegerRing()
270
p = z.padicprime()
271
rprec = Z(z.padicprec(p)) - Z(z._valp())
272
K = Qp(Z(p), rprec)
273
return K(z.lift())
274
else:
275
from sage.misc.sage_eval import sage_eval
276
return sage_eval(str(z), locals=locals)
277
278