# OptimizationOptimJL: single function returning objective and gradients

**URL:** https://discourse.julialang.org/t/optimizationoptimjl-single-function-returning-objective-and-gradients/103704
**Category:** Optimization (Mathematical)
**Created:** [September 9, 2023, 3:52pm UTC](https://discourse.julialang.org/t/optimizationoptimjl-single-function-returning-objective-and-gradients/103704 "2023-09-09T15:52:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)
#### Post date: [September 9, 2023, 3:52pm UTC](https://discourse.julialang.org/t/optimizationoptimjl-single-function-returning-objective-and-gradients/103704/1 "2023-09-09T15:52:50Z")

</div>

I am using OptimizationOptimJL, and wish to supply my own gradients. There are many common calculations that are shared when evaluating the objective function, and the gradients, so I wish to have a single function that can return both. Is it possible to do this when defining the OptimizationFunction?

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [September 10, 2023, 5:34am UTC](https://discourse.julialang.org/t/optimizationoptimjl-single-function-returning-objective-and-gradients/103704/2 "2023-09-10T05:34:50Z")

</div>

I think this might be answered in their [Tips and Tricks section in the manual](https://julianlsolvers.github.io/Optim.jl/stable/#user/tipsandtricks/#avoid-repeating-computations).

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 12, 2023, 5:33pm UTC](https://discourse.julialang.org/t/optimizationoptimjl-single-function-returning-objective-and-gradients/103704/3 "2023-09-12T17:33:12Z")

</div>

Not right now. The optimizer doesn’t make great use of it anyways so it wasn’t a major optimization loss, but we may add it to the interface in the near future.
