Skip to content

Commit

Permalink
Added function to expand matrix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Niceno committed Oct 13, 2017
1 parent 3719be8 commit 22cc144
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 21 deletions.
5 changes: 5 additions & 0 deletions Compress_Matrix.f90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ subroutine Compress_Matrix(c, a)

n = size(a, 1)

!------------------------------------!
! Stored dimension of the matrix !
!------------------------------------!
c % n = n

!----------------------------------------!
! Count non-zero terms in the matrix !
!----------------------------------------!
Expand Down
42 changes: 26 additions & 16 deletions Driver.f90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program Driver
include "Matrix_Vector_Multiply.int"
include "Transpose_Matrix.int"
include "Compress_Matrix.int"
include "Expand_Matrix.int"
!-----------------------------------[Locals]-----------------------------------!
integer :: row, col, choice, i
!==============================================================================!
Expand Down Expand Up @@ -47,10 +48,11 @@ program Driver
data (a2(7,col), col=1,n2) / 0., 0., -1., 0., -3., -4., 44. / ! =--> row 7
data (b2(row), row=1,n2) / 1., 2., 3., 4., 3., 2., 1. /

!-------------------!
! Compressed matrix !
!-------------------!
type(Matrix) :: ac2
!----------------------------------------!
! Matrices for compressing and expanding !
!----------------------------------------!
type(Matrix) :: ac3
real, allocatable :: a3(:,:)
!------------------------------------------------------------------------------!

write(*,*) '============================'
Expand Down Expand Up @@ -109,28 +111,36 @@ program Driver
call Print_Vector("Vector y2 should recover the source term:", y2)
end if

!---------------------------------------------------!
! Demonstrate compressing and expanding matrices !
!---------------------------------------------------!
if(choice == 3) then

call Print_Matrix("Original matrix a2", a2)

! Compress matrix "a2" and store it in "ac2"
call Compress_Matrix(ac2, a2)
! Compress matrix "a2" and store it in "ac3"
call Compress_Matrix(ac3, a2)

call Print_Vector("ac2 % val:", ac2 % val)
call Print_Vector("ac3 % val:", ac3 % val)

write(*,*) "ac2 % col = "
do i=1, size(ac2 % col)
write(*,'(2I4)'), i, ac2 % col(i)
write(*,*) "ac3 % col = "
do i=1, size(ac3 % col)
write(*,'(2I4)'), i, ac3 % col(i)
end do
write(*,*) "ac2 % row = "
do i=1, size(ac2 % row)
write(*,'(2I4)'), i, ac2 % row(i)
write(*,*) "ac3 % row = "
do i=1, size(ac3 % row)
write(*,'(2I4)'), i, ac3 % row(i)
end do
write(*,*) "ac2 % dia = "
do i=1, size(ac2 % dia)
write(*,'(2I4)'), i, ac2 % dia(i)
write(*,*) "ac3 % dia = "
do i=1, size(ac3 % dia)
write(*,'(2I4)'), i, ac3 % dia(i)
end do

! Expand matrix "ac2" and store it in "a3"
call Expand_Matrix(a3, ac3)

call Print_Matrix("Epanded matrix ac3:", a3)

end if

end program Driver
36 changes: 36 additions & 0 deletions Expand_Matrix.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
!==============================================================================!
subroutine Expand_Matrix(a, c)
!------------------------------------------------------------------------------!
!----------------------------------[Modules]-----------------------------------!
use Matrix_Mod
!------------------------------------------------------------------------------!
implicit none
!------------------------------------------------------------------------------!
real, allocatable :: a(:,:)
type(Matrix) :: c
!------------------------------------------------------------------------------!
integer :: row, col ! row used to be "i", col used to be "j"
integer :: n, pos
integer :: non_zeros
!==============================================================================!

n = c % n

!---------------------!
! Allocate memory !
!---------------------!
allocate (a(n,n));
a = 0.0

!------------------------------!
! Form the expanded matrix !
!------------------------------!
pos = 1
do row = 1, n ! browse through rows
do pos = c % row(row), c % row(row + 1) - 1 ! brows through columns
col = c % col(pos) ! take the real column number
a(row, col) = c % val(pos)
end do
end do

end subroutine Expand_Matrix
8 changes: 8 additions & 0 deletions Expand_Matrix.int
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface
subroutine Expand_Matrix(a, c)
use Matrix_Mod
implicit none
real, allocatable :: a(:,:)
type(Matrix) :: c
end subroutine Expand_Matrix
end interface
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SFS_SOURCES = \
Cholesky_Factorization_U.f90 \
Compress_Matrix.f90 \
Driver.f90 \
Expand_Matrix.f90 \
Forward_Substitution.f90 \
Forward_Substitution_L.f90 \
Forward_Substitution_U.f90 \
Expand Down
11 changes: 6 additions & 5 deletions Matrix_Mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ module Matrix_Mod
! A % dia = [ 1 5 9 12 ] !
!==============================================================================!
type Matrix
integer :: nonzeros ! number of nonzero entries
real, allocatable :: val(:) ! value
integer, allocatable :: col(:) ! beginning of each row
integer, allocatable :: row(:) ! column positions
integer, allocatable :: dia(:) ! diagonal positions
integer :: n ! matrix dimension
integer :: nonzeros ! number of nonzero entries
real, allocatable :: val(:) ! value
integer, allocatable :: col(:) ! beginning of each row
integer, allocatable :: row(:) ! column positions
integer, allocatable :: dia(:) ! diagonal positions
end type Matrix

end module Matrix_Mod

0 comments on commit 22cc144

Please sign in to comment.