modelparameters.sympy.assumptions package

Subpackages

Submodules

modelparameters.sympy.assumptions.ask module

Module for querying SymPy objects about assumptions.

class modelparameters.sympy.assumptions.ask.AssumptionKeys[source]

Bases: object

This class contains all the supported keys by ask.

property algebraic

Algebraic number predicate.

Q.algebraic(x) is true iff x belongs to the set of algebraic numbers. x is algebraic if there is some polynomial in p(x)\in \mathbb\{Q\}[x] such that p(x) = 0.

Examples

>>> from .. import ask, Q, sqrt, I, pi
>>> ask(Q.algebraic(sqrt(2)))
True
>>> ask(Q.algebraic(I))
True
>>> ask(Q.algebraic(pi))
False

References

property antihermitian

Antihermitian predicate.

Q.antihermitian(x) is true iff x belongs to the field of antihermitian operators, i.e., operators in the form x*I, where x is Hermitian.

References

property bounded

See documentation of Q.finite.

property commutative

Commutative predicate.

ask(Q.commutative(x)) is true iff x commutes with any other object with respect to multiplication operation.

property complex

Complex number predicate.

Q.complex(x) is true iff x belongs to the set of complex numbers. Note that every complex number is finite.

Examples

>>> from .. import Q, Symbol, ask, I, oo
>>> x = Symbol('x')
>>> ask(Q.complex(0))
True
>>> ask(Q.complex(2 + 3*I))
True
>>> ask(Q.complex(oo))
False

References

property complex_elements

Complex elements matrix predicate.

Q.complex_elements(x) is true iff all the elements of x are complex numbers.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
>>> ask(Q.complex(X[1, 2]), Q.complex_elements(X))
True
>>> ask(Q.complex_elements(X), Q.integer_elements(X))
True
property composite

Composite number predicate.

ask(Q.composite(x)) is true iff x is a positive integer and has at least one positive divisor other than 1 and the number itself.

Examples

>>> from .. import Q, ask
>>> ask(Q.composite(0))
False
>>> ask(Q.composite(1))
False
>>> ask(Q.composite(2))
False
>>> ask(Q.composite(20))
True
property diagonal

Diagonal matrix predicate.

Q.diagonal(x) is true iff x is a diagonal matrix. A diagonal matrix is a matrix in which the entries outside the main diagonal are all zero.

Examples

>>> from .. import Q, ask, MatrixSymbol, ZeroMatrix
>>> X = MatrixSymbol('X', 2, 2)
>>> ask(Q.diagonal(ZeroMatrix(3, 3)))
True
>>> ask(Q.diagonal(X), Q.lower_triangular(X) &
...     Q.upper_triangular(X))
True

References

property even

Even number predicate.

ask(Q.even(x)) is true iff x belongs to the set of even integers.

Examples

>>> from .. import Q, ask, pi
>>> ask(Q.even(0))
True
>>> ask(Q.even(2))
True
>>> ask(Q.even(3))
False
>>> ask(Q.even(pi))
False
property extended_real

Extended real predicate.

Q.extended_real(x) is true iff x is a real number or {-infty, infty}.

See documentation of Q.real for more information about related facts.

Examples

>>> from .. import ask, Q, oo, I
>>> ask(Q.extended_real(1))
True
>>> ask(Q.extended_real(I))
False
>>> ask(Q.extended_real(oo))
True
property finite

Finite predicate.

Q.finite(x) is true if x is neither an infinity nor a NaN. In other words, ask(Q.finite(x)) is true for all x having a bounded absolute value.

Examples

>>> from .. import Q, ask, Symbol, S, oo, I
>>> x = Symbol('x')
>>> ask(Q.finite(S.NaN))
False
>>> ask(Q.finite(oo))
False
>>> ask(Q.finite(1))
True
>>> ask(Q.finite(2 + 3*I))
True

References

property fullrank

Fullrank matrix predicate.

Q.fullrank(x) is true iff x is a full rank matrix. A matrix is full rank if all rows and columns of the matrix are linearly independent. A square matrix is full rank iff its determinant is nonzero.

Examples

>>> from .. import Q, ask, MatrixSymbol, ZeroMatrix, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> ask(Q.fullrank(X.T), Q.fullrank(X))
True
>>> ask(Q.fullrank(ZeroMatrix(3, 3)))
False
>>> ask(Q.fullrank(Identity(3)))
True
property hermitian

Hermitian predicate.

ask(Q.hermitian(x)) is true iff x belongs to the set of Hermitian operators.

References

property imaginary

Imaginary number predicate.

Q.imaginary(x) is true iff x can be written as a real number multiplied by the imaginary unit I. Please note that 0 is not considered to be an imaginary number.

Examples

