Skip to content

Commit a1a17a6

Browse files
committed
Do the same for the Hessian
1 parent d24e326 commit a1a17a6

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ function generate_jacobian(
220220
end
221221

222222
function calculate_hessian(sys::NonlinearSystem; sparse = false, simplify = false)
223-
rhs = [eq.rhs for eq in equations(sys)]
223+
obs = Dict(eq.lhs => eq.rhs for eq in observed(sys))
224+
rhs = map(eq -> fixpoint_sub(eq.rhs, obs), equations(sys))
224225
vals = [dv for dv in unknowns(sys)]
225226
if sparse
226227
hess = [sparsehessian(rhs[i], vals, simplify = simplify) for i in 1:length(rhs)]

test/nonlinearsystem.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ sys = structural_simplify(ns; conservative = true)
285285
@test length(equations(sys)) == 1
286286

287287
# https://github.com/SciML/ModelingToolkit.jl/issues/2858
288-
@testset "Jacobian with observed equations that depend on unknowns" begin
288+
@testset "Jacobian/Hessian with observed equations that depend on unknowns" begin
289289
@variables x y z
290290
@parameters σ ρ β
291291
eqs = [0 ~ σ * (y - x)
@@ -312,6 +312,7 @@ sys = structural_simplify(ns; conservative = true)
312312
eqs = [0 ~ x^2 + 2*z + y, z ~ y, y ~ x] # analytical solution x = y = z = 0 or -3
313313
@mtkbuild ns = NonlinearSystem(eqs) # solve for y with observed chain z -> x -> y
314314
@test isequal(expand.(calculate_jacobian(ns)), [3//2 + y;;])
315+
@test isequal(calculate_hessian(ns), [[1;;]])
315316
prob = NonlinearProblem(ns, unknowns(ns) .=> -4.0) # give guess < -3 to reach -3
316317
sol = solve(prob, NewtonRaphson())
317318
@test sol[x] sol[y] sol[z] -3

0 commit comments

Comments
 (0)