Esempio n. 1
0
    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)
Esempio n. 2
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