/
gen.pyx
9715 lines (7514 loc) · 290 KB
/
gen.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Sage class for PARI's GEN type
See the ``PariInstance`` class for documentation and examples.
AUTHORS:
- William Stein (2006-03-01): updated to work with PARI 2.2.12-beta
- William Stein (2006-03-06): added newtonpoly
- Justin Walker: contributed some of the function definitions
- Gonzalo Tornaria: improvements to conversions; much better error
handling.
- Robert Bradshaw, Jeroen Demeyer, William Stein (2010-08-15):
Upgrade to PARI 2.4.3 (:trac:`9343`)
- Jeroen Demeyer (2011-11-12): rewrite various conversion routines
(:trac:`11611`, :trac:`11854`, :trac:`11952`)
- Peter Bruin (2013-11-17): move PariInstance to a separate file
(:trac:`15185`)
- Jeroen Demeyer (2014-02-09): upgrade to PARI 2.7 (:trac:`15767`)
- Martin von Gagern (2014-12-17): Added some Galois functions (:trac:`17519`)
- Jeroen Demeyer (2015-01-12): upgrade to PARI 2.8 (:trac:`16997`)
- Jeroen Demeyer (2015-03-17): automatically generate methods from
``pari.desc`` (:trac:`17631` and :trac:`17860`)
"""
#*****************************************************************************
# Copyright (C) 2006,2010 William Stein <wstein@gmail.com>
# Copyright (C) ???? Justin Walker
# Copyright (C) ???? Gonzalo Tornaria
# Copyright (C) 2010 Robert Bradshaw <robertwb@math.washington.edu>
# Copyright (C) 2010-2015 Jeroen Demeyer <jdemeyer@cage.ugent.be>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
import math
import types
import operator
import sage.structure.element
from sage.structure.element cimport ModuleElement, RingElement, Element
from sage.misc.randstate cimport randstate, current_randstate
from sage.structure.sage_object cimport rich_to_bool
from sage.misc.superseded import deprecation, deprecated_function_alias
from .paridecl cimport *
from .paripriv cimport *
include 'pari_err.pxi'
include 'sage/ext/stdsage.pxi'
include 'sage/ext/python.pxi'
include 'sage/ext/interrupt.pxi'
cimport cython
cdef extern from "misc.h":
int factorint_withproof_sage(GEN* ans, GEN x, GEN cutoff)
from sage.libs.gmp.mpz cimport *
from sage.libs.gmp.pylong cimport mpz_set_pylong
from sage.libs.pari.closure cimport objtoclosure
from pari_instance cimport PariInstance, prec_bits_to_words, pari_instance
cdef PariInstance P = pari_instance
from sage.rings.integer cimport Integer
include 'auto_gen.pxi'
@cython.final
cdef class gen(gen_auto):
"""
Cython extension class that models the PARI GEN type.
"""
def __init__(self):
raise RuntimeError("PARI objects cannot be instantiated directly; use pari(x) to convert x to PARI")
def __dealloc__(self):
if self.b:
sage_free(<void*> self.b)
def __repr__(self):
"""
Display representation of a gen.
OUTPUT: a Python string
EXAMPLES::
sage: pari('vector(5,i,i)')
[1, 2, 3, 4, 5]
sage: pari('[1,2;3,4]')
[1, 2; 3, 4]
sage: pari('Str(hello)')
"hello"
"""
cdef char *c
pari_catch_sig_on()
# Use sig_block(), which is needed because GENtostr() uses
# malloc(), which is dangerous inside sig_on()
sig_block()
c = GENtostr(self.g)
sig_unblock()
pari_catch_sig_off()
s = str(c)
pari_free(c)
return s
def __str__(self):
"""
Convert this gen to a string.
Except for PARI strings, we have ``str(x) == repr(x)``.
For strings (type ``t_STR``), the returned string is not quoted.
OUTPUT: a Python string
EXAMPLES::
sage: print(pari('vector(5,i,i)'))
[1, 2, 3, 4, 5]
sage: print(pari('[1,2;3,4]'))
[1, 2; 3, 4]
sage: print(pari('Str(hello)'))
hello
"""
# Use __repr__ except for strings
if typ(self.g) == t_STR:
return GSTR(self.g)
return repr(self)
def __hash__(self):
"""
Return the hash of self, computed using PARI's hash_GEN().
TESTS::
sage: type(pari('1 + 2.0*I').__hash__())
<type 'int'>
"""
cdef long h
pari_catch_sig_on()
h = hash_GEN(self.g)
pari_catch_sig_off()
return h
def _testclass(self):
import test
T = test.testclass()
T._init(self)
return T
def list(self):
"""
Convert self to a list of PARI gens.
EXAMPLES:
A PARI vector becomes a Sage list::
sage: L = pari("vector(10,i,i^2)").list()
sage: L
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
sage: type(L)
<type 'list'>
sage: type(L[0])
<type 'sage.libs.pari.gen.gen'>
For polynomials, list() behaves as for ordinary Sage polynomials::
sage: pol = pari("x^3 + 5/3*x"); pol.list()
[0, 5/3, 0, 1]
For power series or Laurent series, we get all coefficients starting
from the lowest degree term. This includes trailing zeros::
sage: R.<x> = LaurentSeriesRing(QQ)
sage: s = x^2 + O(x^8)
sage: s.list()
[1]
sage: pari(s).list()
[1, 0, 0, 0, 0, 0]
sage: s = x^-2 + O(x^0)
sage: s.list()
[1]
sage: pari(s).list()
[1, 0]
For matrices, we get a list of columns::
sage: M = matrix(ZZ,3,2,[1,4,2,5,3,6]); M
[1 4]
[2 5]
[3 6]
sage: pari(M).list()
[[1, 2, 3]~, [4, 5, 6]~]
For "scalar" types, we get a 1-element list containing ``self``::
sage: pari("42").list()
[42]
"""
if typ(self.g) == t_POL:
return list(self.Vecrev())
return list(self.Vec())
def __reduce__(self):
"""
EXAMPLES::
sage: f = pari('x^3 - 3')
sage: loads(dumps(f)) == f
True
sage: f = pari('"hello world"')
sage: loads(dumps(f)) == f
True
"""
s = repr(self)
return (objtogen, (s,))
cpdef ModuleElement _add_(self, ModuleElement right):
pari_catch_sig_on()
return P.new_gen(gadd(self.g, (<gen>right).g))
cpdef ModuleElement _sub_(self, ModuleElement right):
pari_catch_sig_on()
return P.new_gen(gsub(self.g, (<gen> right).g))
cpdef RingElement _mul_(self, RingElement right):
pari_catch_sig_on()
return P.new_gen(gmul(self.g, (<gen>right).g))
cpdef RingElement _div_(self, RingElement right):
pari_catch_sig_on()
return P.new_gen(gdiv(self.g, (<gen>right).g))
def _add_one(gen self):
"""
Return self + 1.
OUTPUT: pari gen
EXAMPLES::
sage: n = pari(5)
sage: n._add_one()
6
sage: n = pari('x^3')
sage: n._add_one()
x^3 + 1
"""
pari_catch_sig_on()
return P.new_gen(gaddsg(1, self.g))
def __mod__(self, other):
"""
Return ``self`` modulo ``other``.
EXAMPLES::
sage: pari(15) % pari(6)
3
sage: pari("x^3+x^2+x+1") % pari("x^2")
x + 1
sage: pari(-2) % int(3)
1
sage: int(-2) % pari(3)
1
"""
cdef gen selfgen = objtogen(self)
cdef gen othergen = objtogen(other)
pari_catch_sig_on()
return P.new_gen(gmod(selfgen.g, othergen.g))
def __pow__(self, n, m):
"""
Return ``self`` to the power ``n`` (if ``m`` is ``None``) or
``Mod(self, m)^n`` if ``m`` is not ``None``.
EXAMPLES::
sage: pari(5) ^ pari(3)
125
sage: pari("x-1") ^ 3
x^3 - 3*x^2 + 3*x - 1
sage: pow(pari(5), pari(28), int(29))
Mod(1, 29)
sage: int(2) ^ pari(-5)
1/32
sage: pari(2) ^ int(-5)
1/32
"""
cdef gen t0 = objtogen(self)
cdef gen t1 = objtogen(n)
if m is not None:
t0 = t0.Mod(m)
pari_catch_sig_on()
return P.new_gen(gpow(t0.g, t1.g, prec_bits_to_words(0)))
def __neg__(gen self):
pari_catch_sig_on()
return P.new_gen(gneg(self.g))
def __rshift__(self, long n):
"""
Divide ``self`` by `2^n` (truncating or not, depending on the
input type).
EXAMPLES::
sage: pari(25) >> 3
3
sage: pari(25/2) >> 2
25/8
sage: pari("x") >> 3
1/8*x
sage: pari(1.0) >> 100
7.88860905221012 E-31
sage: int(33) >> pari(2)
8
"""
cdef gen t0 = objtogen(self)
pari_catch_sig_on()
return P.new_gen(gshift(t0.g, -n))
def __lshift__(self, long n):
"""
Multiply ``self`` by `2^n`.
EXAMPLES::
sage: pari(25) << 3
200
sage: pari(25/32) << 2
25/8
sage: pari("x") << 3
8*x
sage: pari(1.0) << 100
1.26765060022823 E30
sage: int(33) << pari(2)
132
"""
cdef gen t0 = objtogen(self)
pari_catch_sig_on()
return P.new_gen(gshift(t0.g, n))
def __invert__(gen self):
pari_catch_sig_on()
return P.new_gen(ginv(self.g))
def getattr(gen self, attr):
"""
Return the PARI attribute with the given name.
EXAMPLES::
sage: K = pari("nfinit(x^2 - x - 1)")
sage: K.getattr("pol")
x^2 - x - 1
sage: K.getattr("disc")
5
sage: K.getattr("reg")
Traceback (most recent call last):
...
PariError: _.reg: incorrect type in reg (t_VEC)
sage: K.getattr("zzz")
Traceback (most recent call last):
...
PariError: not a function in function call
"""
cdef str s = "_." + attr
cdef char *t = PyString_AsString(s)
pari_catch_sig_on()
return P.new_gen(closure_callgen1(strtofunction(t), self.g))
def mod(self):
"""
Given an INTMOD or POLMOD ``Mod(a,m)``, return the modulus `m`.
EXAMPLES::
sage: pari(4).Mod(5).mod()
5
sage: pari("Mod(x, x*y)").mod()
y*x
sage: pari("[Mod(4,5)]").mod()
Traceback (most recent call last):
...
TypeError: Not an INTMOD or POLMOD in mod()
"""
if typ(self.g) != t_INTMOD and typ(self.g) != t_POLMOD:
raise TypeError("Not an INTMOD or POLMOD in mod()")
pari_catch_sig_on()
# The hardcoded 1 below refers to the position in the internal
# representation of a INTMOD or POLDMOD where the modulus is
# stored.
return P.new_gen(gel(self.g, 1))
def nf_get_pol(self):
"""
Returns the defining polynomial of this number field.
INPUT:
- ``self`` -- A PARI number field being the output of ``nfinit()``,
``bnfinit()`` or ``bnrinit()``.
EXAMPLES::
sage: K.<a> = NumberField(x^4 - 4*x^2 + 1)
sage: pari(K).nf_get_pol()
y^4 - 4*y^2 + 1
sage: bnr = pari("K = bnfinit(x^4 - 4*x^2 + 1); bnrinit(K, 2*x)")
sage: bnr.nf_get_pol()
x^4 - 4*x^2 + 1
For relative number fields, this returns the relative
polynomial. However, beware that ``pari(L)`` returns an absolute
number field::
sage: L.<b> = K.extension(x^2 - 5)
sage: pari(L).nf_get_pol() # Absolute
y^8 - 28*y^6 + 208*y^4 - 408*y^2 + 36
sage: L.pari_rnf().nf_get_pol() # Relative
x^2 - 5
TESTS::
sage: x = polygen(QQ)
sage: K.<a> = NumberField(x^4 - 4*x^2 + 1)
sage: K.pari_nf().nf_get_pol()
y^4 - 4*y^2 + 1
sage: K.pari_bnf().nf_get_pol()
y^4 - 4*y^2 + 1
An error is raised for invalid input::
sage: pari("[0]").nf_get_pol()
Traceback (most recent call last):
...
PariError: incorrect type in pol (t_VEC)
"""
pari_catch_sig_on()
return P.new_gen(member_pol(self.g))
def nf_get_diff(self):
"""
Returns the different of this number field as a PARI ideal.
INPUT:
- ``self`` -- A PARI number field being the output of ``nfinit()``,
``bnfinit()`` or ``bnrinit()``.
EXAMPLES::
sage: K.<a> = NumberField(x^4 - 4*x^2 + 1)
sage: pari(K).nf_get_diff()
[12, 0, 0, 0; 0, 12, 8, 0; 0, 0, 4, 0; 0, 0, 0, 4]
"""
pari_catch_sig_on()
return P.new_gen(member_diff(self.g))
def nf_get_sign(self):
"""
Returns a Python list ``[r1, r2]``, where ``r1`` and ``r2`` are
Python ints representing the number of real embeddings and pairs
of complex embeddings of this number field, respectively.
INPUT:
- ``self`` -- A PARI number field being the output of ``nfinit()``,
``bnfinit()`` or ``bnrinit()``.
EXAMPLES::
sage: K.<a> = NumberField(x^4 - 4*x^2 + 1)
sage: s = K.pari_nf().nf_get_sign(); s
[4, 0]
sage: type(s); type(s[0])
<type 'list'>
<type 'int'>
sage: CyclotomicField(15).pari_nf().nf_get_sign()
[0, 4]
"""
cdef long r1
cdef long r2
cdef GEN sign
pari_catch_sig_on()
sign = member_sign(self.g)
r1 = itos(gel(sign, 1))
r2 = itos(gel(sign, 2))
pari_catch_sig_off()
return [r1, r2]
def nf_get_zk(self):
"""
Returns a vector with a `\ZZ`-basis for the ring of integers of
this number field. The first element is always `1`.
INPUT:
- ``self`` -- A PARI number field being the output of ``nfinit()``,
``bnfinit()`` or ``bnrinit()``.
EXAMPLES::
sage: K.<a> = NumberField(x^4 - 4*x^2 + 1)
sage: pari(K).nf_get_zk()
[1, y, y^3 - 4*y, y^2 - 2]
"""
pari_catch_sig_on()
return P.new_gen(member_zk(self.g))
def bnf_get_no(self):
"""
Returns the class number of ``self``, a "big number field" (``bnf``).
EXAMPLES::
sage: K.<a> = QuadraticField(-65)
sage: K.pari_bnf().bnf_get_no()
8
"""
pari_catch_sig_on()
return P.new_gen(bnf_get_no(self.g))
def bnf_get_cyc(self):
"""
Returns the structure of the class group of this number field as
a vector of SNF invariants.
NOTE: ``self`` must be a "big number field" (``bnf``).
EXAMPLES::
sage: K.<a> = QuadraticField(-65)
sage: K.pari_bnf().bnf_get_cyc()
[4, 2]
"""
pari_catch_sig_on()
return P.new_gen(bnf_get_cyc(self.g))
def bnf_get_gen(self):
"""
Returns a vector of generators of the class group of this
number field.
NOTE: ``self`` must be a "big number field" (``bnf``).
EXAMPLES::
sage: K.<a> = QuadraticField(-65)
sage: G = K.pari_bnf().bnf_get_gen(); G
[[3, 2; 0, 1], [2, 1; 0, 1]]
sage: map(lambda J: K.ideal(J), G)
[Fractional ideal (3, a + 2), Fractional ideal (2, a + 1)]
"""
pari_catch_sig_on()
return P.new_gen(bnf_get_gen(self.g))
def bnf_get_reg(self):
"""
Returns the regulator of this number field.
NOTE: ``self`` must be a "big number field" (``bnf``).
EXAMPLES::
sage: K.<a> = NumberField(x^4 - 4*x^2 + 1)
sage: K.pari_bnf().bnf_get_reg()
2.66089858019037...
"""
pari_catch_sig_on()
return P.new_gen(bnf_get_reg(self.g))
def pr_get_p(self):
"""
Returns the prime of `\ZZ` lying below this prime ideal.
NOTE: ``self`` must be a PARI prime ideal (as returned by
``idealfactor`` for example).
EXAMPLES::
sage: K.<i> = QuadraticField(-1)
sage: F = pari(K).idealfactor(K.ideal(5)); F
[[5, [-2, 1]~, 1, 1, [2, -1; 1, 2]], 1; [5, [2, 1]~, 1, 1, [-2, -1; 1, -2]], 1]
sage: F[0,0].pr_get_p()
5
"""
pari_catch_sig_on()
return P.new_gen(pr_get_p(self.g))
def pr_get_e(self):
"""
Returns the ramification index (over `\QQ`) of this prime ideal.
NOTE: ``self`` must be a PARI prime ideal (as returned by
``idealfactor`` for example).
EXAMPLES::
sage: K.<i> = QuadraticField(-1)
sage: pari(K).idealfactor(K.ideal(2))[0,0].pr_get_e()
2
sage: pari(K).idealfactor(K.ideal(3))[0,0].pr_get_e()
1
sage: pari(K).idealfactor(K.ideal(5))[0,0].pr_get_e()
1
"""
cdef long e
pari_catch_sig_on()
e = pr_get_e(self.g)
pari_catch_sig_off()
return e
def pr_get_f(self):
"""
Returns the residue class degree (over `\QQ`) of this prime ideal.
NOTE: ``self`` must be a PARI prime ideal (as returned by
``idealfactor`` for example).
EXAMPLES::
sage: K.<i> = QuadraticField(-1)
sage: pari(K).idealfactor(K.ideal(2))[0,0].pr_get_f()
1
sage: pari(K).idealfactor(K.ideal(3))[0,0].pr_get_f()
2
sage: pari(K).idealfactor(K.ideal(5))[0,0].pr_get_f()
1
"""
cdef long f
pari_catch_sig_on()
f = pr_get_f(self.g)
pari_catch_sig_off()
return f
def pr_get_gen(self):
"""
Returns the second generator of this PARI prime ideal, where the
first generator is ``self.pr_get_p()``.
NOTE: ``self`` must be a PARI prime ideal (as returned by
``idealfactor`` for example).
EXAMPLES::
sage: K.<i> = QuadraticField(-1)
sage: g = pari(K).idealfactor(K.ideal(2))[0,0].pr_get_gen(); g; K(g)
[1, 1]~
i + 1
sage: g = pari(K).idealfactor(K.ideal(3))[0,0].pr_get_gen(); g; K(g)
[3, 0]~
3
sage: g = pari(K).idealfactor(K.ideal(5))[0,0].pr_get_gen(); g; K(g)
[-2, 1]~
i - 2
"""
pari_catch_sig_on()
return P.new_gen(pr_get_gen(self.g))
def bid_get_cyc(self):
"""
Returns the structure of the group `(O_K/I)^*`, where `I` is the
ideal represented by ``self``.
NOTE: ``self`` must be a "big ideal" (``bid``) as returned by
``idealstar`` for example.
EXAMPLES::
sage: K.<i> = QuadraticField(-1)
sage: J = pari(K).idealstar(K.ideal(4*i + 2))
sage: J.bid_get_cyc()
[4, 2]
"""
pari_catch_sig_on()
return P.new_gen(bid_get_cyc(self.g))
def bid_get_gen(self):
"""
Returns a vector of generators of the group `(O_K/I)^*`, where
`I` is the ideal represented by ``self``.
NOTE: ``self`` must be a "big ideal" (``bid``) with generators,
as returned by ``idealstar`` with ``flag`` = 2.
EXAMPLES::
sage: K.<i> = QuadraticField(-1)
sage: J = pari(K).idealstar(K.ideal(4*i + 2), 2)
sage: J.bid_get_gen()
[7, [-2, -1]~]
We get an exception if we do not supply ``flag = 2`` to
``idealstar``::
sage: J = pari(K).idealstar(K.ideal(3))
sage: J.bid_get_gen()
Traceback (most recent call last):
...
PariError: missing bid generators. Use idealstar(,,2)
"""
pari_catch_sig_on()
return P.new_gen(bid_get_gen(self.g))
def __getitem__(gen self, n):
"""
Return the nth entry of self. The indexing is 0-based, like in
Python. Note that this is *different* than the default behavior
of the PARI/GP interpreter.
EXAMPLES::
sage: p = pari('1 + 2*x + 3*x^2')
sage: p[0]
1
sage: p[2]
3
sage: p[100]
0
sage: p[-1]
0
sage: q = pari('x^2 + 3*x^3 + O(x^6)')
sage: q[3]
3
sage: q[5]
0
sage: q[6]
Traceback (most recent call last):
...
IndexError: index out of range
sage: m = pari('[1,2;3,4]')
sage: m[0]
[1, 3]~
sage: m[1,0]
3
sage: l = pari('List([1,2,3])')
sage: l[1]
2
sage: s = pari('"hello, world!"')
sage: s[0]
'h'
sage: s[4]
'o'
sage: s[12]
'!'
sage: s[13]
Traceback (most recent call last):
...
IndexError: index out of range
sage: v = pari('[1,2,3]')
sage: v[0]
1
sage: c = pari('Col([1,2,3])')
sage: c[1]
2
sage: sv = pari('Vecsmall([1,2,3])')
sage: sv[2]
3
sage: type(sv[2])
<type 'int'>
sage: tuple(pari(3/5))
(3, 5)
sage: tuple(pari('1 + 5*I'))
(1, 5)
sage: tuple(pari('Qfb(1, 2, 3)'))
(1, 2, 3)
sage: pari(57)[0]
Traceback (most recent call last):
...
TypeError: PARI object of type 't_INT' cannot be indexed
sage: m = pari("[[1,2;3,4],5]") ; m[0][1,0]
3
sage: v = pari(xrange(20))
sage: v[2:5]
[2, 3, 4]
sage: v[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
sage: v[10:]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
sage: v[:5]
[0, 1, 2, 3, 4]
sage: v
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
sage: v[-1]
Traceback (most recent call last):
...
IndexError: index out of range
sage: v[:-3]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
sage: v[5:]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
sage: pari([])[::]
[]
"""
cdef int pari_type
pari_type = typ(self.g)
if isinstance(n, tuple):
if pari_type != t_MAT:
raise TypeError("self must be of pari type t_MAT")
if len(n) != 2:
raise IndexError("index must be an integer or a 2-tuple (i,j)")
i = int(n[0])
j = int(n[1])
if i < 0 or i >= glength(<GEN>(self.g[1])):
raise IndexError("row index out of range")
if j < 0 or j >= glength(self.g):
raise IndexError("column index out of range")
ind = (i,j)
if self.refers_to is not None and ind in self.refers_to:
return self.refers_to[ind]
else:
## In this case, we're being asked to return
## a GEN that has no gen pointing to it, so
## we need to create such a gen, add it to
## self.refers_to, and return it.
val = P.new_ref(gmael(self.g, j+1, i+1), self)
if self.refers_to is None:
self.refers_to = {ind: val}
else:
self.refers_to[ind] = val
return val
elif isinstance(n, slice):
l = glength(self.g)
start,stop,step = n.indices(l)
inds = xrange(start,stop,step)
k = len(inds)
# fast exit
if k==0:
return P.vector(0)
# fast call, beware pari is one based
if pari_type == t_VEC:
if step==1:
return self.vecextract('"'+str(start+1)+".."+str(stop)+'"')
if step==-1:
return self.vecextract('"'+str(start+1)+".."+str(stop+2)+'"')
# slow call
v = P.vector(k)
for i,j in enumerate(inds):
v[i] = self[j]
return v
## there are no "out of bounds" problems
## for a polynomial or power series, so these go before
## bounds testing
if pari_type == t_POL:
return self.polcoeff(n)
elif pari_type == t_SER:
bound = valp(self.g) + lg(self.g) - 2
if n >= bound:
raise IndexError("index out of range")
return self.polcoeff(n)
elif pari_type in (t_INT, t_REAL, t_PADIC, t_QUAD, t_FFELT, t_INTMOD, t_POLMOD):
# these are definitely scalar!
raise TypeError("PARI object of type %r cannot be indexed" % self.type())
elif n < 0 or n >= glength(self.g):
raise IndexError("index out of range")
elif pari_type == t_VEC or pari_type == t_MAT:
#t_VEC : row vector [ code ] [ x_1 ] ... [ x_k ]
#t_MAT : matrix [ code ] [ col_1 ] ... [ col_k ]
if self.refers_to is not None and n in self.refers_to:
return self.refers_to[n]
else:
## In this case, we're being asked to return
## a GEN that has no gen pointing to it, so
## we need to create such a gen, add it to
## self.refers_to, and return it.
val = P.new_ref(gel(self.g, n+1), self)
if self.refers_to is None:
self.refers_to = {n: val}
else:
self.refers_to[n] = val
return val
elif pari_type == t_VECSMALL:
#t_VECSMALL: vec. small ints [ code ] [ x_1 ] ... [ x_k ]
return self.g[n+1]
elif pari_type == t_STR:
#t_STR : string [ code ] [ man_1 ] ... [ man_k ]
return chr( (<char *>(self.g+1))[n] )
elif pari_type == t_LIST:
return self.component(n+1)
#elif pari_type in (t_FRAC, t_RFRAC):
# generic code gives us:
# [0] = numerator
# [1] = denominator
#elif pari_type == t_COMPLEX:
# generic code gives us
# [0] = real part
# [1] = imag part
#elif type(self.g) in (t_QFR, t_QFI):
# generic code works ok
else:
## generic code, which currently handles cases
## as mentioned above
return P.new_ref(gel(self.g,n+1), self)
def __setitem__(gen self, n, y):
r"""
Set the nth entry to a reference to y.
- The indexing is 0-based, like everywhere else in Python, but
*unlike* in PARI/GP.
- Assignment sets the nth entry to a reference to y, assuming y is
an object of type gen. This is the same as in Python, but
*different* than what happens in the gp interpreter, where
assignment makes a copy of y.
- Because setting creates references it is *possible* to make
circular references, unlike in GP. Do *not* do this (see the
example below). If you need circular references, work at the Python
level (where they work well), not the PARI object level.
EXAMPLES::
sage: v = pari(range(10))
sage: v
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sage: v[0] = 10
sage: w = pari([5,8,-20])
sage: v
[10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sage: v[1] = w
sage: v
[10, [5, 8, -20], 2, 3, 4, 5, 6, 7, 8, 9]
sage: w[0] = -30
sage: v
[10, [-30, 8, -20], 2, 3, 4, 5, 6, 7, 8, 9]
sage: t = v[1]; t[1] = 10 ; v
[10, [-30, 10, -20], 2, 3, 4, 5, 6, 7, 8, 9]
sage: v[1][0] = 54321 ; v
[10, [54321, 10, -20], 2, 3, 4, 5, 6, 7, 8, 9]
sage: w
[54321, 10, -20]
sage: v = pari([[[[0,1],2],3],4]) ; v[0][0][0][1] = 12 ; v
[[[[0, 12], 2], 3], 4]
sage: m = pari(matrix(2,2,range(4))) ; l = pari([5,6]) ; n = pari(matrix(2,2,[7,8,9,0])) ; m[1,0] = l ; l[1] = n ; m[1,0][1][1,1] = 1111 ; m
[0, 1; [5, [7, 8; 9, 1111]], 3]
sage: m = pari("[[1,2;3,4],5,6]") ; m[0][1,1] = 11 ; m
[[1, 2; 3, 11], 5, 6]
Finally, we create a circular reference::
sage: v = pari([0])
sage: w = pari([v])
sage: v
[0]
sage: w
[[0]]
sage: v[0] = w
Now there is a circular reference. Accessing v[0] will crash Sage.
::
sage: s=pari.vector(2,[0,0])
sage: s[:1]
[0]
sage: s[:1]=[1]