# How to add other arguments to function turned to LinearOperator?

**URL:** <https://discourse.julialang.org/t/how-to-add-other-arguments-to-function-turned-to-linearoperator/110475>\
**Category:** General Usage\
**Tags:** package, iterative-solvers, linearsolve\
**Created:** [February 20, 2024, 8:20pm UTC](https://discourse.julialang.org/t/how-to-add-other-arguments-to-function-turned-to-linearoperator/110475 "2024-02-20T20:20:02Z")\
**Posts on this page:** 4\
**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 20, 2024, 8:20pm UTC](https://discourse.julialang.org/t/how-to-add-other-arguments-to-function-turned-to-linearoperator/110475/1 "2024-02-20T20:20:02Z")

</div>

Trying to write a matrix-free solver using LinearOperators.jl and Krylov.jl  
However, from this: [Introduction to Linear Operators](https://jso.dev/tutorials/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:

```julia
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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [February 20, 2024, 8:53pm UTC](https://discourse.julialang.org/t/how-to-add-other-arguments-to-function-turned-to-linearoperator/110475/2 "2024-02-20T20:53:56Z")

</div>

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.

---

<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 20, 2024, 8:58pm UTC](https://discourse.julialang.org/t/how-to-add-other-arguments-to-function-turned-to-linearoperator/110475/3 "2024-02-20T20:58:16Z")

</div>

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?

---

<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 21, 2024, 9:16pm UTC](https://discourse.julialang.org/t/how-to-add-other-arguments-to-function-turned-to-linearoperator/110475/4 "2024-02-21T21:16:47Z")

</div>

I figured it out.

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

```julia
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:

```julia
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)`