>>> from .. import Q, ask, I
>>> ask(Q.imaginary(3*I))
True
>>> ask(Q.imaginary(2 + 3*I))
False
>>> ask(Q.imaginary(0))
False

References

property infinite

Infinite number predicate.

Q.infinite(x) is true iff the absolute value of x is infinity.

property infinitesimal

See documentation of Q.zero.

property infinity

See documentation of Q.infinite.

property integer

Integer predicate.

Q.integer(x) is true iff x belongs to the set of integer numbers.

Examples

>>> from .. import Q, ask, S
>>> ask(Q.integer(5))
True
>>> ask(Q.integer(S(1)/2))
False

References

property integer_elements

Integer elements matrix predicate.

Q.integer_elements(x) is true iff all the elements of x are integers.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
>>> ask(Q.integer(X[1, 2]), Q.integer_elements(X))
True
property invertible

Invertible matrix predicate.

Q.invertible(x) is true iff x is an invertible matrix. A square matrix is called invertible only if its determinant is 0.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
>>> ask(Q.invertible(X*Y), Q.invertible(X))
False
>>> ask(Q.invertible(X*Z), Q.invertible(X) & Q.invertible(Z))
True
>>> ask(Q.invertible(X), Q.fullrank(X) & Q.square(X))
True

References

property irrational

Irrational number predicate.

Q.irrational(x) is true iff x is any real number that cannot be expressed as a ratio of integers.

Examples

>>> from .. import ask, Q, pi, S, I
>>> ask(Q.irrational(0))
False
>>> ask(Q.irrational(S(1)/2))
False
>>> ask(Q.irrational(pi))
True
>>> ask(Q.irrational(I))
False

References

property is_true

Generic predicate.

ask(Q.is_true(x)) is true iff x is true. This only makes sense if x is a predicate.

Examples

>>> from .. import ask, Q, symbols
>>> x = symbols('x')
>>> ask(Q.is_true(True))
True
property lower_triangular

Lower triangular matrix predicate.

A matrix M is called lower triangular matrix if \(a_{ij}=0\) for \(i>j\).

Examples

>>> from .. import Q, ask, ZeroMatrix, Identity
>>> ask(Q.lower_triangular(Identity(3)))
True
>>> ask(Q.lower_triangular(ZeroMatrix(3, 3)))
True

References

property negative

Negative number predicate.

Q.negative(x) is true iff x is a real number and \(x < 0\), that is, it is in the interval \((-\infty, 0)\). Note in particular that negative infinity is not negative.

A few important facts about negative numbers:

  • Note that Q.nonnegative and ~Q.negative are not the same thing. ~Q.negative(x) simply means that x is not negative, whereas Q.nonnegative(x) means that x is real and not negative, i.e., Q.nonnegative(x) is logically equivalent to Q.zero(x) | Q.positive(x). So for example, ~Q.negative(I) is true, whereas Q.nonnegative(I) is false.

  • See the documentation of Q.real for more information about related facts.

Examples

>>> from .. import Q, ask, symbols, I
>>> x = symbols('x')
>>> ask(Q.negative(x), Q.real(x) & ~Q.positive(x) & ~Q.zero(x))
True
>>> ask(Q.negative(-1))
True
>>> ask(Q.nonnegative(I))
False
>>> ask(~Q.negative(I))
True
property nonnegative

Nonnegative real number predicate.

ask(Q.nonnegative(x)) is true iff x belongs to the set of positive numbers including zero.

  • Note that Q.nonnegative and ~Q.negative are not the same thing. ~Q.negative(x) simply means that x is not negative, whereas Q.nonnegative(x) means that x is real and not negative, i.e., Q.nonnegative(x) is logically equivalent to Q.zero(x) | Q.positive(x). So for example, ~Q.negative(I) is true, whereas Q.nonnegative(I) is false.

Examples

>>> from .. import Q, ask, I
>>> ask(Q.nonnegative(1))
True
>>> ask(Q.nonnegative(0))
True
>>> ask(Q.nonnegative(-1))
False
>>> ask(Q.nonnegative(I))
False
>>> ask(Q.nonnegative(-I))
False
property nonpositive

Nonpositive real number predicate.

