Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite helpers module #1025

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import MacroTools: splitdef, combinedef, postwalk, striplines
include("wrapper-types.jl")

include("num.jl")
include("rewrite-helpers.jl")
include("complex.jl")

"""
Expand Down
85 changes: 85 additions & 0 deletions src/rewrite-helpers.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
replace(expr::Symbolic, rules...)
Walk the expression and replace subexpressions according to `rules`. `rules`
could be rules constructed with `@rule`, a function, or a pair where the
left hand side is matched with equality (using `isequal`) and is replaced by the right hand side.

Rules will be applied left-to-right simultaneously,
so only one pattern will be applied to any subexpression,
and the patterns will only be applied to the input text,
not the replacements.

Set `fixpoint = true` to repeatedly apply rules until no
change to the expression remains to be made.
"""
function _replace(expr::Symbolic, rules...; fixpoint=false)
rs = map(r -> r isa Pair ? (x -> isequal(x, r[1]) ? r[2] : nothing) : r, rules)
R = Prewalk(Chain(rs))
if fixpoint
Fixpoint(R)(expr)
else
R(expr)
end
end
# Fix ambiguity
function Base.replace(expr::Num, r::Pair, rules::Pair...)
_replace(unwrap(expr), r, rules...)
end

function Base.replace(expr::Num, rules...)
_replace(unwrap(expr), rules...)
end

function Base.replace(expr::Symbolic, r, rules...)
_replace(expr, r, rules)
end


Base.occursin(x::Num, y::Num) = occursin(unwrap(x), unwrap(y))
@wrapped function Base.occursin(r::Any, y::Real)
y = unwrap(y)
if isequal(r, y)
return true
elseif r isa Function
if r(y)
return true
end
end

if istree(y)
return r(operation(y)) ||
any(y->occursin(r, y), arguments(y))
else
return false
end
end

function filterchildren!(r::Any, y::Union{Num, Symbolic}, acc)
y = unwrap(y)
if isequal(r, y)
push!(acc, y)
return acc
elseif r isa Function
if r(y)
push!(acc, y)
return acc
end
end

if istree(y)
if r(operation(y))
push!(acc, y)
end
foreach(c->filterchildren!(r, c, acc),
arguments(y))
return acc
end
end

filterchildren(r, y) = filterchildren!(r, y, [])

module RewriteHelpers
import Symbolics: is_derivative, filterchildren, unwrap
export replace, occursin, is_derivative,
filterchildren, unwrap
end
Loading