# Share system of equations solution between g(x) and dg(x)dx functions (i.e. constraint and its jacobian) in IPOPT

Hi,
I planning to use IPOPT in an optimization project.

I would like to know if there is a way to share the solution of a system of equations (like Ax=b) between different functions called by IPOPT, say the constraint and its jacobian functions. Because otherwise Ax=b needs to be solved twice, once per function call, g and dgdx.

Thanks!

It seems difficult even at C API level because we don’t know the order Ipopt calls `eval_g` and `eval_jac_g` (I am not sure if Ipopt guarantees the order to call user-provided functions). Otherwise we can solve `Ax=b` in `eval_g` and use the result directly in `eval_jac_g`.
Maybe a silly solution is to create an extra array to memorize recent values of `x` and corresponding solution of expensive `Ax=b` (for example, latest 5 values of `x`), then check the distance between current `x` and previously encountered `x`. If the distance is small enough, the memorized equation solution can be used directly.

1 Like

Thanks, you are right. We do not know the order of function calls and hence who comes first…
The solution you propose makes sense, thanks. But I think it requires to have global variables(or arrays) and to check whether their value has changed since the last function call before solving again the system within a certain function. Right?

Yep. I am afraid that global state is inevitable because we need more information beyond arguments of `eval_g` and `eval_jac_g` : `x` and the output array.

1 Like

It is what it is! Thanks again for your attention!

1 Like

Here the working link: Nonlinear tips and tricks · JuMP

1 Like