# Vectorize optimize

**URL:** https://discourse.julialang.org/t/vectorize-optimize/51513
**Category:** General Usage
**Tags:** optim, optimization, vector
**Created:** [December 9, 2020, 11:20am UTC](https://discourse.julialang.org/t/vectorize-optimize/51513 "2020-12-09T11:20:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [December 9, 2020, 11:20am UTC](https://discourse.julialang.org/t/vectorize-optimize/51513/1 "2020-12-09T11:20:53Z")

</div>

Is there a better way to write this?

```julia
@pipe [(1,2),(2,3)] .|> (f(x) = (x-_[1])^2 + (x-_[2])^2 ) .|> optimize(_, -10,10).minimizer

```

I’d like to minimize a function over a given variable for different values of the other two variables.  
Can the optimize function be directly vectorised ? It didn’t seem to work for me.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [December 9, 2020, 1:27pm UTC](https://discourse.julialang.org/t/vectorize-optimize/51513/2 "2020-12-09T13:27:09Z")

</div>

Maybe this?

```julia
v = [(1, 2), (2, 3)]
[optimize(x -> sum(abs2, x .- y), -10, 10).minimizer for y in v]

```

Or equivalently with map-do

```julia
map(v) do y
    optimize(x -> sum(abs2, x .- y), -10, 10).minimizer
end

```

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [December 10, 2020, 10:43pm UTC](https://discourse.julialang.org/t/vectorize-optimize/51513/3 "2020-12-10T22:43:42Z")

</div>

Thanks tomerarnon. Much appreciated  
realised there is also this

`( (a,b) -> optimize( x -> (x-a)^2 + (x-b)^2, -10,10).minimizer ).( [1,2], [2,3] )`

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 11, 2020, 12:00am UTC](https://discourse.julialang.org/t/vectorize-optimize/51513/4 "2020-12-11T00:00:04Z")

</div>

Realize that you are not actually “vectorizing” the optimization in any meaningful way with the above solutions: you are just calling `optimize` twice in sequence.

(That’s okay! Julia is not a language where you need to “vectorize” everything to get good performance. And in general it is tricky to benefit from sharing computations between optimizations with different parameters, although there are algorithms to compute the Pareto front that attempt this. I suppose you could try numerical continuation of the KKT conditions to track a local optimum as you vary parameters continuously.)

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [December 11, 2020, 12:09am UTC](https://discourse.julialang.org/t/vectorize-optimize/51513/5 "2020-12-11T00:09:51Z")

</div>

Thanks Steven. Perhaps Vectorize was the wrong word here. in this case its not about performance. I’m just trying to find the most concise code. Often the function. “dot” notation allows this but it doesn’t seem to work in the case of optimize so this is the next best i think in terms of readability

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 12, 2020, 2:21am UTC](https://discourse.julialang.org/t/vectorize-optimize/51513/6 "2020-12-12T02:21:03Z")

</div>

> [@Lincoln\_Hannah](#):
>
> so this is the next best i think in terms of readability

Have you considered a `for` loop? I suspect that an ordinary loop is probably clearer than any of the suggestions above.

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [December 14, 2020, 9:58am UTC](https://discourse.julialang.org/t/vectorize-optimize/51513/7 "2020-12-14T09:58:02Z")

</div>

I’ve always hated `for` loops. but i guess they have their place:)
