# Methods for vectors of sparse vs dense matrices

**URL:** https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404
**Category:** General Usage
**Tags:** function, vector, sparse, matrices
**Created:** [May 18, 2021, 11:03pm UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404 "2021-05-18T23:03:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gvdr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gvdr/32/6387_2.png) [@gvdr](https://discourse.julialang.org/u/gvdr)
#### Post date: [May 18, 2021, 11:03pm UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/1 "2021-05-18T23:03:34Z")

</div>

I’m writing a function which should have two methods, one for vectors of sparse matrices (`SparseMatrixCSC`) and one for vectors of dense matrices.

I don’t want to pre/dicate about the nature of those matrices beyond the point of distinguishing between sparse and dense. So far, my definition look like:

```julia
function f(Tₜ::T) where T <: Vector{SparseMatrixCSC{Float64, Int64}}.

```

and

```julia
function f(Tₜ::T) where T <: Vector{Matrix{Float64}}.

```

yet I want to get rid of that `{Float64}` specification.

How would I do it?

Eventually, I would like to write the method so that it would apply to _any_ `SparseMatrixCSC` or to any `Matrix` (but distinguishing them).

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [May 18, 2021, 11:07pm UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/2 "2021-05-18T23:07:32Z")

</div>

Does it work if you just remove the element type? Like

```julia
Vector{Matrix}

```

or

```julia
Vector{<:Matrix}

```

---

<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: [May 18, 2021, 11:09pm UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/3 "2021-05-18T23:09:14Z")

</div>

> [@gvdr](#):
>
> `function f(Tₜi::T) <: where T <: Vector{SparseMatrixCSC{Float64, Int64}}.`

I would do something more abstract, like:

```julia
f(Tₜ::AbstractVector{<:AbstractSparseMatrix}) = ...
f(Tₜ::AbstractVector{<:AbstractMatrix}) = ...

```

---

<div class="post-metadata">

### Author: ![gvdr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gvdr/32/6387_2.png) [@gvdr](https://discourse.julialang.org/u/gvdr)
#### Post date: [May 18, 2021, 11:11pm UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/4 "2021-05-18T23:11:10Z")

</div>

The first does not work (that was my initial guess), but the second does!

I didn’t know you could use a nested inherit type in that way, nice!

---

<div class="post-metadata">

### Author: ![gvdr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gvdr/32/6387_2.png) [@gvdr](https://discourse.julialang.org/u/gvdr)
#### Post date: [May 18, 2021, 11:12pm UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/5 "2021-05-18T23:12:26Z")

</div>

Thanks! This is perfect 🙂

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 18, 2021, 11:12pm UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/6 "2021-05-18T23:12:34Z")

</div>

```julia
function f(v::T) where T <: Vector{Matrix{N}} where N v[1]; end                            
f (generic function with 1 method)                                                                  
                                                                                                    
julia> v1 = [rand(2,3)]
1-element Vector{Matrix{Float64}}:                                                                  
 [0.6498353443464961 0.8556641584648517 0.05535954378049679; 0.39224917246518864 0.35720373820135043 0.23334843425685636]                                                                               
                                                                                                    
julia> f(v1)
2×3 Matrix{Float64}:                                                                                
 0.649835 0.855664 0.0553595                                                                      
 0.392249 0.357204 0.233348                                                                       
                                                                                                    
julia> 

```

Will that do?

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [May 19, 2021, 1:23am UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/7 "2021-05-19T01:23:51Z")

</div>

> [@stevengj](#):
>
> ```julia
> f(Tₜ::AbstractVector{<:AbstractSparseMatrix}) = ...
> f(Tₜ::AbstractVector{<:AbstractMatrix}) = ...
> 
> ```

I like @stevengj’s answer and I think it could still be made slightly shorter.

```julia
f(Tₜ::Vector{<:AbstractSparseMatrix}) = ...
f(Tₜ::Vector{<:AbstractMatrix}) = ...

```

---

<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: [May 19, 2021, 3:02am UTC](https://discourse.julialang.org/t/methods-for-vectors-of-sparse-vs-dense-matrices/61404/8 "2021-05-19T03:02:07Z")

</div>

> [@Seif\_Shebl](#):
>
> I like @stevengj’s answer and I think it could still be made slightly shorter.
> 
> ```julia
> f(Tₜ::Vector{<:AbstractSparseMatrix}) = ...
> f(Tₜ::Vector{<:AbstractMatrix}) = ...
> 
> ```

That’s less generic. Your way only allows the outer container to be Julia’s built-in `Vector` type, as opposed to some other `AbstractVector` container (e.g. an `OffsetArray`, an `AxisArrays`, etcetera).