ask(Q.nonpositive(x)) is true iff x belongs to the set of negative numbers including zero.

  • Note that Q.nonpositive and ~Q.positive are not the same thing. ~Q.positive(x) simply means that x is not positive, whereas Q.nonpositive(x) means that x is real and not positive, i.e., Q.nonpositive(x) is logically equivalent to Q.negative(x) | Q.zero(x)`. So for example, ~Q.positive(I) is true, whereas Q.nonpositive(I) is false.

Examples

>>> from .. import Q, ask, I
>>> ask(Q.nonpositive(-1))
True
>>> ask(Q.nonpositive(0))
True
>>> ask(Q.nonpositive(1))
False
>>> ask(Q.nonpositive(I))
False
>>> ask(Q.nonpositive(-I))
False
property nonzero

Nonzero real number predicate.

ask(Q.nonzero(x)) is true iff x is real and x is not zero. Note in particular that Q.nonzero(x) is false if x is not real. Use ~Q.zero(x) if you want the negation of being zero without any real assumptions.

A few important facts about nonzero numbers:

  • Q.nonzero is logically equivalent to Q.positive | Q.negative.

  • See the documentation of Q.real for more information about related facts.

Examples

>>> from .. import Q, ask, symbols, I, oo
>>> x = symbols('x')
>>> print(ask(Q.nonzero(x), ~Q.zero(x)))
None
>>> ask(Q.nonzero(x), Q.positive(x))
True
>>> ask(Q.nonzero(x), Q.zero(x))
False
>>> ask(Q.nonzero(0))
False
>>> ask(Q.nonzero(I))
False
>>> ask(~Q.zero(I))
True
>>> ask(Q.nonzero(oo))  
False
property normal

Normal matrix predicate.

A matrix is normal if it commutes with its conjugate transpose.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
>>> ask(Q.normal(X), Q.unitary(X))
True

References

property odd

Odd number predicate.

ask(Q.odd(x)) is true iff x belongs to the set of odd numbers.

Examples

>>> from .. import Q, ask, pi
>>> ask(Q.odd(0))
False
>>> ask(Q.odd(2))
False
>>> ask(Q.odd(3))
True
>>> ask(Q.odd(pi))
False
property orthogonal

Orthogonal matrix predicate.

Q.orthogonal(x) is true iff x is an orthogonal matrix. A square matrix M is an orthogonal matrix if it satisfies M^TM = MM^T = I where M^T is the transpose matrix of M and I is an identity matrix. Note that an orthogonal matrix is necessarily invertible.

Examples

>>> from .. import Q, ask, MatrixSymbol, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
>>> ask(Q.orthogonal(Y))
False
>>> ask(Q.orthogonal(X*Z*X), Q.orthogonal(X) & Q.orthogonal(Z))
True
>>> ask(Q.orthogonal(Identity(3)))
True
>>> ask(Q.invertible(X), Q.orthogonal(X))
True

References

property positive

Positive real number predicate.

Q.positive(x) is true iff x is real and x > 0, that is if x is in the interval (0, infty). In particular, infinity is not positive.

A few important facts about positive numbers:

  • Note that Q.nonpositive and ~Q.positive are not the same thing. ~Q.positive(x) simply means that x is not positive, whereas Q.nonpositive(x) means that x is real and not positive, i.e., Q.nonpositive(x) is logically equivalent to Q.negative(x) | Q.zero(x)`. So for example, ~Q.positive(I) is true, whereas Q.nonpositive(I) is false.

  • See the documentation of Q.real for more information about related facts.

Examples

>>> from .. import Q, ask, symbols, I
>>> x = symbols('x')
>>> ask(Q.positive(x), Q.real(x) & ~Q.negative(x) & ~Q.zero(x))
True
>>> ask(Q.positive(1))
True
>>> ask(Q.nonpositive(I))
False
>>> ask(~Q.positive(I))
True
property positive_definite

Positive definite matrix predicate.

If M is a :math:n \times n symmetric real matrix, it is said to be positive definite if \(Z^TMZ\) is positive for every non-zero column vector Z of n real numbers.

Examples

>>> from .. import Q, ask, MatrixSymbol, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
>>> ask(Q.positive_definite(Y))
False
>>> ask(Q.positive_definite(Identity(3)))
True
>>> ask(Q.positive_definite(X + Z), Q.positive_definite(X) &
...     Q.positive_definite(Z))
True

References

property prime

Prime number predicate.

ask(Q.prime(x)) is true iff x is a natural number greater than 1 that has no positive divisors other than 1 and the number itself.

Examples

>>> from .. import Q, ask
>>> ask(Q.prime(0))
False
>>> ask(Q.prime(1))
False
>>> ask(Q.prime(2))
True
>>> ask(Q.prime(20))
False
>>> ask(Q.prime(-3))
False
property rational

Rational number predicate.

Q.rational(x) is true iff x belongs to the set of rational numbers.

Examples

>>> from .. import ask, Q, pi, S
>>> ask(Q.rational(0))
True
>>> ask(Q.rational(S(1)/2))
True
>>> ask(Q.rational(pi))
False

References

https://en.wikipedia.org/wiki/Rational_number

property real

Real number predicate.

Q.real(x) is true iff x is a real number, i.e., it is in the interval (-infty, infty). Note that, in particular the infinities are not real. Use Q.extended_real if you want to consider those as well.

