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

**URL:** <https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization, function\
**Created:** [December 10, 2020, 1:50pm UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608 "2020-12-10T13:50:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nico](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nico/32/211667_2.png) [@nico](https://discourse.julialang.org/u/nico)\
**Post date:** [December 10, 2020, 1:50pm UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608/1 "2020-12-10T13:50:57Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)\
**Post date:** [December 10, 2020, 3:02pm UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608/2 "2020-12-10T15:02:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nico](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nico/32/211667_2.png) [@nico](https://discourse.julialang.org/u/nico)\
**Post date:** [December 10, 2020, 3:17pm UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608/3 "2020-12-10T15:17:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)\
**Post date:** [December 10, 2020, 3:20pm UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608/4 "2020-12-10T15:20:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nico](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nico/32/211667_2.png) [@nico](https://discourse.julialang.org/u/nico)\
**Post date:** [December 10, 2020, 3:21pm UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608/5 "2020-12-10T15:21:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 10, 2020, 5:58pm UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608/6 "2020-12-10T17:58:22Z")

</div>

[https://jump.dev/JuMP.jl/dev/examples/nlp\_tricks/#User-defined-functions-with-vector-outputs](https://jump.dev/JuMP.jl/dev/examples/nlp_tricks/#User-defined-functions-with-vector-outputs)

---

<div class="post-metadata">

**Author:** ![nico](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nico/32/211667_2.png) [@nico](https://discourse.julialang.org/u/nico)\
**Post date:** [February 7, 2021, 8:50am UTC](https://discourse.julialang.org/t/share-system-of-equations-solution-between-g-x-and-dg-x-dx-functions-i-e-constraint-and-its-jacobian-in-ipopt/51608/7 "2021-02-07T08:50:31Z")

</div>

Here the working link: [https://jump.dev/JuMP.jl/dev/examples/Nonlinear%20programs/nlp\_tricks/](https://jump.dev/JuMP.jl/dev/examples/Nonlinear%20programs/nlp_tricks/)
