| 1 | """ |
|---|
| 2 | Fraction Field of Integral Domains |
|---|
| 3 | |
|---|
| 4 | AUTHOR: William Stein (with input from David Joyner, David Kohel, and |
|---|
| 5 | Joe Wetherell) |
|---|
| 6 | |
|---|
| 7 | EXAMPLES: |
|---|
| 8 | Quotienting is a constructor for an element of the fraction field: |
|---|
| 9 | sage: R.<x> = QQ[] |
|---|
| 10 | sage: (x^2-1)/(x+1) |
|---|
| 11 | x - 1 |
|---|
| 12 | sage: parent((x^2-1)/(x+1)) |
|---|
| 13 | Fraction Field of Univariate Polynomial Ring in x over Rational Field |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | The GCD is not taken (since it doesn't converge sometimes) in the inexact case. |
|---|
| 17 | sage: Z.<z> = CC[] |
|---|
| 18 | sage: I = CC.gen() |
|---|
| 19 | sage: (1+I+z)/(z+0.1*I) |
|---|
| 20 | (1.00000000000000*z + 1.00000000000000 + 1.00000000000000*I)/(1.00000000000000*z + 0.100000000000000*I) |
|---|
| 21 | sage: (1+I*z)/(z+1.1) |
|---|
| 22 | (1.00000000000000*I*z + 1.00000000000000)/(1.00000000000000*z + 1.10000000000000) |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | TESTS: |
|---|
| 26 | sage: F = FractionField(IntegerRing()) |
|---|
| 27 | sage: F == loads(dumps(F)) |
|---|
| 28 | True |
|---|
| 29 | |
|---|
| 30 | sage: F = FractionField(PolynomialRing(RationalField(),'x')) |
|---|
| 31 | sage: F == loads(dumps(F)) |
|---|
| 32 | True |
|---|
| 33 | |
|---|
| 34 | sage: F = FractionField(PolynomialRing(IntegerRing(),'x')) |
|---|
| 35 | sage: F == loads(dumps(F)) |
|---|
| 36 | True |
|---|
| 37 | |
|---|
| 38 | sage: F = FractionField(MPolynomialRing(RationalField(),2,'x')) |
|---|
| 39 | sage: F == loads(dumps(F)) |
|---|
| 40 | True |
|---|
| 41 | |
|---|
| 42 | """ |
|---|
| 43 | |
|---|
| 44 | #***************************************************************************** |
|---|
| 45 | # |
|---|
| 46 | # SAGE: System for Algebra and Geometry Experimentation |
|---|
| 47 | # |
|---|
| 48 | # Copyright (C) 2005 William Stein <wstein@gmail.com> |
|---|
| 49 | # |
|---|
| 50 | # Distributed under the terms of the GNU General Public License (GPL) |
|---|
| 51 | # |
|---|
| 52 | # This code is distributed in the hope that it will be useful, |
|---|
| 53 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 54 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 55 | # General Public License for more details. |
|---|
| 56 | # |
|---|
| 57 | # The full text of the GPL is available at: |
|---|
| 58 | # |
|---|
| 59 | # http://www.gnu.org/licenses/ |
|---|
| 60 | #***************************************************************************** |
|---|
| 61 | |
|---|
| 62 | import ring |
|---|
| 63 | import integral_domain |
|---|
| 64 | import field |
|---|
| 65 | import fraction_field_element |
|---|
| 66 | import sage.misc.latex as latex |
|---|
| 67 | |
|---|
| 68 | from sage.structure.parent_base import ParentWithBase |
|---|
| 69 | |
|---|
| 70 | def FractionField(R, names=None): |
|---|
| 71 | """ |
|---|
| 72 | Create the fraction field of the integral domain R. |
|---|
| 73 | |
|---|
| 74 | INPUT: |
|---|
| 75 | R -- an integral domain |
|---|
| 76 | names -- ignored |
|---|
| 77 | |
|---|
| 78 | EXAMPLES: |
|---|
| 79 | We create some example fraction fields. |
|---|
| 80 | sage: FractionField(IntegerRing()) |
|---|
| 81 | Rational Field |
|---|
| 82 | sage: FractionField(PolynomialRing(RationalField(),'x')) |
|---|
| 83 | Fraction Field of Univariate Polynomial Ring in x over Rational Field |
|---|
| 84 | sage: FractionField(PolynomialRing(IntegerRing(),'x')) |
|---|
| 85 | Fraction Field of Univariate Polynomial Ring in x over Integer Ring |
|---|
| 86 | sage: FractionField(MPolynomialRing(RationalField(),2,'x')) |
|---|
| 87 | Fraction Field of Multivariate Polynomial Ring in x0, x1 over Rational Field |
|---|
| 88 | |
|---|
| 89 | Dividing elements often implicitly creates elements of the fraction field. |
|---|
| 90 | sage: x = PolynomialRing(RationalField(), 'x').gen() |
|---|
| 91 | sage: f = x/(x+1) |
|---|
| 92 | sage: g = x**3/(x+1) |
|---|
| 93 | sage: f/g |
|---|
| 94 | 1/x^2 |
|---|
| 95 | sage: g/f |
|---|
| 96 | x^2 |
|---|
| 97 | |
|---|
| 98 | The input must be an integral domain. |
|---|
| 99 | sage: Frac(Integers(4)) |
|---|
| 100 | Traceback (most recent call last): |
|---|
| 101 | ... |
|---|
| 102 | TypeError: R must be an integral domain. |
|---|
| 103 | """ |
|---|
| 104 | if not ring.is_Ring(R): |
|---|
| 105 | raise TypeError, "R must be a ring" |
|---|
| 106 | if not R.is_integral_domain(): |
|---|
| 107 | raise TypeError, "R must be an integral domain." |
|---|
| 108 | return R.fraction_field() |
|---|
| 109 | |
|---|
| 110 | def is_FractionField(x): |
|---|
| 111 | return isinstance(x, FractionField_generic) |
|---|
| 112 | |
|---|
| 113 | class FractionField_generic(field.Field): |
|---|
| 114 | """ |
|---|
| 115 | The fraction field of an integral domain. |
|---|
| 116 | """ |
|---|
| 117 | def __init__(self, R): |
|---|
| 118 | """ |
|---|
| 119 | Create the fraction field of the integral domain R. |
|---|
| 120 | |
|---|
| 121 | INPUT: |
|---|
| 122 | R -- an integral domain |
|---|
| 123 | |
|---|
| 124 | EXAMPLES: |
|---|
| 125 | sage: Frac(QQ['x']) |
|---|
| 126 | Fraction Field of Univariate Polynomial Ring in x over Rational Field |
|---|
| 127 | sage: Frac(QQ['x,y']).variable_names() |
|---|
| 128 | ('x', 'y') |
|---|
| 129 | """ |
|---|
| 130 | self.__R = R |
|---|
| 131 | ParentWithBase.__init__(self, R) |
|---|
| 132 | self._assign_names(R._names) |
|---|
| 133 | |
|---|
| 134 | def is_field(self): |
|---|
| 135 | """ |
|---|
| 136 | Returns True, since the fraction field is a field. |
|---|
| 137 | |
|---|
| 138 | EXAMPLES: |
|---|
| 139 | sage: Frac(ZZ).is_field() |
|---|
| 140 | True |
|---|
| 141 | """ |
|---|
| 142 | return True |
|---|
| 143 | |
|---|
| 144 | def base_ring(self): |
|---|
| 145 | """ |
|---|
| 146 | Return the base ring of self; this is the base ring of the ring which |
|---|
| 147 | this fraction field is the fraction field of. |
|---|
| 148 | |
|---|
| 149 | EXAMPLES: |
|---|
| 150 | sage: R = Frac(ZZ['t']) |
|---|
| 151 | sage: R.base_ring() |
|---|
| 152 | Integer Ring |
|---|
| 153 | """ |
|---|
| 154 | return self.__R.base_ring() |
|---|
| 155 | |
|---|
| 156 | def characteristic(self): |
|---|
| 157 | """ |
|---|
| 158 | Return the characteristic of this fraction field. |
|---|
| 159 | |
|---|
| 160 | EXAMPLES: |
|---|
| 161 | sage: R = Frac(ZZ['t']) |
|---|
| 162 | sage: R.base_ring() |
|---|
| 163 | Integer Ring |
|---|
| 164 | sage: R = Frac(ZZ['t']); R.characteristic() |
|---|
| 165 | 0 |
|---|
| 166 | sage: R = Frac(GF(5)['w']); R.characteristic() |
|---|
| 167 | 5 |
|---|
| 168 | """ |
|---|
| 169 | return self.ring().characteristic() |
|---|
| 170 | |
|---|
| 171 | def __repr__(self): |
|---|
| 172 | return "Fraction Field of %s"%self.ring() |
|---|
| 173 | |
|---|
| 174 | def _latex_(self): |
|---|
| 175 | return "\\mbox{\\rm Frac}(%s)"%latex.latex(self.ring()) |
|---|
| 176 | |
|---|
| 177 | def ring(self): |
|---|
| 178 | """ |
|---|
| 179 | Return the ring that this is the fraction field of. |
|---|
| 180 | |
|---|
| 181 | EXAMPLES: |
|---|
| 182 | sage: R = Frac(QQ['x,y']) |
|---|
| 183 | sage: R |
|---|
| 184 | Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field |
|---|
| 185 | sage: R.ring() |
|---|
| 186 | Multivariate Polynomial Ring in x, y over Rational Field |
|---|
| 187 | """ |
|---|
| 188 | return self.__R |
|---|
| 189 | |
|---|
| 190 | def __call__(self, x, y=1, coerce=True): |
|---|
| 191 | if isinstance(x, fraction_field_element.FractionFieldElement): |
|---|
| 192 | if x.parent() is self: |
|---|
| 193 | return x |
|---|
| 194 | elif x.parent() == self: |
|---|
| 195 | return fraction_field_element.FractionFieldElement(self, x.numerator(), x.denominator()) |
|---|
| 196 | else: |
|---|
| 197 | R = self.ring() |
|---|
| 198 | return fraction_field_element.FractionFieldElement(self, R(x.numerator()), R(x.denominator())) |
|---|
| 199 | if coerce: |
|---|
| 200 | R = self.ring() |
|---|
| 201 | x = R(x); y = R(y) |
|---|
| 202 | return fraction_field_element.FractionFieldElement(self, x, y, |
|---|
| 203 | coerce=False, reduce = self.is_exact()) |
|---|
| 204 | |
|---|
| 205 | def is_exact(self): |
|---|
| 206 | """ |
|---|
| 207 | EXAMPLES: |
|---|
| 208 | sage: Z.<z>=CC[] |
|---|
| 209 | sage: Z.is_exact() |
|---|
| 210 | False |
|---|
| 211 | """ |
|---|
| 212 | try: |
|---|
| 213 | return self.__is_exact |
|---|
| 214 | except AttributeError: |
|---|
| 215 | r = self.ring().is_exact() |
|---|
| 216 | self.__is_exact = r |
|---|
| 217 | return r |
|---|
| 218 | |
|---|
| 219 | def construction(self): |
|---|
| 220 | from sage.categories.pushout import FractionField |
|---|
| 221 | return FractionField(), self.ring() |
|---|
| 222 | |
|---|
| 223 | def _coerce_impl(self, x): |
|---|
| 224 | """ |
|---|
| 225 | Return the canonical coercion of x into this fraction field, or raise a TypeError. |
|---|
| 226 | |
|---|
| 227 | The rings that canonically coerce to the fraction field are |
|---|
| 228 | * the fraction field itself |
|---|
| 229 | * any fraction field that of the form Frac(S) where S canonically |
|---|
| 230 | coerces to this ring. |
|---|
| 231 | * any ring that canonically coerces to the ring R such that this |
|---|
| 232 | fraction field is Frac(R) |
|---|
| 233 | |
|---|
| 234 | """ |
|---|
| 235 | try: |
|---|
| 236 | P = x.parent() |
|---|
| 237 | if is_FractionField(P): |
|---|
| 238 | R = P.ring() |
|---|
| 239 | S = self.ring() |
|---|
| 240 | if S.has_coerce_map_from(R): |
|---|
| 241 | return self(x) |
|---|
| 242 | else: |
|---|
| 243 | S = self.ring() |
|---|
| 244 | if S.has_coerce_map_from(P): |
|---|
| 245 | return self(x) |
|---|
| 246 | except AttributeError: |
|---|
| 247 | pass |
|---|
| 248 | return self._coerce_try(x, [self.ring()]) |
|---|
| 249 | |
|---|
| 250 | def __cmp__(self, other): |
|---|
| 251 | if not isinstance(other, FractionField_generic): |
|---|
| 252 | return cmp(type(self), type(other)) |
|---|
| 253 | return cmp(self.ring(), other.ring()) |
|---|
| 254 | |
|---|
| 255 | def ngens(self): |
|---|
| 256 | """ |
|---|
| 257 | This is the same as for the parent object. |
|---|
| 258 | |
|---|
| 259 | EXAMPLES: |
|---|
| 260 | sage: R = Frac(PolynomialRing(QQ,'z',10)); R |
|---|
| 261 | Fraction Field of Multivariate Polynomial Ring in z0, z1, z2, z3, z4, z5, z6, z7, z8, z9 over Rational Field |
|---|
| 262 | sage: R.ngens() |
|---|
| 263 | 10 |
|---|
| 264 | """ |
|---|
| 265 | return self.ring().ngens() |
|---|
| 266 | |
|---|
| 267 | def gen(self, i=0): |
|---|
| 268 | """ |
|---|
| 269 | Return the ith generator of self. |
|---|
| 270 | |
|---|
| 271 | EXAMPLES: |
|---|
| 272 | sage: R = Frac(PolynomialRing(QQ,'z',10)); R |
|---|
| 273 | Fraction Field of Multivariate Polynomial Ring in z0, z1, z2, z3, z4, z5, z6, z7, z8, z9 over Rational Field |
|---|
| 274 | sage: R.0 |
|---|
| 275 | z0 |
|---|
| 276 | sage: R.gen(3) |
|---|
| 277 | z3 |
|---|
| 278 | sage: R.3 |
|---|
| 279 | z3 |
|---|
| 280 | """ |
|---|
| 281 | x = self.ring().gen(i) |
|---|
| 282 | one = self.ring()(1) |
|---|
| 283 | r = fraction_field_element.FractionFieldElement(self, x, one, |
|---|
| 284 | coerce=False, reduce=False) |
|---|
| 285 | return r |
|---|