Assumptions¶
The GenericDeclaration
class provides assumptions about a symbol or
function in verbal form. Such assumptions can be made using the assume()
function in this module, which also can take any relation of symbolic
expressions as argument. Use forget()
to clear all assumptions.
Creating a variable with a specific domain is equivalent with making an
assumption about it.
There is only rudimentary support for consistency and satisfiability checking in Sage. Assumptions are used both in Maxima and Pynac to support or refine some computations. In the following we show how to make and query assumptions. Please see the respective modules for more practical examples.
In addition to the global assumptions()
database, assuming()
creates reusable, stackable context managers allowing for temporary
updates of the database for evaluation of a (block of) statements.
EXAMPLES:
The default domain of a symbolic variable is the complex plane:
sage: var('x')
x
sage: x.is_real()
False
sage: assume(x,'real')
sage: x.is_real()
True
sage: forget()
sage: x.is_real()
False
>>> from sage.all import *
>>> var('x')
x
>>> x.is_real()
False
>>> assume(x,'real')
>>> x.is_real()
True
>>> forget()
>>> x.is_real()
False
var('x') x.is_real() assume(x,'real') x.is_real() forget() x.is_real()
Here is the list of acceptable features:
sage: ", ".join(map(str, maxima("features")._sage_()))
'integer, noninteger, even, odd, rational, irrational, real, imaginary,
complex, analytic, increasing, decreasing, oddfun, evenfun, posfun,
constant, commutative, lassociative, rassociative, symmetric,
antisymmetric, integervalued'
>>> from sage.all import *
>>> ", ".join(map(str, maxima("features")._sage_()))
'integer, noninteger, even, odd, rational, irrational, real, imaginary,
complex, analytic, increasing, decreasing, oddfun, evenfun, posfun,
constant, commutative, lassociative, rassociative, symmetric,
antisymmetric, integervalued'
", ".join(map(str, maxima("features")._sage_()))
Set positive domain using a relation:
sage: assume(x>0)
sage: x.is_positive()
True
sage: x.is_real()
True
sage: assumptions()
[x > 0]
>>> from sage.all import *
>>> assume(x>Integer(0))
>>> x.is_positive()
True
>>> x.is_real()
True
>>> assumptions()
[x > 0]
assume(x>0) x.is_positive() x.is_real() assumptions()
Assumptions also affect operations that do not use Maxima:
sage: forget()
sage: assume(x, 'even')
sage: assume(x, 'real')
sage: (-1)^x
1
sage: (-gamma(pi))^x
gamma(pi)^x
sage: binomial(2*x, x).is_integer()
True
>>> from sage.all import *
>>> forget()
>>> assume(x, 'even')
>>> assume(x, 'real')
>>> (-Integer(1))**x
1
>>> (-gamma(pi))**x
gamma(pi)^x
>>> binomial(Integer(2)*x, x).is_integer()
True
forget() assume(x, 'even') assume(x, 'real') (-1)^x (-gamma(pi))^x binomial(2*x, x).is_integer()
Assumptions are added and in some cases checked for consistency:
sage: assume(x>0)
sage: assume(x<0)
Traceback (most recent call last):
...
ValueError: Assumption is inconsistent
sage: forget()
>>> from sage.all import *
>>> assume(x>Integer(0))
>>> assume(x<Integer(0))
Traceback (most recent call last):
...
ValueError: Assumption is inconsistent
>>> forget()
assume(x>0) assume(x<0) forget()
- class sage.symbolic.assumptions.GenericDeclaration(var, assumption)[source]¶
Bases:
UniqueRepresentation
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) and to Pynac.
INPUT:
var
– the variable about which assumptions are being madeassumption
– string containing a Maxima feature, either user defined or in the list given bymaxima('features')
EXAMPLES:
sage: from sage.symbolic.assumptions import GenericDeclaration sage: decl = GenericDeclaration(x, 'integer') sage: decl.assume() sage: sin(x*pi) 0 sage: decl.forget() sage: sin(x*pi) sin(pi*x) sage: sin(x*pi).simplify() sin(pi*x)
>>> from sage.all import * >>> from sage.symbolic.assumptions import GenericDeclaration >>> decl = GenericDeclaration(x, 'integer') >>> decl.assume() >>> sin(x*pi) 0 >>> decl.forget() >>> sin(x*pi) sin(pi*x) >>> sin(x*pi).simplify() sin(pi*x)
from sage.symbolic.assumptions import GenericDeclaration decl = GenericDeclaration(x, 'integer') decl.assume() sin(x*pi) decl.forget() sin(x*pi) sin(x*pi).simplify()
Here is the list of acceptable features:
sage: ", ".join(map(str, maxima("features")._sage_())) 'integer, noninteger, even, odd, rational, irrational, real, imaginary, complex, analytic, increasing, decreasing, oddfun, evenfun, posfun, constant, commutative, lassociative, rassociative, symmetric, antisymmetric, integervalued'
>>> from sage.all import * >>> ", ".join(map(str, maxima("features")._sage_())) 'integer, noninteger, even, odd, rational, irrational, real, imaginary, complex, analytic, increasing, decreasing, oddfun, evenfun, posfun, constant, commutative, lassociative, rassociative, symmetric, antisymmetric, integervalued'
", ".join(map(str, maxima("features")._sage_()))
Test unique representation behavior:
sage: GenericDeclaration(x, 'integer') is GenericDeclaration(SR.var("x"), 'integer') True
>>> from sage.all import * >>> GenericDeclaration(x, 'integer') is GenericDeclaration(SR.var("x"), 'integer') True
GenericDeclaration(x, 'integer') is GenericDeclaration(SR.var("x"), 'integer')
- contradicts(soln)[source]¶
Return
True
if this assumption is violated by the given variable assignment(s).INPUT:
soln
– either a dictionary with variables as keys or a symbolic relation with a variable on the left hand side
EXAMPLES:
sage: from sage.symbolic.assumptions import GenericDeclaration sage: GenericDeclaration(x, 'integer').contradicts(x==4) False sage: GenericDeclaration(x, 'integer').contradicts(x==4.0) False sage: GenericDeclaration(x, 'integer').contradicts(x==4.5) True sage: GenericDeclaration(x, 'integer').contradicts(x==sqrt(17)) True sage: GenericDeclaration(x, 'noninteger').contradicts(x==sqrt(17)) False sage: GenericDeclaration(x, 'noninteger').contradicts(x==17) True sage: GenericDeclaration(x, 'even').contradicts(x==3) True sage: GenericDeclaration(x, 'complex').contradicts(x==3) False sage: GenericDeclaration(x, 'imaginary').contradicts(x==3) True sage: GenericDeclaration(x, 'imaginary').contradicts(x==I) False sage: var('y,z') (y, z) sage: GenericDeclaration(x, 'imaginary').contradicts(x==y+z) False sage: GenericDeclaration(x, 'rational').contradicts(y==pi) False sage: GenericDeclaration(x, 'rational').contradicts(x==pi) True sage: GenericDeclaration(x, 'irrational').contradicts(x!=pi) False sage: GenericDeclaration(x, 'rational').contradicts({x: pi, y: pi}) True sage: GenericDeclaration(x, 'rational').contradicts({z: pi, y: pi}) False
>>> from sage.all import * >>> from sage.symbolic.assumptions import GenericDeclaration >>> GenericDeclaration(x, 'integer').contradicts(x==Integer(4)) False >>> GenericDeclaration(x, 'integer').contradicts(x==RealNumber('4.0')) False >>> GenericDeclaration(x, 'integer').contradicts(x==RealNumber('4.5')) True >>> GenericDeclaration(x, 'integer').contradicts(x==sqrt(Integer(17))) True >>> GenericDeclaration(x, 'noninteger').contradicts(x==sqrt(Integer(17))) False >>> GenericDeclaration(x, 'noninteger').contradicts(x==Integer(17)) True >>> GenericDeclaration(x, 'even').contradicts(x==Integer(3)) True >>> GenericDeclaration(x, 'complex').contradicts(x==Integer(3)) False >>> GenericDeclaration(x, 'imaginary').contradicts(x==Integer(3)) True >>> GenericDeclaration(x, 'imaginary').contradicts(x==I) False >>> var('y,z') (y, z) >>> GenericDeclaration(x, 'imaginary').contradicts(x==y+z) False >>> GenericDeclaration(x, 'rational').contradicts(y==pi) False >>> GenericDeclaration(x, 'rational').contradicts(x==pi) True >>> GenericDeclaration(x, 'irrational').contradicts(x!=pi) False >>> GenericDeclaration(x, 'rational').contradicts({x: pi, y: pi}) True >>> GenericDeclaration(x, 'rational').contradicts({z: pi, y: pi}) False
from sage.symbolic.assumptions import GenericDeclaration GenericDeclaration(x, 'integer').contradicts(x==4) GenericDeclaration(x, 'integer').contradicts(x==4.0) GenericDeclaration(x, 'integer').contradicts(x==4.5) GenericDeclaration(x, 'integer').contradicts(x==sqrt(17)) GenericDeclaration(x, 'noninteger').contradicts(x==sqrt(17)) GenericDeclaration(x, 'noninteger').contradicts(x==17) GenericDeclaration(x, 'even').contradicts(x==3) GenericDeclaration(x, 'complex').contradicts(x==3) GenericDeclaration(x, 'imaginary').contradicts(x==3) GenericDeclaration(x, 'imaginary').contradicts(x==I) var('y,z') GenericDeclaration(x, 'imaginary').contradicts(x==y+z) GenericDeclaration(x, 'rational').contradicts(y==pi) GenericDeclaration(x, 'rational').contradicts(x==pi) GenericDeclaration(x, 'irrational').contradicts(x!=pi) GenericDeclaration(x, 'rational').contradicts({x: pi, y: pi}) GenericDeclaration(x, 'rational').contradicts({z: pi, y: pi})
- has(arg)[source]¶
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
>>> from sage.all import * >>> from sage.symbolic.assumptions import GenericDeclaration as GDecl >>> var('y') y >>> d = GDecl(x, 'integer') >>> d.has(x) True >>> d.has(y) False
from sage.symbolic.assumptions import GenericDeclaration as GDecl var('y') d = GDecl(x, 'integer') d.has(x) d.has(y)
- sage.symbolic.assumptions.assume(*args)[source]¶
Make the given assumptions.
INPUT:
*args
– a variable-length sequence of assumptions, each consisting of:any number of symbolic inequalities, like
0 < x, x < 1
a subsequence of variable names, followed by some property that should be assumed for those variables; for example,
x, y, z, 'integer'
would assume that each ofx
,y
, andz
are integer variables, andx, 'odd'
would assume thatx
is odd (as opposed to even).
The two types can be combined, but a symbolic inequality cannot appear in the middle of a list of variables.
OUTPUT: if everything goes as planned, there is no output
If you assume something that is not one of the two forms above, then an
AttributeError
is raised as we try to call itsassume
method.If you make inconsistent assumptions (for example, that
x
is both even and odd), then aValueError
is raised.Warning
Do not use Python’s chained comparison notation in assumptions. Python literally translates the expression
0 < x < 1
to(0 < x) and (x < 1)
, but the value ofbool(0 < x)
isFalse
whenx
is a symbolic variable. Therefore, by the definition of Python’s logical “and” operator, the entire expression is equal to0 < x
.EXAMPLES:
Assumptions are typically used to ensure certain relations are evaluated as true that are not true in general.
Here, we verify that for \(x>0\), \(\sqrt{x^2}=x\):
sage: assume(x > 0) sage: bool(sqrt(x^2) == x) True
>>> from sage.all import * >>> assume(x > Integer(0)) >>> bool(sqrt(x**Integer(2)) == x) True
assume(x > 0) bool(sqrt(x^2) == x)
This will be assumed in the current Sage session until forgotten:
sage: bool(sqrt(x^2) == x) True sage: forget() sage: bool(sqrt(x^2) == x) False
>>> from sage.all import * >>> bool(sqrt(x**Integer(2)) == x) True >>> forget() >>> bool(sqrt(x**Integer(2)) == x) False
bool(sqrt(x^2) == x) forget() bool(sqrt(x^2) == x)
Another major use case is in taking certain integrals and limits where the answers may depend on some sign condition:
sage: var('x, n') (x, n) sage: assume(n+1>0) sage: integral(x^n,x) x^(n + 1)/(n + 1) sage: forget()
>>> from sage.all import * >>> var('x, n') (x, n) >>> assume(n+Integer(1)>Integer(0)) >>> integral(x**n,x) x^(n + 1)/(n + 1) >>> forget()
var('x, n') assume(n+1>0) integral(x^n,x) forget()
sage: var('q, a, k') (q, a, k) sage: assume(q > 1) sage: sum(a*q^k, k, 0, oo) Traceback (most recent call last): ... ValueError: Sum is divergent. sage: forget() sage: assume(abs(q) < 1) sage: sum(a*q^k, k, 0, oo) -a/(q - 1) sage: forget()
>>> from sage.all import * >>> var('q, a, k') (q, a, k) >>> assume(q > Integer(1)) >>> sum(a*q**k, k, Integer(0), oo) Traceback (most recent call last): ... ValueError: Sum is divergent. >>> forget() >>> assume(abs(q) < Integer(1)) >>> sum(a*q**k, k, Integer(0), oo) -a/(q - 1) >>> forget()
var('q, a, k') assume(q > 1) sum(a*q^k, k, 0, oo) forget() assume(abs(q) < 1) sum(a*q^k, k, 0, oo) forget()
An integer constraint:
sage: n,P,r,r2 = SR.var('n, P, r, r2') sage: assume(n, 'integer') sage: c = P*e^(r*n) sage: d = P*(1+r2)^n sage: solve(c==d,r2) [r2 == e^r - 1] sage: forget()
>>> from sage.all import * >>> n,P,r,r2 = SR.var('n, P, r, r2') >>> assume(n, 'integer') >>> c = P*e**(r*n) >>> d = P*(Integer(1)+r2)**n >>> solve(c==d,r2) [r2 == e^r - 1] >>> forget()
n,P,r,r2 = SR.var('n, P, r, r2') assume(n, 'integer') c = P*e^(r*n) d = P*(1+r2)^n solve(c==d,r2) forget()
Simplifying certain well-known identities works as well:
sage: n = SR.var('n') sage: assume(n, 'integer') sage: sin(n*pi) 0 sage: forget() sage: sin(n*pi).simplify() sin(pi*n)
>>> from sage.all import * >>> n = SR.var('n') >>> assume(n, 'integer') >>> sin(n*pi) 0 >>> forget() >>> sin(n*pi).simplify() sin(pi*n)
n = SR.var('n') assume(n, 'integer') sin(n*pi) forget() sin(n*pi).simplify()
Instead of using chained comparison notation, each relationship should be passed as a separate assumption:
sage: x = SR.var('x') sage: assume(0 < x, x < 1) # instead of assume(0 < x < 1) sage: assumptions() [0 < x, x < 1] sage: forget()
>>> from sage.all import * >>> x = SR.var('x') >>> assume(Integer(0) < x, x < Integer(1)) # instead of assume(0 < x < 1) >>> assumptions() [0 < x, x < 1] >>> forget()
x = SR.var('x') assume(0 < x, x < 1) # instead of assume(0 < x < 1) assumptions() forget()
If you make inconsistent or meaningless assumptions, Sage will let you know:
sage: assume(x<0) sage: assume(x>0) Traceback (most recent call last): ... ValueError: Assumption is inconsistent sage: assume(x<1) Traceback (most recent call last): ... ValueError: Assumption is redundant sage: assumptions() [x < 0] sage: forget() sage: assume(x,'even') sage: assume(x,'odd') Traceback (most recent call last): ... ValueError: Assumption is inconsistent sage: forget()
>>> from sage.all import * >>> assume(x<Integer(0)) >>> assume(x>Integer(0)) Traceback (most recent call last): ... ValueError: Assumption is inconsistent >>> assume(x<Integer(1)) Traceback (most recent call last): ... ValueError: Assumption is redundant >>> assumptions() [x < 0] >>> forget() >>> assume(x,'even') >>> assume(x,'odd') Traceback (most recent call last): ... ValueError: Assumption is inconsistent >>> forget()
assume(x<0) assume(x>0) assume(x<1) assumptions() forget() assume(x,'even') assume(x,'odd') forget()
You can also use assumptions to evaluate simple truth values:
sage: x, y, z = var('x, y, z') sage: assume(x>=y,y>=z,z>=x) sage: bool(x==z) True sage: bool(z<x) False sage: bool(z>y) False sage: bool(y==z) True sage: forget() sage: assume(x>=1,x<=1) sage: bool(x==1) True sage: bool(x>1) False sage: forget()
>>> from sage.all import * >>> x, y, z = var('x, y, z') >>> assume(x>=y,y>=z,z>=x) >>> bool(x==z) True >>> bool(z<x) False >>> bool(z>y) False >>> bool(y==z) True >>> forget() >>> assume(x>=Integer(1),x<=Integer(1)) >>> bool(x==Integer(1)) True >>> bool(x>Integer(1)) False >>> forget()
x, y, z = var('x, y, z') assume(x>=y,y>=z,z>=x) bool(x==z) bool(z<x) bool(z>y) bool(y==z) forget() assume(x>=1,x<=1) bool(x==1) bool(x>1) forget()
- class sage.symbolic.assumptions.assuming(*args, **kwds)[source]¶
Bases:
object
Temporarily modify assumptions.
Create a context manager in which temporary assumptions are added (or substituted) to the current assumptions set.
The set of possible assumptions and declarations is the same as for
assume()
.This can be useful in interactive mode to discover the assumptions necessary to a given integration, or the exact solution to a system of equations.
It can also be used to explore the branches of a
cases()
expression.As with
assume()
, it is an error to add an assumption either redundant or inconsistent with the current assumption set (unlessreplace=True
is used). See examples.INPUT:
*args
– assumptions (same format as forassume()
)replace
– boolean (default:False
); specifies whether the new assumptions are added to (default) or replace (ifreplace=True
) the current assumption set
OUTPUT:
A context manager useable in a
with
statement (see examples).EXAMPLES:
Basic functionality : inside a
with assuming:()
block, Sage uses the updated assumptions database. After exit, the original database is restored.sage: var("x") x sage: forget(assumptions()) sage: solve(x^2 == 4,x) [x == -2, x == 2] sage: with assuming(x > 0): ....: solve(x^2 == 4,x) [x == 2] sage: assumptions() []
>>> from sage.all import * >>> var("x") x >>> forget(assumptions()) >>> solve(x**Integer(2) == Integer(4),x) [x == -2, x == 2] >>> with assuming(x > Integer(0)): ... solve(x**Integer(2) == Integer(4),x) [x == 2] >>> assumptions() []
var("x") forget(assumptions()) solve(x^2 == 4,x) with assuming(x > 0): solve(x^2 == 4,x) assumptions()
The local assumptions can be stacked. We can use this functionality to discover incrementally the assumptions necessary to a given calculation (and by the way, to check that Sage’s default integrator (Maxima’s, that is), sometimes nitpicks for naught).
sage: var("y,k,theta") (y, k, theta) sage: dgamma(y,k,theta)=y^(k-1)*e^(-y/theta)/(theta^k*gamma(k)) sage: integrate(dgamma(y,k,theta),y,0,oo) Traceback (most recent call last): ... ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(theta>0)', see `assume?` for more details) Is theta positive or negative? sage: a1=assuming(theta>0) sage: with a1:integrate(dgamma(y,k,theta),y,0,oo) Traceback (most recent call last): ... ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(k>0)', see `assume?` for more details) Is k positive, negative or zero? sage: a2=assuming(k>0) sage: with a1,a2:integrate(dgamma(y,k,theta),y,0,oo) Traceback (most recent call last): ... ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(k>0)', see `assume?` for more details) Is k an integer? sage: a3=assuming(k,"noninteger") sage: with a1,a2,a3:integrate(dgamma(y,k,theta),y,0,oo) 1 sage: a4=assuming(k,"integer") sage: with a1,a2,a4:integrate(dgamma(y,k,theta),y,0,oo) 1
>>> from sage.all import * >>> var("y,k,theta") (y, k, theta) >>> __tmp__=var("y,k,theta"); dgamma = symbolic_expression(y**(k-Integer(1))*e**(-y/theta)/(theta**k*gamma(k))).function(y,k,theta) >>> integrate(dgamma(y,k,theta),y,Integer(0),oo) Traceback (most recent call last): ... ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(theta>0)', see `assume?` for more details) Is theta positive or negative? >>> a1=assuming(theta>Integer(0)) >>> with a1:integrate(dgamma(y,k,theta),y,Integer(0),oo) Traceback (most recent call last): ... ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(k>0)', see `assume?` for more details) Is k positive, negative or zero? >>> a2=assuming(k>Integer(0)) >>> with a1,a2:integrate(dgamma(y,k,theta),y,Integer(0),oo) Traceback (most recent call last): ... ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(k>0)', see `assume?` for more details) Is k an integer? >>> a3=assuming(k,"noninteger") >>> with a1,a2,a3:integrate(dgamma(y,k,theta),y,Integer(0),oo) 1 >>> a4=assuming(k,"integer") >>> with a1,a2,a4:integrate(dgamma(y,k,theta),y,Integer(0),oo) 1
var("y,k,theta") dgamma(y,k,theta)=y^(k-1)*e^(-y/theta)/(theta^k*gamma(k)) integrate(dgamma(y,k,theta),y,0,oo) a1=assuming(theta>0) with a1:integrate(dgamma(y,k,theta),y,0,oo) a2=assuming(k>0) with a1,a2:integrate(dgamma(y,k,theta),y,0,oo) a3=assuming(k,"noninteger") with a1,a2,a3:integrate(dgamma(y,k,theta),y,0,oo) a4=assuming(k,"integer") with a1,a2,a4:integrate(dgamma(y,k,theta),y,0,oo)
As mentioned above, it is an error to try to introduce redundant or inconsistent assumptions.
sage: assume(x > 0) sage: with assuming(x > -1): "I won't see this" Traceback (most recent call last): ... ValueError: Assumption is redundant sage: with assuming(x < -1): "I won't see this" Traceback (most recent call last): ... ValueError: Assumption is inconsistent
>>> from sage.all import * >>> assume(x > Integer(0)) >>> with assuming(x > -Integer(1)): "I won't see this" Traceback (most recent call last): ... ValueError: Assumption is redundant >>> with assuming(x < -Integer(1)): "I won't see this" Traceback (most recent call last): ... ValueError: Assumption is inconsistent
assume(x > 0) with assuming(x > -1): "I won't see this" with assuming(x < -1): "I won't see this"
- sage.symbolic.assumptions.assumptions(*args)[source]¶
List all current symbolic assumptions.
INPUT:
args
– list of variables which can be empty
- OUTPUT: list of assumptions on variables; if
args
is empty it returns all assumptions
EXAMPLES:
sage: var('x, y, z, w') (x, y, z, w) sage: forget() sage: assume(x^2+y^2 > 0) sage: assumptions() [x^2 + y^2 > 0] sage: forget(x^2+y^2 > 0) sage: assumptions() [] sage: assume(x > y) sage: assume(z > w) sage: sorted(assumptions(), key=lambda x: str(x)) [x > y, z > w] sage: forget() sage: assumptions() []
>>> from sage.all import * >>> var('x, y, z, w') (x, y, z, w) >>> forget() >>> assume(x**Integer(2)+y**Integer(2) > Integer(0)) >>> assumptions() [x^2 + y^2 > 0] >>> forget(x**Integer(2)+y**Integer(2) > Integer(0)) >>> assumptions() [] >>> assume(x > y) >>> assume(z > w) >>> sorted(assumptions(), key=lambda x: str(x)) [x > y, z > w] >>> forget() >>> assumptions() []
var('x, y, z, w') forget() assume(x^2+y^2 > 0) assumptions() forget(x^2+y^2 > 0) assumptions() assume(x > y) assume(z > w) sorted(assumptions(), key=lambda x: str(x)) forget() assumptions()
It is also possible to query for assumptions on a variable independently:
sage: x, y, z = var('x y z') sage: assume(x, 'integer') sage: assume(y > 0) sage: assume(y**2 + z**2 == 1) sage: assume(x < 0) sage: assumptions() [x is integer, y > 0, y^2 + z^2 == 1, x < 0] sage: assumptions(x) [x is integer, x < 0] sage: assumptions(x, y) [x is integer, x < 0, y > 0, y^2 + z^2 == 1] sage: assumptions(z) [y^2 + z^2 == 1]
>>> from sage.all import * >>> x, y, z = var('x y z') >>> assume(x, 'integer') >>> assume(y > Integer(0)) >>> assume(y**Integer(2) + z**Integer(2) == Integer(1)) >>> assume(x < Integer(0)) >>> assumptions() [x is integer, y > 0, y^2 + z^2 == 1, x < 0] >>> assumptions(x) [x is integer, x < 0] >>> assumptions(x, y) [x is integer, x < 0, y > 0, y^2 + z^2 == 1] >>> assumptions(z) [y^2 + z^2 == 1]
x, y, z = var('x y z') assume(x, 'integer') assume(y > 0) assume(y**2 + z**2 == 1) assume(x < 0) assumptions() assumptions(x) assumptions(x, y) assumptions(z)
- sage.symbolic.assumptions.forget(*args)[source]¶
Forget the given assumption, or call with no arguments to forget all assumptions.
Here an assumption is some sort of symbolic constraint.
INPUT:
*args
– assumptions (default: forget all assumptions)
EXAMPLES:
We define and forget multiple assumptions:
sage: forget() sage: var('x,y,z') (x, y, z) sage: assume(x>0, y>0, z == 1, y>0) sage: sorted(assumptions(), key=lambda x:str(x)) [x > 0, y > 0, z == 1] sage: forget(x>0, z==1) sage: assumptions() [y > 0] sage: assume(y, 'even', z, 'complex') sage: assumptions() [y > 0, y is even, z is complex] sage: cos(y*pi).simplify() 1 sage: forget(y,'even') sage: cos(y*pi).simplify() cos(pi*y) sage: assumptions() [y > 0, z is complex] sage: forget() sage: assumptions() []
>>> from sage.all import * >>> forget() >>> var('x,y,z') (x, y, z) >>> assume(x>Integer(0), y>Integer(0), z == Integer(1), y>Integer(0)) >>> sorted(assumptions(), key=lambda x:str(x)) [x > 0, y > 0, z == 1] >>> forget(x>Integer(0), z==Integer(1)) >>> assumptions() [y > 0] >>> assume(y, 'even', z, 'complex') >>> assumptions() [y > 0, y is even, z is complex] >>> cos(y*pi).simplify() 1 >>> forget(y,'even') >>> cos(y*pi).simplify() cos(pi*y) >>> assumptions() [y > 0, z is complex] >>> forget() >>> assumptions() []
forget() var('x,y,z') assume(x>0, y>0, z == 1, y>0) sorted(assumptions(), key=lambda x:str(x)) forget(x>0, z==1) assumptions() assume(y, 'even', z, 'complex') assumptions() cos(y*pi).simplify() forget(y,'even') cos(y*pi).simplify() assumptions() forget() assumptions()
- sage.symbolic.assumptions.preprocess_assumptions(args)[source]¶
Turn a list of the form
(var1, var2, ..., 'property')
into a sequence of declarations(var1 is property), (var2 is property), ...
EXAMPLES:
sage: from sage.symbolic.assumptions import preprocess_assumptions sage: preprocess_assumptions([x, 'integer', x > 4]) [x is integer, x > 4] sage: var('x, y') (x, y) sage: preprocess_assumptions([x, y, 'integer', x > 4, y, 'even']) [x is integer, y is integer, x > 4, y is even]
>>> from sage.all import * >>> from sage.symbolic.assumptions import preprocess_assumptions >>> preprocess_assumptions([x, 'integer', x > Integer(4)]) [x is integer, x > 4] >>> var('x, y') (x, y) >>> preprocess_assumptions([x, y, 'integer', x > Integer(4), y, 'even']) [x is integer, y is integer, x > 4, y is even]
from sage.symbolic.assumptions import preprocess_assumptions preprocess_assumptions([x, 'integer', x > 4]) var('x, y') preprocess_assumptions([x, y, 'integer', x > 4, y, 'even'])