# Matrix free GMRES with structs?

**URL:** https://discourse.julialang.org/t/matrix-free-gmres-with-structs/79364
**Category:** General Usage
**Tags:** iterative
**Created:** [April 12, 2022, 3:58am UTC](https://discourse.julialang.org/t/matrix-free-gmres-with-structs/79364 "2022-04-12T03:58:32Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Srikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srikumar/32/34565_2.png) [@Srikumar](https://discourse.julialang.org/u/Srikumar)
#### Post date: [April 12, 2022, 3:58am UTC](https://discourse.julialang.org/t/matrix-free-gmres-with-structs/79364/1 "2022-04-12T03:58:32Z")

</div>

I am wondering if there is a way to solve Ax=b with GMRES where x is not a regular vector but a struct consisting of different vectors? A is available as a linearmap/function but I am not how to use GMRES here since x is not one particular vector but a struct consisting of different vectors. Any help would be appreciated!

---

<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: [April 12, 2022, 4:48am UTC](https://discourse.julialang.org/t/matrix-free-gmres-with-structs/79364/2 "2022-04-12T04:48:38Z")

</div>

Maybe use [https://github.com/jonniedie/ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl) ?

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [April 12, 2022, 5:53am UTC](https://discourse.julialang.org/t/matrix-free-gmres-with-structs/79364/3 "2022-04-12T05:53:51Z")

</div>

try `KrylovKit`

---

<div class="post-metadata">

### Author: ![amontoison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amontoison/32/218741_2.png) [@amontoison](https://discourse.julialang.org/u/amontoison)
#### Post date: [April 15, 2022, 9:36pm UTC](https://discourse.julialang.org/t/matrix-free-gmres-with-structs/79364/4 "2022-04-15T21:36:05Z")

</div>

@Srikumar  
All solvers in Krylov.jl are matrix-free which means that you can use your linearmap/function with GMRES ([Factorization-free operators · Krylov.jl](https://juliasmoothoptimizers.github.io/Krylov.jl/dev/factorization-free/)).

You will just need to adapt a little bit your linearmap such that `mul!(y::Vector, A, x::Vector)` is defined.

```julia
x2 = ...
y2 = ...

function mul!(y::Vector, A, x::Vector)
  x2 .= x # convert x in the type of your stuct consisting of different vectors
  mul!(y2, A, x2) # apply your operator-vector routine specialized for your structure  
  y .= y2 # store the result in a basic Vector
end

```
