Skip to content

Commit 28f75f1

Browse files
Merge pull request #118 from JuliaDiffEq/myb/lu
`generate_ode_iW` -> `generate_factorized_W`
2 parents 5ac6f7d + 48b0ec5 commit 28f75f1

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/systems/diffeqs/diffeqsystem.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,29 @@ function generate_function(sys::DiffEqSystem; version::FunctionVersion = ArrayFu
7676
end
7777

7878

79-
function generate_ode_iW(sys::DiffEqSystem, simplify=true; version::FunctionVersion = ArrayFunction)
79+
function generate_factorized_W(sys::DiffEqSystem, simplify=true; version::FunctionVersion = ArrayFunction)
8080
jac = calculate_jacobian(sys)
8181

8282
gam = Variable(:gam; known = true)
8383

8484
W = LinearAlgebra.I - gam*jac
85-
W = SMatrix{size(W,1),size(W,2)}(W)
86-
iW = inv(W)
85+
Wfact = lu(W, Val(false), check=false).factors
8786

8887
if simplify
89-
iW = simplify_constants.(iW)
88+
Wfact = simplify_constants.(Wfact)
9089
end
9190

92-
W = inv(LinearAlgebra.I/gam - jac)
93-
W = SMatrix{size(W,1),size(W,2)}(W)
94-
iW_t = inv(W)
91+
W_t = LinearAlgebra.I/gam - jac
92+
Wfact_t = lu(W_t, Val(false), check=false).factors
9593
if simplify
96-
iW_t = simplify_constants.(iW_t)
94+
Wfact_t = simplify_constants.(Wfact_t)
9795
end
9896

9997
vs, ps = sys.dvs, sys.ps
100-
iW_func = build_function(iW , vs, ps, (:gam,:t); version = version)
101-
iW_t_func = build_function(iW_t, vs, ps, (:gam,:t); version = version)
98+
Wfact_func = build_function(Wfact , vs, ps, (:gam,:t); version = version)
99+
Wfact_t_func = build_function(Wfact_t, vs, ps, (:gam,:t); version = version)
102100

103-
return (iW_func, iW_t_func)
101+
return (Wfact_func, Wfact_t_func)
104102
end
105103

106104
function DiffEqBase.ODEFunction(sys::DiffEqSystem; version::FunctionVersion = ArrayFunction)

test/system_construction.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ generate_function(de;version=ModelingToolkit.SArrayFunction)
1616
jac_expr = generate_jacobian(de)
1717
jac = calculate_jacobian(de)
1818
f = ODEFunction(de)
19-
ModelingToolkit.generate_ode_iW(de)
19+
fw, fwt = map(eval, ModelingToolkit.generate_factorized_W(de))
20+
du = zeros(3)
21+
u = collect(1:3)
22+
p = collect(4:6)
23+
f(du, u, p, 0.1)
24+
@test du == [4, 0, -16]
25+
FW = zeros(3, 3)
26+
fw(FW, u, p, 0.2, 0.1)
27+
fwt(FW, u, p, 0.2, 0.1)
2028

2129
# Differential equation with automatic extraction of variables
2230
de2 = DiffEqSystem(eqs, t)

0 commit comments

Comments
 (0)