123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- !
- ! NAME
- ! num_matrix_full - defines full matrix
- !
- ! USAGE
- !
- ! use num_matrix_full
- !
- ! type(TFullMatrix) :: A
- !
- ! ! initialize matrix:
- ! ! o no memory allocated
- ! ! o zero flag
- ! call Init( A )
- !
- ! ! (re)allocate memory to store contents:
- ! call SetStorage( A, am, an )
- ! call ClearStorage( A )
- !
- ! ! zero flag ?
- ! if ( IsZero(A) ) ...
- !
- ! ! define full matrix:
- ! call SetFull( A, m, n )
- ! call SetFull( A, arr(:,:) )
- !
- ! ! done
- ! call Done( A )
- !
- module num_matrix_full
- implicit none
-
-
- ! --- in/out ------------------------------------
-
- private
-
- public :: TFullMatrix
- public :: Init, Done
- public :: SetStorage, ClearStorage
- public :: IsZero
- public :: SetFull
-
-
- ! --- const ----------------------------------
- character(len=*), parameter :: mname = 'module num_matrix_full'
- ! --- types ------------------------------------
-
- type TFullMatrix
- ! key name for error mesages
- character(len=20) :: key
- ! logical matrix dimension:
- integer :: m, n
- ! contents flags
- logical :: zero
- ! physical storage:
- real, pointer :: a(:,:)
- integer :: am, an
- integer :: knd
- end type TFullMatrix
-
-
- ! --- interfaces ------------------------------
-
- interface Init
- module procedure mat_Init
- end interface
-
- interface Done
- module procedure mat_Done
- end interface
-
- interface SetStorage
- module procedure mat_SetStorage
- end interface
-
- interface ClearStorage
- module procedure mat_ClearStorage
- end interface
-
- interface IsZero
- module procedure mat_IsZero
- end interface
-
- interface SetFull
- module procedure mat_SetFull_range
- module procedure mat_SetFull_array
- end interface
-
-
-
-
-
- contains
- ! ==============================================================
-
-
- subroutine mat_Init( mat, key )
-
- ! --- in/out ------------------------------
-
- type(TFullMatrix), intent(out) :: mat
- character(len=*), intent(in) :: key
-
- ! --- const ----------------------------------
-
- character(len=*), parameter :: name = mname//', mat_Init'
-
- ! --- begin -------------------------------
-
- ! store key
- mat%key = key
-
- ! dummy size:
- mat%m = 0
- mat%n = 0
-
- ! start with zero matrix:
- mat%zero = .true.
- ! initialize physical storage
- nullify( mat%a )
- mat%knd = kind(1.0)
-
- end subroutine mat_Init
-
-
- ! ***
-
-
- subroutine mat_Done( mat )
-
- ! --- in/out ------------------------------
-
- type(TFullMatrix), intent(inout) :: mat
-
- ! --- const ----------------------------------
-
- character(len=*), parameter :: name = mname//', mat_Done'
-
- ! --- begin -------------------------------
-
- ! clear memory ...
- call ClearStorage( mat )
-
- end subroutine mat_Done
- ! =========================================================
- subroutine mat_SetStorage( mat, am, an )
-
- ! --- in/out ------------------------------
-
- type(TFullMatrix), intent(inout) :: mat
- integer, intent(in) :: am, an
-
- ! --- const ----------------------------------
-
- character(len=*), parameter :: name = mname//', mat_SetStorage'
-
- ! --- local -------------------------------
-
- integer :: stat
-
- ! --- begin -------------------------------
-
- ! check ...
- if ( (am < 1) .or. (an < 1) ) then
- write (*,'("ERROR - strange storage definition:")')
- write (*,'("ERROR - storage : ",2i6)') am, an
- write (*,'("ERROR in ",a)') name; stop
- end if
-
- ! set maximum shape:
- mat%am = am
- mat%an = an
-
- ! allocate memory:
- if ( associated(mat%a) ) then
- if ( any( shape(mat%a) /= (/mat%am,mat%an/) ) ) then
- deallocate( mat%a, stat=stat )
- if ( stat /= 0 ) then
- write (*,'("ERROR - error during deallocation of matrix;")')
- write (*,'("ERROR - status : ",i6)') stat
- write (*,'("ERROR - matrix key : ",a)') mat%key
- write (*,'("ERROR in ",a)') name; stop
- end if
- end if
- end if
- if ( .not. associated(mat%a) ) then
- allocate( mat%a(mat%am,mat%an), stat=stat )
- if ( stat /= 0 ) then
- write (*,'("ERROR - error during allocation of matrix;")')
- write (*,'("ERROR - status : ",i6)') stat
- write (*,'("ERROR - matrix key : ",a)') mat%key
- write (*,'("ERROR in ",a)') name; stop
- end if
- end if
-
- end subroutine mat_SetStorage
-
-
- ! ***
-
-
- subroutine mat_ClearStorage( mat )
-
- ! --- in/out ------------------------------
-
- type(TFullMatrix), intent(inout) :: mat
-
- ! --- const ----------------------------------
-
- character(len=*), parameter :: name = mname//', mat_ClearStorage'
-
- ! --- local -------------------------------
-
- integer :: stat
-
- ! --- begin -------------------------------
-
- ! remove physical storage ...
- if ( associated(mat%a) ) then
- deallocate( mat%a, stat=stat )
- if ( stat /= 0 ) then
- write (*,'("ERROR - error during deallocation of matrix;")')
- write (*,'("ERROR - status : ",i6)') stat
- write (*,'("ERROR - matrix key : ",a)') mat%key
- write (*,'("ERROR in ",a)') name; stop
- end if
- nullify( mat%a )
- end if
- end subroutine mat_ClearStorage
-
-
- ! =========================================================
-
-
- logical function mat_IsZero( mat )
-
- ! --- in/out ------------------------------
-
- type(TFullMatrix), intent(in) :: mat
-
- ! --- begin -------------------------------
-
- mat_IsZero = mat%zero
-
- end function mat_IsZero
-
-
- ! =========================================================
- subroutine mat_SetFull_range( mat, m, n )
-
- ! --- in/out ------------------------------
-
- type(TFullMatrix), intent(inout) :: mat
- integer, intent(in) :: m, n
-
- ! --- const ----------------------------------
-
- character(len=*), parameter :: name = mname//', mat_SetFull_range'
-
- ! --- begin -------------------------------
-
- ! memory allocated ?
- if ( .not. associated(mat%a) ) then
- write (*,'("ERROR - no storage allocated")')
- write (*,'("ERROR in ",a)') name; stop
- end if
-
- ! check ...
- if ( (m < 1) .or. (n < 1) .or. &
- (m > mat%am) .or. (n > mat%an) ) then
- write (*,'("ERROR - strange full matrix definition:")')
- write (*,'("ERROR - matrix shape : ",2i6)') m, n
- write (*,'("ERROR - storage : ",2i6)') mat%am, mat%an
- write (*,'("ERROR in ",a)') name; stop
- end if
-
- ! logical shape of the matrix:
- mat%m = m
- mat%n = n
-
- end subroutine mat_SetFull_range
-
-
- ! ***
-
-
- subroutine mat_SetFull_array( mat, A, transA )
-
- ! --- in/out ------------------------------
-
- type(TFullMatrix), intent(inout) :: mat
- real, intent(in) :: A(:,:)
- character(len=1), intent(in) :: transA
-
- ! --- const ----------------------------------
-
- character(len=*), parameter :: name = mname//', mat_SetFull_array'
-
- ! --- local --------------------------------
-
- integer :: m, n
-
- ! --- begin -------------------------------
-
- ! extract shape
- m = size(A,1)
- n = size(A,2)
-
- if ( transA == 'N' ) then
-
- ! set shape etc, checks included:
- call SetFull( mat, m, n )
-
- ! fill contents
- mat%a(1:mat%m,1:mat%n) = A
-
- else if ( transA == 'T' ) then
-
- ! set shape etc, checks included:
- call SetFull( mat, n, m )
-
- ! fill contents
- mat%a(1:mat%m,1:mat%n) = transpose(A)
-
- else
-
- write (*,'("ERROR - unknown key for normal/transposed : ",a)') transA
- write (*,'("ERROR in ",a)') name; stop
- end if
-
- ! not zero anymore
- mat%zero = .false.
-
- end subroutine mat_SetFull_array
-
-
- end module num_matrix_full
|