CosmicFish  Reference documentation for version 1.0
Looking into future Cosmology
001_Matrix_utilities.f90
Go to the documentation of this file.
1 !----------------------------------------------------------------------------------------
2 !
3 ! This file is part of CosmicFish.
4 !
5 ! Copyright (C) 2015-2016 by the CosmicFish authors
6 !
7 ! The CosmicFish code is free software;
8 ! You can use it, redistribute it, and/or modify it under the terms
9 ! of the GNU General Public License as published by the Free Software Foundation;
10 ! either version 3 of the License, or (at your option) any later version.
11 ! The full text of the license can be found in the file LICENSE at
12 ! the top level of the CosmicFish distribution.
13 !
14 !----------------------------------------------------------------------------------------
15 
20 
21 !----------------------------------------------------------------------------------------
23 
25 
27 
28  use precision
29 
30  implicit none
31 
32  private
33 
35 
36 contains
37 
38  ! ---------------------------------------------------------------------------------------------
41  subroutine sym_matrix_inversion(A, error_code)
42 
43  implicit none
44 
45  real(dl), dimension(:,:), intent(inout) :: A
46  integer , intent(out) :: error_code
49 
50  real(dl), dimension(size(A,1)) :: work ! work array for LAPACK
51  integer , dimension(size(A,1)) :: ipiv ! pivot indices
52  integer :: n, info
53 
54  ! External procedures defined in LAPACK
55  external dgetrf
56  external dgetri
57 
58  error_code = 0
59  n = size(a,1)
60 
61  ! dgetrf computes an LU factorization of a general M-by-N matrix A
62  ! using partial pivoting with row interchanges.
63  call dgetrf(n, n, a, n, ipiv, info)
64 
65  if (info /= 0) then
66  error_code = 1
67  write(*,*) 'Matrix is numerically singular!'
68  return
69  end if
70 
71  ! dgetri computes the inverse of a matrix using the LU factorization
72  ! computed by dgetrf.
73  call dgetri(n, a, n, ipiv, work, n, info)
74 
75  if (info /= 0) then
76  error_code = 2
77  write(*,*) 'Matrix inversion failed!'
78  return
79  end if
80 
81  end subroutine sym_matrix_inversion
82 
83  ! ---------------------------------------------------------------------------------------------
85  subroutine matrix_determinant(A, det, error_code)
86 
87  implicit none
88 
89  real(dl), dimension(:,:), intent(in) :: A
90  real(dl), intent(out) :: det
91  integer , intent(out) :: error_code
92 
93  integer :: N, i, info, AllocateStatus
94  real(dl) :: sgn
95  integer , dimension(:), allocatable :: ipiv ! pivot indices
96  ! External procedures defined in LAPACK
97  external dgetrf
98 
99  n = size(a,1)
100  error_code = 0
101 
102  allocate( ipiv(n), stat = allocatestatus )
103  if (allocatestatus /= 0) stop "matrix_determinant: allocation failed. Not enough memory."
104 
105  ipiv=0
106 
107  ! dgetrf computes an LU factorization of a general M-by-N matrix A
108  ! using partial pivoting with row interchanges.
109  call dgetrf(n, n, a, n, ipiv, info)
110 
111  if (info /= 0) then
112  error_code = 1
113  write(*,*) 'Matrix is numerically singular!'
114  stop
115  end if
116 
117  det = 1._dl
118 
119  do i=1, n
120  det = det*a(i,i)
121  end do
122 
123  sgn = 1._dl
124 
125  do i=1, n
126  if(ipiv(i)/=i)then
127  sgn=-sgn
128  end if
129  end do
130 
131  det=sgn*det
132 
133  return
134 
135  end subroutine matrix_determinant
136 
137  ! ---------------------------------------------------------------------------------------------
139  subroutine matrix_eigenvalues_eigenvectors_sym(A, eigenvalues, eigenvectors, error_code)
141  implicit none
142 
143  real(dl), dimension(:,:), intent(in) :: A
144  real(dl), dimension(:) , intent(out) :: eigenvalues
145  real(dl), dimension(:,:), intent(out) :: eigenvectors
146  integer , intent(out) :: error_code
147 
148  eigenvectors = 0._dl
149  eigenvalues = 0._dl
150  error_code = 0
151 
152  stop 'matrix_eigenvalues_eigenvectors_sym not yet implemented.'
153 
155 
156  ! ---------------------------------------------------------------------------------------------
157 
158 end module matrix_utilities
subroutine, public sym_matrix_inversion(A, error_code)
This subroutine computes the inverse of a square symmetric matrix by means of LAPACK LU decomposition...
subroutine, public matrix_determinant(A, det, error_code)
This subroutine computes the determinant of a matrix.
subroutine, public matrix_eigenvalues_eigenvectors_sym(A, eigenvalues, eigenvectors, error_code)
This subroutine computes the eigenvalues and eigenvectors of a matrix.
This module contains the subroutine and functions to manipulate and modify matrices.