Skip to content

Commit 63c5c24

Browse files
committed
Merge branch 'master' into ys/makesym
2 parents f3b6bb7 + f9e5c33 commit 63c5c24

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/direct.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ function jacobian_sparsity(du, u)
7979
sparse(I, J, true, length(du), length(u))
8080
end
8181

82+
"""
83+
exprs_occur_in(exprs::Vector, expr)
84+
85+
Return an array of booleans `finds` where `finds[i]` is true if `exprs[i]` occurs in `expr`
86+
false otherwise.
87+
"""
88+
function exprs_occur_in(exprs, expr)
89+
vec(jacobian_sparsity([expr], exprs))
90+
end
91+
8292
"""
8393
```julia
8494
hessian(O, vars::AbstractVector; simplify = true)

src/utils.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ end
119119

120120
@deprecate substitute_expr!(expr,s) substitute(expr,s)
121121

122-
function states_to_sym(states)
122+
function states_to_sym(states::Set)
123123
function _states_to_sym(O)
124124
if O isa Equation
125125
Expr(:(=), _states_to_sym(O.lhs), _states_to_sym(O.rhs))
126126
elseif O isa Term
127127
if isa(O.op, Sym)
128-
any(isequal(O), states) && return tosymbol(O) # dependent variables
128+
O in states && return tosymbol(O)
129+
# dependent variables
129130
return build_expr(:call, Any[O.op.name; _states_to_sym.(O.args)])
130131
else
131132
return build_expr(:call, Any[O.op; _states_to_sym.(O.args)])
@@ -137,6 +138,7 @@ function states_to_sym(states)
137138
end
138139
end
139140
end
141+
states_to_sym(states) = states_to_sym(Set(states))
140142

141143
"""
142144
toparam(s::Sym) -> Sym{<:Parameter}

test/direct.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ reference_jac = sparse(ModelingToolkit.jacobian(du, [x,y,z]))
3636

3737
@test findnz(ModelingToolkit.jacobian_sparsity(du, [x,y,z]))[[1,2]] == findnz(reference_jac)[[1,2]]
3838

39+
let
40+
@variables t x(t) y(t) z(t)
41+
@test ModelingToolkit.exprs_occur_in([x,y,z], x^2*y) == [true, true, false]
42+
end
43+
3944
@test isequal(ModelingToolkit.sparsejacobian(du, [x,y,z]), reference_jac)
4045

4146
using ModelingToolkit

0 commit comments

Comments
 (0)