Ejemplo n.º 1
0
    def _coerce_impl(self, x):
        """
        Check to see if we can coerce ``x`` into a homomorphism with the
        correct rings.

        EXAMPLES::

            sage: H = Hom(ZZ, QQ)
            sage: phi = H([1])
            sage: H2 = Hom(QQ, QQ)
            sage: phi2 = H2(phi); phi2 # indirect doctest
            Ring endomorphism of Rational Field
              Defn: 1 |--> 1
            sage: H(phi2) # indirect doctest
            Ring morphism:
              From: Integer Ring
              To:   Rational Field
              Defn: 1 |--> 1
        """
        if not isinstance(x, morphism.RingHomomorphism):
            raise TypeError
        if x.parent() is self:
            return x
        # Case 1: the parent fits
        if x.parent() == self:
            if isinstance(x, morphism.RingHomomorphism_im_gens):
                return morphism.RingHomomorphism_im_gens(self, x.im_gens())
            elif isinstance(x, morphism.RingHomomorphism_cover):
                return morphism.RingHomomorphism_cover(self)
            elif isinstance(x, morphism.RingHomomorphism_from_base):
                return morphism.RingHomomorphism_from_base(
                    self, x.underlying_map())
        # Case 2: unique extension via fraction field
        try:
            if isinstance(x, morphism.RingHomomorphism_im_gens) and x.domain(
            ).fraction_field().has_coerce_map_from(self.domain()):
                return morphism.RingHomomorphism_im_gens(self, x.im_gens())
        except StandardError:
            pass
        # Case 3: the homomorphism can be extended by coercion
        try:
            return x.extend_codomain(self.codomain()).extend_domain(
                self.domain())
        except StandardError:
            pass
        # Last resort, case 4: the homomorphism is induced from the base ring
        if self.domain() == self.domain().base() or self.codomain(
        ) == self.codomain().base():
            raise TypeError
        try:
            x = self.domain().base().Hom(self.codomain().base())(x)
            return morphism.RingHomomorphism_from_base(self, x)
        except StandardError:
            raise TypeError
Ejemplo n.º 2
0
    def __call__(self, im_gens, check=True):
        """
        Create a homomorphism.

        EXAMPLES::

            sage: H = Hom(ZZ, QQ)
            sage: H([1])
            Ring morphism:
              From: Integer Ring
              To:   Rational Field
              Defn: 1 |--> 1

        TESTS::

            sage: H = Hom(ZZ, QQ)
            sage: H == loads(dumps(H))
            True
        """
        if isinstance(im_gens, (morphism.RingHomomorphism_im_gens,
                                morphism.RingHomomorphism_cover,
                                morphism.RingHomomorphism_from_base)):
            return self._coerce_impl(im_gens)
        try:
            return morphism.RingHomomorphism_im_gens(self,
                                                     im_gens,
                                                     check=check)
        except (NotImplementedError, ValueError), err:
            try:
                return self._coerce_impl(im_gens)
            except TypeError:
                raise TypeError, "images do not define a valid homomorphism"
Ejemplo n.º 3
0
 def __call__(self, im_gens, check=True):
     """
     EXAMPLES::
     
         sage: H = Hom(ZZ, QQ)
         sage: phi = H([])
         Traceback (most recent call last):
         ...
         TypeError: images do not define a valid homomorphism
     
     TESTS::
     
         sage: H = Hom(ZZ, QQ)
         sage: H == loads(dumps(H))
         True
     """
     if isinstance(im_gens, (morphism.RingHomomorphism_im_gens,
                             morphism.RingHomomorphism_cover,
                             morphism.RingHomomorphism_from_base)):
         return self._coerce_impl(im_gens)
     try:
         return morphism.RingHomomorphism_im_gens(self,
                                                  im_gens,
                                                  check=check)
     except (NotImplementedError, ValueError), err:
         try:
             return self._coerce_impl(im_gens)
         except TypeError:
             raise TypeError, "images do not define a valid homomorphism"
Ejemplo n.º 4
0
 def _coerce_impl(self, x):
     if not isinstance(x, morphism.RingHomomorphism):
         raise TypeError
     if x.parent() is self:
         return x
     # Case 1: the parent fits
     if x.parent() == self:
         if isinstance(x, morphism.RingHomomorphism_im_gens):
             return morphism.RingHomomorphism_im_gens(self, x.im_gens())
         elif isinstance(x, morphism.RingHomomorphism_cover):
             return morphism.RingHomomorphism_cover(self)
         elif isinstance(x, morphism.RingHomomorphism_from_base):
             return morphism.RingHomomorphism_from_base(
                 self, x.underlying_map())
     # Case 2: unique extension via fraction field
     try:
         if isinstance(x, morphism.RingHomomorphism_im_gens) and x.domain(
         ).fraction_field().has_coerce_map_from(self.domain()):
             return morphism.RingHomomorphism_im_gens(self, x.im_gens())
     except:
         pass
     # Case 3: the homomorphism can be extended by coercion
     try:
         return x.extend_codomain(self.codomain()).extend_domain(
             self.domain())
     except:
         pass
     # Last resort, case 4: the homomorphism is induced from the base ring
     if self.domain() == self.domain().base() or self.codomain(
     ) == self.codomain().base():
         raise TypeError
     try:
         x = self.domain().base().Hom(self.codomain().base())(x)
         return morphism.RingHomomorphism_from_base(self, x)
     except:
         raise TypeError