# Combining \`Optim\` and \`FieldVector\` from \`StaticArrays\`

**URL:** https://discourse.julialang.org/t/combining-optim-and-fieldvector-from-staticarrays/131472
**Category:** Optimization (Mathematical)
**Tags:** optim, staticarrays
**Created:** [August 8, 2025, 10:10am UTC](https://discourse.julialang.org/t/combining-optim-and-fieldvector-from-staticarrays/131472 "2025-08-08T10:10:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![NoFishLikeIan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nofishlikeian/32/20917_2.png) [@NoFishLikeIan](https://discourse.julialang.org/u/NoFishLikeIan)
#### Post date: [August 8, 2025, 10:10am UTC](https://discourse.julialang.org/t/combining-optim-and-fieldvector-from-staticarrays/131472/1 "2025-08-08T10:10:16Z")

</div>

I’m getting a `MethodError` in `LBFGS` when trying to use a custom mutable struct that inherits from `FieldVector` for an optimization problem.

```julia
using StaticArrays
using Optimization, OptimizationOptimJL

mutable struct Point{T} <: FieldVector{2, T}
    x::T
    y::T
end

function rosenbrock(v, p)
    (p[1] - v[1])^2 + p[2] * (v[2] - v[1]^2)^2
end

v₀ = Point(0., 0.)
p = (1., 100.)

f = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())
prob = OptimizationProblem(f, v₀, p)
sol = solve(prob, LBFGS()) # ERROR: MethodError: no method matching Optim.LBFGSState(::Point{…}, ...)

```

My first attept was to write a new `rosenbrock` method as

```julia
function rosenbrock(v::Point, p)
    rosenbrock(SVector(v.x, v.y), p)
end

```

but of course this still fails. Is there a good way to get this to work?

---

<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: [August 8, 2025, 1:36pm UTC](https://discourse.julialang.org/t/combining-optim-and-fieldvector-from-staticarrays/131472/2 "2025-08-08T13:36:18Z")

</div>

Hi! Can you share the complete stack trace?

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [August 8, 2025, 8:41pm UTC](https://discourse.julialang.org/t/combining-optim-and-fieldvector-from-staticarrays/131472/3 "2025-08-08T20:41:29Z")

</div>

I get

```julia-auto
julia> sol = solve(prob, LBFGS()) # ERROR: MethodError: no method matching Optim.LBFGSState(::Point{…}, ...)
ERROR: MethodError: no method matching Optim.LBFGSState(::Point{…}, ::Point{…}, ::Point{…}, ::Vector{…}, ::Vector{…}, ::Vector{…}, ::Point{…}, ::Point{…}, ::Point{…}, ::Float64, ::MVector{…}, ::Vector{…}, ::Int64, ::Point{…}, ::MVector{…}, ::Float64)
The type `Optim.LBFGSState` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  Optim.LBFGSState(::Tx, ::Tx, ::G, ::Vector{T}, ::Tdx, ::Tdg, ::Tx, ::Tx, ::Tx, ::T, ::Any, ::Any, ::Int64, ::Tx, ::Tx, ::T) where {Tx, Tdx, Tdg, T, G}
   @ Optim ~/.julia/packages/Optim/7krni/src/multivariate/solvers/first_order/l_bfgs.jl:139

```

where the penultimate argument is of type `MVector` instead of `Point`. I guess this is caused by the default method of `similar` for `FieldVector`. With

```julia-auto
Base.similar(::Type{<:Point}, ::Type{T}) where T = Point(zero(T), zero(T))

```

it works:

```julia-auto
julia> sol = solve(prob, LBFGS())
retcode: Success
u: 2-element Point{Float64} with indices SOneTo(2):
 0.999999999999928
 0.9999999999998559

```

One could also add a constructor for `Point` that doesn’t initialize the fields.

---

<div class="post-metadata">

### Author: ![NoFishLikeIan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nofishlikeian/32/20917_2.png) [@NoFishLikeIan](https://discourse.julialang.org/u/NoFishLikeIan)
#### Post date: [August 9, 2025, 10:15am UTC](https://discourse.julialang.org/t/combining-optim-and-fieldvector-from-staticarrays/131472/4 "2025-08-09T10:15:56Z")

</div>

You are absolutely right, this works! It is suggested in the `FieldVector` [documentation](https://juliaarrays.github.io/StaticArrays.jl/v0.7/pages/api.html#FieldVector-1) to write a `Base.similar` method. I missed it.
