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

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 and in this package IterativeSolvers.jl, 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.

1 Like

You can pass a function f (which computes f(x) = A*x somehow) to KrylovKit.linsolve. Alternatively, if you wrap f in LinearMap(f, n) then you can pass it to IterativeSolvers.jl or LinearSolve.jl. LinearSolve.jl also has its own matrix-free interface described in its documentation.

2 Likes

Thank you! This is what I was looking for.