100 Days of Code Log R1d7

Today, I found this amazing site for Fortran tutorials - https://masuday.github.io/fortran_tutorial/index.html

I went through the tutorials which was organized in a very neat way. Then I proceeded to implement the following simple code.

Make a tridiagonal matrix with 1 on diagonal and 0 or on off-diagonal with an arbitrary $n$. See the following example for $n=5$. $$ \displaystyle{\begin{bmatrix} 1 & 0 & 0 & 0 & 0 \ 0 & 1 & 0 & 0 & 0 \ 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 1 & 0 \ 0 & 0 & 0 & 0 & 1 \end{bmatrix} } $$ The following is my implementation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
program tridiagonal
    implicit none
    integer, parameter :: n = 5
    integer :: m(n,n), i

    call tridiag(m, -2)
    do i = 1,n
        print '(*(i3))', m(i,:)
    end do
    
contains
subroutine tridiag(m, r)
    implicit none
    integer,intent(inout) :: m(:,:)
    integer,intent(in), optional :: r
    integer :: order(2), i
    m = 0
    order = shape(m)
    do i = 1, order(1)
        m(i,i) = 1
    end do
    if (present(r)) then
        do i = 1, order(1)-1
            m(i,i+1) = r
            m(i+1, i) = r
        end do
    end if
end subroutine tridiag 
end program tridiagonal