# Choosing a package for univariate (convex) optimization in Julia

**URL:** <https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089>\
**Category:** Optimization (Mathematical)\
**Tags:** optim, optimization, convex-optimization\
**Created:** [January 22, 2024, 11:04am UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089 "2024-01-22T11:04:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 22, 2024, 11:04am UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/1 "2024-01-22T11:04:45Z")

</div>

I need to solve 1d convex optimization programs, where I have access to all derivatives. Ideally I’d use Optim.jl but the interface only supports vectors. The backend NLSolvers.jl works but the documentation is not as detailed.  
I know I can wrap `x::Real` into `StaticArrays.MVector(x)` and use any vector optimization package but I was wondering if there are easier options. My goal is to minimize code complexity and setup costs, especially memory, cause this optimization subroutine will get called a gazillion times.  
Related:

> [@How to make Optim's optimize work for scalars](https://discourse.julialang.org/t/how-to-make-optims-optimize-work-for-scalars/34343/2):
>
> I am not sure you are aware of the possible pitfalls. Curiously, multivariate methods can break down in surprising ways in 1D, and can easily yield suboptimal performance. Optim also has GoldenSection(), see That said, you can always write a wrapper like using Optim function univariate\_optimize(f, x0, args...; kwargs...) opt = Optim.optimize(x -\> f(x[1]), [x0], args...; kwargs...) @assert Optim.converged(opt) Optim.minimizer(opt)[1] end univariate\_optimize(x -\> abs2(x), 1.0, B…

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 22, 2024, 11:35am UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/3 "2024-01-22T11:35:46Z")

</div>

Note that NLSolvers.jl is not the backend of Optim.jl, it is a re-write that Patrick has been slowly working on solo for a while now. One of the goals iirc was to make your use case more “first class” as shown in the README. For example, optimising small problems with static array decision variables or scalars should not allocate.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 22, 2024, 11:37am UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/4 "2024-01-22T11:37:38Z")

</div>

I would still recommend a 1d algorithm for 1d problems.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 22, 2024, 11:41am UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/5 "2024-01-22T11:41:00Z")

</div>

If you have the first and second derivatives, using a second-order optimisation algorithm may be better for local optimisation (e.g. Newton’s method converges in a single iteration in the quadratic case).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 22, 2024, 12:05pm UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/6 "2024-01-22T12:05:28Z")

</div>

Yes, I would just write a custom implementation of the Newton method for that.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 22, 2024, 12:22pm UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/7 "2024-01-22T12:22:51Z")

</div>

Ya it’s probably hard to beat a simple Newton + back-tracking line search for 1D. But if you want fancier line search algorithms, it gets slightly more complex.

---

<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:** [January 22, 2024, 12:33pm UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/8 "2024-01-22T12:33:31Z")

</div>

Why line search at all? It’s convex.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 22, 2024, 1:07pm UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/9 "2024-01-22T13:07:59Z")

</div>

Ya it might not be necessary if you know the second derivative and the problem is strictly convex.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 28, 2024, 4:34pm UTC](https://discourse.julialang.org/t/choosing-a-package-for-univariate-convex-optimization-in-julia/109089/10 "2024-11-28T16:34:55Z")

</div>

Sorry to revive an old topic but I had the same question: I want to minimize a univariate convex function.

What approach ended up working best for you ?
