Skip to content

Commit 0d2f6b7

Browse files
authored
Merge pull request #266 from Suavesito-Olimpiada/suave/canon
Extend and document canonical form of `Symbolic{<:Number}`
2 parents 0dcbd29 + 75cee99 commit 0d2f6b7

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

page/index.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,24 @@ SymbolicUtils contains [a rule-based rewriting language](/rewrite/#rule-based_re
149149

150150
## Simplification
151151

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

154-
Commutativity and associativity are assumed over `+` and `*` operations on `Symbolic{<:Number}`.
154+
The rules with which the canonical form of `Symbolic{<:Number}` terms are constructed are the next (where `x isa Symbolic` and `c isa Number`)
155+
156+
- `0 + x`, `1 * x` and `x^1` always gives `x`
157+
- `0 * x` always gives `0` and `x ^ 0` gives `1`
158+
- `-x`, `1/x` and `x\1` get transformed into `(-1)*x`, `x^(-1)` and `x^(-1)`.
159+
- 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)
160+
- `x + ... + x` will be fused into `n*x` with type `Mul`
161+
- `x * ... * x` will be fused into `x^n` with type `Pow`
162+
- sum of `Add`'s are fused
163+
- product of `Mul`'s are fused
164+
- `c * (c₁x₁ + ... + cₙxₙ)` will be converted into `c*c₁*x₁ + ... + c*cₙ*xₙ`
165+
- `(x₁^c₁ * ... * xₙ^cₙ)^c` will be converted into `x₁^(c*c₁) * ... * xₙ^(c*cₙ)`
166+
- there are come other simplifications on construction that you can check [here](https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/src/methods.jl)
167+
168+
169+
Here is an example of this
155170

156171
```julia:simplify1
157172
2 * (w+w+α+β + sin(z)^2 + cos(z)^2 - 1)

src/types.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ function (::Type{<:Pow{T}})(a, b; metadata=NO_METADATA) where {T}
831831
Pow{T, typeof(a), typeof(b), typeof(metadata)}(a,b,metadata)
832832
end
833833
function Pow(a, b; metadata=NO_METADATA)
834-
Pow{promote_symtype(^, symtype(a), symtype(b))}(a, b, metadata=metadata)
834+
Pow{promote_symtype(^, symtype(a), symtype(b))}(makepow(a, b)..., metadata=metadata)
835835
end
836836
symtype(a::Pow{X}) where {X} = X
837837

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

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

850+
function makepow(a, b)
851+
base = a
852+
exp = b
853+
if a isa Pow
854+
base = a.base
855+
exp = a.exp * b
856+
end
857+
return (base, exp)
858+
end
859+
850860
^(a::SN, b) = Pow(a, b)
851861

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

test/basics.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ end
195195
@test x - x === 0
196196
@test isequal(-x, -1x)
197197
@test isequal(x^1, x)
198+
@test isequal((x^-1)*inv(x^-1), 1)
198199
end
199200
end
200201

0 commit comments

Comments
 (0)