A few important facts about reals:

  • Every real number is positive, negative, or zero. Furthermore, because these sets are pairwise disjoint, each real number is exactly one of those three.

  • Every real number is also complex.

  • Every real number is finite.

  • Every real number is either rational or irrational.

  • Every real number is either algebraic or transcendental.

  • The facts Q.negative, Q.zero, Q.positive, Q.nonnegative, Q.nonpositive, Q.nonzero, Q.integer, Q.rational, and Q.irrational all imply Q.real, as do all facts that imply those facts.

  • The facts Q.algebraic, and Q.transcendental do not imply Q.real; they imply Q.complex. An algebraic or transcendental number may or may not be real.

  • The “non” facts (i.e., Q.nonnegative, Q.nonzero, Q.nonpositive and Q.noninteger) are not equivalent to not the fact, but rather, not the fact and Q.real. For example, Q.nonnegative means ~Q.negative & Q.real. So for example, I is not nonnegative, nonzero, or nonpositive.

Examples

>>> from .. import Q, ask, symbols
>>> x = symbols('x')
>>> ask(Q.real(x), Q.positive(x))
True
>>> ask(Q.real(0))
True

References

property real_elements

Real elements matrix predicate.

Q.real_elements(x) is true iff all the elements of x are real numbers.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
>>> ask(Q.real(X[1, 2]), Q.real_elements(X))
True
property singular

Singular matrix predicate.

A matrix is singular iff the value of its determinant is 0.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
>>> ask(Q.singular(X), Q.invertible(X))
False
>>> ask(Q.singular(X), ~Q.invertible(X))
True

References

property square

Square matrix predicate.

Q.square(x) is true iff x is a square matrix. A square matrix is a matrix with the same number of rows and columns.

Examples

>>> from .. import Q, ask, MatrixSymbol, ZeroMatrix, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('X', 2, 3)
>>> ask(Q.square(X))
True
>>> ask(Q.square(Y))
False
>>> ask(Q.square(ZeroMatrix(3, 3)))
True
>>> ask(Q.square(Identity(3)))
True

References

property symmetric

Symmetric matrix predicate.

Q.symmetric(x) is true iff x is a square matrix and is equal to its transpose. Every square diagonal matrix is a symmetric matrix.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
>>> ask(Q.symmetric(X*Z), Q.symmetric(X) & Q.symmetric(Z))
True
>>> ask(Q.symmetric(X + Z), Q.symmetric(X) & Q.symmetric(Z))
True
>>> ask(Q.symmetric(Y))
False

References

property transcendental

Transcedental number predicate.

Q.transcendental(x) is true iff x belongs to the set of transcendental numbers. A transcendental number is a real or complex number that is not algebraic.

property triangular

Triangular matrix predicate.

Q.triangular(X) is true if X is one that is either lower triangular or upper triangular.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
>>> ask(Q.triangular(X), Q.upper_triangular(X))
True
>>> ask(Q.triangular(X), Q.lower_triangular(X))
True

References

property unit_triangular

Unit triangular matrix predicate.

A unit triangular matrix is a triangular matrix with 1s on the diagonal.

Examples

>>> from .. import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
>>> ask(Q.triangular(X), Q.unit_triangular(X))
True
property unitary

Unitary matrix predicate.

Q.unitary(x) is true iff x is a unitary matrix. Unitary matrix is an analogue to orthogonal matrix. A square matrix M with complex elements is unitary if :math:M^TM = MM^T= I where :math:M^T is the conjugate transpose matrix of M.

Examples

>>> from .. import Q, ask, MatrixSymbol, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
>>> ask(Q.unitary(Y))
False
>>> ask(Q.unitary(X*Z*X), Q.unitary(X) & Q.unitary(Z))
True
>>> ask(Q.unitary(Identity(3)))
True

References

property upper_triangular

Upper triangular matrix predicate.

A matrix M is called upper triangular matrix if \(M_{ij}=0\) for \(i<j\).

Examples

>>> from .. import Q, ask, ZeroMatrix, Identity
>>> ask(Q.upper_triangular(Identity(3)))
True
>>> ask(Q.upper_triangular(ZeroMatrix(3, 3)))
True

References

property zero

Zero number predicate.

ask(Q.zero(x)) is true iff the value of x is zero.

Examples

>>> from .. import ask, Q, oo, symbols
>>> x, y = symbols('x, y')
>>> ask(Q.zero(0))
True
>>> ask(Q.zero(1/oo))
True
>>> ask(Q.zero(0*oo))
False
>>> ask(Q.zero(1))
False
>>> ask(Q.zero(x*y), Q.zero(x) | Q.zero(y))
True
modelparameters.sympy.assumptions.ask.ask(proposition, assumptions=True, context={})[source]

Method for inferring properties about objects.

Syntax

  • ask(proposition)

  • ask(proposition, assumptions)

    where proposition is any boolean expression

Examples

