# Solving linear problem Ax = b, using function handle that computes Ax

**URL:** https://discourse.julialang.org/t/solving-linear-problem-ax-b-using-function-handle-that-computes-ax/96917
**Category:** General Usage
**Created:** [March 31, 2023, 10:41pm UTC](https://discourse.julialang.org/t/solving-linear-problem-ax-b-using-function-handle-that-computes-ax/96917 "2023-03-31T22:41:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sanjeev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjeev/32/212058_2.png) [@sanjeev](https://discourse.julialang.org/u/sanjeev)
#### Post date: [March 31, 2023, 10:41pm UTC](https://discourse.julialang.org/t/solving-linear-problem-ax-b-using-function-handle-that-computes-ax/96917/1 "2023-03-31T22:41:34Z")

</div>

I want to solve Ax = b, using some iterative solvers. I don’t have matrix `A` available at hand (too expensive to compute), but I can readily calculate `A * x` for any given `x`.

I tried looking at options that are exposed in this awesome package, [LinearSolve.jl](https://github.com/SciML/LinearSolve.jl) and in this package [IterativeSolvers.jl](https://iterativesolvers.julialinearalgebra.org/stable/), but didn’t find any such option.  
I would like to use GMRES or Algebraic Multigrid for solving this system.

MATLAB has an option in GMRES in to compute Ax using a function handle instead of using a numeric A matrix, so I’m hoping I’ve overlooked something that’s available in julia.

I can solve this using NLsolve by calculating the residue `Ax-b` and minimizing it, but something simpler using an iterative solver would be better.

Thanks in advance.

---

<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: [March 31, 2023, 10:47pm UTC](https://discourse.julialang.org/t/solving-linear-problem-ax-b-using-function-handle-that-computes-ax/96917/2 "2023-03-31T22:47:11Z")

</div>

You can pass a function `f` (which computes `f(x) = A*x` somehow) to [KrylovKit.linsolve](https://jutho.github.io/KrylovKit.jl/stable/man/linear/#KrylovKit.linsolve). Alternatively, if you wrap `f` in [`LinearMap(f, n)`](https://julialinearalgebra.github.io/LinearMaps.jl/stable/) then you can pass it to IterativeSolvers.jl or LinearSolve.jl. LinearSolve.jl also has its own [matrix-free interface](https://docs.sciml.ai/LinearSolve/stable/basics/LinearProblem/) described in its documentation.

---

<div class="post-metadata">

### Author: ![sanjeev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjeev/32/212058_2.png) [@sanjeev](https://discourse.julialang.org/u/sanjeev)
#### Post date: [March 31, 2023, 10:58pm UTC](https://discourse.julialang.org/t/solving-linear-problem-ax-b-using-function-handle-that-computes-ax/96917/3 "2023-03-31T22:58:45Z")

</div>

Thank you! This is what I was looking for.
