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

feat: accept additional metadata in diff2term #1104

Merged
Merged
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
13 changes: 8 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ Base.Symbol(x::Union{Num,Symbolic}) = tosymbol(x)
tosymbol(t::Num; kwargs...) = tosymbol(value(t); kwargs...)

"""
diff2term(x) -> Symbolic
diff2term(x, x_metadata::Dict{Datatype, Any}) -> Symbolic

Convert a differential variable to a `Term`. Note that it only takes a `Term`
not a `Num`.
Any upstream metadata can be passed via `x_metadata`

```julia
julia> @variables x t u(x, t) z(t)[1:2]; Dt = Differential(t); Dx = Differential(x);
Expand All @@ -98,7 +99,7 @@ julia> Symbolics.diff2term(Symbolics.value(Dt(z[1])))
var"z(t)[1]ˍt"
```
"""
function diff2term(O)
function diff2term(O, O_metadata::Union{Dict, Nothing, Base.ImmutableDict}=nothing)
istree(O) || return O
if is_derivative(O)
ds = ""
Expand All @@ -112,7 +113,8 @@ function diff2term(O)
d_separator = 'ˍ'

if ds === nothing
return similarterm(O, operation(O), map(diff2term, arguments(O)), metadata=metadata(O))
return similarterm(O, operation(O), map(diff2term, arguments(O)), metadata = O_metadata isa Nothing ?
metadata(O) : Base.ImmutableDict(metadata(O)..., O_metadata...))
else
oldop = operation(O)
if issym(oldop)
Expand All @@ -126,7 +128,8 @@ function diff2term(O)
return Sym{symtype(O)}(Symbol(opname, d_separator, ds))
end
newname = occursin(d_separator, opname) ? Symbol(opname, ds) : Symbol(opname, d_separator, ds)
return setname(similarterm(O, rename(oldop, newname), arguments(O), metadata=metadata(O)), newname)
return setname(similarterm(O, rename(oldop, newname), arguments(O), metadata = O_metadata isa Nothing ?
metadata(O) : Base.ImmutableDict(metadata(O)..., O_metadata...)), newname)
end
end

Expand Down Expand Up @@ -305,7 +308,7 @@ julia> Symbolics.coeff(x^2 + y, x^2)
"""
function coeff(p, sym=nothing)
p, sym = value(p), value(sym)

if isequal(sym, 1)
sym = nothing
end
Expand Down
2 changes: 2 additions & 0 deletions test/downstream/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[deps]
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
ParameterizedFunctions = "65888b18-ceab-5e60-b2b9-181511a3b968"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
ModelingToolkit = "8.33, 9"
Expand Down
28 changes: 28 additions & 0 deletions test/downstream/modeling_toolkit_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Symbolics: diff2term
using ModelingToolkit
using ModelingToolkit: equivalent, get_unit, value, VariableUnit
using Unitful: @u_str
using Test

@variables t [unit = u"s"] k(t)
D = Differential(t)

@test equivalent(
u"1/s",
get_unit(
diff2term(
value(D(k)),
Dict(VariableUnit => get_unit(D(k)))
)
)
)

@test equivalent(
u"1/s^2",
get_unit(
diff2term(
value(D(D(k))),
Dict(VariableUnit => get_unit(D(D(k))))
)
)
)
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ if GROUP == "All" || GROUP == "Core" || GROUP == "SymbolicIndexingInterface"
end
end

if GROUP == "Downstream"
if GROUP == "All" || GROUP == "Downstream"
activate_downstream_env()
#@time @safetestset "ParameterizedFunctions MATLABDiffEq Regression Test" begin include("downstream/ParameterizedFunctions_MATLAB.jl") end
@safetestset "ModelingToolkit Variable Utils Test" begin include("downstream/modeling_toolkit_utils.jl") end
end
Loading