# JuMP User-Defined Function, Value and Gradient At Once

**URL:** <https://discourse.julialang.org/t/jump-user-defined-function-value-and-gradient-at-once/8681>\
**Category:** Optimization (Mathematical)\
**Created:** [January 29, 2018, 11:59pm UTC](https://discourse.julialang.org/t/jump-user-defined-function-value-and-gradient-at-once/8681 "2018-01-29T23:59:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhipsman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhipsman/32/3263_2.png) [@nhipsman](https://discourse.julialang.org/u/nhipsman)\
**Post date:** [January 29, 2018, 11:59pm UTC](https://discourse.julialang.org/t/jump-user-defined-function-value-and-gradient-at-once/8681/1 "2018-01-29T23:59:56Z")

</div>

Hi -

I have a user-defined function for which I have an analytical gradient I’d like to use. Furthermore, most of the effort in calculating that gradient is already done in calculating the value of the function. Is there a way to pass JuMP a function that does both, in a way that is efficient? I recognize I could have a “wrapper” function that extracts only the part that JuMP wants (value or gradient), but surely that will be implemented in a very slow fashion. NLsolve, for instance, has a capability like this.

Thank you!

Nathan

---

<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:** [January 30, 2018, 2:25am UTC](https://discourse.julialang.org/t/jump-user-defined-function-value-and-gradient-at-once/8681/2 "2018-01-30T02:25:27Z")

</div>

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:** ![nhipsman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhipsman/32/3263_2.png) [@nhipsman](https://discourse.julialang.org/u/nhipsman)\
**Post date:** [January 30, 2018, 4:49am UTC](https://discourse.julialang.org/t/jump-user-defined-function-value-and-gradient-at-once/8681/3 "2018-01-30T04:49:00Z")

</div>

Brilliant - thanks!

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [February 1, 2018, 8:27pm UTC](https://discourse.julialang.org/t/jump-user-defined-function-value-and-gradient-at-once/8681/4 "2018-02-01T20:27:26Z")

</div>

> [@miles.lubin](#):
>
> It sounds like your function has a nontrivial evaluation cost; in this case the extra time spent in this check should be negligible.

I think that check is almost always negligible. We do it by default every time f(x), g(x) or h(x) is called in Optim for example.
