Пример #1
0
    def assume(self):
        """
        Make this assumption.

        TEST::

            sage: from sage.symbolic.assumptions import GenericDeclaration
            sage: decl = GenericDeclaration(x, 'even')
            sage: decl.assume()
            sage: cos(x*pi).simplify()
            1
            sage: decl2 = GenericDeclaration(x, 'odd')
            sage: decl2.assume()
            Traceback (most recent call last):
            ...
            ValueError: Assumption is inconsistent
            sage: decl.forget()
        """
        from sage.calculus.calculus import maxima
        if self._context is None:
            # We get the list here because features may be added with time.
            valid_features = list(maxima("features"))
            if self._assumption not in [
                    repr(x).strip() for x in list(valid_features)
            ]:
                raise ValueError(
                    "%s not a valid assumption, must be one of %s" %
                    (self._assumption, valid_features))
            cur = maxima.get("context")
            self._context = maxima.newcontext('context' +
                                              maxima._next_var_name())
            try:
                maxima.eval("declare(%s, %s)" %
                            (self._var._maxima_init_(), self._assumption))


#            except TypeError, mess:
#                if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant
#                    raise ValueError, "Assumption is inconsistent"
            except RuntimeError as mess:
                if 'inconsistent' in str(
                        mess
                ):  # note Maxima doesn't tell you if declarations are redundant
                    raise ValueError("Assumption is inconsistent")
                else:
                    raise
            maxima.set("context", cur)

        if not self in _assumptions:
            maxima.activate(self._context)
            _assumptions.append(self)
Пример #2
0
    def assume(self):
        """
        Make this assumption.

        TEST::

            sage: from sage.symbolic.assumptions import GenericDeclaration
            sage: decl = GenericDeclaration(x, 'even')
            sage: decl.assume()
            sage: cos(x*pi).simplify()
            1
            sage: decl2 = GenericDeclaration(x, 'odd')
            sage: decl2.assume()
            Traceback (most recent call last):
            ...
            ValueError: Assumption is inconsistent
            sage: decl.forget()
        """
        from sage.calculus.calculus import maxima
        if self._context is None:
            # We get the list here because features may be added with time.
            valid_features = list(maxima("features"))
            if self._assumption not in [repr(x).strip() for x in list(valid_features)]:
                raise ValueError("%s not a valid assumption, must be one of %s" % (self._assumption, valid_features))
            cur = maxima.get("context")
            self._context = maxima.newcontext('context' + maxima._next_var_name())
            try:
                maxima.eval("declare(%s, %s)" % (self._var._maxima_init_(), self._assumption))
#            except TypeError, mess:
#                if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant
#                    raise ValueError, "Assumption is inconsistent"
            except RuntimeError as mess:
                if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant
                    raise ValueError("Assumption is inconsistent")
                else:
                    raise
            maxima.set("context", cur)

        if not self in _assumptions:
            maxima.activate(self._context)
            _assumptions.append(self)
Пример #3
0
    def assume(self):
        """
        Make this assumption.

        TESTS::

            sage: from sage.symbolic.assumptions import GenericDeclaration
            sage: decl = GenericDeclaration(x, 'even')
            sage: decl.assume()
            sage: cos(x*pi).simplify()
            1
            sage: decl2 = GenericDeclaration(x, 'odd')
            sage: decl2.assume()
            Traceback (most recent call last):
            ...
            ValueError: Assumption is inconsistent
            sage: decl.forget()
        """
        if self in _assumptions:
            return
        from sage.calculus.calculus import maxima
        cur = None
        context = None
        if self._context is None:
            self._validate_feature()
            cur = maxima.get("context")
            # newcontext makes a fresh context that only has $global as
            # a subcontext, and makes it the current $context,
            # but does not deactivate other current contexts.
            context = maxima.newcontext('context' + maxima._next_var_name())
            must_declare = True
        elif not maxima.featurep(self._var, self._assumption):
            # Reactivating a previously active context.
            # Run $declare again with the assumption
            # to catch possible inconsistency
            # with the active contexts.
            cur = maxima.get("context")
            # Redeclaring on the existing context does not seem to trigger
            # inconsistency checking.
            ## maxima.set("context", self._context._maxima_init_())
            # Instead, use a temporary context for this purpose
            context = maxima.newcontext('context' + maxima._next_var_name())
            must_declare = True
        else:
            must_declare = False

        if must_declare:
            try:
                maxima.eval("declare(%s, %s)" %
                            (self._var._maxima_init_(), self._assumption))
            except RuntimeError as mess:
                if 'inconsistent' in str(
                        mess
                ):  # note Maxima doesn't tell you if declarations are redundant
                    # Inconsistency with one of the active contexts.
                    raise ValueError("Assumption is inconsistent")
                else:
                    raise
            else:
                if self._context is None:
                    self._context = context
                    context = None
            finally:
                assert cur is not None
                maxima.set("context", cur)
                if context is not None:
                    maxima.killcontext(context)

        maxima.activate(self._context)
        self._var.decl_assume(self._assumption)
        _assumptions[self] = True
