There are a few options in `Julia` for symbolic math, for example, the `SymPy` package which wraps a Python library. This section describes a collection of native `Julia` packages providing many features of symbolic math.
The `Symbolics` package bills itself as a "fast and modern Computer Algebra System (CAS) for a fast and modern programming language." This package relies on the `SymbolicUtils` package and is built upon by the `ModelingToolkit` package, which we don't describe here.
Symbolic math at its core involves symbolic variables, which essentially defer evaluation until requested. The creation of symbolic variables differs between the two packages discussed here.
`SymbolicUtils` creates variables which carry `Julia` type information (e.g. `Int`, `Float64`, ...). This type information carries through operations involving these variables. Symbolic variables can be created with the `@syms` macro. For example
This creates `x` a symbolic value with symbolic type `Number`, `y` a symbolic variable holding integer values, and `f` a symbolic function of a single real variable outputting a real variable.
The `SymPy` package also has an `@syms` macro to create variables. Though their names agree, they do different things. Using both packages together would require qualifying many shared method names. For `SymbolicUtils`, the `@syms` macro uses `Julia` types to parameterize the variables. In `SymPy` it is possible to specify *assumptions* on the variables, but that is different and not useful for dispatch without some extra effort.
For `Symbolics`, symbolic variables are created using a wrapper around an underlying `SymbolicUtils` object. This wrapper, `Num`, is a subtype of `Real` (the underlying `SymbolicUtils` object may have symbolic type `Real`, but it won't be a subtype of `Real`.)
Symbolic values are created with the `@variables` macro. For example:
```{julia}
@variables x y::Int z[1:3]::Int f(..)::Int
```
This creates
* a symbolic value `x` of `symtype` `Real`
* a symbolic value `y` of `symtype` `Int`
* a vector of symbolic values each of `symtype` `Int`
* a symbolic function `f` returning an object of `symtype` `Int`
The symbolic type reflects that of the underlying object behind the `Num` wrapper:
```{julia}
typeof(x), symtype(x), typeof(Symbolics.value(x))
```
(The `value` method unwraps the `Num` wrapper.)
## Symbolic expressions
Symbolic expressions are built up from symbolic variables through natural `Julia` idioms. `SymbolicUtils` privileges a few key operations: `Add`, `Mul`, `Pow`, and `Div`. For examples:
```{julia}
@syms x y
typeof(x + y) # `Add`
```
```{julia}
typeof(x * y) # `Mul`
```
Whereas, applying a function leaves a different type:
```{julia}
typeof(sin(x))
```
The `Term` wrapper just represents the effect of calling a function (in this case `sin`) on its arguments (in this case `x`).
This happens in the background with symbolic variables in `Symbolics`:
The `TermInterface` package is used by `SymbolicUtils` to explore the tree structure of an expression. The main methods are (cf. [SymbolicUtils.jl](https://symbolicutils.juliasymbolics.org/#expression_interface)):
The last one, is not likely to be an integer, but that is the inferred type in this case.
##### Example
As an example, we write a function to find the free symbols in a symbolic expression comprised of `SymbolicUtils` variables. (The `Symbolics.get_variables` also does this task.) To find the symbols involves walking the expression tree until a leaf node is found and then adding that to our collection if it matches `issym`.
This defines a symbolic expression, then substitutes the value `1` in for `x`. The `Pair` notation is useful for a *single* substitution. When there is more than one substitution, a dictionary is used:
```{julia}
w = x^3 + y^3 - 2z^3
substitute(w, Dict(x=>2, y=>3))
```
The `fold` argument can be passed `false` to inhibit evaluation of values. Compare:
Algebraic operations with symbolic values can involve an exponentially increasing number of terms. As such, some simplification rules are applied after an operation to reduce the complexity of the computed value.
The `simplify` function applies a series of rewriting rule until the expression stabilizes. The rewrite rules can be user generated, if desired. For example, the Pythagorean identity of trigonometry, just used, can be implement with this rule:
```{julia}
r = @acrule(sin(~x)^2 + cos(~x)^2 => one(~x))
ex |> Symbolics.value |> r |> Num
```
The rewrite rule, `r`, is defined by the `@acrule` macro. The `a` is for associative, the `c` for commutative, assumptions made by the macro. (The `c` means `cos(x)^2 + sin(x)^2` will also simplify.) Rewrite rules are called on the underlying `SymbolicUtils` expression, so we first unwrap, then after re-wrap.
The above expression for `r` is fairly easy to appreciate. The value `~x` matches the same variable or expression. So the above rule will also simplify more complicated expressions:
Rewrite rules when applied return the rewritten expression, if there is a match, or `nothing`.
Rules involving two values are also easily created. This one, again, comes from the set of simplifications defined for trigonometry and exponential simplifications:
This rule is not commutative or associative, as `x^y` is not the same as `y^x` and `(x^y)^z` is not `x^(y^z)` in general.
The application of rules can be filtered through qualifying predicates. This artificial example uses `iseven` which returns `true` for even numbers. Here we subtract `1` when a number is not even, and otherwise leave the number alone. We do this with two rules:
```{julia}
reven = @rule ~x::iseven => ~x
rodd = @rule ~x::(!iseven) => ~x - 1
r = SymbolicUtils.Chain([rodd, reven])
r(2), r(3)
```
The `Chain` function conveniently allows the sequential application of rewrite rules.
The notation `~x` is called a "slot variable" in the [documentation](https://symbolicutils.juliasymbolics.org/rewrite/) for `SymbolicUtils`. It matches a single expression. To match more than one expression, a "segment variable", denoted with two `~`s is used.
### Creating functions
By utilizing the tree-like nature of a symbolic expression, a `Julia` expression can be built from an symbolic expression easily enough. The `Symbolics.toexpr` function does this:
This output shows an internal representation of the steps for computing the value `ex` given different inputs. (The number `(-1)` multiplies `x`, this is added to `z` and the result passed to `exp`. That values is then used as the base for `^` with exponent `y`.)
Such `Julia` expressions are one step away from building `Julia` functions for evaluating symbolic expressions fast (though with some technical details about "world age" to be reckoned with). The `build_function` function with the argument `expression=Val(false)` will compile a `Julia` function:
```{julia}
h = build_function(ex, x, y, z; expression=Val(false))
h(1, 2, 3)
```
The above is *similar* to substitution:
```{julia}
substitute(ex, Dict(x=>1, y=>2, z=>3))
```
However, `build_function` will be **significantly** more performant, which when many function calls are used – such as with plotting – is a big advantage.
:::{.callout-note}
## Note
The documentation colorfully says "`build_function` is kind of like if `lambdify` (from `SymPy`) ate its spinach."
:::
The above, through passing $3$ variables after the expression, creates a function of $3$ variables. Functions of a vector of inputs can also be created, just by expressing the variables in that manner:
Using `Plots`, the plotting of symbolic expressions is similar to the plotting of a function, as there is a plot recipe that converts the expression into a function via `build_function`.
Expressions to be plotted can represent multivariate functions.
```{julia}
@variables x y
ex = 3*(1-x)^2*exp(-x^2 - (y+1)^2) - 10(x/5-x^3-y^5)*exp(-x^2-y^2) - 1/3*exp(-(x+1)^2-y^2)
xs = ys = range(-5, 5, length=100)
surface(xs, ys, ex)
```
The ordering of the variables is determined by `Symbolics.get_variables`:
```{julia}
Symbolics.get_variables(ex)
```
### Polynomial manipulations
There are some facilities for manipulating polynomial expressions in `Symbolics`. A polynomial, mathematically, is an expression involving one or more symbols with coefficients from a collection that has, at a minimum, addition and multiplication defined. The basic building blocks of polynomials are *monomials*, which are comprised of products of powers of the symbols. Mathematically, monomials are often allowed to have a multiplying coefficient and may be just a coefficient (if each symbol is taken to the power $0$), but here we consider just expressions of the type $x_1^{a_1} \cdot x_2^{a_2} \cdots x_k^{a_k}$ with the $a_i > 0$ as monomials.
With this understanding, then an expression can be broken up into monomials with a possible leading coefficient (possibly $1$) *and* terms which are not monomials (such as a constant or a more complicated function of the symbols). This is what is returned by the `polynomial_coeffs` function.
The first term output is dictionary with keys which are the monomials and with values which are the coefficients. The second term, the residual, is all the remaining parts of the expression, in this case just the constant `c`.
The above has `a,b,c` as parameters and `x` as the symbol. This separation is designated by passing the desired polynomial symbols to `polynomial_coeff` as an iterable. (Above as a $1$-element tuple.)
More complicated polynomials can be similarly decomposed:
```{julia}
@variables a b c x y z
ex = a*x^2*y*z + b*x*y^2*z + c*x*y*z^2
d, r = polynomial_coeffs(ex, (x, y, z))
```
The (sparse) decomposition of the polynomial is returned through `d`. The same pattern as above can be used to reconstruct the expression. To extract the coefficient for a monomial term, indexing can be used. Of note, is an expression like `x^2*y*z` could *possibly* not equal the algebraically equal `x*y*z*x`, as they are only equal after some simplification, but the keys are in a canonical form, so this is not a concern:
```{julia}
d[x*y*z*x], d[z*y*x^2]
```
The residual term will capture any non-polynomial terms:
```{julia}
ex = sin(x) - x + x^3/6
d, r = polynomial_coeffs(ex, (x,))
r
```
To find the degree of a monomial expression, the `degree` function is available. Here it is applied to each monomial in `d`:
```{julia}
[degree(k) for (k,v) ∈ d]
```
The `degree` function will also identify the degree of more complicated terms:
```{julia}
degree(1 + x + x^2)
```
Mathematically the degree of the $0$ polynomial may be $-1$ or undefined, but here it is $0$:
```{julia}
degree(0), degree(1), degree(x), degree(x^a)
```
The coefficients are returned as *values* of a dictionary, and dictionaries are unsorted. To have a natural map between polynomials of a single symbol in the standard basis and a vector, we could use a pattern like this:
```{julia}
@variables x a0 as[1:10]
p = a0 + sum(as[i]*x^i for i ∈ eachindex(collect(as)))
d, r = polynomial_coeffs(p, (x,))
d
```
To sort the values we can use a pattern like the following:
```{julia}
vcat(r, [d[k] for k ∈ sort(collect(keys(d)), by=degree)])
```
---
Rational expressions can be decomposed into a numerator and denominator using the following idiom, which ensures the outer operation is division (a binary operation):
```{julia}
@variables x
ex = (1 + x + x^2) / (1 + x + x^2 + x^3)
function nd(ex)
ex1 = Symbolics.value(ex)
(operation(ex1) == /) || return (ex, one(ex))
Num.(arguments(ex1))
end
nd(ex)
```
With this, the study of asymptotic behaviour of a univariate rational expression would involve an investigation like the following:
```{julia}
m,n = degree.(nd(ex))
m > n ? "limit is infinite" : m < n ? "limit is 0" : "limit is a constant"
Using `Differential`, differential equations can be specified. An example was given in [ODEs](../ODEs/differential_equations.html), using `ModelingToolkit`.
Higher order derivatives can be done through composition:
The `SymbolicNumericIntegration` package provides a means to integrate *univariate* expressions through its `integrate` function.
Symbolic integration can be approached in different ways. SymPy implements part of the Risch algorithm in addition to other algorithms. Rules-based algorithms could also be implemented.
The `SymbolicNumericIntegration` package includes many more predicates for doing rules-based integration, but it primarily approaches the task in a different manner.
If $f(x)$ is to be integrated, a set of *candidate* answers is generated. The following is **proposed** as an answer: $\sum q_i \Theta_i(x)$. Differentiating the proposed answer leads to a *linear system of equations* that can be solved.
The example in the [paper](https://arxiv.org/pdf/2201.12468v2.pdf) describing the method is with $f(x) = x \sin(x)$ and the candidate thetas are ${x, \sin(x), \cos(x), x\sin(x), x\cos(x)}$ so that the propose answer is:
The package provides an algorithm for the creation of candidates and the means to solve when possible. The `integrate` function is the main entry point. It returns three values: `solved`, `unsolved`, and `err`. The `unsolved` is the part of the integrand which can not be solved through this package. It is `0` for a given problem when `integrate` is successful in identifying an antiderivative, in which case `solved` is the answer. The value of `err` is a bound on the numerical error introduced by the algorithm.
This returns `exp(x^2)` for the unsolved part, as this function has no simple antiderivative.
Powers of trig functions have antiderivatives, as can be deduced using integration by parts. When the fifth power is used, there is a numeric aspect to the algorithm that is seen:
```{julia}
u,v,w = integrate(sin(x)^5)
```
The derivative of `u` matches up to some numeric tolerance:
The integration of rational functions (ratios of polynomials) can be done algorithmically, provided the underlying factorizations can be identified. The `SymbolicNumericIntegration` package has a function `factor_rational` that can identify factorizations.
We can see a bit of how much through the following, which needs a tolerance set to identify the rational numbers of the mathematical factorization correctly: