# LinearMaps.jl

**URL:** <https://discourse.julialang.org/t/linearmaps-jl/79413>\
**Category:** General Usage\
**Tags:** linearalgebra\
**Created:** [April 13, 2022, 4:10am UTC](https://discourse.julialang.org/t/linearmaps-jl/79413 "2022-04-13T04:10:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srikumar/32/34565_2.png) [@Srikumar](https://discourse.julialang.org/u/Srikumar)\
**Post date:** [April 13, 2022, 4:10am UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/1 "2022-04-13T04:10:31Z")

</div>

Is it possible to use Linearmaps on custom data types? I have a struct data type v and wish to construct a function wrapped linear map A that can act on v like A\*v. Would love to know how I could do this! Thanks in advance

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 13, 2022, 5:35am UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/2 "2022-04-13T05:35:27Z")

</div>

I believe I somewhat understand how `LinearMaps` is working, but I’m not sure if I understand your question correctly. Therefore let us start with a simple example

```julia
using LinearMaps

A = rand(2, 2)
fA!(y, x) = y .= A * x
LA = LinearMap{Float64}(fA!, 2, 2)
v = rand(2)
@assert LA * v ≈ A * v

```

Is that similar to what you are looking for?

Edit: if v is a `struct` of your own making, you could either

- support converting it to a vector
- or add a `Base.*(LinearMap, YourStruct)` method

---

<div class="post-metadata">

**Author:** ![suavesito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suavesito/32/34386_2.png) [@suavesito](https://discourse.julialang.org/u/suavesito)\
**Post date:** [April 13, 2022, 5:53am UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/3 "2022-04-13T05:53:46Z")

</div>

> [@goerch](#):
>
> Edit: if v is a `struct` of your own making, you could either
> 
> - support converting it to a vector
> - or add a `Base.*(LinearMap, YourStruct)` method

You can also make the struct a subtype of `AbstractVector` and implement the interface for `Arrays`. But probably adding `Base.*(LinearMap, YourStruct)` is easier/better.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 13, 2022, 6:01am UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/4 "2022-04-13T06:01:06Z")

</div>

> [@suavesito](#):
>
> You can also make the struct a subtype of `AbstractVector` and implement the interface for `Arrays` .

Correct, this would most probably be the way to go if OP 's `struct` is part of a public API.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 13, 2022, 6:06am UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/5 "2022-04-13T06:06:56Z")

</div>

Maybe using ComponentArrays is faster than implementing the AbstractVector yourself?

> [@Matrix free GMRES with structs?](https://discourse.julialang.org/t/matrix-free-gmres-with-structs/79364/2):
>
> Maybe use [https://github.com/jonniedie/ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl) ?

---

<div class="post-metadata">

**Author:** ![Srikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srikumar/32/34565_2.png) [@Srikumar](https://discourse.julialang.org/u/Srikumar)\
**Post date:** [April 13, 2022, 11:04pm UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/6 "2022-04-13T23:04:16Z")

</div>

Thanks, yes that is what I am looking for. Are you suggesting that I define the action of the linearmap inside Base.\*(LinearMap, struct)? I already have a function based linearmap(like the fA!+LA in your example) that takes in the struct and defines the action of the function/linearmap. Since I have already defined the action of the linearmap / \* here I am not exactly sure what to define under Base. \*(). Thoughts?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 13, 2022, 11:22pm UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/7 "2022-04-13T23:22:19Z")

</div>

> [@Srikumar](#):
>
> Thanks, yes that is what I am looking for.

I’m still not sure;) But then the natural continuation is something like

```julia
using LinearMaps
using StaticArrays

A = rand(2, 2)
fA!(y, x) = y .= A * x
LA = LinearMap{Float64}(fA!, 2, 2)
v = rand(2)
@assert LA * v ≈ A * v

struct SpecialVector
    v1::Float64
    v2::Float64
end

import Base.*
function *(l::LinearMap{Float64}, v::SpecialVector)
    l * [v.v1, v.v2]
end

v = rand(2)
sv = SpecialVector(v...)
@assert LA * sv ≈ A * v

```

Does this help?

---

<div class="post-metadata">

**Author:** ![Srikumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srikumar/32/34565_2.png) [@Srikumar](https://discourse.julialang.org/u/Srikumar)\
**Post date:** [April 14, 2022, 6:08pm UTC](https://discourse.julialang.org/t/linearmaps-jl/79413/8 "2022-04-14T18:08:21Z")

</div>

Yeah I now get it, thanks a lot!
