-
-
Notifications
You must be signed in to change notification settings - Fork 405
/
laurent_polynomial.pyx
3285 lines (2734 loc) · 97 KB
/
laurent_polynomial.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
r"""
Elements of Laurent polynomial rings
"""
# ****************************************************************************
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from __future__ import print_function
from sage.rings.integer cimport Integer
from sage.structure.element import is_Element, coerce_binop
from sage.misc.misc import union
from sage.structure.factorization import Factorization
from sage.misc.derivative import multi_derivative
from sage.rings.polynomial.polynomial_element import Polynomial
from sage.structure.richcmp cimport richcmp, rich_to_bool
cdef class LaurentPolynomial(CommutativeAlgebraElement):
"""
Base class for Laurent polynomials.
"""
cdef LaurentPolynomial _new_c(self):
"""
Return a new Laurent polynomial.
EXAMPLES::
sage: L.<x,y> = LaurentPolynomialRing(QQ) # indirect doctest
sage: x*y
x*y
"""
cdef type t = type(self)
cdef LaurentPolynomial ans
ans = t.__new__(t)
ans._parent = self._parent
return ans
cpdef _add_(self, other):
"""
Abstract addition method
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: from sage.rings.polynomial.laurent_polynomial import LaurentPolynomial
sage: LaurentPolynomial._add_(x, x)
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
cpdef _mul_(self, other):
"""
Abstract multiplication method
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: from sage.rings.polynomial.laurent_polynomial import LaurentPolynomial
sage: LaurentPolynomial._mul_(x, x)
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
cpdef _floordiv_(self, other):
"""
Abstract floor division method
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: from sage.rings.polynomial.laurent_polynomial import LaurentPolynomial
sage: LaurentPolynomial._floordiv_(x, x)
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
def _integer_(self, ZZ):
r"""
Convert this Laurent polynomial to an integer.
This is only possible if the Laurent polynomial is constant.
OUTPUT:
An integer.
TESTS::
sage: L.<a> = LaurentPolynomialRing(QQ)
sage: L(42)._integer_(ZZ)
42
sage: a._integer_(ZZ)
Traceback (most recent call last):
...
ValueError: a is not constant
sage: L(2/3)._integer_(ZZ)
Traceback (most recent call last):
...
TypeError: no conversion of this rational to integer
sage: ZZ(L(42))
42
::
sage: L.<a, b> = LaurentPolynomialRing(QQ)
sage: L(42)._integer_(ZZ)
42
sage: a._integer_(ZZ)
Traceback (most recent call last):
...
ValueError: a is not constant
sage: L(2/3)._integer_(ZZ)
Traceback (most recent call last):
...
TypeError: no conversion of this rational to integer
sage: ZZ(L(42))
42
"""
if not self.is_constant():
raise ValueError('{} is not constant'.format(self))
return ZZ(self.constant_coefficient())
def _rational_(self):
r"""
Convert this Laurent polynomial to a rational.
This is only possible if the Laurent polynomial is constant.
OUTPUT:
A rational.
TESTS::
sage: L.<a> = LaurentPolynomialRing(QQ)
sage: L(42)._rational_()
42
sage: a._rational_()
Traceback (most recent call last):
...
ValueError: a is not constant
sage: QQ(L(2/3))
2/3
::
sage: L.<a, b> = LaurentPolynomialRing(QQ)
sage: L(42)._rational_()
42
sage: a._rational_()
Traceback (most recent call last):
...
ValueError: a is not constant
sage: QQ(L(2/3))
2/3
"""
if not self.is_constant():
raise ValueError('{} is not constant'.format(self))
from sage.rings.rational_field import QQ
return QQ(self.constant_coefficient())
def change_ring(self, R):
"""
Return a copy of this Laurent polynomial, with coefficients in ``R``.
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(QQ)
sage: a = x^2 + 3*x^3 + 5*x^-1
sage: a.change_ring(GF(3))
2*x^-1 + x^2
Check that :trac:`22277` is fixed::
sage: R.<x, y> = LaurentPolynomialRing(QQ)
sage: a = 2*x^2 + 3*x^3 + 4*x^-1
sage: a.change_ring(GF(3))
-x^2 + x^-1
"""
return self._parent.change_ring(R)(self)
cpdef long number_of_terms(self) except -1:
"""
Abstract method for number of terms
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: from sage.rings.polynomial.laurent_polynomial import LaurentPolynomial
sage: LaurentPolynomial.number_of_terms(x)
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
def hamming_weight(self):
"""
Return the hamming weight of ``self``.
The hamming weight is number of non-zero coefficients and
also known as the weight or sparsity.
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: f = x^3 - 1
sage: f.hamming_weight()
2
"""
return self.number_of_terms()
cpdef dict dict(self):
"""
Abstract ``dict`` method.
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: from sage.rings.polynomial.laurent_polynomial import LaurentPolynomial
sage: LaurentPolynomial.dict(x)
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
cdef class LaurentPolynomial_univariate(LaurentPolynomial):
"""
A univariate Laurent polynomial in the form of `t^n \cdot f`
where `f` is a polynomial in `t`.
INPUT:
- ``parent`` -- a Laurent polynomial ring
- ``f`` -- a polynomial (or something can be coerced to one)
- ``n`` -- (default: 0) an integer
AUTHORS:
- Tom Boothby (2011) copied this class almost verbatim from
``laurent_series_ring_element.pyx``, so most of the credit goes to
William Stein, David Joyner, and Robert Bradshaw
- Travis Scrimshaw (09-2013): Cleaned-up and added a few extra methods
"""
def __init__(self, parent, f, n=0):
r"""
Create the Laurent polynomial `t^n \cdot f`.
EXAMPLES::
sage: R.<q> = LaurentPolynomialRing(ZZ)
sage: R([1,2,3])
1 + 2*q + 3*q^2
sage: TestSuite(q^-3 + 3*q + 2).run()
::
sage: S.<s> = LaurentPolynomialRing(GF(5))
sage: T.<t> = PolynomialRing(pAdicRing(5))
sage: S(t)
s
sage: parent(S(t))
Univariate Laurent Polynomial Ring in s over Finite Field of size 5
sage: parent(S(t)[1])
Finite Field of size 5
::
sage: R({})
0
"""
CommutativeAlgebraElement.__init__(self, parent)
if isinstance(f, LaurentPolynomial_univariate):
n += (<LaurentPolynomial_univariate>f).__n
if (<LaurentPolynomial_univariate>f).__u._parent is parent._R:
f = (<LaurentPolynomial_univariate>f).__u
else:
f = parent._R((<LaurentPolynomial_univariate>f).__u)
elif (not isinstance(f, Polynomial)) or (parent is not f.parent()):
if isinstance(f, dict):
v = min(f) if f else 0
f = {i-v: c for i,c in f.items()}
n += v
f = parent._R(f)
# self is that t^n * u:
self.__u = f
self.__n = n
self.__normalize()
def __reduce__(self):
"""
Used in pickling.
EXAMPLES::
sage: R.<q> = LaurentPolynomialRing(ZZ)
sage: elt = q^-3 + 2 + q
sage: loads(dumps(elt)) == elt
True
"""
return LaurentPolynomial_univariate, (self._parent, self.__u, self.__n)
def is_unit(self):
"""
Return ``True`` if this Laurent polynomial is a unit in this ring.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: (2+t).is_unit()
False
sage: f = 2*t
sage: f.is_unit()
True
sage: 1/f
1/2*t^-1
sage: R(0).is_unit()
False
sage: R.<s> = LaurentPolynomialRing(ZZ)
sage: g = 2*s
sage: g.is_unit()
False
sage: 1/g
1/2*s^-1
ALGORITHM: A Laurent polynomial is a unit if and only if its "unit
part" is a unit.
"""
return self.__u.is_term() and self.__u.coefficients()[0].is_unit()
def is_zero(self):
"""
Return ``1`` if ``self`` is 0, else return ``0``.
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(QQ)
sage: f = 1/x + x + x^2 + 3*x^4
sage: f.is_zero()
0
sage: z = 0*f
sage: z.is_zero()
1
"""
return self.__u.is_zero()
def __nonzero__(self):
"""
Check if ``self`` is non-zero.
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(QQ)
sage: f = 1/x + x + x^2 + 3*x^4
sage: not f
False
sage: z = 0*f
sage: not z
True
"""
return not self.__u.is_zero()
def _im_gens_(self, codomain, im_gens):
"""
Return the image of ``self`` under the morphism defined by
``im_gens`` in ``codomain``.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: H = Hom(R, QQ)
sage: mor = H(2)
sage: mor(t^2 + t^-2)
17/4
sage: 4 + 1/4
17/4
"""
return codomain(self(im_gens[0]))
cpdef __normalize(self):
r"""
A Laurent series is a pair `(u(t), n)`, where either `u = 0`
(to some precision) or `u` is a unit. This pair corresponds to
`t^n \cdot u(t)`.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: elt = t^2 + t^4 # indirect doctest
sage: elt.polynomial_construction()
(t^2 + 1, 2)
Check that :trac:`21272` is fixed::
sage: (t - t).polynomial_construction()
(0, 0)
"""
if self.__u[0]:
return
elif self.__u.is_zero():
self.__n = 0
return
# we already caught the infinity and zero cases
cdef long v = <long> self.__u.valuation()
self.__n += v
self.__u = self.__u >> v
def _repr_(self):
"""
Return a string representation of ``self``.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: 2 + (2/3)*t^3
2 + 2/3*t^3
"""
if self.is_zero():
return "0"
s = " "
v = self.__u.list()
valuation = self.__n
m = len(v)
X = self._parent.variable_name()
atomic_repr = self._parent.base_ring()._repr_option('element_is_atomic')
first = True
for n in xrange(m):
x = v[n]
e = n + valuation
x = str(x)
if x != '0':
if not first:
s += " + "
if not atomic_repr and (x[1:].find("+") != -1 or x[1:].find("-") != -1):
x = "({})".format(x)
if e == 1:
var = "*{}".format(X)
elif e == 0:
var = ""
else:
var = "*{}^{}".format(X,e)
s += "{}{}".format(x,var)
first = False
s = s.replace(" + -", " - ")
s = s.replace(" 1*"," ")
s = s.replace(" -1*", " -")
return s[1:]
def _latex_(self):
r"""
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(QQ)
sage: f = (17/2)*x^-2 + x + x^2 + 3*x^4
sage: latex(f)
\frac{\frac{17}{2}}{x^{2}} + x + x^{2} + 3x^{4}
Verify that :trac:`6656` has been fixed::
sage: R.<a,b>=PolynomialRing(QQ)
sage: T.<x>=LaurentPolynomialRing(R)
sage: y = a*x+b*x
sage: y._latex_()
'\\left(a + b\\right)x'
sage: latex(y)
\left(a + b\right)x
TESTS::
sage: L.<lambda2> = LaurentPolynomialRing(QQ)
sage: latex(L.an_element())
\lambda_{2}
sage: L.<y2> = LaurentPolynomialRing(QQ)
sage: latex(L.an_element())
y_{2}
"""
from sage.misc.latex import latex
if self.is_zero():
return "0"
s = " "
v = self.__u.list()
valuation = self.__n
m = len(v)
X = self._parent.latex_variable_names()[0]
atomic_repr = self._parent.base_ring()._repr_option('element_is_atomic')
first = True
for n in xrange(m):
x = v[n]
e = n + valuation
x = latex(x)
if x != '0':
if not first:
s += " + "
if not atomic_repr and e > 0 and (x[1:].find("+") != -1 or x[1:].find("-") != -1):
x = "\\left({}\\right)".format(x)
if e == 1:
var = "|{}".format(X)
elif e == 0:
var = ""
elif e > 0:
var = "|{}^{{{}}}".format(X,e)
if e >= 0:
s += "{}{}".format(x,var)
else: # negative e
if e == -1:
s += "\\frac{{{}}}{{{}}}".format(x, X)
else:
s += "\\frac{{{}}}{{{}^{{{}}}}}".format(x, X,-e)
first = False
s = s.replace(" + -", " - ")
s = s.replace(" 1|"," ")
s = s.replace(" -1|", " -")
s = s.replace("|","")
return s[1:]
def __hash__(self):
"""
Return the hash of ``self``.
TESTS::
sage: R = LaurentPolynomialRing(QQ, 't')
sage: assert hash(R.zero()) == 0
sage: assert hash(R.one()) == 1
sage: assert hash(QQ['t'].gen()) == hash(R.gen())
sage: for _ in range(20):
....: p = QQ.random_element()
....: assert hash(R(p)) == hash(p), "p = {}".format(p)
sage: S.<t> = QQ[]
sage: for _ in range(20):
....: p = S.random_element()
....: assert hash(R(p)) == hash(p), "p = {}".format(p)
....: assert hash(R(t*p)) == hash(t*p), "p = {}".format(p)
Check that :trac:`21272` is fixed::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: hash(R.zero()) == hash(t - t)
True
"""
# we reimplement below the hash of polynomials to handle negative
# degrees
cdef long result = 0
cdef long result_mon
cdef int i,j
cdef long var_hash_name = hash(self.__u._parent._names[0])
for i in range(self.__u.degree()+1):
result_mon = hash(self.__u[i])
if result_mon:
j = i + self.__n
if j > 0:
result_mon = (1000003 * result_mon) ^ var_hash_name
result_mon = (1000003 * result_mon) ^ j
elif j < 0:
result_mon = (1000003 * result_mon) ^ var_hash_name
result_mon = (700005 * result_mon) ^ j
result += result_mon
return result
def __getitem__(self, i):
"""
Return the `i`-th coefficient of ``self``.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: f = -5/t^(10) + t + t^2 - 10/3*t^3; f
-5*t^-10 + t + t^2 - 10/3*t^3
sage: f[-10]
-5
sage: f[1]
1
sage: f[3]
-10/3
sage: f[-9]
0
sage: f = -5/t^(10) + 1/3 + t + t^2 - 10/3*t^3; f
-5*t^-10 + 1/3 + t + t^2 - 10/3*t^3
Slicing is deprecated::
sage: f[-10:2]
doctest:...: DeprecationWarning: polynomial slicing with a start index is deprecated, use list() and slice the resulting list instead
See http://trac.sagemath.org/18940 for details.
-5*t^-10 + 1/3 + t
sage: f[0:]
1/3 + t + t^2 - 10/3*t^3
sage: f[:3]
-5*t^-10 + 1/3 + t + t^2
sage: f[-14:5:2]
Traceback (most recent call last):
...
NotImplementedError: polynomial slicing with a step is not defined
"""
cdef LaurentPolynomial_univariate ret
if isinstance(i, slice):
start = i.start - self.__n if i.start is not None else 0
stop = i.stop - self.__n if i.stop is not None else self.__u.degree() + 1
f = self.__u[start:stop:i.step] # deprecation(18940)
ret = <LaurentPolynomial_univariate> self._new_c()
ret.__u = f
ret.__n = self.__n
ret.__normalize()
return ret
return self.__u[i - self.__n]
cpdef long number_of_terms(self) except -1:
"""
Return the number of non-zero coefficients of ``self``.
Also called weight, hamming weight or sparsity.
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: f = x^3 - 1
sage: f.number_of_terms()
2
sage: R(0).number_of_terms()
0
sage: f = (x+1)^100
sage: f.number_of_terms()
101
The method :meth:`hamming_weight` is an alias::
sage: f.hamming_weight()
101
"""
return self.__u.number_of_terms()
def __iter__(self):
"""
Iterate through the coefficients from the first nonzero one to the
last nonzero one.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: f = -5/t^(2) + t + t^2 - 10/3*t^3; f
-5*t^-2 + t + t^2 - 10/3*t^3
sage: for a in f: print(a)
-5
0
0
1
1
-10/3
"""
return iter(self.__u)
def _symbolic_(self, R):
"""
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(QQ)
sage: f = x^3 + 2/x
sage: g = f._symbolic_(SR); g
(x^4 + 2)/x
sage: g(x=2)
9
sage: g = SR(f)
sage: g(x=2)
9
Since :trac:`24072` the symbolic ring does not accept positive
characteristic::
sage: R.<w> = LaurentPolynomialRing(GF(7))
sage: SR(2*w^3 + 1)
Traceback (most recent call last):
...
TypeError: positive characteristic not allowed in symbolic computations
"""
d = {repr(g): R.var(g) for g in self._parent.gens()}
return self.subs(**d)
cpdef dict dict(self):
"""
Return a dictionary representing ``self``.
EXAMPLES::
sage: R.<x,y> = ZZ[]
sage: Q.<t> = LaurentPolynomialRing(R)
sage: f = (x^3 + y/t^3)^3 + t^2; f
y^3*t^-9 + 3*x^3*y^2*t^-6 + 3*x^6*y*t^-3 + x^9 + t^2
sage: f.dict()
{-9: y^3, -6: 3*x^3*y^2, -3: 3*x^6*y, 0: x^9, 2: 1}
"""
cdef dict d = self.__u.dict()
return {k+self.__n: d[k] for k in d}
def coefficients(self):
"""
Return the nonzero coefficients of ``self``.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: f = -5/t^(2) + t + t^2 - 10/3*t^3
sage: f.coefficients()
[-5, 1, 1, -10/3]
"""
return self.__u.coefficients()
def exponents(self):
"""
Return the exponents appearing in ``self`` with nonzero coefficients.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: f = -5/t^(2) + t + t^2 - 10/3*t^3
sage: f.exponents()
[-2, 1, 2, 3]
"""
return [i + self.__n for i in self.__u.exponents()]
def __setitem__(self, n, value):
"""
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: f = t^2 + t^-3
sage: f[2] = 5
Traceback (most recent call last):
...
IndexError: Laurent polynomials are immutable
"""
raise IndexError("Laurent polynomials are immutable")
cpdef _unsafe_mutate(self, i, value):
r"""
Sage assumes throughout that commutative ring elements are
immutable. This is relevant for caching, etc. But sometimes you
need to change a Laurent polynomial and you really know what you're
doing. That's when this function is for you.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: f = t^2 + t^-3
sage: f._unsafe_mutate(2, 3)
sage: f
t^-3 + 3*t^2
"""
j = i - self.__n
if j >= 0:
self.__u._unsafe_mutate(j, value)
else: # off to the left
if value != 0:
self.__n = self.__n + j
R = self._parent.base_ring()
coeffs = [value] + [R.zero() for _ in range(1,-j)] + self.__u.list()
self.__u = self.__u._parent(coeffs)
self.__normalize()
cpdef _add_(self, right_m):
"""
Add two Laurent polynomials with the same parent.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: t + t
2*t
sage: f = 1/t + t^2 + t^3 - 17/3 * t^4
sage: g = 2/t + t^3
sage: f + g
3*t^-1 + t^2 + 2*t^3 - 17/3*t^4
sage: f + 0
t^-1 + t^2 + t^3 - 17/3*t^4
sage: 0 + f
t^-1 + t^2 + t^3 - 17/3*t^4
sage: R(0) + R(0)
0
sage: t^3 + t^-3
t^-3 + t^3
ALGORITHM: Shift the unit parts to align them, then add.
"""
cdef LaurentPolynomial_univariate right = <LaurentPolynomial_univariate>right_m
cdef long m
cdef LaurentPolynomial_univariate ret
# 1. Special case when one or the other is 0.
if not right:
return self
if not self:
return right
# 2. Align the unit parts.
if self.__n < right.__n:
m = self.__n
f1 = self.__u
f2 = right.__u << right.__n - m
elif self.__n > right.__n:
m = right.__n
f1 = self.__u << self.__n - m
f2 = right.__u
else:
m = self.__n
f1 = self.__u
f2 = right.__u
# 3. Add
ret = <LaurentPolynomial_univariate> self._new_c()
ret.__u = <ModuleElement> (f1 + f2)
ret.__n = m
ret.__normalize()
return ret
cpdef _sub_(self, right_m):
"""
Subtract two Laurent polynomials with the same parent.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(QQ)
sage: t - t
0
sage: t^5 + 2 * t^-5
2*t^-5 + t^5
ALGORITHM: Shift the unit parts to align them, then subtract.
"""
cdef LaurentPolynomial_univariate right = <LaurentPolynomial_univariate>right_m
cdef long m
cdef LaurentPolynomial_univariate ret
# 1. Special case when one or the other is 0.
if not right:
return self
if not self:
return -right
# 2. Align the unit parts.
if self.__n < right.__n:
m = self.__n
f1 = self.__u
f2 = right.__u << right.__n - m
else:
m = right.__n
f1 = self.__u << self.__n - m
f2 = right.__u
# 3. Subtract
ret = <LaurentPolynomial_univariate> self._new_c()
ret.__u = <ModuleElement> (f1 - f2)
ret.__n = m
ret.__normalize()
return ret
def degree(self):
"""
Return the degree of ``self``.
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: g = x^2 - x^4
sage: g.degree()
4
sage: g = -10/x^5 + x^2 - x^7
sage: g.degree()
7
"""
return self.__u.degree() + self.__n
def __neg__(self):
"""
Return the negative of ``self``.
EXAMPLES::
sage: R.<t> = LaurentPolynomialRing(ZZ)
sage: -(1+t^5)
-1 - t^5
"""
cdef LaurentPolynomial_univariate ret
ret = <LaurentPolynomial_univariate> self._new_c()
ret.__u = <ModuleElement> -self.__u
ret.__n = self.__n
# No need to normalize
return ret
cpdef _mul_(self, right_r):
"""
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(GF(2))
sage: f = 1/x^3 + x + x^2 + 3*x^4
sage: g = 1 - x + x^2 - x^4
sage: f*g
x^-3 + x^-2 + x^-1 + x^8
"""
cdef LaurentPolynomial_univariate right = <LaurentPolynomial_univariate>right_r
cdef LaurentPolynomial_univariate ret
ret = <LaurentPolynomial_univariate> self._new_c()
ret.__u = <ModuleElement> (self.__u * right.__u)
ret.__n = self.__n + right.__n
ret.__normalize()
return ret
cpdef _rmul_(self, Element c):
"""
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: f = 1/x^3 + x + x^2 + 3*x^4
sage: 3 * f
3*x^-3 + 3*x + 3*x^2 + 9*x^4
"""
cdef LaurentPolynomial_univariate ret
ret = <LaurentPolynomial_univariate> self._new_c()
ret.__u = <ModuleElement> self.__u._rmul_(c)
ret.__n = self.__n
ret.__normalize()
return ret
cpdef _lmul_(self, Element c):
"""
EXAMPLES::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: f = 1/x^3 + x + x^2 + 3*x^4
sage: f * 3
3*x^-3 + 3*x + 3*x^2 + 9*x^4
"""
cdef LaurentPolynomial_univariate ret
ret = <LaurentPolynomial_univariate> self._new_c()
ret.__u = <ModuleElement> self.__u._lmul_(c)
ret.__n = self.__n
ret.__normalize()
return ret
def is_monomial(self):
r"""
Return ``True`` if ``self`` is a monomial; that is, if ``self``
is `x^n` for some integer `n`.
EXAMPLES::
sage: k.<z> = LaurentPolynomialRing(QQ)
sage: z.is_monomial()
True
sage: k(1).is_monomial()
True
sage: (z+1).is_monomial()
False
sage: (z^-2909).is_monomial()
True
sage: (38*z^-2909).is_monomial()
False
"""
return self.__u.is_monomial()
def __pow__(_self, r, dummy):
"""
EXAMPLES::
sage: x = LaurentPolynomialRing(QQ,'x').0
sage: f = x + x^2 + 3*x^4
sage: g = 1/x^10 - x
sage: f^3
x^3 + 3*x^4 + 3*x^5 + 10*x^6 + 18*x^7 + 9*x^8 + 27*x^9 + 27*x^10 + 27*x^12
sage: g^4
x^-40 - 4*x^-29 + 6*x^-18 - 4*x^-7 + x^4
"""
cdef LaurentPolynomial_univariate self = _self
cdef long right = r
if right != r:
raise ValueError("exponent must be an integer")
return self._parent.element_class(self._parent, self.__u**right, self.__n*right)
cpdef _floordiv_(self, rhs):
"""
Perform division with remainder and return the quotient.