How to add other arguments to function turned to LinearOperator?

Trying to write a matrix-free solver using LinearOperators.jl and Krylov.jl
However, from this: Introduction to Linear Operators
I’m seeing that you can only use functions that follow the 5-argument (and 3) mul! operation.

But I would like to create a linear operator that is able to take in more variables such as:

function customfunc!(out, in, a, b, dt, nu)
for i in 1:length(in)
out[i] = in[i]*dt*(nu[i]+nu[i+1])/2.0
end

In LinearMaps.jl it would be something like:

customfunc = (N, dt, nu) -> LinearMap(N; ismutating=true) do Z,U

for i in 1:N
Z[i] = U[i]*dt*(nu[i]+nu[i+1])/2.0
end
return Z
end

CFunc = customfunc(100, 0.01, [1.0,2.0,3.0])

Then I could use CFunc in IterativeSolvers.jl.

Any suggestions?

This seems exactly like you would solve this problem. What is your issue with it?

If you don’t like using an anonymous function to create a closure with a lot of captured variables or you want to change some paramteres without making a new LinearMap, you can of course convert it to a proper type from which you construct the LinearMap.

What I’m asking is to use LinearOperators.jl rather than LinearMaps.jl. The tutorials show that matrix-free functions should follow the 5-argument mul! which wouldn’t allow for extra variables to define a linear operator.

Are you saying that LinearOperators.jl is useless when it comes to changing operators and one should stick to LinearMaps.jl?

I figured it out.

You can use an anonymous function inside another anonymous function such as:

customfunc = (N, dt, nu) -> (Z,U,alpha,beta) -> begin

for i in 1:N
Z[i] = U[i]*dt*(nu[i]+nu[i+1])/2.0
end
return Z
end

Then you initialize with your parameters:

N =12
dt = 0.5
nu=0.01
custmul! = customfun(N,dt,nu)

Then you can put this into a linear operator because this is now a 4 argument function that works as:

custmul!(Out, In, alpha, beta)