Skip to contents
library(symcompR)
x <- Symbol("x"); y <- Symbol("y")
n <- Symbol("n", integer = TRUE)

symcompR has both assumptions systems of SymPy: facts attached to symbols (the “old” assumptions of sympy.core.assumptions) and predicates queried with ask() (sympy.assumptions).

Assumptions on symbols

Symbol() accepts assumptions as arguments. The is_*() queries return TRUE, FALSE, or NA when the answer is not known, and ask_fact() queries any fact by name.

p <- Symbol("p", positive = TRUE)
r <- Symbol("r", real = TRUE)
is_negative(1 - sqrt(S(2)))
#> [1] TRUE
is_nonnegative(p)
#> [1] TRUE
is_nonpositive(-p)
#> [1] TRUE
is_nonzero(p)
#> [1] TRUE
is_zero(p - p)
#> [1] TRUE
is_finite(r)
#> [1] TRUE
is_infinite(oo * sym_I)
#> [1] TRUE
is_complex(r)
#> [1] TRUE
is_imaginary(sym_I * r)
#> [1] NA
is_imaginary(2 * sym_I)
#> [1] TRUE
is_rational(Rational(1, 3))
#> [1] TRUE
is_irrational(sqrt(S(2)))
#> [1] TRUE
is_algebraic(sqrt(S(2)))
#> [1] TRUE
is_transcendental(sym_pi)
#> [1] TRUE
is_odd(2 * n + 1)
#> [1] TRUE
is_prime_expr(S(7))
#> [1] TRUE
is_commutative(x)
#> [1] TRUE
ask_fact(p + 1, "positive")
#> [1] TRUE
ask_fact(x, "positive")
#> [1] NA

check_assumptions() tests an expression against a set of assumptions, failing_assumptions() lists the ones that fail, and assumptions() lists the known assumptions of an expression.

check_assumptions(S(-2), integer = TRUE, positive = TRUE)
#> [1] FALSE
check_assumptions(p + 1, positive = TRUE)
#> [1] TRUE
failing_assumptions(S(-2), integer = TRUE, positive = TRUE)
#> $positive
#> [1] FALSE
assumptions(x)
#> commutative 
#>        TRUE

Predicates with ask() and refine()

The second assumptions system keeps facts separate from symbols: predicates such as Q$positive(x) are queried with ask(), which returns TRUE, FALSE, or NA (unknown). Further arguments are assumptions to use.

ask(Q$positive(x))
#> [1] NA
ask(Q$positive(x^2 + 1), Q$real(x))
#> [1] TRUE
ask(Q$integer(x + y), Q$integer(x), Q$integer(y))
#> [1] TRUE
ask(Q$positive(x * y), Q$negative(x), Q$negative(y))
#> [1] TRUE
ask(Q$even(2 * n))
#> [1] TRUE
ask(Q$prime(7))
#> [1] TRUE
ask(Q$rational(sqrt(S(2))))
#> [1] FALSE

refine() simplifies an expression using assumptions.

refine(sqrt(x^2), Q$positive(x))
#> x
refine(Abs(x), Q$negative(x))
#> -x
refine((-1)^n, Q$even(n))
#> 1

assuming() evaluates an expression with assumptions added to the global context, and removes them afterwards.

assuming(Q$positive(x), refine(sqrt(x^2)))
#> x
assuming(Q$positive(x), ask(Q$real(x)))
#> [1] TRUE
ask(Q$real(x))
#> [1] NA

An AssumptionsContext is a set of facts. global_assumptions is the context that ask() always consults; assuming() adds to it temporarily.

ctx <- AssumptionsContext(Q$positive(x))
ctx
#> AssumptionsContext({Q.positive(x)})
global_assumptions
#> AssumptionsContext()

Custom predicates and other solvers

Predicate() creates a new predicate and AppliedPredicate() applies one to arguments. A predicate created with dispatch = TRUE answers through handlers added by register_predicate() for given expression types.

P <- Predicate("P")
P(x)
#> Q.P(x)
ask(P(x), P(x) & Q$positive(x))
#> [1] TRUE
AppliedPredicate(Q$integer, x)
#> Q.integer(x)
K <- Predicate("my_key", dispatch = TRUE)
register_predicate(K, "Symbol", function(expr, assumptions) TRUE)
ask(K(x))
#> [1] TRUE
ask(K(x + 1))
#> [1] NA

satask() decides a query with a SAT solver, and lra_satask() with linear real arithmetic, which also handles inequalities between real symbols:

y <- Symbol("y")
satask(Q$zero(Abs(y)), Q$zero(y))
#> [1] TRUE
satask(Q$positive(x * y), Q$positive(x) & Q$positive(y))
#> [1] TRUE
xr <- Symbol("xr", real = TRUE)
lra_satask(Q$gt(xr, 0), Q$gt(xr, 1))
#> [1] TRUE
lra_satask(Q$lt(xr, 0), Q$gt(xr, 1))
#> [1] FALSE
lra_satask(Q$eq(xr, 1), !Q$ne(xr, 0))
#> [1] FALSE

AssumptionsWrapper() reads the assumption properties of an expression under extra assumptions, and is_extended_real() and is_extended_nonnegative() are shortcuts for two of them.

AssumptionsWrapper(y)$is_positive
#> [1] NA
AssumptionsWrapper(y, Q$positive(y))$is_positive
#> [1] TRUE
is_extended_real(y, Q$extended_real(y))
#> [1] TRUE
is_extended_nonnegative(y, Q$positive(y))
#> [1] TRUE
is_extended_nonnegative(y)
#> [1] NA

The binary relation predicates Q$eq, Q$gt and the rest are BinaryRelation objects; binrel_properties() lists their properties, and AppliedBinaryRelation() applies one. test_closed_group() asks whether every term of a sum or product has a property, as SymPy’s handlers do.

AppliedBinaryRelation(StrictLessThanPredicate(), y, 2)
#> Q.lt(y, 2)
binrel_properties(Q$gt)
#> $is_reflexive
#> [1] FALSE
#> 
#> $is_symmetric
#> [1] FALSE
#> 
#> $reversed
#> Q.lt
#> 
#> $negated
#> Q.le
test_closed_group(y + 1, Q$positive(y), Q$positive)
#> [1] TRUE