Building a symbolic Hessian function with ModelingToolkit.jl

I seek to build a Hessian function for the Lagrangian using ModelingToolkit.jl. This Hessian function is supposed to take two (non-symbolic) functions as arguments. My question is, how would you “correctly” write (if possible) the following using the same idea as found in this post, or any available workaround? I couldn’t figure it out from the different docs.

ℒ = func1 + sum(λ .* func2)
ℋ = ModelingToolkit.hessian(ℒ, [vec1; λ])
hess,_ = ModelingToolkit.build_function(ℋ, [func1;func2;λ]; expression=Val{false})
hess(obj(inp1,inp2,inp3), constr(inp4, inp5, inp6), lbda)

where obj outputs the objective value, and constr outputs the constraints vector; λ is the Lagrange multiplier.

Thank you.

I don’t quite get the question. Looks like that would work fine, other than the build_function call needs to match the signature of what you want to be passing in, so

hess,_ = ModelingToolkit.build_function(ℋ, [func1;func2], λ; expression=Val{false})
1 Like

Thanks a lot. It truly works fine, I was passing the functions wrongly.

Hi @ChrisRackauckas , thanks again. How would one show the output as a matrix with numeric values?

once you create the function you just call it with the inputs. Are you actually using ModelingToolkit here or just Symbolics?

Yes, I’m using ModelingToolkit, and there are the symbolic Differential() terms in the output.

Wait so is this coming from an OptimizationSystem? What kind of system type?

(EDIT) Oops… Yes! Constrained OptimizationSystem.
The goal is to find an effective way to compute the Lagrangian Hessian (preferably symbolically store it as this is a large system).

A very similar description of the problem would be this post. I think the original poster settled with their approach (which works but may not be very efficient). For a system with a large number of parameters, I would prefer to store this Hessian symbolically in a function and provide the Lagrange multiplier (and other parameters) each time we call the function. This is what I attempt doing with ModelingToolkit.jl.

Okay yeah, when you do OptimizationProblem you can just pass the hes=true and it should generate it.

Thank you very much for your help Chris. I opted for a different tool. I saved this answer for later!