# Function and gradient evaluation in same function Ipopt

**URL:** <https://discourse.julialang.org/t/function-and-gradient-evaluation-in-same-function-ipopt/9311>\
**Category:** General Usage\
**Tags:** optimization\
**Created:** [February 25, 2018, 8:13am UTC](https://discourse.julialang.org/t/function-and-gradient-evaluation-in-same-function-ipopt/9311 "2018-02-25T08:13:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [February 25, 2018, 8:13am UTC](https://discourse.julialang.org/t/function-and-gradient-evaluation-in-same-function-ipopt/9311/1 "2018-02-25T08:13:11Z")

</div>

I am using Ipopt.jl as my optimizer and would like to compute the function value and its gradient in the same Julia function (for performance reasons).

See this example: [https://github.com/JuliaOpt/Ipopt.jl/blob/master/example/hs071.jl](https://github.com/JuliaOpt/Ipopt.jl/blob/master/example/hs071.jl)  
Here the function value and gradient and hessian are all specified with a different Julia function. I however need the function value to compute my gradient, so I end up calculating it anyway in my gradient evaluation function (thereby computing it twice).

Is there a workaround such that one can combine the function and gradient evaluation in the same Julia call? In the same way as NLopt.jl does that?

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [February 25, 2018, 8:44am UTC](https://discourse.julialang.org/t/function-and-gradient-evaluation-in-same-function-ipopt/9311/2 "2018-02-25T08:44:41Z")

</div>

> [@JuMP User-Defined Function, Value and Gradient At Once](https://discourse.julialang.org/t/jump-user-defined-function-value-and-gradient-at-once/8681/2):
>
> You can cache the evaluation point and last value/gradient. If the evaluation point doesn’t change, return the value that you’ve already computed. It sounds like your function has a nontrivial evaluation cost; in this case the extra time spent in this check should be negligible.

---

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [February 25, 2018, 1:57pm UTC](https://discourse.julialang.org/t/function-and-gradient-evaluation-in-same-function-ipopt/9311/3 "2018-02-25T13:57:36Z")

</div>

Ok, I tried that already, and that didn’t seem to be any faster. But must have done something wrong there then. Thanks.
