Package sage :: Package rings :: Module ideal
[hide private]
[frames] | no frames]

Source Code for Module sage.rings.ideal

  1  r""" 
  2  Ideals 
  3   
  4  \SAGE provides functionality for computing with ideals.  One can 
  5  create an ideal in any commutative ring $R$ by giving a list of 
  6  generators, using the notation \code{R.ideal([a,b,...])}. 
  7  """ 
  8   
  9  #***************************************************************************** 
 10  #       Copyright (C) 2005 William Stein <wstein@gmail.com> 
 11  # 
 12  #  Distributed under the terms of the GNU General Public License (GPL) 
 13  # 
 14  #    This code is distributed in the hope that it will be useful, 
 15  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 16  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 17  #    General Public License for more details. 
 18  # 
 19  #  The full text of the GPL is available at: 
 20  # 
 21  #                  http://www.gnu.org/licenses/ 
 22  #***************************************************************************** 
 23   
 24  from types import GeneratorType 
 25   
 26  import sage.misc.latex as latex 
 27  import sage.rings.ring 
 28  import sage.rings.principal_ideal_domain 
 29  import commutative_ring 
 30  from sage.structure.sage_object import SageObject 
 31  from sage.structure.element import MonoidElement 
 32  from sage.interfaces.singular import singular as singular_default, is_SingularElement 
 33  import sage.rings.infinity 
 34  from sage.structure.sequence import Sequence 
 35   
 36   
 37   
