# Using LinearMaps.jl Inversemap for Preconditioning IterativeSolvers.jl

**URL:** <https://discourse.julialang.org/t/using-linearmaps-jl-inversemap-for-preconditioning-iterativesolvers-jl/110070>\
**Category:** General Usage\
**Tags:** linearalgebra, iterative-solvers\
**Created:** [February 11, 2024, 5:52pm UTC](https://discourse.julialang.org/t/using-linearmaps-jl-inversemap-for-preconditioning-iterativesolvers-jl/110070 "2024-02-11T17:52:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![erny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erny123/32/52255_2.png) [@erny123](https://discourse.julialang.org/u/erny123)\
**Post date:** [February 11, 2024, 5:52pm UTC](https://discourse.julialang.org/t/using-linearmaps-jl-inversemap-for-preconditioning-iterativesolvers-jl/110070/1 "2024-02-11T17:52:22Z")

</div>

I’ve seen some examples of creating a matrix-free method via linear operators, structs, and linear maps for IterativeSolvers.jl and LinearMaps.jl . However, I haven’t seen any examples of creating matrix-free preconditioner for solving systems.

So here’s what I’m trying to do. Say I want to solve a system:

\underline{A}\cdot \bar{x} = \bar{b}

With a preconditioning matrix ‘H’ such that:

\underline{H}\cdot\bar{z} = \bar{v}

Then the iterative solver would do something like:

\bar{x}^{k+1} = \bar{x}^{k} + w^{k} \cdot \bar{z}^{k} = \bar{x}^{k} + w^{k} \cdot \underline{H}^{-1} \bar{v}^{k}

Where ‘v^{k}’ could be the residual:

\bar{v} = \bar{r}

So according to the IterativeSolvers.jl, the preconditioner needs to have a method that allos for `ldiv!` and `P\x`

So if I have a preconditioning function for \underline{H} such as:

```julia
Hpre = ( x::Array{Float64}) -> LinearMap(length(x); ismutating=true) do V,Z

#.... do matrix-free H
return Z
end

```

Then would it be possible to just do:

```julia
Hpresolve = (x,H,b) -> IterativeSolvers.sor!(fill!(x, 0), H, b)

Hinvpre = InverseMap(Hpre; solver=Hpresolve)

S = (x) -> LinearMap(length(x); ismutating=true) do X,B
#some linear operator S*X = B
return B
end

#rhs
b = zeros(#something)

x = IterativeSolvers.cg(S, b, Pl = Hinvpre)

```

I’d like to get input on this since I’m having trouble figuring out how I would implement this.

---

<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:** [March 13, 2024, 5:03pm UTC](https://discourse.julialang.org/t/using-linearmaps-jl-inversemap-for-preconditioning-iterativesolvers-jl/110070/2 "2024-03-13T17:03:40Z")

</div>

@erny123 I don’t know well LinearMaps.jl but you can find matrix-free preconditioners that should work with IterativeSolvers.jl [here](https://jso.dev/Krylov.jl/dev/preconditioners/#Examples).  
Note that I only tested them with Krylov.jl.
