def _coerce_map_from_(self, R): r""" Return a coerce map from ``R`` to this field. EXAMPLES:: sage: R.<t> = GF(5)[] sage: K = R.fraction_field() sage: L = K.function_field() sage: f = K.coerce_map_from(L); f # indirect doctest Isomorphism: From: Rational function field in t over Finite Field of size 5 To: Fraction Field of Univariate Polynomial Ring in t over Finite Field of size 5 sage: f(~L.gen()) 1/t """ from sage.rings.function_field.function_field import RationalFunctionField if isinstance(R, RationalFunctionField) and self.variable_name( ) == R.variable_name() and self.base_ring() is R.constant_base_field(): from sage.categories.all import Hom parent = Hom(R, self) from sage.rings.function_field.maps import FunctionFieldToFractionField return parent.__make_element_class__(FunctionFieldToFractionField)( parent) return super(FractionField_1poly_field, self)._coerce_map_from_(R)
def section(self): r""" Return a section of this map. EXAMPLES:: sage: R.<x> = QQ[] sage: R.fraction_field().coerce_map_from(R).section() Section map: From: Fraction Field of Univariate Polynomial Ring in x over Rational Field To: Univariate Polynomial Ring in x over Rational Field """ from sage.categories.sets_with_partial_maps import SetsWithPartialMaps from sage.all import Hom parent = Hom(self.codomain(), self.domain(), SetsWithPartialMaps()) return parent.__make_element_class__(FractionFieldEmbeddingSection)(self)
def _coerce_map_from_(self, S): """ Return ``True`` if elements of ``S`` can be coerced into this fraction field. This fraction field has coercions from: - itself - any fraction field where the base ring coerces to the base ring of this fraction field - any ring that coerces to the base ring of this fraction field EXAMPLES:: sage: F = QQ['x,y'].fraction_field() sage: F.has_coerce_map_from(F) # indirect doctest True :: sage: F.has_coerce_map_from(ZZ['x,y'].fraction_field()) True :: sage: F.has_coerce_map_from(ZZ['x,y,z'].fraction_field()) False :: sage: F.has_coerce_map_from(ZZ) True Test coercions:: sage: F.coerce(1) 1 sage: F.coerce(int(1)) 1 sage: F.coerce(1/2) 1/2 :: sage: K = ZZ['x,y'].fraction_field() sage: x,y = K.gens() sage: F.coerce(F.gen()) x sage: F.coerce(x) x sage: F.coerce(x/y) x/y sage: L = ZZ['x'].fraction_field() sage: K.coerce(L.gen()) x We demonstrate that :trac:`7958` is resolved in the case of number fields:: sage: _.<x> = ZZ[] sage: K.<a> = NumberField(x^5-3*x^4+2424*x^3+2*x-232) sage: R = K.ring_of_integers() sage: S.<y> = R[] sage: F = FractionField(S) sage: F(1/a) (a^4 - 3*a^3 + 2424*a^2 + 2)/232 Some corner cases have been known to fail in the past (:trac:`5917`):: sage: F1 = FractionField( QQ['a'] ) sage: R12 = F1['x','y'] sage: R12('a') a sage: F1(R12(F1('a'))) a sage: F2 = FractionField( QQ['a','b'] ) sage: R22 = F2['x','y'] sage: R22('a') a sage: F2(R22(F2('a'))) a Coercion from Laurent polynomials now works (:trac:`15345`):: sage: R = LaurentPolynomialRing(ZZ, 'x') sage: T = PolynomialRing(ZZ, 'x') sage: R.gen() + FractionField(T).gen() 2*x sage: 1/(R.gen() + 1) 1/(x + 1) sage: R = LaurentPolynomialRing(ZZ, 'x,y') sage: FF = FractionField(PolynomialRing(ZZ, 'x,y')) sage: prod(R.gens()) + prod(FF.gens()) 2*x*y sage: 1/(R.gen(0) + R.gen(1)) 1/(x + y) Coercion from a localization:: sage: R.<x> = ZZ[] sage: L = Localization(R, (x**2 + 1,7)) sage: F = L.fraction_field() sage: f = F.coerce_map_from(L); f Coercion map: From: Univariate Polynomial Ring in x over Integer Ring localized at (7, x^2 + 1) To: Fraction Field of Univariate Polynomial Ring in x over Integer Ring sage: f(L(1/7)) == 1/7 True """ from sage.rings.rational_field import QQ from sage.rings.number_field.number_field_base import NumberField from sage.rings.polynomial.laurent_polynomial_ring import \ LaurentPolynomialRing_generic if S is self._R: parent = self._R.Hom(self) return parent.__make_element_class__(FractionFieldEmbedding)( self._R, self, category=parent.homset_category()) def wrapper(x): return self._element_class(self, x.numerator(), x.denominator()) # The case ``S`` being `\QQ` requires special handling since `\QQ` is # not implemented as a ``FractionField_generic``. if S is QQ and self._R.has_coerce_map_from(ZZ): return CallableConvertMap(S, self, wrapper, parent_as_first_arg=False) # special treatment for localizations from sage.rings.localization import Localization if isinstance(S, Localization): parent = S.Hom(self) return parent.__make_element_class__(FractionFieldEmbedding)( S, self, category=parent.homset_category()) # Number fields also need to be handled separately. if isinstance(S, NumberField): return CallableConvertMap( S, self, self._number_field_to_frac_of_ring_of_integers, parent_as_first_arg=False) # special treatment for LaurentPolynomialRings if isinstance(S, LaurentPolynomialRing_generic): def converter(x, y=None): if y is None: return self._element_class(self, *x._fraction_pair()) xnum, xden = x._fraction_pair() ynum, yden = y._fraction_pair() return self._element_class(self, xnum * yden, xden * ynum) return CallableConvertMap(S, self, converter, parent_as_first_arg=False) if (isinstance(S, FractionField_generic) and self._R.has_coerce_map_from(S.ring())): return CallableConvertMap(S, self, wrapper, parent_as_first_arg=False) if self._R.has_coerce_map_from(S): return CallableConvertMap(S, self, self._element_class, parent_as_first_arg=True) return None