Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend and document canonical form of Symbolic{<:Number} #266

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions page/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,24 @@ SymbolicUtils contains [a rule-based rewriting language](/rewrite/#rule-based_re

## Simplification

By default `*` and `+` operations apply the most basic simplification upon construction of the expression.
By default `+`, `*` and `^` operations apply the most basic simplification upon construction of the expression.

Commutativity and associativity are assumed over `+` and `*` operations on `Symbolic{<:Number}`.
The rules with which the canonical form of `Symbolic{<:Number}` terms are constructed are the next (where `x isa Symbolic` and `c isa Number`)

- `0 + x`, `1 * x` and `x^1` always gives `x`
- `0 * x` always gives `0` and `x ^ 0` gives `1`
- `-x`, `1/x` and `x\1` get transformed into `(-1)*x`, `x^(-1)` and `x^(-1)`.
- commutativity and associativity over `+` and `*` are assumed. Re-ordering of terms will be done under a [total order](https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/src/ordering.jl)
- `x + ... + x` will be fused into `n*x` with type `Mul`
- `x * ... * x` will be fused into `x^n` with type `Pow`
- sum of `Add`'s are fused
- product of `Mul`'s are fused
- `c * (c₁x₁ + ... + cₙxₙ)` will be converted into `c*c₁*x₁ + ... + c*cₙ*xₙ`
- `(x₁^c₁ * ... * xₙ^cₙ)^c` will be converted into `x₁^(c*c₁) * ... * xₙ^(c*cₙ)`
- there are come other simplifications on construction that you can check [here](https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/src/methods.jl)


Here is an example of this

```julia:simplify1
2 * (w+w+α+β + sin(z)^2 + cos(z)^2 - 1)
Expand Down
12 changes: 11 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ function (::Type{<:Pow{T}})(a, b; metadata=NO_METADATA) where {T}
Pow{T, typeof(a), typeof(b), typeof(metadata)}(a,b,metadata)
end
function Pow(a, b; metadata=NO_METADATA)
Pow{promote_symtype(^, symtype(a), symtype(b))}(a, b, metadata=metadata)
Pow{promote_symtype(^, symtype(a), symtype(b))}(makepow(a, b)..., metadata=metadata)
end
symtype(a::Pow{X}) where {X} = X

Expand All @@ -847,6 +847,16 @@ Base.isequal(p::Pow, b::Pow) = isequal(p.base, b.base) && isequal(p.exp, b.exp)

Base.show(io::IO, p::Pow) = show_term(io, p)

function makepow(a, b)
base = a
exp = b
if a isa Pow
base = a.base
exp = a.exp * b
end
return (base, exp)
end

^(a::SN, b) = Pow(a, b)

^(a::SN, b::SN) = Pow(a, b)
Expand Down
1 change: 1 addition & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,6 @@ end
@test x - x === 0
@test isequal(-x, -1x)
@test isequal(x^1, x)
@test isequal((x^-1)*inv(x^-1), 1)
end
end