Skip to content

Commit

Permalink
Merge pull request #111 from JuliaSymbolics/myb/sub
Browse files Browse the repository at this point in the history
Move the substitute code from MTK
  • Loading branch information
YingboMa authored Mar 11, 2021
2 parents d03b452 + 6ed85a6 commit 751031a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Symbolics"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
authors = ["Shashi Gowda <[email protected]>"]
version = "0.1.6"
version = "0.1.7"

[deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
Expand Down
34 changes: 27 additions & 7 deletions src/num.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,33 @@ function Base.show(io::IO, z::Complex{<:Num})
end

SymbolicUtils.simplify(n::Num; kw...) = Num(SymbolicUtils.simplify(value(n); kw...))
function substitute(x::Num, rule::Dict; kw...)
rule′ = if any(v -> v isa Num, values(rule))
Dict((k => v isa Num ? value(v) : v for (k, v) in rule))
else
rule
end
Num(substitute(value(x), rule′; kw...))
"""
substitute(expr, s::Dict)
Performs the substitution on `expr` according to rule(s) `s`.
# Examples
```julia
julia> @parameters t
(t,)
julia> @variables x y z(t)
(x, y, z(t))
julia> ex = x + y + sin(z)
(x + y) + sin(z(t))
julia> substitute(ex, Dict([x => z, sin(z) => z^2]))
(z(t) + y) + (z(t) ^ 2)
```
"""
substitute(expr::Num, s::Pair; kw...) = Num(substituter(s)(value(expr); kw...)) # backward compat
substitute(expr::Num, s::Vector; kw...) = Num(substituter(s)(value(expr); kw...))
substitute(expr::Num, s::Dict; kw...) = Num(substituter(s)(value(expr); kw...))
# TODO: move this to SymbolicUtils
substitute(expr, s::Pair; kw...) = substituter([s[1] => s[2]])(expr; kw...)
substitute(expr, s::Vector; kw...) = substituter(s)(expr; kw...)

substituter(pair::Pair) = substituter((pair,))
function substituter(pairs)
dict = Dict(value(k) => value(v) for (k, v) in pairs)
(expr; kw...) -> SymbolicUtils.substitute(expr, dict; kw...)
end

SymbolicUtils.symtype(n::Num) = symtype(n.val)
Expand Down

2 comments on commit 751031a

@YingboMa
Copy link
Member Author

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/31736

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.1.7 -m "<description of version>" 751031ab71c382a4e207831180c2ec3f13d7465f
git push origin v0.1.7

Please sign in to comment.