Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/rings/ideal.py
4066 views
1
r"""
2
Ideals of commutative rings.
3
4
Sage provides functionality for computing with ideals. One can create
5
an ideal in any commutative or non-commutative ring `R` by giving a
6
list of generators, using the notation ``R.ideal([a,b,...])``. The case
7
of non-commutative rings is implemented in :mod:`~sage.rings.noncommutative_ideals`.
8
9
A more convenient notation may be ``R*[a,b,...]`` or ``[a,b,...]*R``.
10
If `R` is non-commutative, the former creates a left and the latter
11
a right ideal, and ``R*[a,b,...]*R`` creates a two-sided ideal.
12
"""
13
14
#*****************************************************************************
15
# Copyright (C) 2005 William Stein <[email protected]>
16
#
17
# Distributed under the terms of the GNU General Public License (GPL)
18
#
19
# This code is distributed in the hope that it will be useful,
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22
# General Public License for more details.
23
#
24
# The full text of the GPL is available at:
25
#
26
# http://www.gnu.org/licenses/
27
#*****************************************************************************
28
29
from types import GeneratorType
30
31
import sage.misc.latex as latex
32
import sage.rings.ring
33
import commutative_ring
34
from sage.structure.element import MonoidElement
35
from sage.interfaces.singular import singular as singular_default
36
import sage.rings.infinity
37
from sage.structure.sequence import Sequence
38
39
def Ideal(*args, **kwds):
40
r"""
41
Create the ideal in ring with given generators.
42
43
There are some shorthand notations for creating an ideal, in
44
addition to using the Ideal function::
45
46
-- R.ideal(gens, coerce=True)
47
-- gens*R
48
-- R*gens
49
50
51
INPUT:
52
53
54
- ``R (optional)`` - a ring (if not given, will try to
55
infer it from gens)
56
57
- ``gens`` - list of elements generating the ideal
58
59
- ``coerce (optional)`` - bool (default: True);
60
whether gens need to be coerced into ring.
61
62
63
OUTPUT: The ideal of ring generated by gens.
64
65
EXAMPLES::
66
67
sage: R, x = PolynomialRing(ZZ, 'x').objgen()
68
sage: I = R.ideal([4 + 3*x + x^2, 1 + x^2])
69
sage: I
70
Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring
71
sage: Ideal(R, [4 + 3*x + x^2, 1 + x^2])
72
Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring
73
sage: Ideal((4 + 3*x + x^2, 1 + x^2))
74
Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring
75
76
::
77
78
sage: ideal(x^2-2*x+1, x^2-1)
79
Ideal (x^2 - 2*x + 1, x^2 - 1) of Univariate Polynomial Ring in x over Integer Ring
80
sage: ideal([x^2-2*x+1, x^2-1])
81
Ideal (x^2 - 2*x + 1, x^2 - 1) of Univariate Polynomial Ring in x over Integer Ring
82
sage: l = [x^2-2*x+1, x^2-1]
83
sage: ideal(f^2 for f in l)
84
Ideal (x^4 - 4*x^3 + 6*x^2 - 4*x + 1, x^4 - 2*x^2 + 1) of
85
Univariate Polynomial Ring in x over Integer Ring
86
87
This example illustrates how Sage finds a common ambient ring for
88
the ideal, even though 1 is in the integers (in this case).
89
90
::
91
92
sage: R.<t> = ZZ['t']
93
sage: i = ideal(1,t,t^2)
94
sage: i
95
Ideal (1, t, t^2) of Univariate Polynomial Ring in t over Integer Ring
96
sage: ideal(1/2,t,t^2)
97
Principal ideal (1) of Univariate Polynomial Ring in t over Rational Field
98
99
This shows that the issues at trac #1104 are resolved::
100
101
sage: Ideal(3, 5)
102
Principal ideal (1) of Integer Ring
103
sage: Ideal(ZZ, 3, 5)
104
Principal ideal (1) of Integer Ring
105
sage: Ideal(2, 4, 6)
106
Principal ideal (2) of Integer Ring
107
108
You have to provide enough information that Sage can figure out
109
which ring to put the ideal in.
110
111
::
112
113
sage: I = Ideal([])
114
Traceback (most recent call last):
115
...
116
ValueError: unable to determine which ring to embed the ideal in
117
118
sage: I = Ideal()
119
Traceback (most recent call last):
120
...
121
ValueError: need at least one argument
122
123
Note that some rings use different ideal implementations than the standard, even if they are PIDs.::
124
125
sage: R.<x> = GF(5)[]
126
sage: I = R*(x^2+3)
127
sage: type(I)
128
<class 'sage.rings.polynomial.ideal.Ideal_1poly_field'>
129
130
You can also pass in a specific ideal type::
131
132
sage: from sage.rings.ideal import Ideal_pid
133
sage: I = Ideal(x^2+3,ideal_class=Ideal_pid)
134
sage: type(I)
135
<class 'sage.rings.ideal.Ideal_pid'>
136
137
TESTS::
138
139
sage: R, x = PolynomialRing(ZZ, 'x').objgen()
140
sage: I = R.ideal([4 + 3*x + x^2, 1 + x^2])
141
sage: I == loads(dumps(I))
142
True
143
144
::
145
146
sage: I = Ideal(R, [4 + 3*x + x^2, 1 + x^2])
147
sage: I == loads(dumps(I))
148
True
149
150
::
151
152
sage: I = Ideal((4 + 3*x + x^2, 1 + x^2))
153
sage: I == loads(dumps(I))
154
True
155
156
This shows that the issue at trac ticket #5477 is fixed::
157
158
sage: R.<x> = QQ[]
159
sage: I = R.ideal([x + x^2])
160
sage: J = R.ideal([2*x + 2*x^2])
161
sage: J
162
Principal ideal (x^2 + x) of Univariate Polynomial Ring in x over Rational Field
163
sage: S = R.quotient_ring(I)
164
sage: U = R.quotient_ring(J)
165
sage: I == J
166
True
167
sage: S == U
168
True
169
"""
170
if len(args) == 0:
171
raise ValueError, "need at least one argument"
172
if len(args) == 1 and args[0] == []:
173
raise ValueError, "unable to determine which ring to embed the ideal in"
174
175
first = args[0]
176
177
if not isinstance(first, sage.rings.ring.Ring):
178
if isinstance(first, Ideal_generic) and len(args) == 1:
179
R = first.ring()
180
gens = first.gens()
181
else:
182
if isinstance(first, (list, tuple, GeneratorType)) and len(args) == 1:
183
gens = first
184
else:
185
gens = args
186
gens = Sequence(gens)
187
R = gens.universe()
188
else:
189
R = first
190
gens = args[1:]
191
192
if not commutative_ring.is_CommutativeRing(R):
193
raise TypeError, "R must be a commutative ring"
194
195
return R.ideal(*gens, **kwds)
196
197
def is_Ideal(x):
198
r"""
199
Returns True if object is an ideal of a ring.
200
201
EXAMPLES:
202
203
A simple example involving the ring of integers. Note
204
that Sage does not interpret rings objects themselves as ideals.
205
However, one can still explicitly construct these ideals::
206
207
sage: from sage.rings.ideal import is_Ideal
208
sage: R = ZZ
209
sage: is_Ideal(R)
210
False
211
sage: 1*R; is_Ideal(1*R)
212
Principal ideal (1) of Integer Ring
213
True
214
sage: 0*R; is_Ideal(0*R)
215
Principal ideal (0) of Integer Ring
216
True
217
218
Sage recognizes ideals of polynomial rings as well::
219
220
sage: R = PolynomialRing(QQ, 'x'); x = R.gen()
221
sage: I = R.ideal(x^2 + 1); I
222
Principal ideal (x^2 + 1) of Univariate Polynomial Ring in x over Rational Field
223
sage: is_Ideal(I)
224
True
225
sage: is_Ideal((x^2 + 1)*R)
226
True
227
"""
228
return isinstance(x, Ideal_generic)
229
230
231
class Ideal_generic(MonoidElement):
232
"""
233
An ideal.
234
"""
235
def __init__(self, ring, gens, coerce=True):
236
self.__ring = ring
237
if not isinstance(gens, (list, tuple)):
238
gens = [gens]
239
if coerce:
240
gens = [ring(x) for x in gens]
241
242
gens = tuple(gens)
243
if len(gens)==0: gens=(ring.zero_element(),)
244
self.__gens = gens
245
MonoidElement.__init__(self, ring.ideal_monoid())
246
247
def _repr_short(self):
248
"""
249
Represent the list of generators.
250
251
EXAMPLE::
252
253
sage: P.<a,b,c> = QQ[]
254
sage: P*[a^2,a*b+c,c^3]
255
Ideal (a^2, a*b + c, c^3) of Multivariate Polynomial Ring in a, b, c over Rational Field
256
sage: (P*[a^2,a*b+c,c^3])._repr_short()
257
'(a^2, a*b + c, c^3)'
258
259
If the string representation of a generator contains a line break,
260
the generators are not represented from left to right but from
261
top to bottom. This is the case, e.g., for matrices::
262
263
sage: MS = MatrixSpace(QQ,2,2)
264
sage: MS*[MS.1,2]
265
Left Ideal
266
(
267
[0 1]
268
[0 0],
269
<BLANKLINE>
270
[2 0]
271
[0 2]
272
)
273
of Full MatrixSpace of 2 by 2 dense matrices over Rational Field
274
275
276
"""
277
L = []
278
has_return = False
279
for x in self.gens():
280
s = repr(x)
281
if '\n' in s:
282
has_return = True
283
s = s.replace('\n','\n ')
284
L.append(s)
285
if has_return:
286
return '\n(\n %s\n)\n'%(',\n\n '.join(L))
287
return '(%s)'%(', '.join(L))
288
289
def __repr__(self):
290
return "Ideal %s of %s"%(self._repr_short(), self.ring())
291
292
def __cmp__(self, other):
293
S = set(self.gens())
294
T = set(other.gens())
295
if S == T:
296
return 0
297
return cmp(self.gens(), other.gens())
298
299
def __contains__(self, x):
300
try:
301
return self._contains_(self.__ring(x))
302
except TypeError:
303
return False
304
305
def _contains_(self, x):
306
# check if x, which is assumed to be in the ambient ring, is actually in this ideal.
307
raise NotImplementedError
308
309
def __nonzero__(self):
310
r"""
311
Return True if this ideal is not (0).
312
313
TESTS::
314
315
sage: I = ZZ.ideal(5)
316
sage: bool(I)
317
True
318
319
::
320
321
sage: I = ZZ['x'].ideal(0)
322
sage: bool(I)
323
False
324
325
::
326
327
sage: I = ZZ['x'].ideal(ZZ['x'].gen()^2)
328
sage: bool(I)
329
True
330
331
::
332
333
sage: I = QQ['x', 'y'].ideal(0)
334
sage: bool(I)
335
False
336
"""
337
for g in self.gens():
338
if not g.is_zero():
339
return True
340
return False
341
342
def base_ring(self):
343
r"""
344
Returns the base ring of this ideal.
345
346
EXAMPLES::
347
348
sage: R = ZZ
349
sage: I = 3*R; I
350
Principal ideal (3) of Integer Ring
351
sage: J = 2*I; J
352
Principal ideal (6) of Integer Ring
353
sage: I.base_ring(); J.base_ring()
354
Integer Ring
355
Integer Ring
356
357
We construct an example of an ideal of a quotient ring::
358
359
sage: R = PolynomialRing(QQ, 'x'); x = R.gen()
360
sage: I = R.ideal(x^2 - 2)
361
sage: I.base_ring()
362
Rational Field
363
364
And p-adic numbers::
365
366
sage: R = Zp(7, prec=10); R
367
7-adic Ring with capped relative precision 10
368
sage: I = 7*R; I
369
Principal ideal (7 + O(7^11)) of 7-adic Ring with capped relative precision 10
370
sage: I.base_ring()
371
7-adic Ring with capped relative precision 10
372
"""
373
return self.ring().base_ring()
374
375
def apply_morphism(self, phi):
376
r"""
377
Apply the morphism ``phi`` to every element of this ideal.
378
Returns an ideal in the domain of ``phi``.
379
380
EXAMPLES::
381
382
sage: psi = CC['x'].hom([-CC['x'].0])
383
sage: J = ideal([CC['x'].0 + 1]); J
384
Principal ideal (x + 1.00000000000000) of Univariate Polynomial Ring in x over Complex Field with 53 bits of precision
385
sage: psi(J)
386
Principal ideal (x - 1.00000000000000) of Univariate Polynomial Ring in x over Complex Field with 53 bits of precision
387
sage: J.apply_morphism(psi)
388
Principal ideal (x - 1.00000000000000) of Univariate Polynomial Ring in x over Complex Field with 53 bits of precision
389
390
::
391
392
sage: psi = ZZ['x'].hom([-ZZ['x'].0])
393
sage: J = ideal([ZZ['x'].0, 2]); J
394
Ideal (x, 2) of Univariate Polynomial Ring in x over Integer Ring
395
sage: psi(J)
396
Ideal (-x, 2) of Univariate Polynomial Ring in x over Integer Ring
397
sage: J.apply_morphism(psi)
398
Ideal (-x, 2) of Univariate Polynomial Ring in x over Integer Ring
399
400
TESTS::
401
402
sage: K.<a> = NumberField(x^2 + 1)
403
sage: A = K.ideal(a)
404
sage: taus = K.embeddings(K)
405
sage: A.apply_morphism(taus[0]) # identity
406
Fractional ideal (a)
407
sage: A.apply_morphism(taus[1]) # complex conjugation
408
Fractional ideal (-a)
409
sage: A.apply_morphism(taus[0]) == A.apply_morphism(taus[1])
410
True
411
412
::
413
414
sage: K.<a> = NumberField(x^2 + 5)
415
sage: B = K.ideal([2, a + 1]); B
416
Fractional ideal (2, a + 1)
417
sage: taus = K.embeddings(K)
418
sage: B.apply_morphism(taus[0]) # identity
419
Fractional ideal (2, a + 1)
420
421
Since 2 is totally ramified, complex conjugation fixes it::
422
423
sage: B.apply_morphism(taus[1]) # complex conjugation
424
Fractional ideal (2, a + 1)
425
sage: taus[1](B)
426
Fractional ideal (2, a + 1)
427
"""
428
from sage.categories.morphism import is_Morphism
429
if not is_Morphism(phi):
430
raise TypeError, "phi must be a morphism"
431
# delegate: morphisms know how to apply themselves to ideals
432
return phi(self)
433
434
def _latex_(self):
435
return '\\left(%s\\right)%s'%(", ".join([latex.latex(g) for g in \
436
self.gens()]),
437
latex.latex(self.ring()))
438
439
def ring(self):
440
"""
441
Returns the ring containing this ideal.
442
443
EXAMPLES::
444
445
sage: R = ZZ
446
sage: I = 3*R; I
447
Principal ideal (3) of Integer Ring
448
sage: J = 2*I; J
449
Principal ideal (6) of Integer Ring
450
sage: I.ring(); J.ring()
451
Integer Ring
452
Integer Ring
453
454
Note that ``self.ring()`` is different from
455
``self.base_ring()``
456
457
::
458
459
sage: R = PolynomialRing(QQ, 'x'); x = R.gen()
460
sage: I = R.ideal(x^2 - 2)
461
sage: I.base_ring()
462
Rational Field
463
sage: I.ring()
464
Univariate Polynomial Ring in x over Rational Field
465
466
Another example using polynomial rings::
467
468
sage: R = PolynomialRing(QQ, 'x'); x = R.gen()
469
sage: I = R.ideal(x^2 - 3)
470
sage: I.ring()
471
Univariate Polynomial Ring in x over Rational Field
472
sage: Rbar = R.quotient(I, names='a')
473
sage: S = PolynomialRing(Rbar, 'y'); y = Rbar.gen(); S
474
Univariate Polynomial Ring in y over Univariate Quotient Polynomial Ring in a over Rational Field with modulus x^2 - 3
475
sage: J = S.ideal(y^2 + 1)
476
sage: J.ring()
477
Univariate Polynomial Ring in y over Univariate Quotient Polynomial Ring in a over Rational Field with modulus x^2 - 3
478
"""
479
return self.__ring
480
481
def reduce(self, f):
482
r"""
483
Return the reduction of the element of `f` modulo the ideal
484
`I` (=self). This is an element of `R` that is
485
equivalent modulo `I` to `f`.
486
487
EXAMPLES::
488
489
sage: ZZ.ideal(5).reduce(17)
490
2
491
sage: parent(ZZ.ideal(5).reduce(17))
492
Integer Ring
493
"""
494
return f # default
495
496
def gens(self):
497
"""
498
Return a set of generators / a basis of self. This is usually the
499
set of generators provided during object creation.
500
501
EXAMPLE::
502
503
sage: P.<x,y> = PolynomialRing(QQ,2)
504
sage: I = Ideal([x,y+1]); I
505
Ideal (x, y + 1) of Multivariate Polynomial Ring in x, y over Rational Field
506
sage: I.gens()
507
[x, y + 1]
508
509
::
510
511
sage: ZZ.ideal(5,10).gens()
512
(5,)
513
"""
514
return self.__gens
515
516
def gen(self, i):
517
"""
518
Return the ``i``-th generator in the current basis of this ideal.
519
520
EXAMPLE::
521
522
sage: P.<x,y> = PolynomialRing(QQ,2)
523
sage: I = Ideal([x,y+1]); I
524
Ideal (x, y + 1) of Multivariate Polynomial Ring in x, y over Rational Field
525
sage: I.gen(1)
526
y + 1
527
528
sage: ZZ.ideal(5,10).gen()
529
5
530
"""
531
return self.__gens[i]
532
533
def ngens(self):
534
"""
535
Return the number of generators in the basis.
536
537
EXAMPLE::
538
539
sage: P.<x,y> = PolynomialRing(QQ,2)
540
sage: I = Ideal([x,y+1]); I
541
Ideal (x, y + 1) of Multivariate Polynomial Ring in x, y over Rational Field
542
sage: I.ngens()
543
2
544
545
sage: ZZ.ideal(5,10).ngens()
546
1
547
"""
548
return len(self.__gens)
549
550
def gens_reduced(self):
551
r"""
552
Same as gens() for this ideal, since there is currently no special
553
gens_reduced algorithm implemented for this ring.
554
555
This method is provided so that ideals in ZZ have the method
556
gens_reduced(), just like ideals of number fields.
557
558
EXAMPLES::
559
560
sage: ZZ.ideal(5).gens_reduced()
561
(5,)
562
"""
563
return self.gens()
564
565
def is_maximal(self):
566
r"""
567
Returns True if the ideal is maximal in the ring containing the
568
ideal.
569
570
TODO: This is not implemented for many rings. Implement it!
571
572
EXAMPLES::
573
574
sage: R = ZZ
575
sage: I = R.ideal(7)
576
sage: I.is_maximal()
577
True
578
sage: R.ideal(16).is_maximal()
579
False
580
sage: S = Integers(8)
581
sage: S.ideal(0).is_maximal()
582
False
583
sage: S.ideal(2).is_maximal()
584
True
585
sage: S.ideal(4).is_maximal()
586
False
587
"""
588
from sage.rings.all import ZZ
589
R = self.ring()
590
if hasattr(R, 'cover_ring') and R.cover_ring() is ZZ:
591
# The following test only works for quotients of Z/nZ: for
592
# many other rings in Sage, testing whether R/I is a field
593
# is done by testing whether I is maximal, so this would
594
# result in a loop.
595
return R.quotient(self).is_field()
596
kd = R.krull_dimension()
597
if kd == 0 or (kd == 1 and R.is_integral_domain()):
598
# For rings of Krull dimension 0, or for integral domains of
599
# Krull dimension 1, every nontrivial prime ideal is maximal.
600
return self.is_prime()
601
else:
602
raise NotImplementedError
603
604
def is_primary(self, P=None):
605
r"""
606
Returns ``True`` if this ideal is primary (or `P`-primary, if
607
a prime ideal `P` is specified).
608
609
Recall that an ideal `I` is primary if and only if `I` has a
610
unique associated prime (see page 52 in [AtiMac]_). If this
611
prime is `P`, then `I` is said to be `P`-primary.
612
613
INPUT:
614
615
- ``P`` - (default: ``None``) a prime ideal in the same ring
616
617
EXAMPLES::
618
619
sage: R.<x, y> = QQ[]
620
sage: I = R.ideal([x^2, x*y])
621
sage: I.is_primary()
622
False
623
sage: J = I.primary_decomposition()[1]; J
624
Ideal (y, x^2) of Multivariate Polynomial Ring in x, y over Rational Field
625
sage: J.is_primary()
626
True
627
sage: J.is_prime()
628
False
629
630
Some examples from the Macaulay2 documentation::
631
632
sage: R.<x, y, z> = GF(101)[]
633
sage: I = R.ideal([y^6])
634
sage: I.is_primary()
635
True
636
sage: I.is_primary(R.ideal([y]))
637
True
638
sage: I = R.ideal([x^4, y^7])
639
sage: I.is_primary()
640
True
641
sage: I = R.ideal([x*y, y^2])
642
sage: I.is_primary()
643
False
644
645
NOTES: Uses the list of associated primes.
646
647
REFERENCES:
648
649
.. [AtiMac] Atiyah and Macdonald, "Introduction to commutative
650
algebra", Addison-Wesley, 1969.
651
652
"""
653
try:
654
ass = self.associated_primes()
655
except (NotImplementedError, ValueError):
656
raise NotImplementedError
657
if P is None:
658
return (len(ass) == 1)
659
else:
660
return (len(ass) == 1) and (ass[0] == P)
661
662
663
def primary_decomposition(self):
664
r"""
665
Return a decomposition of this ideal into primary ideals.
666
667
EXAMPLES::
668
669
sage: R = ZZ[x]
670
sage: I = R.ideal(7)
671
sage: I.primary_decomposition()
672
Traceback (most recent call last):
673
...
674
NotImplementedError
675
"""
676
raise NotImplementedError
677
678
def is_prime(self):
679
r"""
680
Returns ``True`` if the ideal is prime.
681
682
EXAMPLES::
683
684
sage: R.<x, y> = QQ[]
685
sage: I = R.ideal([x, y])
686
sage: I.is_prime() # a maximal ideal
687
True
688
sage: I = R.ideal([x^2-y])
689
sage: I.is_prime() # a non-maximal prime ideal
690
True
691
sage: I = R.ideal([x^2, y])
692
sage: I.is_prime() # a non-prime primary ideal
693
False
694
sage: I = R.ideal([x^2, x*y])
695
sage: I.is_prime() # a non-prime non-primary ideal
696
False
697
698
sage: S = Integers(8)
699
sage: S.ideal(0).is_prime()
700
False
701
sage: S.ideal(2).is_prime()
702
True
703
sage: S.ideal(4).is_prime()
704
False
705
706
Note that this method is not implemented for all rings where it
707
could be::
708
709
sage: R = ZZ[x]
710
sage: I = R.ideal(7)
711
sage: I.is_prime() # when implemented, should be True
712
Traceback (most recent call last):
713
...
714
NotImplementedError
715
716
NOTES: For general rings, uses the list of associated primes.
717
"""
718
from sage.rings.all import ZZ
719
R = self.ring()
720
if hasattr(R, 'cover_ring') and R.cover_ring() is ZZ and R.is_finite():
721
# For quotient rings of ZZ, prime is the same as maximal.
722
return self.is_maximal()
723
try:
724
ass = self.associated_primes()
725
except (NotImplementedError, ValueError):
726
raise NotImplementedError
727
if len(ass) <> 1:
728
return False
729
if self == ass[0]:
730
return True
731
else:
732
return False
733
734
def associated_primes(self):
735
r"""
736
Return the list of associated prime ideals of this ideal.
737
738
EXAMPLES::
739
740
sage: R = ZZ[x]
741
sage: I = R.ideal(7)
742
sage: I.associated_primes()
743
Traceback (most recent call last):
744
...
745
NotImplementedError
746
"""
747
raise NotImplementedError
748
749
def minimal_associated_primes(self):
750
r"""
751
Return the list of minimal associated prime ideals of this ideal.
752
753
EXAMPLES::
754
755
sage: R = ZZ[x]
756
sage: I = R.ideal(7)
757
sage: I.minimal_associated_primes()
758
Traceback (most recent call last):
759
...
760
NotImplementedError
761
"""
762
raise NotImplementedError
763
764
def embedded_primes(self):
765
r"""
766
Return the list of embedded primes of this ideal.
767
768
EXAMPLES::
769
770
sage: R.<x, y> = QQ[]
771
sage: I = R.ideal(x^2, x*y)
772
sage: I.embedded_primes()
773
[Ideal (y, x) of Multivariate Polynomial Ring in x, y over Rational Field]
774
"""
775
# by definition, embedded primes are associated primes that
776
# are not minimal (under inclusion)
777
ass = self.associated_primes()
778
min_ass = self.minimal_associated_primes()
779
emb = []
780
for p in ass:
781
try:
782
i = min_ass.index(p)
783
except ValueError:
784
emb.append(p)
785
emb.sort()
786
return emb
787
788
def is_principal(self):
789
r"""
790
Returns ``True`` if the ideal is principal in the ring containing the
791
ideal.
792
793
TODO: Code is naive. Only keeps track of ideal generators as set
794
during initialization of the ideal. (Can the base ring change? See
795
example below.)
796
797
EXAMPLES::
798
799
sage: R = ZZ[x]
800
sage: I = R.ideal(2,x)
801
sage: I.is_principal()
802
Traceback (most recent call last):
803
...
804
NotImplementedError
805
sage: J = R.base_extend(QQ).ideal(2,x)
806
sage: J.is_principal()
807
True
808
"""
809
if len(self.gens()) <= 1:
810
return True
811
raise NotImplementedError
812
813
def is_trivial(self):
814
r"""
815
Return True if this ideal is (0) or (1).
816
817
TESTS::
818
819
sage: I = ZZ.ideal(5)
820
sage: I.is_trivial()
821
False
822
823
::
824
825
sage: I = ZZ['x'].ideal(-1)
826
sage: I.is_trivial()
827
True
828
829
::
830
831
sage: I = ZZ['x'].ideal(ZZ['x'].gen()^2)
832
sage: I.is_trivial()
833
False
834
835
::
836
837
sage: I = QQ['x', 'y'].ideal(-5)
838
sage: I.is_trivial()
839
True
840
841
::
842
843
sage: I = CC['x'].ideal(0)
844
sage: I.is_trivial()
845
True
846
"""
847
if self.is_zero():
848
return True
849
# If self is principal, can give a complete answer
850
if self.is_principal():
851
return self.gens()[0].is_unit()
852
# If self is not principal, can only give an affirmative answer
853
for g in self.gens():
854
if g.is_unit():
855
return True
856
raise NotImplementedError
857
858
def category(self):
859
"""
860
Return the category of this ideal.
861
862
EXAMPLES:
863
864
Note that category is dependent on the ring of the
865
ideal.
866
867
::
868
869
sage: I = ZZ.ideal(7)
870
sage: J = ZZ[x].ideal(7,x)
871
sage: K = ZZ[x].ideal(7)
872
sage: I.category()
873
Category of ring ideals in Integer Ring
874
sage: J.category()
875
Category of ring ideals in Univariate Polynomial Ring in x
876
over Integer Ring
877
sage: K.category()
878
Category of ring ideals in Univariate Polynomial Ring in x
879
over Integer Ring
880
"""
881
import sage.categories.all
882
return sage.categories.all.Ideals(self.__ring)
883
884
def __add__(self, other):
885
if not isinstance(other, Ideal_generic):
886
other = self.ring().ideal(other)
887
return self.ring().ideal(self.gens() + other.gens())
888
889
def __radd__(self, other):
890
if not isinstance(other, Ideal_generic):
891
other = self.ring().ideal(other)
892
return self.ring().ideal(self.gens() + other.gens())
893
894
def __mul__(self, other):
895
"""
896
EXAMPLE::
897
898
sage: P.<x,y,z> = QQ[]
899
sage: I = [x*y+y*z,x^2+x*y-y*x-y^2]*P
900
sage: I*2 # indirect doctest
901
Ideal (2*x*y + 2*y*z, 2*x^2 - 2*y^2) of Multivariate Polynomial Ring in x, y, z over Rational Field
902
903
"""
904
if not isinstance(other, Ideal_generic):
905
try:
906
if self.ring().has_coerce_map_from(other):
907
return self
908
except (TypeError,ArithmeticError,ValueError):
909
pass
910
other = self.ring().ideal(other)
911
return self.ring().ideal([z for z in [x*y for x in self.gens() for y in other.gens()] if z])
912
913
def __rmul__(self, other):
914
"""
915
EXAMPLE::
916
917
sage: P.<x,y,z> = QQ[]
918
sage: I = [x*y+y*z,x^2+x*y-y*x-y^2]*P
919
sage: [2]*I # indirect doctest
920
Ideal (2*x*y + 2*y*z, 2*x^2 - 2*y^2) of Multivariate Polynomial Ring in x, y, z over Rational Field
921
922
"""
923
if not isinstance(other, Ideal_generic):
924
try:
925
if self.ring().has_coerce_map_from(other):
926
return self
927
except (TypeError,ArithmeticError,ValueError):
928
pass
929
other = self.ring().ideal(other)
930
return self.ring().ideal([z for z in [y*x for x in self.gens() for y in other.gens()] if z])
931
932
def norm(self):
933
"""
934
Returns the norm of this ideal.
935
936
In the general case, this is just the ideal itself, since the ring it lies in can't be implicitly assumed to be an extension of anything.
937
938
We include this function for compatibility with cases such as ideals in number fields.
939
940
EXAMPLES::
941
942
sage: R.<t> = GF(8, names='a')[]
943
sage: I = R.ideal(t^4 + t + 1)
944
sage: I.norm()
945
Principal ideal (t^4 + t + 1) of Univariate Polynomial Ring in t over Finite Field in a of size 2^3
946
"""
947
return self
948
949
def absolute_norm(self):
950
"""
951
Returns the absolute norm of this ideal.
952
953
In the general case, this is just the ideal itself, since the ring it lies in can't be implicitly assumed to be an extension of anything.
954
955
We include this function for compatibility with cases such as ideals in number fields.
956
957
EXAMPLES::
958
959
sage: R.<t> = GF(9, names='a')[]
960
sage: I = R.ideal(t^4 + t + 1)
961
sage: I.norm()
962
Principal ideal (t^4 + t + 1) of Univariate Polynomial Ring in t over Finite Field in a of size 3^2
963
"""
964
965
class Ideal_principal(Ideal_generic):
966
"""
967
A principal ideal.
968
"""
969
# now Ideal_principal takes a list.
970
#def __init__(self, ring, gen):
971
# Ideal_generic.__init__(self, ring, [gen])
972
973
def __repr__(self):
974
return "Principal ideal (%s) of %s"%(self.gen(), self.ring())
975
976
def is_principal(self):
977
r"""
978
Returns True if the ideal is principal in the ring containing the
979
ideal. When the ideal construction is explicitly principal (i.e.
980
when we define an ideal with one element) this is always the case.
981
982
EXAMPLES:
983
984
Note that Sage automatically coerces ideals into
985
principal ideals during initialization::
986
987
sage: R = ZZ[x]
988
sage: I = R.ideal(x)
989
sage: J = R.ideal(2,x)
990
sage: K = R.base_extend(QQ).ideal(2,x)
991
sage: I
992
Principal ideal (x) of Univariate Polynomial Ring in x
993
over Integer Ring
994
sage: J
995
Ideal (2, x) of Univariate Polynomial Ring in x over Integer Ring
996
sage: K
997
Principal ideal (1) of Univariate Polynomial Ring in x
998
over Rational Field
999
sage: I.is_principal()
1000
True
1001
sage: K.is_principal()
1002
True
1003
"""
1004
return True
1005
1006
def gen(self):
1007
r"""
1008
Returns the generator of the principal ideal. The generators are
1009
elements of the ring containing the ideal.
1010
1011
EXAMPLES:
1012
1013
A simple example in the integers::
1014
1015
sage: R = ZZ
1016
sage: I = R.ideal(7)
1017
sage: J = R.ideal(7, 14)
1018
sage: I.gen(); J.gen()
1019
7
1020
7
1021
1022
Note that the generator belongs to the ring from which the ideal
1023
was initialized::
1024
1025
sage: R = ZZ[x]
1026
sage: I = R.ideal(x)
1027
sage: J = R.base_extend(QQ).ideal(2,x)
1028
sage: a = I.gen(); a
1029
x
1030
sage: b = J.gen(); b
1031
1
1032
sage: a.base_ring()
1033
Integer Ring
1034
sage: b.base_ring()
1035
Rational Field
1036
"""
1037
return self.gens()[0]
1038
1039
def __contains__(self, x):
1040
"""
1041
Returns True if x is in the ideal self.
1042
1043
EXAMPLES::
1044
1045
sage: P.<x> = PolynomialRing(ZZ)
1046
sage: I = P.ideal(x^2-2)
1047
sage: x^2 in I
1048
False
1049
sage: x^2-2 in I
1050
True
1051
sage: x^2-3 in I
1052
False
1053
"""
1054
if self.gen().is_zero():
1055
return x.is_zero()
1056
return self.gen().divides(x)
1057
1058
def __cmp__(self, other):
1059
if not isinstance(other, Ideal_generic):
1060
other = self.ring().ideal(other)
1061
1062
if not other.is_principal():
1063
return -1
1064
1065
if self.is_zero():
1066
if not other.is_zero():
1067
return -1
1068
return 0
1069
1070
# is other.gen() / self.gen() a unit in the base ring?
1071
g0 = other.gen()
1072
g1 = self.gen()
1073
if g0.divides(g1) and g1.divides(g0):
1074
return 0
1075
return 1
1076
1077
def divides(self, other):
1078
"""
1079
Returns True if self divides other.
1080
1081
EXAMPLES::
1082
1083
sage: P.<x> = PolynomialRing(QQ)
1084
sage: I = P.ideal(x)
1085
sage: J = P.ideal(x^2)
1086
sage: I.divides(J)
1087
True
1088
sage: J.divides(I)
1089
False
1090
"""
1091
if isinstance(other, Ideal_principal):
1092
return self.gen().divides(other.gen())
1093
raise NotImplementedError
1094
1095
class Ideal_pid(Ideal_principal):
1096
"""
1097
An ideal of a principal ideal domain.
1098
"""
1099
def __init__(self, ring, gen):
1100
Ideal_principal.__init__(self, ring, gen)
1101
1102
def __add__(self, other):
1103
if not isinstance(other, Ideal_generic):
1104
other = self.ring().ideal(other)
1105
return self.ring().ideal(self.gcd(other))
1106
1107
def reduce(self, f):
1108
"""
1109
Return the reduction of f modulo self.
1110
1111
EXAMPLES::
1112
1113
sage: I = 8*ZZ
1114
sage: I.reduce(10)
1115
2
1116
sage: n = 10; n.mod(I)
1117
2
1118
"""
1119
f = self.ring()(f)
1120
if self.gen() == 0:
1121
return f
1122
q, r = f.quo_rem(self.gen())
1123
return r
1124
1125
def gcd(self, other):
1126
r"""
1127
Returns the greatest common divisor of the principal ideal with the
1128
ideal ``other``; that is, the largest principal ideal
1129
contained in both the ideal and ``other``
1130
1131
TODO: This is not implemented in the case when
1132
``other`` is neither principal nor when the generator
1133
of ``self`` is contained in ``other``.
1134
Also, it seems that this class is used only in PIDs--is this
1135
redundant? Note: second example is broken.
1136
1137
EXAMPLES:
1138
1139
An example in the principal ideal domain ZZ::
1140
1141
sage: R = ZZ
1142
sage: I = R.ideal(42)
1143
sage: J = R.ideal(70)
1144
sage: I.gcd(J)
1145
Principal ideal (14) of Integer Ring
1146
sage: J.gcd(I)
1147
Principal ideal (14) of Integer Ring
1148
1149
TESTS:
1150
1151
We cannot take the gcd of a principal ideal with a
1152
non-principal ideal as well: ( gcd(I,J) should be (7) )
1153
1154
::
1155
1156
sage: I = ZZ.ideal(7)
1157
sage: J = ZZ[x].ideal(7,x)
1158
sage: I.gcd(J)
1159
Traceback (most recent call last):
1160
...
1161
NotImplementedError
1162
sage: J.gcd(I)
1163
Traceback (most recent call last):
1164
...
1165
AttributeError: 'Ideal_generic' object has no attribute 'gcd'
1166
1167
Note::
1168
1169
sage: type(I)
1170
<class 'sage.rings.ideal.Ideal_pid'>
1171
sage: type(J)
1172
<class 'sage.rings.ideal.Ideal_generic'>
1173
"""
1174
if isinstance(other, Ideal_principal):
1175
return self.ring().ideal(self.gen().gcd(other.gen()))
1176
elif self.gen() in other:
1177
return other
1178
else:
1179
raise NotImplementedError
1180
1181
def is_prime(self):
1182
"""
1183
Returns True if the ideal is prime. This relies on the ring
1184
elements having a method is_irreducible() implemented, since an
1185
ideal (a) is prime iff a is irreducible (or 0)
1186
1187
EXAMPLES::
1188
1189
sage: ZZ.ideal(2).is_prime()
1190
True
1191
sage: ZZ.ideal(-2).is_prime()
1192
True
1193
sage: ZZ.ideal(4).is_prime()
1194
False
1195
sage: ZZ.ideal(0).is_prime()
1196
True
1197
sage: R.<x>=QQ[]
1198
sage: P=R.ideal(x^2+1); P
1199
Principal ideal (x^2 + 1) of Univariate Polynomial Ring in x over Rational Field
1200
sage: P.is_prime()
1201
True
1202
"""
1203
if self.is_zero(): # PIDs are integral domains by definition
1204
return True
1205
g = self.gen()
1206
if hasattr(g, 'is_irreducible'):
1207
return g.is_irreducible()
1208
1209
raise NotImplementedError
1210
1211
def is_maximal(self):
1212
"""
1213
Returns whether this ideal is maximal.
1214
1215
Principal ideal domains have Krull dimension 1 (or 0), so an ideal is maximal if and only if it's prime (and nonzero if the ring is not a field).
1216
1217
EXAMPLES::
1218
1219
sage: R.<t> = GF(5)[]
1220
sage: p = R.ideal(t^2 + 2)
1221
sage: p.is_maximal()
1222
True
1223
sage: p = R.ideal(t^2 + 1)
1224
sage: p.is_maximal()
1225
False
1226
sage: p = R.ideal(0)
1227
sage: p.is_maximal()
1228
False
1229
sage: p = R.ideal(1)
1230
sage: p.is_maximal()
1231
False
1232
"""
1233
if not self.ring().is_field() and self.is_zero():
1234
return False
1235
return self.is_prime()
1236
1237
def residue_field(self):
1238
"""
1239
Return the residue class field of this ideal, which must be prime.
1240
1241
TODO: Implement this for more general rings. Currently only defined
1242
for ZZ and for number field orders.
1243
1244
EXAMPLES::
1245
1246
sage: P = ZZ.ideal(61); P
1247
Principal ideal (61) of Integer Ring
1248
sage: F = P.residue_field(); F
1249
Residue field of Integers modulo 61
1250
sage: pi = F.reduction_map(); pi
1251
Partially defined reduction map:
1252
From: Rational Field
1253
To: Residue field of Integers modulo 61
1254
sage: pi(123/234)
1255
6
1256
sage: pi(1/61)
1257
Traceback (most recent call last):
1258
...
1259
ZeroDivisionError: Cannot reduce rational 1/61 modulo 61: it has negative valuation
1260
sage: lift = F.lift_map(); lift
1261
Lifting map:
1262
From: Residue field of Integers modulo 61
1263
To: Integer Ring
1264
sage: lift(F(12345/67890))
1265
33
1266
sage: (12345/67890) % 61
1267
33
1268
1269
TESTS::
1270
1271
sage: ZZ.ideal(96).residue_field()
1272
Traceback (most recent call last):
1273
...
1274
ValueError: The ideal (Principal ideal (96) of Integer Ring) is not prime
1275
1276
::
1277
1278
sage: R.<x>=QQ[]
1279
sage: I=R.ideal(x^2+1)
1280
sage: I.is_prime()
1281
True
1282
sage: I.residue_field()
1283
Traceback (most recent call last):
1284
...
1285
TypeError: residue fields only supported for polynomial rings over finite fields.
1286
"""
1287
if not self.is_prime():
1288
raise ValueError, "The ideal (%s) is not prime"%self
1289
from sage.rings.integer_ring import ZZ
1290
if self.ring() is ZZ:
1291
return ZZ.residue_field(self, check = False)
1292
raise NotImplementedError, "residue_field() is only implemented for ZZ and rings of integers of number fields."
1293
1294
class Ideal_fractional(Ideal_generic):
1295
def __repr__(self):
1296
return "Fractional ideal %s of %s"%(self._repr_short(), self.ring())
1297
1298
1299
1300
# constructors for standard (benchmark) ideals, written uppercase as
1301
# these are constructors
1302
1303
def Cyclic(R, n=None, homog=False, singular=singular_default):
1304
"""
1305
Ideal of cyclic n-roots from 1-st n variables of R if R is
1306
coercible to Singular. If n==None n is set to R.ngens()
1307
1308
INPUT:
1309
1310
1311
- ``R`` - base ring to construct ideal for
1312
1313
- ``n`` - number of cyclic roots (default: None)
1314
1315
- ``homog`` - if True a homogeneous ideal is returned
1316
using the last variable in the ideal (default: False)
1317
1318
- ``singular`` - singular instance to use
1319
1320
1321
.. note::
1322
1323
R will be set as the active ring in Singular
1324
1325
EXAMPLES:
1326
1327
An example from a multivariate polynomial ring over the
1328
rationals::
1329
1330
sage: P.<x,y,z> = PolynomialRing(QQ,3,order='lex')
1331
sage: I = sage.rings.ideal.Cyclic(P)
1332
sage: I
1333
Ideal (x + y + z, x*y + x*z + y*z, x*y*z - 1) of Multivariate Polynomial
1334
Ring in x, y, z over Rational Field
1335
sage: I.groebner_basis()
1336
[x + y + z, y^2 + y*z + z^2, z^3 - 1]
1337
1338
We compute a Groebner basis for cyclic 6, which is a standard
1339
benchmark and test ideal::
1340
1341
sage: R.<x,y,z,t,u,v> = QQ['x,y,z,t,u,v']
1342
sage: I = sage.rings.ideal.Cyclic(R,6)
1343
sage: B = I.groebner_basis()
1344
sage: len(B)
1345
45
1346
"""
1347
from rational_field import RationalField
1348
1349
if n:
1350
if n > R.ngens():
1351
raise ArithmeticError, "n must be <= R.ngens()"
1352
else:
1353
n = R.ngens()
1354
1355
singular.lib("poly")
1356
R2 = R.change_ring(RationalField())
1357
R2._singular_().set_ring()
1358
1359
if not homog:
1360
I = singular.cyclic(n)
1361
else:
1362
I = singular.cyclic(n).homog(R2.gen(n-1))
1363
return R2.ideal(I).change_ring(R)
1364
1365
def Katsura(R, n=None, homog=False, singular=singular_default):
1366
"""
1367
n-th katsura ideal of R if R is coercible to Singular. If n==None n
1368
is set to R.ngens()
1369
1370
INPUT:
1371
1372
1373
- ``R`` - base ring to construct ideal for
1374
1375
- ``n`` - which katsura ideal of R
1376
1377
- ``homog`` - if True a homogeneous ideal is returned
1378
using the last variable in the ideal (default: False)
1379
1380
- ``singular`` - singular instance to use
1381
1382
1383
EXAMPLES::
1384
1385
sage: P.<x,y,z> = PolynomialRing(QQ,3)
1386
sage: I = sage.rings.ideal.Katsura(P,3); I
1387
Ideal (x + 2*y + 2*z - 1, x^2 + 2*y^2 + 2*z^2 - x, 2*x*y + 2*y*z - y)
1388
of Multivariate Polynomial Ring in x, y, z over Rational Field
1389
1390
::
1391
1392
sage: Q.<x> = PolynomialRing(QQ,1)
1393
sage: J = sage.rings.ideal.Katsura(Q,1); J
1394
Ideal (x - 1) of Multivariate Polynomial Ring in x over Rational Field
1395
"""
1396
from rational_field import RationalField
1397
if n:
1398
if n > R.ngens():
1399
raise ArithmeticError, "n must be <= R.ngens()."
1400
else:
1401
n = R.ngens()
1402
singular.lib("poly")
1403
R2 = R.change_ring(RationalField())
1404
R2._singular_().set_ring()
1405
1406
if not homog:
1407
I = singular.katsura(n)
1408
else:
1409
I = singular.katsura(n).homog(R2.gen(n-1))
1410
return R2.ideal(I).change_ring(R)
1411
1412
def FieldIdeal(R):
1413
"""
1414
Let q = R.base_ring().order() and (x0,...,x_n) = R.gens() then if
1415
q is finite this constructor returns
1416
1417
`< x_0^q - x_0, ... , x_n^q - x_n >.`
1418
1419
We call this ideal the field ideal and the generators the field
1420
equations.
1421
1422
EXAMPLES:
1423
1424
The Field Ideal generated from the polynomial ring over
1425
two variables in the finite field of size 2::
1426
1427
sage: P.<x,y> = PolynomialRing(GF(2),2)
1428
sage: I = sage.rings.ideal.FieldIdeal(P); I
1429
Ideal (x^2 + x, y^2 + y) of Multivariate Polynomial Ring in x, y over
1430
Finite Field of size 2
1431
1432
Another, similar example::
1433
1434
sage: Q.<x1,x2,x3,x4> = PolynomialRing(GF(2^4,name='alpha'), 4)
1435
sage: J = sage.rings.ideal.FieldIdeal(Q); J
1436
Ideal (x1^16 + x1, x2^16 + x2, x3^16 + x3, x4^16 + x4) of
1437
Multivariate Polynomial Ring in x1, x2, x3, x4 over Finite
1438
Field in alpha of size 2^4
1439
"""
1440
1441
q = R.base_ring().order()
1442
1443
if q is sage.rings.infinity.infinity:
1444
raise TypeError, "Cannot construct field ideal for R.base_ring().order()==infinity"
1445
1446
return R.ideal([x**q - x for x in R.gens() ])
1447
1448