Esempio n. 1
0
    def _an_element_(self):
        r"""
        Return an element of this symbolic subring.

        OUTPUT:

        A symbolic expression.

        TESTS::

            sage: from sage.symbolic.subring import SymbolicSubring
            sage: SymbolicSubring(rejecting_variables=('r',)).an_element()
            some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variable r
            sage: SymbolicSubring(rejecting_variables=('some_variable',)).an_element()
            some_some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variable some_variable
            sage: SymbolicSubring(rejecting_variables=('some_some_variable',)).an_element()
            some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variable some_some_variable
            sage: SymbolicSubring(rejecting_variables=('some_variable','some_some_variable')).an_element()
            some_some_some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variables some_some_variable, some_variable
        """
        v = SR.an_element()
        while not self.has_valid_variable(v):
            v = SR('some_' + str(v))
        return self(v)
Esempio n. 2
0
    def create_key_and_extra_args(self,
                                  accepting_variables=None,
                                  rejecting_variables=None,
                                  no_variables=False,
                                  **kwds):
        r"""
        Given the arguments and keyword, create a key that uniquely
        determines this object.

        See :class:`SymbolicSubringFactory` for details.

        TESTS::

            sage: from sage.symbolic.subring import SymbolicSubring
            sage: SymbolicSubring.create_key_and_extra_args()
            Traceback (most recent call last):
            ...
            ValueError: Cannot create a symbolic subring since nothing is specified.
            sage: SymbolicSubring.create_key_and_extra_args(
            ....:     accepting_variables=('a',), rejecting_variables=('r',))
            Traceback (most recent call last):
            ...
            ValueError: Cannot create a symbolic subring since input is ambiguous.
            sage: SymbolicSubring.create_key_and_extra_args(
            ....:     accepting_variables=('a',), no_variables=True)
            Traceback (most recent call last):
            ...
            ValueError: Cannot create a symbolic subring since input is ambiguous.
            sage: SymbolicSubring.create_key_and_extra_args(
            ....:     rejecting_variables=('r',), no_variables=True)
            Traceback (most recent call last):
            ...
            ValueError: Cannot create a symbolic subring since input is ambiguous.
        """
        if accepting_variables is None and \
           rejecting_variables is None and \
           not no_variables:
            raise ValueError('Cannot create a symbolic subring '
                             'since nothing is specified.')
        if accepting_variables is not None and rejecting_variables is not None or \
           rejecting_variables is not None and no_variables or \
           no_variables and accepting_variables is not None:
            raise ValueError('Cannot create a symbolic subring '
                             'since input is ambiguous.')

        if accepting_variables is not None:
            vars = tuple(accepting_variables)
            if vars:
                cls = SymbolicSubringAcceptingVars
            else:
                cls = SymbolicConstantsSubring
        elif rejecting_variables is not None:
            vars = tuple(rejecting_variables)
            cls = SymbolicSubringRejectingVars
        elif no_variables:
            vars = tuple()
            cls = SymbolicConstantsSubring

        vars = tuple(sorted(iter(SR(v) for v in vars), key=str))
        return (cls, vars), kwds
Esempio n. 3
0
    def _an_element_(self):
        r"""
        Return an element of this symbolic subring.

        OUTPUT:

        A symbolic expression.

        TESTS::

            sage: from sage.symbolic.subring import SymbolicSubring
            sage: SymbolicSubring(rejecting_variables=('r',)).an_element()
            some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variable r
            sage: SymbolicSubring(rejecting_variables=('some_variable',)).an_element()
            some_some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variable some_variable
            sage: SymbolicSubring(rejecting_variables=('some_some_variable',)).an_element()
            some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variable some_some_variable
            sage: SymbolicSubring(rejecting_variables=('some_variable','some_some_variable')).an_element()
            some_some_some_variable
            sage: _.parent()
            Symbolic Subring rejecting the variables some_some_variable, some_variable
        """
        v = SR.an_element()
        while not self.has_valid_variable(v):
            v = SR('some_' + str(v))
        return self(v)
Esempio n. 4
0
    def _an_element_(self):
        r"""
        Return an element of this symbolic subring.

        OUTPUT:

        A symbolic expression.

        TESTS::

            sage: from sage.symbolic.subring import SymbolicSubring
            sage: SymbolicSubring(no_variables=True).an_element()
            I*pi*e
            sage: _.parent()
            Symbolic Constants Subring
        """
        return self(SR('I') * SR('pi') * SR('e'))
Esempio n. 5
0
    def has_valid_variable(self, variable):
        r"""
        Return whether the given ``variable`` is valid in this subring.

        INPUT:

        - ``variable`` -- a symbolic variable.

        OUTPUT:

        A boolean.

        EXAMPLES::

            sage: from sage.symbolic.subring import SymbolicSubring
            sage: S = SymbolicSubring(rejecting_variables=('r',))
            sage: S.has_valid_variable('a')
            True
            sage: S.has_valid_variable('r')
            False
            sage: S.has_valid_variable('x')
            True
        """
        return SR(variable) not in self._vars_