>>> from .. import ask, Q, pi
>>> from ..abc import x, y
>>> ask(Q.rational(pi))
False
>>> ask(Q.even(x*y), Q.even(x) & Q.integer(y))
True
>>> ask(Q.prime(4*x), Q.integer(x))
False
Remarks

Relations in assumptions are not implemented (yet), so the following will not give a meaningful result.

>>> ask(Q.positive(x), Q.is_true(x > 0)) 

It is however a work in progress.

modelparameters.sympy.assumptions.ask.ask_full_inference(proposition, assumptions, known_facts_cnf)[source]

Method for inferring properties about objects.

modelparameters.sympy.assumptions.ask.compute_known_facts(known_facts, known_facts_keys)[source]

Compute the various forms of knowledge compilation used by the assumptions system.

This function is typically applied to the results of the get_known_facts and get_known_facts_keys functions defined at the bottom of this file.

modelparameters.sympy.assumptions.ask.get_known_facts()[source]
modelparameters.sympy.assumptions.ask.get_known_facts_keys()[source]
modelparameters.sympy.assumptions.ask.register_handler(key, handler)[source]

Register a handler in the ask system. key must be a string and handler a class inheriting from AskHandler:

>>> from ..assumptions import register_handler, ask, Q
>>> from .handlers import AskHandler
>>> class MersenneHandler(AskHandler):
...     # Mersenne numbers are in the form 2**n + 1, n integer
...     @staticmethod
...     def Integer(expr, assumptions):
...         from .. import log
...         return ask(Q.integer(log(expr + 1, 2)))
>>> register_handler('mersenne', MersenneHandler)
>>> ask(Q.mersenne(7))
True
modelparameters.sympy.assumptions.ask.remove_handler(key, handler)[source]

Removes a handler from the ask system. Same syntax as register_handler

modelparameters.sympy.assumptions.ask.single_fact_lookup(known_facts_keys, known_facts_cnf)[source]

modelparameters.sympy.assumptions.ask_generated module

The contents of this file are the return value of sympy.assumptions.ask.compute_known_facts.

Do NOT manually edit this file. Instead, run ./bin/ask_update.py.

modelparameters.sympy.assumptions.ask_generated.get_known_facts_cnf()[source]
modelparameters.sympy.assumptions.ask_generated.get_known_facts_dict()[source]

modelparameters.sympy.assumptions.assume module

class modelparameters.sympy.assumptions.assume.AppliedPredicate(predicate, arg)[source]

Bases: Boolean

The class of expressions resulting from applying a Predicate.

Examples

>>> from .. import Q, Symbol
>>> x = Symbol('x')
>>> Q.integer(x)
Q.integer(x)
>>> type(Q.integer(x))
<class 'sympy.assumptions.assume.AppliedPredicate'>
property arg

Return the expression used by this assumption.

Examples

>>> from .. import Q, Symbol
>>> x = Symbol('x')
>>> a = Q.integer(x + 1)
>>> a.arg
x + 1
property args

Returns a tuple of arguments of ‘self’.

Examples

>>> from .. import cot
>>> from ..abc import x, y
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y

Notes

Never use self._args, always use self.args. Only use _args in __new__ when creating a new function. Don’t override .args() from Basic (so that it’s easy to change the interface in the future if needed).

default_assumptions = {}
property func

The top-level function in an expression.

The following should hold for all objects:

>> x == x.func(*x.args)

Examples

>>> from ..abc import x
>>> a = 2*x
>>> a.func
<class 'sympy.core.mul.Mul'>
>>> a.args
(2, x)
>>> a.func(*a.args)
2*x
>>> a == a.func(*a.args)
True
is_Atom = True
sort_key(order=None)[source]

Return a sort key.

Examples

>>> from ..core import S, I
>>> sorted([S(1)/2, I, -I], key=lambda x: x.sort_key())
[1/2, -I, I]
>>> S("[x, 1/x, 1/x**2, x**2, x**(1/2), x**(1/4), x**(3/2)]")
[x, 1/x, x**(-2), x**2, sqrt(x), x**(1/4), x**(3/2)]
>>> sorted(_, key=lambda x: x.sort_key())
[x**(-2), 1/x, x**(1/4), sqrt(x), x, x**(3/2), x**2]
class modelparameters.sympy.assumptions.assume.AssumptionsContext[source]

Bases: set

Set representing assumptions.

This is used to represent global assumptions, but you can also use this class to create your own local assumptions contexts. It is basically a thin wrapper to Python’s set, so see its documentation for advanced usage.

Examples

>>> from .. import AppliedPredicate, Q
>>> from .assume import global_assumptions
>>> global_assumptions
AssumptionsContext()
>>> from ..abc import x
>>> global_assumptions.add(Q.real(x))
>>> global_assumptions
AssumptionsContext({Q.real(x)})
>>> global_assumptions.remove(Q.real(x))
>>> global_assumptions
AssumptionsContext()
>>> global_assumptions.clear()
add(*assumptions)[source]

