Skip to content

Commit 25647ef

Browse files
Merge branch 'master' into dps/set_operations
2 parents f595987 + 2a9979a commit 25647ef

File tree

6 files changed

+82
-15
lines changed

6 files changed

+82
-15
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "4.3.4"
4+
version = "4.4.1"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/extra_functions.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,34 @@ ModelingToolkit.@register Distributions.Normal(mu,sigma)
4444
@register (x, y)
4545
@register (x, y)
4646
@register (x, y)
47-
@register (x, y)
47+
@register (x, y)
48+
49+
function LinearAlgebra.det(A::AbstractMatrix{<:Num}; laplace=true)
50+
if laplace
51+
n = LinearAlgebra.checksquare(A)
52+
if n == 1
53+
return A[1, 1]
54+
elseif n == 2
55+
return A[1, 1] * A[2, 2] - A[1, 2] * A[2, 1]
56+
else
57+
temp = 0
58+
# Laplace expansion along the first column
59+
M′ = A[:, 2:end]
60+
for i in axes(A, 1)
61+
M = M′[(1:n) .!= i, :]
62+
d′ = A[i, 1] * det(M)
63+
if iseven(i)
64+
temp = iszero(temp) ? d′ : temp - d′
65+
else
66+
temp = iszero(temp) ? d′ : temp + d′
67+
end
68+
end
69+
end
70+
return temp
71+
else
72+
if istriu(A) || istril(A)
73+
return det(UpperTriangular(A))
74+
end
75+
return det(lu(A; check = false))
76+
end
77+
end

src/systems/jumps/jumpsystem.jl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,8 @@ dprob = DiscreteProblem(js, u₀map, tspan, parammap)
185185
"""
186186
function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Tuple,
187187
parammap=DiffEqBase.NullParameters(); kwargs...)
188-
189-
(u0map isa AbstractVector) || error("For DiscreteProblems u0map must be an AbstractVector.")
190-
u0d = Dict( value(u[1]) => u[2] for u in u0map)
191-
u0 = [u0d[u] for u in states(sys)]
192-
if parammap != DiffEqBase.NullParameters()
193-
(parammap isa AbstractVector) || error("For DiscreteProblems parammap must be an AbstractVector.")
194-
pd = Dict( value(u[1]) => u[2] for u in parammap)
195-
p = [pd[u] for u in parameters(sys)]
196-
else
197-
p = parammap
198-
end
188+
u0 = varmap_to_vars(u0map, states(sys))
189+
p = varmap_to_vars(parammap, parameters(sys))
199190
f = DiffEqBase.DISCRETE_INPLACE_DEFAULT
200191
df = DiscreteFunction{true,true}(f, syms=Symbol.(states(sys)))
201192
DiscreteProblem(df, u0, tspan, p; kwargs...)
@@ -207,7 +198,7 @@ function DiffEqBase.DiscreteProblemExpr(sys::JumpSystem, u0map, tspan,
207198
parammap=DiffEqBase.NullParameters; kwargs...)
208199
```
209200
210-
Generates a black DiscreteProblem for a JumpSystem to utilize as its
201+
Generates a blank DiscreteProblem for a JumpSystem to utilize as its
211202
solving `prob.prob`. This is used in the case where there are no ODEs
212203
and no SDEs associated with the system.
213204

src/variables.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,22 @@ function varmap_to_vars(varmap::AbstractArray{Pair{T,S}},varlist) where {T,S}
227227
# Does things like MArray->SArray
228228
ArrayInterface.restructure(varmap,out)
229229
end
230+
231+
function varmap_to_vars(varmap::NTuple{N,Pair{T,S}},varlist) where {N,T,S}
232+
out = MArray{Tuple{N},S}(undef)
233+
for (ivar, ival) in varmap
234+
j = findfirst(isequal(ivar),varlist)
235+
if isnothing(j)
236+
throw(ArgumentError("Value $(ivar) provided in map not found in $(varlist)"))
237+
elseif j > length(varmap)
238+
throw(ArgumentError("Missing value in $(varmap), need $(varlist)"))
239+
end
240+
out[j] = ival
241+
end
242+
out.data
243+
end
244+
230245
varmap_to_vars(varmap::AbstractArray,varlist) = varmap
246+
varmap_to_vars(varmap::Tuple,varlist) = varmap
231247
varmap_to_vars(varmap::DiffEqBase.NullParameters,varlist) = varmap
232248
varmap_to_vars(varmap::Nothing,varlist) = varmap

test/odesystem.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,14 @@ u0 = [y₁ => 1.0,
192192
p = [k₁ => 0.04,
193193
k₂ => 3e7,
194194
k₃ => 1e4]
195+
p2 = (k₁ => 0.04,
196+
k₂ => 3e7,
197+
k₃ => 1e4)
195198
tspan = (0.0,100000.0)
196199
prob1 = ODEProblem(sys,u0,tspan,p)
200+
prob12 = ODEProblem(sys,u0,tspan,[0.04,3e7,1e4])
201+
prob13 = ODEProblem(sys,u0,tspan,(0.04,3e7,1e4))
202+
prob14 = ODEProblem(sys,u0,tspan,p2)
197203
prob2 = ODEProblem(sys,u0,tspan,p,jac=true)
198204
prob3 = ODEProblem(sys,u0,tspan,p,jac=true,sparse=true)
199205
for (prob, atol) in [(prob1, 1e-12), (prob2, 1e-12), (prob3, 1e-12)]

test/operation_overloads.jl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ aa = a; # old a
1919

2020
# test some matrix operations don't throw errors
2121
X = [0 b c; d e f; g h i]
22-
@test iszero(simplify(det(X) - ((b * f * g) + (c * d * h) - (b * d * i) - (c * e * g)), polynorm=true))
22+
@test iszero(simplify(det(X) - ((d * ((b * i) - (c * h))) + (g * ((b * f) - (c * e))))))
2323
F = lu(X)
2424
@test F.p == [2, 1, 3]
2525
R = simplify.(F.L * F.U - X[F.p, :], polynorm=true)
@@ -85,3 +85,27 @@ M \ reshape(b,2,1)
8585
M = [1 a; 0 2]
8686
M \ b
8787
M \ [1, 2]
88+
89+
# test det
90+
@variables X[1:4,1:4]
91+
d1 = det(X, laplace=true)
92+
d2 = det(X, laplace=false)
93+
_det1 = eval(build_function(d1, X))
94+
_det2 = eval(build_function(d2, X))
95+
A = [1 1 1 1
96+
1 0 1 1
97+
1 1 0 1
98+
1 1 1 0]
99+
@test _det1(A) == -1
100+
@test _det2(A) == -1
101+
102+
@variables X[1:3,1:3]
103+
d1 = det(X, laplace=true)
104+
d2 = det(X, laplace=false)
105+
_det1 = eval(build_function(d1, X))
106+
_det2 = eval(build_function(d2, X))
107+
A = [1 1 1
108+
1 0 1
109+
1 1 1]
110+
@test _det1(A) == 0
111+
@test _det2(A) == 0

0 commit comments

Comments
 (0)