# Minimize f(x) with |x|=1

**URL:** https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307
**Category:** Optimization (Mathematical)
**Tags:** optimization, constraint
**Created:** [November 30, 2024, 10:51pm UTC](https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307 "2024-11-30T22:51:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nichola](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nichola](https://discourse.julialang.org/u/Nichola)
#### Post date: [November 30, 2024, 10:51pm UTC](https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307/1 "2024-11-30T22:51:21Z")

</div>

What is the simplest way to minimize f(x) with |x|=1 where x is a vector ? (I know the gradient of f)  
I’ve checked the example on [Using Equality and Inequality Constraints · Optimization.jl](https://docs.sciml.ai/Optimization/stable/tutorials/constraints/) but the solution calls AmplNLWriter.jl.  
Given that the constraint |x|=1 is a super basic one, is there a more straightforward way to solve the problem just with Optimization.jl ?

---

<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: [December 1, 2024, 1:25am UTC](https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307/2 "2024-12-01T01:25:50Z")

</div>

You can just eliminate one of the values. For example, `z = sqrt(1 - x^2 + y^2)`, and then only optimize the other values.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 1, 2024, 1:54am UTC](https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307/3 "2024-12-01T01:54:08Z")

</div>

Or maybe define a non-constrained optimization problem, with a similar minimum. Something along the lines of:

```julia
function g(x)
    r = norm(x)
    factor = exp(-(r-1)^2)
    return f(x) / factor
end

```

The idea is that `x` with `|x| = 1` is still a minimizer of `g(x)` as `factor = 1` there and smaller everywhere else.

---

<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 1, 2024, 3:06am UTC](https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307/4 "2024-12-01T03:06:53Z")

</div>

> [@Nichola](#):
>
> What is the simplest way to minimize f(x) with |x|=1 where x is a vector

You could just solve the unconstrained problem

\min\_{y\in \mathbb{R}^n} f(y / \Vert y \Vert)

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [December 1, 2024, 5:55am UTC](https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307/5 "2024-12-01T05:55:59Z")

</div>

Another option is to use spherical coordinates:

\begin{aligned} x\_1 &= \cos(\phi\_1) \\ x\_2 &= \sin(\phi\_1) \cos(\phi\_2) \\ &\vdots \\ x\_{n - 1} &= \sin(\phi\_1) \cdots \sin(\phi\_{n - 2}) \cos(\phi\_{n - 1}) \\ x\_n &= \sin(\phi\_1) \cdots \sin(\phi\_{n - 2}) \sin(\phi\_{n - 1}) \end{aligned}

where

\begin{aligned} \phi\_1, \ldots, \phi\_{n - 2} &\in [0, \pi] \\ \phi\_{n - 1} &\in [0, 2\pi) \end{aligned}

This enforces \|\boldsymbol{x}\| = 1, so you can write the optimization problem in terms of \phi\_1, \ldots, \phi\_{n - 1} and solve as an unconstrained problem.

Equations adapted from [n-sphere - Wikipedia](https://en.wikipedia.org/wiki/N-sphere#Spherical_coordinates).

---

<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: [December 1, 2024, 7:21am UTC](https://discourse.julialang.org/t/minimize-f-x-with-x-1/123307/6 "2024-12-01T07:21:05Z")

</div>

If you’re talking about the Euclidean norm, then the sphere is a manifold and you can use Manopt.jl to work directly on that manifold.
