-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixUtilities.f90
275 lines (203 loc) · 8.39 KB
/
MatrixUtilities.f90
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
module MatrixUtilities
use Functions
public Determinant
interface Determinant
module procedure DeterminantScalar
module procedure DeterminantMatrix
end interface Determinant
public MatrixMinor
interface MatrixMinor
module procedure MatrixFirstMinor
end interface MatrixMinor
contains
subroutine ResizeMatrix(z,i,n)
!Matrix to resize
real,dimension(:,:),allocatable :: z
!Temporary matrix
real,dimension(:,:),allocatable :: tmp
!Index of dimension to resize
integer :: i
!Target size
integer :: n
!Only run if input matrix is allocated
if(allocated(z))then
call move_alloc(z,tmp)
if(i.eq.1)then
allocate(z(n,size(tmp,2)))
else if(i.eq.2)then
allocate(z(size(tmp,1),n))
endif
do j=1,size(z,1)
do k=1,size(z,2)
z(j,k)=tmp(j,k)
enddo
enddo
endif
end subroutine ResizeMatrix
function IdentityMatrix(pSize)result(I)
integer :: pSize
integer,dimension(pSize,pSize) :: I
do j=1,size(I,1)
do k=1,size(I,2)
I(j,k)=KroneckerDelta(j,k)
enddo
enddo
end function IdentityMatrix
function LUPDecomposition(A)result(LUP)
complex,dimension(1:,1:) :: A
complex,dimension(1:size(A,1),1:size(A,2)) :: L
complex,dimension(1:size(A,1),1:(2*size(A,2))) :: UP
complex,dimension(1:size(A,1),1:(3*size(A,2))) :: LUP
logical :: Solvable=.true.
! print *, A
!Check square matrix
if(size(A,1).ne.size(A,2))then
print *, "Module MatrixUtilities function LUPDecomposition: Not a square matrix!"
return
endif
!Populate the left half of the UP matrix with the input matrix and the right half with the pivot matrix
!Populate the L matrix with the identity matrix
do i=1,size(A,1)
do j=1,size(A,2)
UP(i,j)=A(i,j)
UP(i,j+size(A,2))=KroneckerDelta(i,j)
L(i,j)=KroneckerDelta(i,j)
enddo
enddo
!Main operations
do i=1,size(UP,1)
!Check if the current diagonal element is 0
if(UP(i,i).eq.(0.,0.))then
!Assume unsolvable until a non-zero element is found
Solvable=.false.
!Check for non-zero element in the same column
do j=i,size(UP,1)
if(UP(j,i).ne.(0.,0.))then
!Set solvable if found
Solvable=.true.
!Swap the rows
swappingRows: block
complex,dimension(size(UP,2)) :: tempUP
tempUP=UP(i,:)
UP(i,:)=UP(j,:)
UP(j,:)=tempUP
end block swappingRows
endif
enddo
!If no non-zero elements found then return not a square matrix
if(.not.Solvable)then
print *, "Module MatrixUtilities function LUPDecomposition: Not a square matrix!"
return
endif
endif
!Eliminate columns
do j=i+1,size(UP,1)
!Save the coefficient into the L matrix
L(j,i)=UP(j,i)/UP(i,i)
!Subtract from each row the current row times the coefficient
do k=1,size(A,2)
UP(j,k)=UP(j,k)-UP(i,k)*L(j,i)
! do mn=1,size(UP,1)
! print *, UP(mn,:)
! enddo
enddo
enddo
enddo
!Save the result into the LUP matrix
do i=1,size(LUP,1)
do j=1,size(L,2)
LUP(i,j)=L(i,j)
enddo
do j=size(L,2)+1,size(LUP,2)
LUP(i,j)=UP(i,j-size(L,2))
enddo
enddo
! print *, (LUP(:,i),NEW_LINE('1'),i=1,size(LUP,2))
end function LUPDecomposition
function DeterminantScalar(a)result(cd)
complex :: a
complex :: cd
cd=a
end function DeterminantScalar
function DeterminantMatrix(A)result(cD)
complex, dimension(1:,1:) :: A
complex :: cD,cDetU
integer :: iDetP
integer,dimension(size(A,1)) :: PIndices
complex, dimension(1:size(A,1),3*size(A,2)) :: LUP
cDetU=1.
!Get the LUP decomposition
LUP=LUPDecomposition(A)
!Get the determinant of the U matrix
do i=1,size(A,1)
! print *, LUP(i,i+size(A,2))
cDetU=cDetU*LUP(i,i+size(A,2))
! print *, cDetU
enddo
! print *, cDetU
!Get the indices of the unity elements in each column of the P matrix
do i=1,size(A,1)
do j=1,size(A,2)
if(LUP(i,j+2*size(A,2)).ne.(0.,0.))then
PIndices(i)=j
endif
enddo
enddo
! print *, PIndices
!Put into the Levi-Civita symbol for the determinant
iDetP=LeviCivita(PIndices)
! print *, iDetP
cD=cDetU/iDetP
end function DeterminantMatrix
function CharacteristicPolynomial(A,lambda)result(p)
complex,dimension(1:,1:) :: A
complex :: lambda
complex :: p
if(size(A,1).ne.size(A,2))then
print *, "Module MatrixUtilities function CharacteristicPolynomial: Not a square matrix!"
return
endif
! print *, lambda*IdentityMatrix(size(A,1))-A
p=Determinant(lambda*IdentityMatrix(size(A,1))-A)
end function CharacteristicPolynomial
function MatrixFirstMinor(A,i,j)result(m)
complex,dimension(1:,1:) :: A
!i is row index and j is column index
integer :: i,j
complex :: m
!Write the matrix with i row and j column removed here
complex,dimension(1:(size(A,1)-1),1:(size(A,2)-1)) :: modA
!First check if indices are within range
if(i.lt.1.or.j.lt.1)then
print *, "Module MatrixUtilities function MatrixFirstMinor in MatrixMinor: Indices must be greater than 0"
return
elseif(i.gt.size(A,1))then
print *, "Module MatrixUtilities function MatrixFirstMinor in MatrixMinor: Row index out of row range"
return
elseif(j.gt.size(A,2))then
print *, "Module MatrixUtilities function MatrixFirstMinor in MatrixMinor: Column index out of column range"
return
endif
! print *,(A(k,:),NEW_LINE('1'),k=1,size(A,1))
!Start populating the minor matrix
!First populate the upper left corner
modA(1:i-1,1:j-1)=A(1:i-1,1:j-1)
!Only populate the lower left corner if there are rows left
if(i.lt.size(A,1))then
modA(i:,1:j-1)=A(i+1:,:j-1)
endif
!Only populate the upper right corner if there are columns left
if(j.lt.size(A,2))then
modA(:i-1,j:)=A(:i-1,j+1:)
endif
!Only populate the lower right corner if there are elements left
if(i.lt.size(A,1).and.j.lt.size(A,2))then
modA(i:,j:)=A(i+1:,j+1:)
endif
! print *,(modA(k,:),NEW_LINE('1'),k=1,size(modA,1))
m=Determinant(modA)
! print *, m
end function MatrixFirstMinor
! function HouseholderTransformation(A,i)result(B)
! end function HouseholderTransformation
end module MatrixUtilities