Add an assumption.

class modelparameters.sympy.assumptions.assume.Predicate(name, handlers=None)[source]

Bases: Boolean

A predicate is a function that returns a boolean value.

Predicates merely wrap their argument and remain unevaluated:

>>> from .. import Q, ask, Symbol, S
>>> x = Symbol('x')
>>> Q.prime(7)
Q.prime(7)

To obtain the truth value of an expression containing predicates, use the function ask:

>>> ask(Q.prime(7))
True

The tautological predicate Q.is_true can be used to wrap other objects:

>>> Q.is_true(x > 1)
Q.is_true(x > 1)
>>> Q.is_true(S(1) < x)
Q.is_true(1 < x)
add_handler(handler)[source]
default_assumptions = {}
eval(expr, assumptions=True)[source]

Evaluate self(expr) under the given assumptions.

This uses only direct resolution methods, not logical inference.

is_Atom = True
remove_handler(handler)[source]
sort_key(order=None)[source]

Return a sort key.

Examples

>>> from ..core import S, I
>>> sorted([S(1)/2, I, -I], key=lambda x: x.sort_key())
[1/2, -I, I]
>>> S("[x, 1/x, 1/x**2, x**2, x**(1/2), x**(1/4), x**(3/2)]")
[x, 1/x, x**(-2), x**2, sqrt(x), x**(1/4), x**(3/2)]
>>> sorted(_, key=lambda x: x.sort_key())
[x**(-2), 1/x, x**(1/4), sqrt(x), x, x**(3/2), x**2]
modelparameters.sympy.assumptions.assume.assuming(*assumptions)[source]

Context manager for assumptions

Examples

>>> from ..assumptions import assuming, Q, ask
>>> from ..abc import x, y
>>> print(ask(Q.integer(x + y)))
None
>>> with assuming(Q.integer(x), Q.integer(y)):
...     print(ask(Q.integer(x + y)))
True

modelparameters.sympy.assumptions.refine module

modelparameters.sympy.assumptions.refine.refine(expr, assumptions=True)[source]

Simplify an expression using assumptions.

Gives the form of expr that would be obtained if symbols in it were replaced by explicit numerical expressions satisfying the assumptions.

Examples

>>> from .. import refine, sqrt, Q
>>> from ..abc import x
>>> refine(sqrt(x**2), Q.real(x))
Abs(x)
>>> refine(sqrt(x**2), Q.positive(x))
x
modelparameters.sympy.assumptions.refine.refine_Pow(expr, assumptions)[source]

Handler for instances of Pow.

>>> from .. import Symbol, Q
>>> from .refine import refine_Pow
>>> from ..abc import x,y,z
>>> refine_Pow((-1)**x, Q.real(x))
>>> refine_Pow((-1)**x, Q.even(x))
1
>>> refine_Pow((-1)**x, Q.odd(x))
-1

For powers of -1, even parts of the exponent can be simplified:

>>> refine_Pow((-1)**(x+y), Q.even(x))
(-1)**y
>>> refine_Pow((-1)**(x+y+z), Q.odd(x) & Q.odd(z))
(-1)**y
>>> refine_Pow((-1)**(x+y+2), Q.odd(x))
(-1)**(y + 1)
>>> refine_Pow((-1)**(x+3), True)
(-1)**(x + 1)
modelparameters.sympy.assumptions.refine.refine_Relational(expr, assumptions)[source]

Handler for Relational

>>> from .refine import refine_Relational
>>> from .ask import Q
>>> from ..abc import x
>>> refine_Relational(x<0, ~Q.is_true(x<0))
False
modelparameters.sympy.assumptions.refine.refine_abs(expr, assumptions)[source]

Handler for the absolute value.

Examples

>>> from .. import Symbol, Q, refine, Abs
>>> from .refine import refine_abs
>>> from ..abc import x
>>> refine_abs(Abs(x), Q.real(x))
>>> refine_abs(Abs(x), Q.positive(x))
x
>>> refine_abs(Abs(x), Q.negative(x))
-x
modelparameters.sympy.assumptions.refine.refine_atan2(expr, assumptions)[source]

Handler for the atan2 function

Examples

>>> from .. import Symbol, Q, refine, atan2
>>> from .refine import refine_atan2
>>> from ..abc import x, y
>>> refine_atan2(atan2(y,x), Q.real(y) & Q.positive(x))
atan(y/x)
>>> refine_atan2(atan2(y,x), Q.negative(y) & Q.negative(x))
atan(y/x) - pi
>>> refine_atan2(atan2(y,x), Q.positive(y) & Q.negative(x))
atan(y/x) + pi
>>> refine_atan2(atan2(y,x), Q.zero(y) & Q.negative(x))
pi
>>> refine_atan2(atan2(y,x), Q.positive(y) & Q.zero(x))
pi/2
>>> refine_atan2(atan2(y,x), Q.negative(y) & Q.zero(x))
-pi/2
>>> refine_atan2(atan2(y,x), Q.zero(y) & Q.zero(x))
nan