Esempio n. 6
0
    def _coerce_map_from_(self, P):
        r"""
        Return whether ``P`` coerces into this symbolic subring.

        INPUT:

        - ``P`` -- a parent.

        OUTPUT:

        A boolean or ``None``.

        TESTS::

            sage: from sage.symbolic.subring import GenericSymbolicSubring
            sage: GenericSymbolicSubring(vars=tuple()).has_coerce_map_from(SR)  # indirect doctest  # not tested see #19231
            False

        ::
            sage: from sage.symbolic.subring import SymbolicSubring
            sage: C = SymbolicSubring(no_variables=True)
            sage: C.has_coerce_map_from(ZZ)  # indirect doctest
            True
            sage: C.has_coerce_map_from(QQ)  # indirect doctest
            True
            sage: C.has_coerce_map_from(RR)  # indirect doctest
            True
            sage: C.has_coerce_map_from(RIF)  # indirect doctest
            True
            sage: C.has_coerce_map_from(CC)  # indirect doctest
            True
            sage: C.has_coerce_map_from(CIF)  # indirect doctest
            True
            sage: C.has_coerce_map_from(AA)  # indirect doctest
            True
            sage: C.has_coerce_map_from(QQbar)  # indirect doctest
            True
            sage: C.has_coerce_map_from(SR)  # indirect doctest
            False
        """
        if P == SR:
            # Workaround; can be deleted once #19231 is fixed
            return False

        from sage.rings.real_mpfr import mpfr_prec_min
        from sage.rings.all import (ComplexField, RLF, CLF, AA, QQbar,
                                    InfinityRing)
        from sage.rings.real_mpfi import is_RealIntervalField
        from sage.rings.complex_interval_field import is_ComplexIntervalField

        if isinstance(P, type):
            return SR._coerce_map_from_(P)

        elif RLF.has_coerce_map_from(P) or \
             CLF.has_coerce_map_from(P) or \
             AA.has_coerce_map_from(P) or \
             QQbar.has_coerce_map_from(P):
            return True

        elif (P is InfinityRing or is_RealIntervalField(P)
              or is_ComplexIntervalField(P)):
            return True

        elif ComplexField(mpfr_prec_min()).has_coerce_map_from(P):
            return P not in (RLF, CLF, AA, QQbar)
Esempio n. 7
0
    def _coerce_map_from_(self, P):
        r"""
        Return whether ``P`` coerces into this symbolic subring.

        INPUT:

        - ``P`` -- a parent.

        OUTPUT:

        A boolean or ``None``.

        TESTS::

            sage: from sage.symbolic.subring import GenericSymbolicSubring
            sage: GenericSymbolicSubring(vars=tuple()).has_coerce_map_from(SR)  # indirect doctest  # not tested see #19231
            False

        ::
            sage: from sage.symbolic.subring import SymbolicSubring
            sage: C = SymbolicSubring(no_variables=True)
            sage: C.has_coerce_map_from(ZZ)  # indirect doctest
            True
            sage: C.has_coerce_map_from(QQ)  # indirect doctest
            True
            sage: C.has_coerce_map_from(RR)  # indirect doctest
            True
            sage: C.has_coerce_map_from(RIF)  # indirect doctest
            True
            sage: C.has_coerce_map_from(CC)  # indirect doctest
            True
            sage: C.has_coerce_map_from(CIF)  # indirect doctest
            True
            sage: C.has_coerce_map_from(AA)  # indirect doctest
            True
            sage: C.has_coerce_map_from(QQbar)  # indirect doctest
            True
            sage: C.has_coerce_map_from(SR)  # indirect doctest
            False
        """
        if P == SR:
            # Workaround; can be deleted once #19231 is fixed
            return False

        from sage.rings.real_mpfr import mpfr_prec_min
        from sage.rings.all import (ComplexField,
                                    RLF, CLF, AA, QQbar, InfinityRing)
        from sage.rings.real_mpfi import is_RealIntervalField
        from sage.rings.complex_interval_field import is_ComplexIntervalField

        if isinstance(P, type):
            return SR._coerce_map_from_(P)

        elif RLF.has_coerce_map_from(P) or \
             CLF.has_coerce_map_from(P) or \
             AA.has_coerce_map_from(P) or \
             QQbar.has_coerce_map_from(P):
            return True

        elif (P is InfinityRing or
              is_RealIntervalField(P) or is_ComplexIntervalField(P)):
            return True

        elif ComplexField(mpfr_prec_min()).has_coerce_map_from(P):
            return P not in (RLF, CLF, AA, QQbar)