Symbolic results are only useful when they can be put in a convenient
form. symcompR ports SymPy’s simplification module: a general purpose
simplify(), and a set of targeted functions that each
perform one well defined transformation. The targeted functions are
predictable and fast; simplify() tries many of them and
keeps the shortest result.
library(symcompR)
x <- Symbol("x"); y <- Symbol("y"); z <- Symbol("z")
a <- Symbol("a"); b <- Symbol("b"); c <- Symbol("c")
n <- Symbol("n", integer = TRUE); k <- Symbol("k", integer = TRUE)
simplify()
simplify() applies a sequence of heuristics
(cancellation, trigonometric, power and gamma function simplification,
and others) and returns the simplest form it finds, measured by
count_ops().
simplify(sin(x)^2 + cos(x)^2)
#> 1
simplify((x^3 + x^2 - x - 1) / (x^2 + 2 * x + 1))
#> x - 1
simplify(gamma(x) / gamma(x - 2))
#> (x - 2)*(x - 1)
simplify(exp(x) * exp(y) / exp(x + y - 1))
#> E
simplify(2 * sin(x) * cos(x))
#> sin(2*x)
count_ops(sin(x)^2 + cos(x)^2)
#> [1] 5simplify() never applies an identity that is false for
some values of the symbols. \sqrt{x^2} =
x only holds for nonnegative x,
so it is left alone until the symbol carries an assumption:
Because the result of simplify() depends on heuristics,
code that needs a particular form should call the targeted function
directly. The rest of this vignette describes those functions.
Expansion
expand() multiplies out products and powers of sums.
expand((x + 1)^2)
#> x^2 + 2*x + 1
expand((x + 2) * (x - 3))
#> x^2 - x - 6
expand((x + y)^2 * (x - y))
#> x^3 + x^2*y - x*y^2 - y^3
expand((x + 1)^2 / (x - 1))
#> x^2/(x - 1) + 2*x/(x - 1) + 1/(x - 1)It accepts hints that switch on other kinds of expansion. Each hint
also exists as a standalone function: expand_log(),
expand_trig(), expand_power_exp(),
expand_power_base(), expand_complex(),
expand_func(), expand_mul(), and
expand_multinomial().
Logarithms and powers of products only split when the symbols allow
it, or when force = TRUE tells symcompR to ignore
assumptions:
expand(log(x * y), log = TRUE)
#> log(x*y)
expand_log(log(x * y), force = TRUE)
#> log(x) + log(y)
expand_log(log(a^2 * b), force = TRUE)
#> 2*log(a) + log(b)
expand_power_base((x * y)^z, force = TRUE)
#> x^z*y^z
expand_power_exp(exp(x + y))
#> exp(x)*exp(y)Trigonometric, complex, and special function expansion:
expand_trig(sin(x + y))
#> sin(x)*cos(y) + sin(y)*cos(x)
expand_trig(cos(2 * x))
#> 2*cos(x)^2 - 1
expand_complex(exp(x + sym_I * y))
#> I*exp(re(x) - im(y))*sin(re(y) + im(x)) + exp(re(x) - im(y))*cos(re(y) + im(x))
expand_func(gamma(x + 3))
#> x*(x + 1)*(x + 2)*gamma(x)
expand_mul(x * (y + 1))
#> x*y + x
expand_multinomial((x + y + 1)^2)
#> x^2 + 2*x*y + 2*x + y^2 + 2*y + 1Factoring
factor_sym() (also sym_factor()) factors a
polynomial into irreducible factors over the rationals. It works for
multivariate polynomials too.
factor_sym(x^3 - x^2 + x - 1)
#> (x - 1)*(x^2 + 1)
factor_sym(x^2 * z + 4 * x * y * z + 4 * y^2 * z)
#> z*(x + 2*y)^2
factor_sym(x^4 - 4)
#> (x^2 - 2)*(x^2 + 2)Other domains are selected with options: an algebraic extension, the Gaussian integers, or a finite field.
factor_sym(x^4 - 4, extension = sqrt(S(2)))
#> (x - sqrt(2))*(x + sqrt(2))*(x^2 + 2)
factor_sym(x^2 + 1, gaussian = TRUE)
#> (x - I)*(x + I)
factor_sym(x^4 + 1, modulus = 2)
#> (x + 1)^4factor_list() returns the content and a list of factors,
each paired with its multiplicity. sqf() computes only the
square free decomposition, which is much cheaper.
fl <- factor_list(x^5 - x^3)
fl$coeff
#> 1
vapply(fl$factors, function(f) paste0("(", sstr(f[[1]]), ")^", f[[2]]), "")
#> [1] "(x - 1)^1" "(x + 1)^1" "(x)^3"
sqf(x^5 - x^3)
#> x^3*(x^2 - 1)factor_terms() pulls out common factors without
factoring the remaining polynomial, and collect_const()
groups numerical coefficients.
factor_terms(3 * x + 3 * y)
#> 3*(x + y)
factor_terms(x * (x + x * y) + x^2)
#> x^2*(y + 2)
collect_const(2 * x + 2 * y + 3 * z)
#> 3*z + 2*(x + y)Collecting terms
collect() groups the terms of a sum by powers of an
expression. coeff() extracts one coefficient.
e <- x * y + x - 3 + 2 * x^2 - z * x^2 + x^3
collect(e, x)
#> x^3 + x^2*(2 - z) + x*(y + 1) - 3
coeff(e, x, 2)
#> 2 - z
collect(a * x^2 + b * x^2 + a * x - b * x + c, x)
#> c + x^2*(a + b) + x*(a - b)
collect(a * sin(2 * x) + b * sin(2 * x), sin(2 * x))
#> (a + b)*sin(2*x)
rcollect((x + y * x) * (1 + x) + x, x)
#> x*((x + 1)*(y + 1) + 1)Rational functions
cancel() puts a rational function in the canonical form
p/q, with p and q
expanded polynomials without common factors. together()
combines terms over a common denominator without expanding, and
ratsimp() does both.
cancel((x^2 + 2 * x + 1) / (x^2 + x))
#> (x + 1)/x
cancel(1 / x + (3 * x / 2 - 2) / (x - 4))
#> (3*x^2 - 2*x - 8)/(2*x^2 - 8*x)
cancel((x * y^2 - 2 * x * y * z + x * z^2 + y^2 - 2 * y * z + z^2) / (x^2 - 1))
#> (y^2 - 2*y*z + z^2)/(x - 1)
together(1 / x + 1 / y + 1 / z)
#> (x*y + x*z + y*z)/(x*y*z)
together(1 / (x + 1) + 1 / (x - 1))
#> 2*x/((x - 1)*(x + 1))
ratsimp(1 / x + 1 / y)
#> (x + y)/(x*y)
fraction((x + 1) / x^2)
#> [[1]]
#> x + 1
#>
#> [[2]]
#> x^2apart() performs the partial fraction decomposition, the
inverse of together(). With full = TRUE it
uses the Bronstein algorithm and splits over the roots of the
denominator; doit() writes the result out explicitly.
apart((4 * x^3 + 21 * x^2 + 10 * x + 12) / (x^4 + 5 * x^3 + 5 * x^2 + 4 * x))
#> (2*x - 1)/(x^2 + x + 1) - 1/(x + 4) + 3/x
apart(1 / (x^2 + 1) / (x - 1))
#> -(x + 1)/(2*(x^2 + 1)) + 1/(2*(x - 1))
doit(apart(1 / (x^3 - 2), full = TRUE))
#> (-2^(1/3)/2 - 2^(1/3)*sqrt(3)*I/2)/(6*(x + 2^(1/3)/2 + 2^(1/3)*sqrt(3)*I/2)) + (-2^(1/3)/2 + 2^(1/3)*sqrt(3)*I/2)/(6*(x + 2^(1/3)/2 - 2^(1/3)*sqrt(3)*I/2)) + 2^(1/3)/(6*(x - 2^(1/3)))Trigonometric simplification
trigsimp() simplifies expressions with trigonometric and
hyperbolic functions using the Fu et al. algorithm, and
exptrigsimp() converts between exponential and hyperbolic
forms.
trigsimp(sin(x)^4 - 2 * cos(x)^2 * sin(x)^2 + cos(x)^4)
#> cos(4*x)/2 + 1/2
trigsimp(sin(x) * tan(x) / sec(x))
#> sin(x)^2
trigsimp(cosh(x)^2 + sinh(x)^2)
#> cosh(2*x)
exptrigsimp(exp(z) + exp(-z))
#> 2*cosh(z)
fu() and the TR rules
fu() is the full Fu, Zhong and Zeng algorithm. It
searches combinations of transformation rules for the result with the
fewest trigonometric functions.
fu(sin(S(50))^2 + cos(S(50))^2 + sin(sym_pi / 6))
#> 3/2
fu(sqrt(S(3)) * cos(x) / 2 + sin(x) / 2)
#> sin(x + pi/3)
fu(1 - sin(2 * x)^2 / 4 - sin(y)^2 - cos(x)^4)
#> -cos(x)^2 + cos(y)^2
fu(cos(4 * sym_pi / 9))
#> sin(pi/18)
fu(cos(x) * cos(2 * x) * cos(4 * x))
#> sin(8*x)/(8*sin(x))Each individual rule is exported under its name in the paper, from
TR0() to TR22(), together with
TRmorrie(), TR10i(), and others, so a
transformation can be applied by hand:
TR2(tan(x)) # tan and cot to sin and cos
#> sin(x)/cos(x)
TR5(sin(x)^4) # sin^2 to 1 - cos^2
#> (1 - cos(x)^2)^2
TR8(cos(2 * x) * cos(3 * x)) # products to sums
#> cos(x)/2 + cos(5*x)/2
TR10(sin(a + b)) # expand sums of angles
#> sin(a)*cos(b) + sin(b)*cos(a)
TR10i(cos(S(1)) * cos(S(3)) + sin(S(1)) * sin(S(3))) # and back
#> cos(2)
TR11(sin(2 * x)) # double angles
#> 2*sin(x)*cos(x)
TRmorrie(cos(x) * cos(2 * x))
#> sin(4*x)/(4*sin(x))
TR22(1 + tan(x)^2)
#> sec(x)^2The remaining rules cover reciprocal functions, products of tangents, squares, and powers:
TR0(x * (x + 1)) # plain simplification step
#> x^2 + x
TR1(2 * csc(x) + sec(x)) # sec and csc to 1/cos and 1/sin
#> 1/cos(x) + 2/sin(x)
TR111(1 - 1 / tan(x)^2) # negative powers to reciprocal functions
#> 1 - cot(x)^2
TR2i(sin(x) / cos(x)) # ratios of sin and cos to tan
#> tan(x)
TR3(cos(sym_pi / 2 + x)) # induced angles
#> -sin(x)
lapply(list(sin, cos, tan), function(f) TR4(f(sym_pi / 6))) # special angles
#> [[1]]
#> 1/2
#>
#> [[2]]
#> sqrt(3)/2
#>
#> [[3]]
#> sqrt(3)/3
TR6(cos(x)^4) # cos^2 to 1 - sin^2
#> (1 - sin(x)^2)^2
TR7(cos(x)^2) # lower the degree of cos^2
#> cos(2*x)/2 + 1/2
TR9(cos(S(1)) + cos(S(2))) # sums of cos or sin to products
#> 2*cos(1/2)*cos(3/2)
TR12(tan(x + y)) # expand tan of a sum
#> (tan(x) + tan(y))/(-tan(x)*tan(y) + 1)
TR12i((tan(x) + tan(y)) / (1 - tan(x) * tan(y))) # and back
#> tan(x + y)
TR13(tan(S(3)) * tan(S(2))) # products of tan or cot
#> -tan(2)/tan(5) - tan(3)/tan(5) + 1
TR14((cos(x) - 1) * (cos(x) + 1)) # factored powers of sin and cos
#> -sin(x)^2
TR15(1 - 1 / sin(x)^2) # negative powers of sin
#> -cot(x)^2
TR16(1 - 1 / cos(x)^2) # negative powers of cos
#> -tan(x)^2
TRpower(sin(x)^3) # powers to sums of multiple angles
#> 3*sin(x)/4 - sin(3*x)/4futrig() applies a fixed sequence of these rules,
trigsimp_old() is the older pattern based
trigsimp(), and trigsimp_groebner() simplifies
with a Groebner basis of the trigonometric identities.
hyper_as_trig() rewrites hyperbolic functions as
trigonometric ones and returns a function that undoes the change.
Powers and logarithms
powsimp() combines powers with the same base or the same
exponent. Combining bases is only valid for suitable assumptions, so it
needs force = TRUE for general symbols.
combine selects which of the two transformations to
apply.
powsimp(x^a * x^b)
#> x^(a + b)
powsimp(x^a * y^a)
#> x^a*y^a
powsimp(x^a * y^a, force = TRUE)
#> (x*y)^a
powsimp(exp(x) * exp(y))
#> exp(x + y)
powsimp(x^a * x^b * y^a * y^b, combine = "exp")
#> x^(a + b)*y^(a + b)
powsimp(x^a * x^b * y^a * y^b, combine = "base", force = TRUE)
#> (x*y)^a*(x*y)^bpowdenest() simplifies powers of powers, and
logcombine() merges sums of logarithms.
powdenest((x^a)^b, force = TRUE)
#> x^(a*b)
powdenest(sqrt(x^2), force = TRUE)
#> x
logcombine(log(x) + log(y), force = TRUE)
#> log(x*y)
logcombine(a * log(x) + log(y) - log(z), force = TRUE)
#> log(x^a*y/z)Radicals
radsimp() rationalizes denominators, and
sqrtdenest() removes nested square roots when possible.
Recognizing numbers
nsimplify() finds a simple exact expression that is
close to a floating point number. A list of constants can be offered as
building blocks.
Gamma functions and combinatorial expressions
gammasimp() simplifies products and ratios of gamma
functions, and combsimp() does the same for factorials and
binomial coefficients.
gammasimp(gamma(x) / gamma(x - 3))
#> (x - 3)*(x - 2)*(x - 1)
gammasimp(gamma(x) * gamma(1 - x))
#> pi/sin(pi*x)
combsimp(sym_factorial(n) / sym_factorial(n - 3))
#> n*(n - 2)*(n - 1)
combsimp(sym_binomial(n + 1, k + 1) / sym_binomial(n, k))
#> (n + 1)/(k + 1)Special functions
hyperexpand() rewrites generalized hypergeometric
functions in terms of elementary or simpler special functions, and
besselsimp() simplifies Bessel functions.
hyperexpand(hyper(list(1, 1), list(2), z))
#> -log(1 - z)/z
besselsimp(besselj(Rational(1, 2), z))
#> sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))Other helpers
signsimp() moves minus signs into a canonical position,
separatevars() writes an expression as a product of factors
that each depend on fewer symbols, and cse() finds common
subexpressions, which is useful before generating code.
signsimp(exp(-(x - y)))
#> exp(-x + y)
separatevars(2 * x^2 * z * sin(y) + 2 * z * x^2)
#> 2*x^2*z*(sin(y) + 1)
res <- cse(list((x + y)^2 + sin(x + y), cos(x + y)))
res$replacements[[1]]
#> [[1]]
#> x0
#>
#> [[2]]
#> x + y
res$reduced
#> [[1]]
#> x0^2 + sin(x0)
#>
#> [[2]]
#> cos(x0)cse() returns the replacements as pairs (here x_0 = x + y) and the reduced expressions
written in terms of the new symbols.
More specialized simplifiers
collect_sqrt() collects terms with the same square root,
collect_abs() merges products of absolute values, and
split_surds() and rad_rationalize() are the
helpers behind radsimp().
collect_sqrt(sqrt(S(2)) * x + sqrt(S(2)) * y + sqrt(S(3)))
#> sqrt(2)*(x + y) + sqrt(3)
collect_abs(Abs(x) * Abs(y) + 1 / Abs(x))
#> Abs(1/x) + Abs(x*y)
show <- function(l) vapply(l, sstr, "")
show(split_surds(3 * sqrt(S(3)) + sqrt(S(5)) / 7 + sqrt(S(6)) +
sqrt(S(10)) + sqrt(S(15))))
#> [1] "3" "sqrt(2) + sqrt(5) + 3" "sqrt(5)/7 + sqrt(10)"
show(rad_rationalize(1, sqrt(S(2)) + 1))
#> [1] "-1 + sqrt(2)" "1"inversecombine() cancels a function against its inverse,
nc_simplify() shortens products of noncommutative symbols,
sum_simplify() and product_simplify() join
adjacent sums and products, and kroneckersimp() simplifies
products of Kronecker deltas.
inversecombine(asin(sin(x)))
#> x
inversecombine(exp(log(x)) + log(exp(y)))
#> x + y
A <- Symbol("A", commutative = FALSE); B <- Symbol("B", commutative = FALSE)
nc_simplify(A * B * A * B * A * B)
#> (A*B)^3
sum_simplify(Sum(x, list(x, 1, n)) + Sum(x, list(x, n + 1, 2 * n)))
#> Sum(x, (x, 1, 2*n))
product_simplify(Product(x, list(x, 1, n)) * Product(x, list(x, n + 1, 2 * n)))
#> Product(x, (x, 1, 2*n))
kroneckersimp(1 + KroneckerDelta(0, k) * KroneckerDelta(1, k))
#> 1ratsimpmodprime() simplifies a rational function modulo
the ideal generated by a list of polynomials.
ratsimpmodprime((x + y^5 + y) / (x - y), list(x * y^5 - x - y), x, y,
order = "lex")
#> (-x^2 - x*y - x - y)/(-x^2 + x*y)posify() replaces symbols without a sign by positive
dummies, which allows simplifications that are only valid for positive
values, and returns the substitution that undoes it.
p <- posify(1 + x)
p$expr
#> _x + 1
show(p$reps[[1]])
#> [1] "_x" "x"piecewise_simplify() merges and drops branches of a
Piecewise expression, and
simplify_univariate() is the helper that rewrites a
condition in one variable as a union of intervals. denoms()
lists the denominators of an expression, and
distribute_or_over_and() converts a logical expression to
disjunctive normal form.
piecewise_simplify(Piecewise(list(x, Eq(x, 0)), list(0, TRUE)))
#> 0
simplify_univariate(Or(And(x > 0, x < 2), And(x > 1, x < 3)))
#> ((1 <= x) & (x <= 2)) | ((1 <= x) & (x < 3)) | ((x <= 2) & (0 < x))
show(denoms(1 / x + 1 / y + 1 / (x * y)))
#> [1] "x" "y"
distribute_or_over_and(And(Or(Not(a), b), c))
#> (b & c) | (c & ~a)