def _coerce_map_from_(self, other): """ TESTS:: sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_basicmonoids import * sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_module import * sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_element import * sage: from psage.modform.fourier_expansion_framework.gradedexpansions.gradedexpansion_grading import DegreeGrading sage: from psage.modform.fourier_expansion_framework.gradedexpansions.gradedexpansion_module import * sage: m = FreeModule(QQ, 3) sage: mpsm = MonoidPowerSeriesModule(m, NNMonoid(False)) sage: mps = mpsm.base_ring() sage: ger = GradedExpansionModule_class(Sequence([MonoidPowerSeries(mps, {1: 1}, mps.monoid().filter(4))]), Sequence([MonoidPowerSeries(mpsm, {1: m([1,1,1]), 2: m([1,3,-3])}, mpsm.monoid().filter(4))]), PolynomialRing(QQ, ['a', 'b']).ideal(0), DegreeGrading((1,2))) sage: ger._coerce_map_from_(ZZ) """ if other is self.relations().ring(): from sage.structure.coerce_maps import CallableConvertMap return CallableConvertMap(other, self, self._element_constructor_) if isinstance(other, GradedExpansionSubmodule_abstract): if other.graded_ambient() is self \ or self.has_coerce_map_from(other.graded_ambient()) : from sage.structure.coerce_maps import CallableConvertMap return CallableConvertMap( other, self, other._graded_expansion_submodule_to_graded_ambient_) return Module._coerce_map_from_(self, other)
def _coerce_map_from_(self, R): """ EXAMPLES:: sage: L.<x,y> = LaurentPolynomialRing(QQ) sage: L.coerce_map_from(QQ) Composite map: From: Rational Field To: Multivariate Laurent Polynomial Ring in x, y over Rational Field Defn: Polynomial base injection morphism: From: Rational Field To: Multivariate Polynomial Ring in x, y over Rational Field then Call morphism: From: Multivariate Polynomial Ring in x, y over Rational Field To: Multivariate Laurent Polynomial Ring in x, y over Rational Field """ if R is self._R: from sage.structure.coerce_maps import CallableConvertMap return CallableConvertMap(R, self, self._element_constructor_, parent_as_first_arg=False) else: f = self._R.coerce_map_from(R) if f is not None: from sage.categories.homset import Hom from sage.categories.morphism import CallMorphism return CallMorphism(Hom(self._R, self)) * f
def _coerce_map_from_(self, S): """ Return a coerce map from ``S``. EXAMPLES:: sage: f = QQ.coerce_map_from(ZZ); f # indirect doctest Natural morphism: From: Integer Ring To: Rational Field sage: f(3) 3 sage: f(3^99) - 3^99 0 sage: f = QQ.coerce_map_from(int); f # indirect doctest Native morphism: From: Set of Python objects of class 'int' To: Rational Field sage: f(44) 44 :: sage: QQ.coerce_map_from(long) # indirect doctest py2 Native morphism: From: Set of Python objects of class 'long' To: Rational Field :: sage: L = Localization(ZZ, (3,5)) sage: 1/45 in L # indirect doctest True sage: 1/43 in L # indirect doctest False """ global ZZ from . import rational if ZZ is None: from . import integer_ring ZZ = integer_ring.ZZ if S is ZZ: return rational.Z_to_Q() elif S is _long_type: return rational.long_to_Q() elif S is int: return rational.int_to_Q() elif ZZ.has_coerce_map_from(S): return rational.Z_to_Q() * ZZ._internal_coerce_map_from(S) from sage.rings.localization import Localization if isinstance(S, Localization): if S.fraction_field() is self: from sage.structure.coerce_maps import CallableConvertMap return CallableConvertMap(S, self, lambda x: x._value, parent_as_first_arg=False)
def _coerce_map_from_(self, other): r""" TESTS:: sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_basicmonoids import * sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_ring import MonoidPowerSeriesRing sage: mps = MonoidPowerSeriesRing(QQ, NNMonoid(False)) sage: mps.has_coerce_map_from( MonoidPowerSeriesRing(ZZ, NNMonoid(False)) ) # indirect doctest True """ if isinstance(other, MonoidPowerSeriesAmbient_abstract): if self.monoid() == other.monoid() and \ self.coefficient_domain().has_coerce_map_from(other.coefficient_domain()) : from sage.structure.coerce_maps import CallableConvertMap return CallableConvertMap(other, self, self._element_constructor_)
def _coerce_map_from_(self, R): """ EXAMPLES:: sage: L.<x,y> = LaurentPolynomialRing(QQ) sage: L.coerce_map_from(QQ) Composite map: From: Rational Field To: Multivariate Laurent Polynomial Ring in x, y over Rational Field Defn: Polynomial base injection morphism: From: Rational Field To: Multivariate Polynomial Ring in x, y over Rational Field then Call morphism: From: Multivariate Polynomial Ring in x, y over Rational Field To: Multivariate Laurent Polynomial Ring in x, y over Rational Field Let us check that coercion between Laurent Polynomials over different base rings works (:trac:`15345`):: sage: R = LaurentPolynomialRing(ZZ, 'x') sage: T = LaurentPolynomialRing(QQ, 'x') sage: R.gen() + 3*T.gen() 4*x """ if R is self._R or (isinstance(R, LaurentPolynomialRing_generic) and self._R.has_coerce_map_from(R._R)): from sage.structure.coerce_maps import CallableConvertMap return CallableConvertMap(R, self, self._element_constructor_, parent_as_first_arg=False) elif isinstance(R, LaurentPolynomialRing_generic) and \ R.variable_names() == self.variable_names() and \ self.base_ring().has_coerce_map_from(R.base_ring()): return True f = self._R.coerce_map_from(R) if f is not None: from sage.categories.homset import Hom from sage.categories.morphism import CallMorphism return CallMorphism(Hom(self._R, self)) * f
def _coerce_map_from_(self, other): """ TESTS:: sage: from psage.modform.fourier_expansion_framework.modularforms.modularform_ambient import * sage: from psage.modform.fourier_expansion_framework.modularforms.modularform_testtype import * sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_basicmonoids import * sage: ma = ModularFormsAmbient( QQ, ModularFormTestType_scalar(), NNFilter(5) ) sage: ma._coerce_map_from_(ma) Conversion via _element_constructor_ map: From: Graded expansion ring with generators g1, g2, g3, g4, g5 To: Graded expansion ring with generators g1, g2, g3, g4, g5 """ from sage.structure.coerce_maps import CallableConvertMap if isinstance(other, ModularFormsRing_generic) and \ self.base_ring().has_coerce_map_from(other.base_ring()) and \ self.type().is_vector_valued() and \ self.type().non_vector_valued() == other.type() : return CallableConvertMap(other, self, self._element_constructor_) return ModularFormsAmbient_abstract._coerce_map_from_(self, other)
def _coerce_map_from_(self, other): r""" TODO: This is a stub. The dream is that representations know about compatible coercions and so would actions and characters. Then every equivariant monoid power series ring would be a functorial construction in all three parameters (The functor would then be applied to a representation within a universe of representations). TESTS:: sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_basicmonoids import * sage: from psage.modform.fourier_expansion_framework.monoidpowerseries.monoidpowerseries_ring import EquivariantMonoidPowerSeriesRing sage: emps = EquivariantMonoidPowerSeriesRing(NNMonoid(True), TrivialCharacterMonoid("1", QQ), TrivialRepresentation("1", QQ)) sage: emps.has_coerce_map_from( EquivariantMonoidPowerSeriesRing(NNMonoid(True), TrivialCharacterMonoid("1", QQ), TrivialRepresentation("1", ZZ)) ) # indirect doctest True """ if isinstance(other, EquivariantMonoidPowerSeriesAmbient_abstract): if self.action() == other.action() and \ self.characters() == other.characters() : if self.representation().extends(other.representation()): from sage.structure.coerce_maps import CallableConvertMap return CallableConvertMap(other, self, self._element_constructor_)
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.<b> = 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) """ from sage.rings.integer_ring import ZZ 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 # 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, \ lambda x: self._element_class(self, x.numerator(), x.denominator()), parent_as_first_arg=False) # 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, \ lambda x: self._element_class(self, x.numerator(), x.denominator()), 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
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.<b> = 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 """ from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ from sage.rings.number_field.number_field_base import NumberField # 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, \ lambda x: self._element_class(self, x.numerator(), x.denominator()), parent_as_first_arg=False) # 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) if isinstance(S, FractionField_generic) and \ self._R.has_coerce_map_from(S.ring()): return CallableConvertMap(S, self, \ lambda x: self._element_class(self, x.numerator(), x.denominator()), 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