# Defining Sparse Matrix from Scratch using vectors

**URL:** https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772
**Category:** General Usage
**Tags:** question, sparse
**Created:** [January 3, 2021, 11:44am UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772 "2021-01-03T11:44:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)
#### Post date: [January 3, 2021, 11:44am UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772/1 "2021-01-03T11:44:26Z")

</div>

Hi

I want to create a custom tridiagonal matrix type whose inputs will be 3 different vectors (`c`, `d` and `e`).  
 ![Vecs](https://global.discourse-cdn.com/julialang/original/3X/d/2/d231a83f21c1346dedd57f8acda1e872352034f4.png)  
These 3 vectors will make the matrix in a format as follows:  
 ![TD_mat](https://global.discourse-cdn.com/julialang/original/3X/b/f/bfe5ccb5a3af6677cfd6e6cedbf81109889b0718.png)

I tried writing a struct

```julia
struct tridiag <: AbstractMatrix{<:AbstractFloat}
    c::Array{<:AbstractFloat}
    d::Array{<:AbstractFloat}
    e::Array{<:AbstractFloat}
end

```

but I’m getting an error

```julia
invalid subtyping in definition of tridiag

Stacktrace:
 [1] top-level scope at In[14]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

```

I’m also not sure how I can define a custom type for tridiagonal (sprase) matrix effectively like this. Any other ideas are welcome and I want to writing this type from scratch and not use something like `Sparce matrix`.

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [January 3, 2021, 12:13pm UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772/2 "2021-01-03T12:13:51Z")

</div>

Will `Tridiagonal` do what you want? You may have to do `using LinearAlgebra` to get it, but it’s built in.

```julia
julia> a=[1,1,1]; b=[2,2,2,2]; c=[3,3,3];

julia> Tridiagonal(a,b,c)
4×4 Tridiagonal{Int64,Array{Int64,1}}:
 2 3 ⋅ ⋅
 1 2 3 ⋅
 ⋅ 1 2 3
 ⋅ ⋅ 1 2

```

---

<div class="post-metadata">

### Author: ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)
#### Post date: [January 3, 2021, 1:13pm UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772/3 "2021-01-03T13:13:19Z")

</div>

I’m just starting out with Julia and learning by writing basic numerical methods from scratch without any libraries. I want to do something like this but want to apply from scratch. I think I can create something similar using struct. Maybe I can read the source code for this linear algebra implementation of tridiagonal. Can you suggest me something here?

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [January 3, 2021, 1:20pm UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772/4 "2021-01-03T13:20:14Z")

</div>

Here are the docs [Linear Algebra · The Julia Language](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.Tridiagonal) and there’s a link to the source code in the lower right corner (you might need to select or hover for it to show up).

---

<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: [January 3, 2021, 1:46pm UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772/6 "2021-01-03T13:46:42Z")

</div>

You want the syntax:

```julia
struct Tridiag{T<:Number} <: AbstractMatrix{T}
    c::Vector{T}
    d::Vector{T}
    e::Vector{T}
end

```

Note that this defines a whole _set_ of types `Tridiag{Float64}`, `Tridiag{Int}`, and so on, where for each of which the fields are concrete types. (Note also that `Array{T}` is abstract because you didn’t specify the dimensionality — you want `Array{T,1}` or equivalently `Vector{T}`.)

---

<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: [January 3, 2021, 2:07pm UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772/7 "2021-01-03T14:07:26Z")

</div>

> [@tgautam03](#):
>
> Maybe I can read the source code for this linear algebra implementation of tridiagonal.

You can also see the Alan Edelman’s video on [Matrix Structures](https://www.youtube.com/watch?v=zoUeUG-Sm6g) for inspiration.

I have written (to learn and for fun) the structure as suggested by Steven and some two other functions, and already matrix multiplications work. This is very nice 🙂

```julia
julia> A = Tridiag([1,1],[2,2,2],[3,3])
3×3 Tridiag{Int64}:
 2 3 0
 1 2 3
 0 1 2

julia> A*A
3×3 Array{Int64,2}:
 7 12 9
 4 10 12
 1 4 7

```

If you want to see what I did so far, here is the code:

> **Code**
>
> ```julia
> julia> struct Tridiag{T<:Number} <: AbstractMatrix{T} 
> a :: Vector{T}
> b :: Vector{T}
> c :: Vector{T}
> end
> 
> julia> function Base.getindex(M::Tridiag{T}, i::Int, j::Int) where T
> if i == j
> M.b[i]
> elseif i == j + 1
> M.a[j]
> elseif i == j - 1
> M.c[i]
> else
> zero(T)
> end
> end
> 
> julia> Base.size(M::Tridiag) = (length(M.b),length(M.b))
> 
> julia> A = Tridiag([1,1],[2,2,2],[3,3])
> 3×3 Tridiag{Int64}:
> 2 3 0
> 1 2 3
> 0 1 2
> 
> julia> A*A
> 3×3 Array{Int64,2}:
> 7 12 9
> 4 10 12
> 1 4 7
> 
> ```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 3, 2021, 6:56pm UTC](https://discourse.julialang.org/t/defining-sparse-matrix-from-scratch-using-vectors/52772/8 "2021-01-03T18:56:45Z")

</div>

@lmiq, thanks for sharing that nice video link and your code too. If one has missed the previous lessons in the series, one might not know that some of the examples shown require using several packages (ex: LinearAlgebra, SparseArrays, Statistics).