38 -def Ideal(R, gens=[], coerce=True):
39 r""" 40 Create the ideal in ring with given generators. 41 42 There are some shorthand notations for creating an ideal, in 43 addition to use the Ideal function: 44 45 \begin{verbatim} 46 -- R.ideal(gens, coerce=True) 47 -- gens*R 48 -- R*gens 49 \end{verbatim} 50 51 INPUT: 52 R -- a ring 53 gens -- list of elements 54 coerce -- bool (default: True); whether gens need to be coerced into ring. 55 56 Alternatively, one can also call this function with the syntax 57 Ideal(gens) 58 where gens is a nonempty list of generators or a single generator. 59 60 OUTPUT: 61 The ideal of ring generated by gens. 62 63 EXAMPLES: 64 sage: R, x = PolynomialRing(ZZ, 'x').objgen() 65 sage: I = R.ideal([4 + 3*x + x^2, 1 + x^2]) 66 sage: I 67 Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring 68 sage: Ideal(R, [4 + 3*x + x^2, 1 + x^2]) 69 Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring 70 sage: Ideal((4 + 3*x + x^2, 1 + x^2)) 71 Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring 72 73 sage: ideal(x^2-2*x+1, x^2-1) 74 Ideal (x^2 - 1, x^2 - 2*x + 1) of Univariate Polynomial Ring in x over Integer Ring 75 sage: ideal([x^2-2*x+1, x^2-1]) 76 Ideal (x^2 - 1, x^2 - 2*x + 1) of Univariate Polynomial Ring in x over Integer Ring 77 sage: l = [x^2-2*x+1, x^2-1] 78 sage: ideal(f^2 for f in l) 79 Ideal (x^4 - 4*x^3 + 6*x^2 - 4*x + 1, x^4 - 2*x^2 + 1) of 80 Univariate Polynomial Ring in x over Integer Ring 81 82 This example illustrates how \SAGE finds a common ambient ring for 83 the ideal, even though 1 is in the integers (in this case). 84 85 sage: R.<t> = ZZ['t'] 86 sage: i = ideal(1,t,t^2) 87 sage: i 88 Ideal (t^2, 1, t) of Univariate Polynomial Ring in t over Integer Ring 89 sage: ideal(1/2,t,t^2) 90 Principal ideal (1) of Univariate Polynomial Ring in t over Rational Field 91 92 TESTS: 93 sage: R, x = PolynomialRing(ZZ, 'x').objgen() 94 sage: I = R.ideal([4 + 3*x + x^2, 1 + x^2]) 95 sage: I == loads(dumps(I)) 96 True 97 98 sage: I = Ideal(R, [4 + 3*x + x^2, 1 + x^2]) 99 sage: I == loads(dumps(I)) 100 True 101 102 sage: I = Ideal((4 + 3*x + x^2, 1 + x^2)) 103 sage: I == loads(dumps(I)) 104 True 105 106 """ 107 if isinstance(R, Ideal_generic): 108 return Ideal(R.ring(), R.gens()) 109 110 if isinstance(R, tuple) and len(R) == 1 and isinstance(R[0], GeneratorType): 111 R = Sequence(R[0]) 112 if not isinstance(R.universe(), sage.rings.ring.Ring): 113 raise TypeError, "unable to find common ring into which all ideal generators map" 114 return R[0].parent().ideal(R) 115 116 if isinstance(R, (list, tuple, GeneratorType)) and len(R) > 0: 117 R = Sequence(R) 118 if not isinstance(R.universe(), sage.rings.ring.Ring): 119 raise TypeError, "unable to find common ring into which all ideal generators map" 120 return R[0].parent().ideal(R) 121 122 if not isinstance(R, sage.rings.ring.Ring): 123 try: 124 S = R.parent() 125 except AttributeError: 126 raise TypeError, "ring must be a ring, list, or element." 127 if isinstance(S, sage.rings.ring.Ring): 128 return Ideal(S, [R]) 129 else: 130 raise TypeError, "ring must be a ring, list, or element." 131 132 if not commutative_ring.is_CommutativeRing(R): 133 raise TypeError, "R must be a commutative ring" 134 135 if isinstance(gens, Ideal_generic): 136 gens = gens.gens() 137 138 if not isinstance(gens, (list, tuple)): 139 gens = [R(gens)] 140 coerce = False 141 142 elif len(gens) == 0: 143 gens = [R(0)] 144 coerce = False 145 146 if coerce: 147 gens = [R(g) for g in gens] 148 149 gens = list(set(gens)) 150 if isinstance(R, sage.rings.principal_ideal_domain.PrincipalIdealDomain): 151 # Use GCD algorithm to obtain a principal ideal 152 g = gens[0] 153 for h in gens[1:]: 154 g = R.gcd(g, h) 155 return Ideal_pid(R, g) 156 157 if len(gens) == 1: 158 return Ideal_principal(R, gens[0]) 159 160 return Ideal_generic(R, gens, coerce=False)
161
162 -def is_Ideal(x):
163 r""" 164 Returns True if object is an ideal of a ring. 165 166 EXAMPLES: 167 A simple example involving the ring of integers. Note that SAGE does 168 not interpret rings objects themselves as ideals. However, one can 169 still explicitly construct these ideals: 170 sage: R = ZZ 171 sage: is_Ideal(R) 172 False 173 sage: 1*R; is_Ideal(1*R) 174 Principal ideal (1) of Integer Ring 175 True 176 sage: 0*R; is_Ideal(0*R) 177 Principal ideal (0) of Integer Ring 178 True 179 180 Sage recognizes ideals of polynomial rings as well: 181 sage: R = PolynomialRing(QQ, 'x'); x = R.gen() 182 sage: I = R.ideal(x^2 + 1); I 183 Principal ideal (x^2 + 1) of Univariate Polynomial Ring in x over Rational Field 184 sage: is_Ideal(I) 185 True 186 sage: is_Ideal((x^2 + 1)*R) 187 True 188 189 """ 190 return isinstance(x, Ideal_generic)
191 192
193 -class Ideal_generic(MonoidElement):
194 """ 195 An ideal. 196 """
197 - def __init__(self, ring, gens, coerce=True):
198 self.__ring = ring 199 if not isinstance(gens, (list, tuple)): 200 gens = [gens] 201 if coerce: 202 gens = [ring(x) for x in gens] 203 204 gens = tuple(gens) 205 if len(gens)==0: gens=(ring.zero_element(),) 206 self.__gens = gens 207 MonoidElement.