modelparameters.sympy.assumptions.satask module

modelparameters.sympy.assumptions.satask.get_all_relevant_facts(proposition, assumptions=True, context={}, use_known_facts=True, iterations=oo)[source]
modelparameters.sympy.assumptions.satask.get_relevant_facts(proposition, assumptions=(True,), context={}, use_known_facts=True, exprs=None, relevant_facts=None)[source]
modelparameters.sympy.assumptions.satask.satask(proposition, assumptions=True, context={}, use_known_facts=True, iterations=oo)[source]

modelparameters.sympy.assumptions.sathandlers module

class modelparameters.sympy.assumptions.sathandlers.AllArgs(*args)[source]

Bases: UnevaluatedOnFree

Class representing vectorizing a predicate over all the .args of an expression

See the docstring of UnevaluatedOnFree for more information on this class.

The typical usage is to evaluate predicates with expressions using .rcall().

Example

>>> from .sathandlers import AllArgs
>>> from .. import symbols, Q
>>> x, y = symbols('x y')
>>> a = AllArgs(Q.positive | Q.negative)
>>> a
AllArgs(Q.negative | Q.positive)
>>> a.rcall(x*y)
(Q.negative(x) | Q.positive(x)) & (Q.negative(y) | Q.positive(y))
apply()[source]
default_assumptions = {}
class modelparameters.sympy.assumptions.sathandlers.AnyArgs(*args)[source]

Bases: UnevaluatedOnFree

Class representing vectorizing a predicate over any of the .args of an expression.

See the docstring of UnevaluatedOnFree for more information on this class.

The typical usage is to evaluate predicates with expressions using .rcall().

Example

>>> from .sathandlers import AnyArgs
>>> from .. import symbols, Q
>>> x, y = symbols('x y')
>>> a = AnyArgs(Q.positive & Q.negative)
>>> a
AnyArgs(Q.negative & Q.positive)
>>> a.rcall(x*y)
(Q.negative(x) & Q.positive(x)) | (Q.negative(y) & Q.positive(y))
apply()[source]
default_assumptions = {}
class modelparameters.sympy.assumptions.sathandlers.CheckIsPrime(*args)[source]

Bases: UnevaluatedOnFree

apply()[source]
default_assumptions = {}
class modelparameters.sympy.assumptions.sathandlers.CheckOldAssump(*args)[source]

Bases: UnevaluatedOnFree

apply()[source]
default_assumptions = {}
class modelparameters.sympy.assumptions.sathandlers.ClassFactRegistry(d=None)[source]

Bases: MutableMapping

Register handlers against classes

registry[C] = handler registers handler for class C. registry[C] returns a set of handlers for class C, or any of its superclasses.

class modelparameters.sympy.assumptions.sathandlers.CustomLambda(lamda)[source]

Bases: object

Interface to lambda with rcall

Workaround until we get a better way to represent certain facts.

rcall(*args)[source]
class modelparameters.sympy.assumptions.sathandlers.ExactlyOneArg(*args)[source]

Bases: UnevaluatedOnFree

Class representing a predicate holding on exactly one of the .args of an expression.

See the docstring of UnevaluatedOnFree for more information on this class.

The typical usage is to evaluate predicate with expressions using .rcall().

Example

>>> from .sathandlers import ExactlyOneArg
>>> from .. import symbols, Q
>>> x, y = symbols('x y')
>>> a = ExactlyOneArg(Q.positive)
>>> a
ExactlyOneArg(Q.positive)
>>> a.rcall(x*y)
(Q.positive(x) & ~Q.positive(y)) | (Q.positive(y) & ~Q.positive(x))
apply()[source]
default_assumptions = {}
class modelparameters.sympy.assumptions.sathandlers.UnevaluatedOnFree(*args)[source]

Bases: BooleanFunction

Represents a Boolean function that remains unevaluated on free predicates

This is intended to be a superclass of other classes, which define the behavior on singly applied predicates.

A free predicate is a predicate that is not applied, or a combination thereof. For example, Q.zero or Or(Q.positive, Q.negative).

A singly applied predicate is a free predicate applied everywhere to a single expression. For instance, Q.zero(x) and Or(Q.positive(x*y), Q.negative(x*y)) are singly applied, but Or(Q.positive(x), Q.negative(y)) and Or(Q.positive, Q.negative(y)) are not.

The boolean literals True and False are considered to be both free and singly applied.

