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