Пример #1
0
def repr_short_to_parent(s):
    r"""
    Helper method for the growth group factory, which converts a short
    representation string to a parent.

    INPUT:

    - ``s`` -- a string, short representation of a parent.

    OUTPUT:

    A parent.

    The possible short representations are shown in the examples below.

    EXAMPLES::

        sage: from sage.rings.asymptotic.misc import repr_short_to_parent
        sage: repr_short_to_parent('ZZ')
        Integer Ring
        sage: repr_short_to_parent('QQ')
        Rational Field
        sage: repr_short_to_parent('SR')
        Symbolic Ring
        sage: repr_short_to_parent('NN')
        Non negative integer semiring

    TESTS::

        sage: repr_short_to_parent('abcdef')
        Traceback (most recent call last):
        ...
        ValueError: Cannot create a parent out of 'abcdef'.
        > *previous* NameError: name 'abcdef' is not defined
    """
    from sage.misc.sage_eval import sage_eval
    try:
        P = sage_eval(s)
    except Exception as e:
        raise combine_exceptions(
            ValueError("Cannot create a parent out of '%s'." % (s,)), e)

    from sage.misc.lazy_import import LazyImport
    if type(P) is LazyImport:
        P = P._get_object()

    from sage.structure.parent import is_Parent
    if not is_Parent(P):
        raise ValueError("'%s' does not describe a parent." % (s,))
    return P
Пример #2
0
    def __mul__(self, other):
        r"""
        Composition

        INPUT:
         - ``self`` -- a map `f`
         - ``other`` -- a map `g`

        Returns the composition map `f\circ g` of `f`` and `g`

        EXAMPLES::

            sage: from sage.categories.poor_man_map import PoorManMap
            sage: f = PoorManMap(lambda x: x+1, domain = (1,2,3), codomain = (2,3,4))
            sage: g = PoorManMap(lambda x: -x,  domain = (2,3,4), codomain = (-2,-3,-4))
            sage: g*f
            A map from (1, 2, 3) to (-2, -3, -4)

        Note that the compatibility of the domains and codomains is for performance
        reasons only checked for proper parents. For example, the incompatibility
        is not detected here::
    
            sage: f*g
            A map from (2, 3, 4) to (2, 3, 4)
    
        But it is detected here::
    
            sage: g = PoorManMap(factorial, domain = ZZ, codomain = ZZ)
            sage: h = PoorManMap(sqrt, domain = RR, codomain = CC)
            sage: g*h
            Traceback (most recent call last):
            ...
            ValueError: the codomain Complex Field with 53 bits of precision does not coerce into the domain Integer Ring
            sage: h*g
            A map from Integer Ring to Complex Field with 53 bits of precision
    
        """
        self_domain = self.domain()

        try:
            other_codomain = other.codomain()
        except AttributeError:
            other_codomain = None

        if self_domain is not None and other_codomain is not None:
            from sage.structure.parent import is_Parent
            if is_Parent(self_domain) and is_Parent(other_codomain):
                if not self_domain.has_coerce_map_from(other_codomain):
                    raise ValueError(
                        "the codomain %r does not coerce into the domain %r" %
                        (other_codomain, self_domain))

        codomain = self.codomain()
        try:
            domain = other.domain()
        except AttributeError:
            domain = None

        if isinstance(other, PoorManMap):
            other = other._functions
        else:
            other = (other, )

        return PoorManMap(self._functions + other,
                          domain=domain,
                          codomain=codomain)
Пример #3
0
    def __mul__(self, other):
        """
        Composition

        INPUT:
         - ``self`` -- a map `f`
         - ``other`` -- a map `g`

        Returns the composition map `f\circ g` of `f`` and `g`

        EXAMPLES::

            sage: from sage.categories.poor_man_map import PoorManMap
            sage: f = PoorManMap(lambda x: x+1, domain = (1,2,3), codomain = (2,3,4))
            sage: g = PoorManMap(lambda x: -x,  domain = (2,3,4), codomain = (-2,-3,-4))
            sage: g*f
            A map from (1, 2, 3) to (-2, -3, -4)

        Note that the compatibility of the domains and codomains is for performance
        reasons only checked for proper parents. For example, the incompatibility
        is not detected here::
    
            sage: f*g
            A map from (2, 3, 4) to (2, 3, 4)
    
        But it is detected here::
    
            sage: g = PoorManMap(factorial, domain = ZZ, codomain = ZZ)
            sage: h = PoorManMap(sqrt, domain = RR, codomain = CC)
            sage: g*h
            Traceback (most recent call last):
            ...
            ValueError: the codomain Complex Field with 53 bits of precision does not coerce into the domain Integer Ring
            sage: h*g
            A map from Integer Ring to Complex Field with 53 bits of precision
    
        """
        self_domain = self.domain()

        try:
            other_codomain = other.codomain()
        except AttributeError:
            other_codomain = None

        if self_domain is not None and other_codomain is not None:
            from sage.structure.parent import is_Parent
            if is_Parent(self_domain) and is_Parent(other_codomain):
                if not self_domain.has_coerce_map_from(other_codomain):
                    raise ValueError("the codomain %r does not coerce into the domain %r"%(other_codomain, self_domain))

        codomain = self.codomain()
        try:
            domain = other.domain()
        except AttributeError:
            domain = None

        if isinstance(other, PoorManMap):
            other = other._functions
        else:
            other = (other,)

        return PoorManMap(self._functions + other, domain=domain, codomain=codomain)
Пример #4
0
def repr_short_to_parent(s):
    r"""
    Helper method for the growth group factory, which converts a short
    representation string to a parent.

    INPUT:

    - ``s`` -- a string, short representation of a parent.

    OUTPUT:

    A parent.

    The possible short representations are shown in the examples below.

    EXAMPLES::

        sage: from sage.rings.asymptotic.misc import repr_short_to_parent
        sage: repr_short_to_parent('ZZ')
        Integer Ring
        sage: repr_short_to_parent('QQ')
        Rational Field
        sage: repr_short_to_parent('SR')
        Symbolic Ring
        sage: repr_short_to_parent('NN')
        Non negative integer semiring
        sage: repr_short_to_parent('UU')
        Group of Roots of Unity

    TESTS::

        sage: repr_short_to_parent('abcdef')
        Traceback (most recent call last):
        ...
        ValueError: Cannot create a parent out of 'abcdef'.
        > *previous* ValueError: unknown specification abcdef
        > *and* NameError: name 'abcdef' is not defined
    """
    from sage.groups.misc_gps.argument_groups import ArgumentGroup
    from sage.misc.sage_eval import sage_eval

    def extract(s):
        try:
            return ArgumentGroup(specification=s)
        except Exception as e:
            e_ag = e
            e_ag.__traceback__ = None

        try:
            return sage_eval(s)
        except Exception as e:
            e_se = e
            e_se.__traceback__ = None

        raise combine_exceptions(
            ValueError("Cannot create a parent out of '%s'." % (s,)),
            e_ag, e_se)

    P = extract(s)

    from sage.misc.lazy_import import LazyImport
    if type(P) is LazyImport:
        P = P._get_object()

    from sage.structure.parent import is_Parent
    if not is_Parent(P):
        raise ValueError("'%s' does not describe a parent." % (s,))
    return P