Skip to content

Commit

Permalink
Merge pull request #263 from JuliaSymbolics/s/expand
Browse files Browse the repository at this point in the history
polynormalize -> expand
  • Loading branch information
shashi authored Apr 1, 2021
2 parents b9843c6 + 00a9a96 commit 5426f7d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 25 deletions.
2 changes: 2 additions & 0 deletions page/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ using SymbolicUtils # hide

{{doc simplify simplify fn}}

{{doc expand expand fn}}

{{doc substitute substitute fn}}

## Utilities
Expand Down
1 change: 1 addition & 0 deletions src/SymbolicUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ include("matchers.jl")
# Convert to an efficient multi-variate polynomial representation
import AbstractAlgebra.Generic: MPoly, PolynomialRing, ZZ, exponent_vector
using AbstractAlgebra: ismonomial, symbols
export expand
include("abstractalgebra.jl")

# Term ordering
Expand Down
12 changes: 11 additions & 1 deletion src/abstractalgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ end

<(a::MPoly, b::MPoly) = false

function polynormalize(x)
"""
expand(expr)
Expand expressions by distributing multiplication over addition.
`a*(b+c)` becomes `ab+ac`. `expand` uses [AbstractAlgebra.jl](https://nemocas.github.io/AbstractAlgebra.jl/latest/) to construct
dense Multi-variate polynomial to do this very fast.
"""
function expand(x)
to_term(x, to_mpoly(x)...)
end

Base.@deprecate polynormalize(x) expand(x)
15 changes: 10 additions & 5 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

"""
```julia
simplify(x; polynorm=false,
simplify(x; expand=false,
threaded=false,
thread_subtree_cutoff=100,
rewriter=nothing)
```
Simplify an expression (`x`) by applying `rewriter` until there are no changes.
`polynorm=true` applies `polynormalize` in the beginning of each fixpoint iteration.
`expand=true` applies [`expand`](/api/#expand) in the beginning of each fixpoint iteration.
"""
function simplify(x;
polynorm=false,
expand=false,
polynorm=nothing,
threaded=false,
thread_subtree_cutoff=100,
rewriter=nothing)
if polynorm !== nothing
Base.depwarn("simplify(..; polynorm=$polynorm) is deprecated, use simplify(..; expand=$polynorm) instead")
end

f = if rewriter === nothing
if threaded
threaded_simplifier(thread_subtree_cutoff)
elseif polynorm
serial_polynormal_simplifier
elseif expand
serial_expand_simplifier
else
serial_simplifier
end
Expand Down
8 changes: 4 additions & 4 deletions src/simplify_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ let
global serial_simplifier
global threaded_simplifier
global serial_simplifier
global serial_polynormal_simplifier
global serial_expand_simplifier

function default_simplifier(; kw...)
IfElse(has_trig,
Expand All @@ -141,8 +141,8 @@ let
threaded_simplifier(cutoff) = Fixpoint(default_simplifier(threaded=true,
thread_cutoff=cutoff))

serial_polynormal_simplifier = If(istree,
Fixpoint(Chain((polynormalize,
Fixpoint(default_simplifier())))))
serial_expand_simplifier = If(istree,
Fixpoint(Chain((expand,
Fixpoint(default_simplifier())))))

end
4 changes: 2 additions & 2 deletions test/fuzz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ using Random: seed!

seed!(6174)
@testset "Fuzz test" begin
@testset "polynormalize fuzz" begin
@testset "expand fuzz" begin
for i=1:500
fuzz_test(5, num_spec, SymbolicUtils.polynormalize; min_depth=3)
fuzz_test(5, num_spec, SymbolicUtils.expand; min_depth=3)
end
end
@testset "num fuzz" begin
Expand Down
2 changes: 1 addition & 1 deletion test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ ex = 1 + (:x - 2)
SymbolicUtils.symtype(::Expr) = Real
SymbolicUtils.symtype(::Symbol) = Real
@test simplify(ex) == -1 + :x
@test simplify(:a * (:b + -1 * :c) + -1 * (:b * :a + -1 * :c * :a), polynorm=true) == 0
@test simplify(:a * (:b + -1 * :c) + -1 * (:b * :a + -1 * :c * :a), expand=true) == 0
24 changes: 12 additions & 12 deletions test/nf.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using SymbolicUtils, Test
using SymbolicUtils: polynormalize, Term, symtype
using SymbolicUtils: Term, symtype
@testset "polyform" begin
@syms a b c d
@test polynormalize(a * (b + -1 * c) + -1 * (b * a + -1 * c * a)) == 0
@eqtest polynormalize(sin(a+b)+sin(c+d)) == sin(a+b) + sin(c+d)
@eqtest simplify(polynormalize(sin((a+b)^2)^2)) == simplify(sin(a^2+2*(b*a)+b^2)^2)
@test simplify(polynormalize(sin((a+b)^2)^2 + cos((a+b)^2)^2)) == 1
@test expand(a * (b + -1 * c) + -1 * (b * a + -1 * c * a)) == 0
@eqtest expand(sin(a+b)+sin(c+d)) == sin(a+b) + sin(c+d)
@eqtest simplify(expand(sin((a+b)^2)^2)) == simplify(sin(a^2+2*(b*a)+b^2)^2)
@test simplify(expand(sin((a+b)^2)^2 + cos((a+b)^2)^2)) == 1
@syms x1::Real f(::Real)::Real

# issue 193
@test isequal(polynormalize(f(x1 + 2.0)), f(2.0 + x1))
@test symtype(polynormalize(f(x1 + 2.0))) == Real
@test isequal(expand(f(x1 + 2.0)), f(2.0 + x1))
@test symtype(expand(f(x1 + 2.0))) == Real

# cleanup rules
@test polynormalize(Term{Number}(identity, 0)) == 0
@test polynormalize(Term{Number}(one, 0)) == 1
@test polynormalize(Term{Number}(zero, 0)) == 0
@test polynormalize(identity(a * b) - b * a) == 0
@test polynormalize(a * b - b * a) == 0
@test expand(Term{Number}(identity, 0)) == 0
@test expand(Term{Number}(one, 0)) == 1
@test expand(Term{Number}(zero, 0)) == 0
@test expand(identity(a * b) - b * a) == 0
@test expand(a * b - b * a) == 0
end

0 comments on commit 5426f7d

Please sign in to comment.