Пример #4
0
class GenericDeclaration(SageObject):

    def __init__(self, var, assumption):
        """
        This class represents generic assumptions, such as a variable being
        an integer or a function being increasing. It passes such
        information to maxima's declare (wrapped in a context so it is able
        to forget).

        INPUT:

        -  ``var`` - the variable about which assumptions are
           being made

        -  ``assumption`` - a Maxima feature, either user
           defined or in the list given by maxima('features')

        EXAMPLES::

            sage: from sage.symbolic.assumptions import GenericDeclaration
            sage: decl = GenericDeclaration(x, 'integer')
            sage: decl.assume()
            sage: sin(x*pi)
            sin(pi*x)
            sage: sin(x*pi).simplify()
            0
            sage: decl.forget()
            sage: sin(x*pi)
            sin(pi*x)

        Here is the list of acceptable features.

        ::

            sage: maxima('features')
            [integer,noninteger,even,odd,rational,irrational,real,imaginary,complex,analytic,increasing,decreasing,oddfun,evenfun,posfun,constant,commutative,lassociative,rassociative,symmetric,antisymmetric,integervalued]
        """
        self._var = var
        self._assumption = assumption
        self._context = None

    def __repr__(self):
        """
        EXAMPLES::

            sage: from sage.symbolic.assumptions import GenericDeclaration
            sage: GenericDeclaration(x, 'foo')
            x is foo
        """
        return "%s is %s" % (self._var, self._assumption)

    def __cmp__(self, other):
        """
        TESTS::

            sage: from sage.symbolic.assumptions import GenericDeclaration as GDecl
            sage: var('y')
            y
            sage: GDecl(x, 'integer') == GDecl(x, 'integer')
            True
            sage: GDecl(x, 'integer') == GDecl(x, 'rational')
            False
            sage: GDecl(x, 'integer') == GDecl(y, 'integer')
            False
        """
        if isinstance(self, GenericDeclaration) and isinstance(other, GenericDeclaration):
            return cmp( (self._var, self._assumption),
                        (other._var, other._assumption) )
        else:
            return cmp(type(self), type(other))

    def has(self, arg):
        """
        Check if this assumption contains the argument ``arg``.

        EXAMPLES::

            sage: from sage.symbolic.assumptions import GenericDeclaration as GDecl
            sage: var('y')
            y
            sage: d = GDecl(x, 'integer')
            sage: d.has(x)
            True
            sage: d.has(y)
            False
        """
        return (arg - self._var).is_trivial_zero()

    def assume(self):
        """
        TEST::

            sage: from sage.symbolic.assumptions import GenericDeclaration
            sage: decl = GenericDeclaration(x, 'even')
            sage: decl.assume()
            sage: cos(x*pi).simplify()
            1
            sage: decl2 = GenericDeclaration(x, 'odd')
            sage: decl2.assume()
            Traceback (most recent call last):
            ...
            ValueError: Assumption is inconsistent
            sage: decl.forget()
        """
        from sage.calculus.calculus import maxima
        if self._context is None:
            # We get the list here because features may be added with time.
            valid_features = list(maxima("features"))
            if self._assumption not in [repr(x).strip() for x in list(valid_features)]:
                raise ValueError, "%s not a valid assumption, must be one of %s" % (self._assumption, valid_features)
            cur = maxima.get("context")
            self._context = maxima.newcontext('context' + maxima._next_var_name())
            try:
                maxima.eval("declare(%s, %s)" % (repr(self._var), self._assumption))
#            except TypeError, mess:
#                if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant
#                    raise ValueError, "Assumption is inconsistent"
            except RuntimeError, mess:
                if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant
                    raise ValueError, "Assumption is inconsistent"
                else:
                    raise
            maxima.set("context", cur)

        if not self in _assumptions:
            maxima.activate(self._context)
            _assumptions.append(self)