This class raises ValueError unless the input is a free predicate or a singly applied predicate.

On a free predicate, this class remains unevaluated. On a singly applied predicate, the method apply() is called and returned, or the original expression returned if apply() returns None. When apply() is called, self.expr is set to the unique expression that the predicates are applied at. self.pred is set to the free form of the predicate.

The typical usage is to create this class with free predicates and evaluate it using .rcall().

apply()[source]
default_assumptions = {}
modelparameters.sympy.assumptions.sathandlers.evaluate_old_assump(pred)[source]

Replace assumptions of expressions replaced with their values in the old assumptions (like Q.negative(-1) => True). Useful because some direct computations for numeric objects is defined most conveniently in the old assumptions.

modelparameters.sympy.assumptions.sathandlers.register_fact(klass, fact, registry=defaultdict(<class 'frozenset'>, {<class 'modelparameters.sympy.core.mul.Mul'>: frozenset({Implies(AllArgs(Q.prime), ~Q.prime), Implies(AllArgs(Q.integer), Q.integer), Implies(AllArgs(Q.rational), Q.rational), Implies(AllArgs(Q.positive), Q.positive), Equivalent(Q.zero, AnyArgs(Q.zero)), Implies(AllArgs(Q.commutative), Q.commutative), Implies(AllArgs(Q.integer), Equivalent(Q.even, AnyArgs(Q.even))), Implies(AllArgs(Q.imaginary | Q.real), Implies(ExactlyOneArg(Q.imaginary), Q.imaginary)), Implies(AllArgs(Q.real), Q.real), Implies(ExactlyOneArg(~Q.rational), ~Q.integer), Implies(AllArgs(Q.real), Implies(ExactlyOneArg(Q.irrational), Q.irrational)), Implies(AllArgs(Q.real), Q.commutative)}), <class 'modelparameters.sympy.matrices.expressions.matmul.MatMul'>: frozenset({Implies(AllArgs(Q.square), Equivalent(Q.invertible, AllArgs(Q.invertible)))}), <class 'modelparameters.sympy.core.add.Add'>: frozenset({Implies(AllArgs(Q.negative), Q.negative), Implies(AllArgs(Q.rational), Q.rational), Implies(AllArgs(Q.integer), Q.integer), Implies(AllArgs(Q.positive), Q.positive), Implies(AllArgs(Q.real), Q.real), Implies(ExactlyOneArg(~Q.integer), ~Q.integer), Implies(AllArgs(Q.real), Implies(ExactlyOneArg(Q.irrational), Q.irrational))}), <class 'modelparameters.sympy.core.power.Pow'>: frozenset({<modelparameters.sympy.assumptions.sathandlers.CustomLambda object>, <modelparameters.sympy.assumptions.sathandlers.CustomLambda object>, <modelparameters.sympy.assumptions.sathandlers.CustomLambda object>, <modelparameters.sympy.assumptions.sathandlers.CustomLambda object>}), <class 'modelparameters.sympy.core.numbers.Integer'>: frozenset({CheckIsPrime(Q.prime)}), Abs: frozenset({Implies(AllArgs(Q.integer), Q.integer), Q.nonnegative, Implies(AllArgs(Q.odd), Q.odd), Implies(AllArgs(Q.even), Q.even), Equivalent(~Q.zero, AllArgs(~Q.zero))}), <class 'modelparameters.sympy.core.numbers.Number'>: frozenset({CheckOldAssump(Q.zero), CheckOldAssump(Q.nonzero), CheckOldAssump(Q.irrational), CheckOldAssump(Q.nonpositive), CheckOldAssump(Q.integer), CheckOldAssump(Q.nonnegative), CheckOldAssump(Q.negative), CheckOldAssump(Q.odd), CheckOldAssump(Q.imaginary), CheckOldAssump(Q.rational), CheckOldAssump(Q.positive), CheckOldAssump(Q.even)}), <class 'modelparameters.sympy.core.numbers.NumberSymbol'>: frozenset({CheckOldAssump(Q.zero), CheckOldAssump(Q.nonzero), CheckOldAssump(Q.irrational), CheckOldAssump(Q.nonpositive), CheckOldAssump(Q.nonnegative), CheckOldAssump(Q.negative), CheckOldAssump(Q.imaginary), CheckOldAssump(Q.rational), CheckOldAssump(Q.positive)}), <class 'modelparameters.sympy.core.numbers.ImaginaryUnit'>: frozenset({CheckOldAssump(Q.zero), CheckOldAssump(Q.nonzero), CheckOldAssump(Q.irrational), CheckOldAssump(Q.nonpositive), CheckOldAssump(Q.nonnegative), CheckOldAssump(Q.negative), CheckOldAssump(Q.imaginary), CheckOldAssump(Q.rational), CheckOldAssump(Q.positive)})}))[source]

Module contents