Skip to content

Commit 609f7a2

Browse files
authored
Merge pull request #370 from JuliaSymbolics/s/fix-expand
fix expand for expressions with array indexing
2 parents 4dad4c3 + 02bf51c commit 609f7a2

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "0.15.4"
4+
version = "0.15.5"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/polyform.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ function PolyForm(x,
160160
recurse=false,
161161
metadata=metadata(x))
162162

163+
if !(symtype(x) <: Number)
164+
return x
165+
end
166+
163167
# Polyize and return a PolyForm
164168
p = polyize(x, pvar2sym, sym2term, vtype, pow, Fs, recurse)
165169
PolyForm{symtype(x)}(p, pvar2sym, sym2term, metadata)
@@ -225,8 +229,12 @@ Expand expressions by distributing multiplication over addition, e.g.,
225229
multivariate polynomials implementation.
226230
`variable_type` can be any subtype of `MultivariatePolynomials.AbstractVariable`.
227231
"""
228-
expand(expr) = Postwalk(identity)(PolyForm(expr, Fs=Union{typeof(+), typeof(*), typeof(^)}, recurse=true))
232+
expand(expr) = unpolyize(PolyForm(expr, Fs=Union{typeof(+), typeof(*), typeof(^)}, recurse=true))
229233

234+
function unpolyize(x)
235+
simterm(x, f, args; kw...) = similarterm(x, f, args, symtype(x); kw...)
236+
Postwalk(identity, similarterm=simterm)(x)
237+
end
230238

231239
## Rational Polynomial form with Div
232240

@@ -280,7 +288,7 @@ function simplify_fractions(x; polyform=false)
280288

281289
expr = Postwalk(sdiv quick_cancel)(Postwalk(add_with_div)(x))
282290

283-
polyform ? expr : Postwalk(identity)(expr)
291+
polyform ? expr : unpolyize(expr)
284292
end
285293

286294
function add_with_div(x, flatten=true)

test/polyform.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ end
3232
#@test expand(Term{Number}(zero, 0)) == 0
3333
#@test expand(identity(a * b) - b * a) == 0
3434
@test expand(a * b - b * a) == 0
35+
36+
@syms A::Vector{Real}
37+
# test that the following works
38+
expand(Term{Real}(getindex, [A, 3]) - 3)
3539
end
3640

3741
@testset "simplify_fractions with quick-cancel" begin

0 commit comments

Comments
 (0)