# How to make Optim's optimize work for scalars

**URL:** <https://discourse.julialang.org/t/how-to-make-optims-optimize-work-for-scalars/34343>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [February 8, 2020, 12:22pm UTC](https://discourse.julialang.org/t/how-to-make-optims-optimize-work-for-scalars/34343 "2020-02-08T12:22:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 8, 2020, 12:22pm UTC](https://discourse.julialang.org/t/how-to-make-optims-optimize-work-for-scalars/34343/1 "2020-02-08T12:22:01Z")

</div>

In the interest of user-friendliness, would anyone know a way to overload ‘optimize’ to work conveniently for scalar input without crashing?  
[please note I am not asking for opinions as to how good/bad an idea it would be - let’s assume I am willing to undertake any ‘risks’].

Of course, I can get it to work if I redefine my objective function to have one-dimensional Array argument and likewise my starting point, and then tell optimize I do not want Nelder-Mead, but let’s assume I would like to use optimize frequently and conveniently for scalar input and don’t want to go through the above steps every time. I would like (for my own use) to make the routine more user-friendly in this regard. Can anyone suggest a way for me to conveniently do this?  
[PS I don’t mean the ‘Brent’s’ solution which apparently works - I want something fast]

---

<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:** [February 8, 2020, 1:08pm UTC](https://discourse.julialang.org/t/how-to-make-optims-optimize-work-for-scalars/34343/2 "2020-02-08T13:08:14Z")

</div>

> [@compleat](#):
>
> let’s assume I am willing to undertake any ‘risks’ […] I don’t mean the ‘Brent’s’ solution which apparently works - I want something fast

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

> **[Optim.jl](https://julianlsolvers.github.io/Optim.jl/stable/#user/minimization/%23minimizing-a-univariate-function-on-a-bounded-interval)**
>
> Pure Julia implementations of optimization algorithms.

That said, you can always write a wrapper like

```julia
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, BFGS(); autodiff = :forward)

```

You just have to make a decision about what to extract from `Optim.MultivariateOptimizationResults`, or write a conversion routine to `Optim.UnivariateOptimizationResults`.

---

<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:** [February 8, 2020, 1:09pm UTC](https://discourse.julialang.org/t/how-to-make-optims-optimize-work-for-scalars/34343/3 "2020-02-08T13:09:39Z")

</div>

Also, see

[https://github.com/JuliaNLSolvers/Optim.jl/issues/672](https://github.com/JuliaNLSolvers/Optim.jl/issues/672)

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 8, 2020, 1:16pm UTC](https://discourse.julialang.org/t/how-to-make-optims-optimize-work-for-scalars/34343/4 "2020-02-08T13:16:30Z")

</div>

Thanks - I searched but didn’t see that one.
