Skip to content

Commit

Permalink
Merge pull request #370 from JuliaSymbolics/s/fix-expand
Browse files Browse the repository at this point in the history
fix expand for expressions with array indexing
  • Loading branch information
shashi authored Sep 17, 2021
2 parents 4dad4c3 + 02bf51c commit 609f7a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SymbolicUtils"
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
authors = ["Shashi Gowda"]
version = "0.15.4"
version = "0.15.5"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
12 changes: 10 additions & 2 deletions src/polyform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function PolyForm(x,
recurse=false,
metadata=metadata(x))

if !(symtype(x) <: Number)
return x
end

# Polyize and return a PolyForm
p = polyize(x, pvar2sym, sym2term, vtype, pow, Fs, recurse)
PolyForm{symtype(x)}(p, pvar2sym, sym2term, metadata)
Expand Down Expand Up @@ -225,8 +229,12 @@ Expand expressions by distributing multiplication over addition, e.g.,
multivariate polynomials implementation.
`variable_type` can be any subtype of `MultivariatePolynomials.AbstractVariable`.
"""
expand(expr) = Postwalk(identity)(PolyForm(expr, Fs=Union{typeof(+), typeof(*), typeof(^)}, recurse=true))
expand(expr) = unpolyize(PolyForm(expr, Fs=Union{typeof(+), typeof(*), typeof(^)}, recurse=true))

function unpolyize(x)
simterm(x, f, args; kw...) = similarterm(x, f, args, symtype(x); kw...)
Postwalk(identity, similarterm=simterm)(x)
end

## Rational Polynomial form with Div

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

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

polyform ? expr : Postwalk(identity)(expr)
polyform ? expr : unpolyize(expr)
end

function add_with_div(x, flatten=true)
Expand Down
4 changes: 4 additions & 0 deletions test/polyform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ end
#@test expand(Term{Number}(zero, 0)) == 0
#@test expand(identity(a * b) - b * a) == 0
@test expand(a * b - b * a) == 0

@syms A::Vector{Real}
# test that the following works
expand(Term{Real}(getindex, [A, 3]) - 3)
end

@testset "simplify_fractions with quick-cancel" begin
Expand Down

2 comments on commit 609f7a2

@shashi
Copy link
Member Author

@shashi shashi commented on 609f7a2 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/45089

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.15.5 -m "<description of version>" 609f7a2a055ce0927fef5f191f7313c3d2bc751e
git push origin v0.15.5

Please sign in to comment.