# Optim.jl : user defined Manifold, error in the definition of the retraction

**URL:** <https://discourse.julialang.org/t/optim-jl-user-defined-manifold-error-in-the-definition-of-the-retraction/130297>\
**Category:** Optimization (Mathematical)\
**Tags:** manifolds\
**Created:** [June 28, 2025, 11:42am UTC](https://discourse.julialang.org/t/optim-jl-user-defined-manifold-error-in-the-definition-of-the-retraction/130297 "2025-06-28T11:42:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)\
**Post date:** [June 28, 2025, 11:42am UTC](https://discourse.julialang.org/t/optim-jl-user-defined-manifold-error-in-the-definition-of-the-retraction/130297/1 "2025-06-28T11:42:56Z")

</div>

Hi, I try to optimize a simple quadratic function on a user-defined manifold (the hyperplan orthogonal to `ones(n)`) with the following code

```julia
using Optim
const c = ones(2) ./ sqrt(2)
f(x) = 0.5 * sum((x - c).^2)
g!(g,x) = g[:] = x - c

struct HypOne <: Manifold end
retract!(S::HypOne, x) = (x ./= sum(x))
function project_tangent!(S::HypOne, g, x)
    α = 1. / size(x,1) * sum(g)
    g .-= α
end
x0 = [1., 0.]
Optim.optimize(f, g!, x0, Optim.ConjugateGradient(manifold=HypOne()))

```

apparently, my way to define the manifold and the retraction seems to be not good, as the following error message says

```julia
ERROR: LoadError: MethodError: no method matching retract!(::HypOne, ::Vector{Float64})
The function `retract!` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  retract!(::Optim.ProductManifold, ::Any)
   @ Optim ~/.julia/packages/Optim/7krni/src/Manifolds.jl:144
  retract!(::Optim.PowerManifold, ::Any)
   @ Optim ~/.julia/packages/Optim/7krni/src/Manifolds.jl:113
  retract!(::Optim.Stiefel_CholQR, ::Any)
   @ Optim ~/.julia/packages/Optim/7krni/src/Manifolds.jl:91
  ...

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 28, 2025, 12:03pm UTC](https://discourse.julialang.org/t/optim-jl-user-defined-manifold-error-in-the-definition-of-the-retraction/130297/2 "2025-06-28T12:03:58Z")

</div>

The error reports that it does not “find” your defined methods, not that they are not good. The reason is, that you define them in your main scope, which in short is a different namespace than the one from Optim. One could in short say that is for safety reasons.

To actually add methods to `Optim.retract!` you have to import that methods with `import Optim: retract!, project_tangent!`. Not that I did not check your formulas in general, but the following code at least runs without errors.

```julia
using Optim
import Optim: retract!, project_tangent! # This is the only new line
const c = ones(2) ./ sqrt(2)
f(x) = 0.5 * sum((x - c).^2)
g!(g,x) = g[:] = x - c

struct HypOne <: Manifold end
retract!(S::HypOne, x) = (x ./= sum(x))
function project_tangent!(S::HypOne, g, x)
    α = 1. / size(x,1) * sum(g)
    g .-= α
end
x0 = [1., 0.]
Optim.optimize(f, g!, x0, Optim.ConjugateGradient(manifold=HypOne()))

```

---

<div class="post-metadata">

**Author:** ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)\
**Post date:** [June 29, 2025, 8:49pm UTC](https://discourse.julialang.org/t/optim-jl-user-defined-manifold-error-in-the-definition-of-the-retraction/130297/3 "2025-06-29T20:49:42Z")

</div>

Thanks, it solved my problem.
