Skip to content

Commit 5426f7d

Browse files
authored
Merge pull request #263 from JuliaSymbolics/s/expand
polynormalize -> expand
2 parents b9843c6 + 00a9a96 commit 5426f7d

File tree

8 files changed

+43
-25
lines changed

8 files changed

+43
-25
lines changed

page/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ using SymbolicUtils # hide
4848

4949
{{doc simplify simplify fn}}
5050

51+
{{doc expand expand fn}}
52+
5153
{{doc substitute substitute fn}}
5254

5355
## Utilities

src/SymbolicUtils.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ include("matchers.jl")
3838
# Convert to an efficient multi-variate polynomial representation
3939
import AbstractAlgebra.Generic: MPoly, PolynomialRing, ZZ, exponent_vector
4040
using AbstractAlgebra: ismonomial, symbols
41+
export expand
4142
include("abstractalgebra.jl")
4243

4344
# Term ordering

src/abstractalgebra.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ end
145145

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

148-
function polynormalize(x)
148+
"""
149+
expand(expr)
150+
151+
Expand expressions by distributing multiplication over addition.
152+
153+
`a*(b+c)` becomes `ab+ac`. `expand` uses [AbstractAlgebra.jl](https://nemocas.github.io/AbstractAlgebra.jl/latest/) to construct
154+
dense Multi-variate polynomial to do this very fast.
155+
"""
156+
function expand(x)
149157
to_term(x, to_mpoly(x)...)
150158
end
159+
160+
Base.@deprecate polynormalize(x) expand(x)

src/api.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22

33
"""
44
```julia
5-
simplify(x; polynorm=false,
5+
simplify(x; expand=false,
66
threaded=false,
77
thread_subtree_cutoff=100,
88
rewriter=nothing)
99
```
1010
1111
Simplify an expression (`x`) by applying `rewriter` until there are no changes.
12-
`polynorm=true` applies `polynormalize` in the beginning of each fixpoint iteration.
12+
`expand=true` applies [`expand`](/api/#expand) in the beginning of each fixpoint iteration.
1313
"""
1414
function simplify(x;
15-
polynorm=false,
15+
expand=false,
16+
polynorm=nothing,
1617
threaded=false,
1718
thread_subtree_cutoff=100,
1819
rewriter=nothing)
20+
if polynorm !== nothing
21+
Base.depwarn("simplify(..; polynorm=$polynorm) is deprecated, use simplify(..; expand=$polynorm) instead")
22+
end
23+
1924
f = if rewriter === nothing
2025
if threaded
2126
threaded_simplifier(thread_subtree_cutoff)
22-
elseif polynorm
23-
serial_polynormal_simplifier
27+
elseif expand
28+
serial_expand_simplifier
2429
else
2530
serial_simplifier
2631
end

src/simplify_rules.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ let
118118
global serial_simplifier
119119
global threaded_simplifier
120120
global serial_simplifier
121-
global serial_polynormal_simplifier
121+
global serial_expand_simplifier
122122

123123
function default_simplifier(; kw...)
124124
IfElse(has_trig,
@@ -141,8 +141,8 @@ let
141141
threaded_simplifier(cutoff) = Fixpoint(default_simplifier(threaded=true,
142142
thread_cutoff=cutoff))
143143

144-
serial_polynormal_simplifier = If(istree,
145-
Fixpoint(Chain((polynormalize,
146-
Fixpoint(default_simplifier())))))
144+
serial_expand_simplifier = If(istree,
145+
Fixpoint(Chain((expand,
146+
Fixpoint(default_simplifier())))))
147147

148148
end

test/fuzz.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ using Random: seed!
44

55
seed!(6174)
66
@testset "Fuzz test" begin
7-
@testset "polynormalize fuzz" begin
7+
@testset "expand fuzz" begin
88
for i=1:500
9-
fuzz_test(5, num_spec, SymbolicUtils.polynormalize; min_depth=3)
9+
fuzz_test(5, num_spec, SymbolicUtils.expand; min_depth=3)
1010
end
1111
end
1212
@testset "num fuzz" begin

test/interface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ ex = 1 + (:x - 2)
2020
SymbolicUtils.symtype(::Expr) = Real
2121
SymbolicUtils.symtype(::Symbol) = Real
2222
@test simplify(ex) == -1 + :x
23-
@test simplify(:a * (:b + -1 * :c) + -1 * (:b * :a + -1 * :c * :a), polynorm=true) == 0
23+
@test simplify(:a * (:b + -1 * :c) + -1 * (:b * :a + -1 * :c * :a), expand=true) == 0

test/nf.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
using SymbolicUtils, Test
2-
using SymbolicUtils: polynormalize, Term, symtype
2+
using SymbolicUtils: Term, symtype
33
@testset "polyform" begin
44
@syms a b c d
5-
@test polynormalize(a * (b + -1 * c) + -1 * (b * a + -1 * c * a)) == 0
6-
@eqtest polynormalize(sin(a+b)+sin(c+d)) == sin(a+b) + sin(c+d)
7-
@eqtest simplify(polynormalize(sin((a+b)^2)^2)) == simplify(sin(a^2+2*(b*a)+b^2)^2)
8-
@test simplify(polynormalize(sin((a+b)^2)^2 + cos((a+b)^2)^2)) == 1
5+
@test expand(a * (b + -1 * c) + -1 * (b * a + -1 * c * a)) == 0
6+
@eqtest expand(sin(a+b)+sin(c+d)) == sin(a+b) + sin(c+d)
7+
@eqtest simplify(expand(sin((a+b)^2)^2)) == simplify(sin(a^2+2*(b*a)+b^2)^2)
8+
@test simplify(expand(sin((a+b)^2)^2 + cos((a+b)^2)^2)) == 1
99
@syms x1::Real f(::Real)::Real
1010

1111
# issue 193
12-
@test isequal(polynormalize(f(x1 + 2.0)), f(2.0 + x1))
13-
@test symtype(polynormalize(f(x1 + 2.0))) == Real
12+
@test isequal(expand(f(x1 + 2.0)), f(2.0 + x1))
13+
@test symtype(expand(f(x1 + 2.0))) == Real
1414

1515
# cleanup rules
16-
@test polynormalize(Term{Number}(identity, 0)) == 0
17-
@test polynormalize(Term{Number}(one, 0)) == 1
18-
@test polynormalize(Term{Number}(zero, 0)) == 0
19-
@test polynormalize(identity(a * b) - b * a) == 0
20-
@test polynormalize(a * b - b * a) == 0
16+
@test expand(Term{Number}(identity, 0)) == 0
17+
@test expand(Term{Number}(one, 0)) == 1
18+
@test expand(Term{Number}(zero, 0)) == 0
19+
@test expand(identity(a * b) - b * a) == 0
20+
@test expand(a * b - b * a) == 0
2121
end

0 commit comments

Comments
 (0)