-
Notifications
You must be signed in to change notification settings - Fork 0
/
linalg.F90
238 lines (192 loc) · 5.73 KB
/
linalg.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
module linalg
contains
subroutine chol(A, N, L)
!Calculate the cholesky decomposition (L) of matrix A that is NxN
implicit none
integer , intent(IN) :: N
real, dimension(N,N), intent(IN) :: A
real, dimension(N,N), intent(OUT) :: L
real, dimension(N,N) :: R
real, dimension(N) :: D
real :: small
integer :: i, j, k
small = 0.
!initialize L
D = 0.
L = 0.
R = 0.
!!$ L(1,1) = SQRT(A(1,1)+small)
!!$ !print *, 1,1, A(1,1)
!!$
!!$ !do decomposition
!!$ do i = 2,N
!!$ do j = 1,i-1
!!$ !do entries below diaganol
!!$ L(i,j) = A(i,j)
!!$ !print *, A(i,j)
!!$ do k = 1, j-1
!!$ L(i,j) = L(i,j) - L(i,k)*L(j,k)
!!$ end do
!!$ L(i,j) = L(i,j)/L(j,j)
!!$ end do
!!$ !after finishing below diaganol, take care of diaganol elements
!!$ L(i,i) = A(i,i)+small
!!$ do k = 1, i-1
!!$ L(i,i) = L(i,i) - L(i,k)**2
!!$ end do
!!$ !print *, i,i, L(i,i)
!!$ L(i,i) = SQRT(L(i,i))
!!$
!!$ end do
!!!!!LDL decomposition !!!!!!!!!
!D(1) = A(1,1)
do i = 1,N
do j = 1,i
if (D(j) == 0.) then
D(j) = A(j,j)+small
do k = 1, j-1
D(j) = D(j) - D(k)*R(j,k)**2
end do
end if
R(i,j) = A(i,j)
do k = 1, j-1
R(i,j) = R(i,j) - R(i,k)*R(j,k)*D(k)
end do
R(i,j) = R(i,j)/D(j)
end do
end do
do i = 1, N
do j = 1,i
L(i,j) = R(i,j)*SQRT(D(j))
end do
end do
!stop
end subroutine chol
subroutine LSTSQ(M, N, A, x, b)
!compute the least squares solution to the overdetermined system (ONLY!) Ax=b
!using the QR factorization of A
!done using Householder reflections
implicit none
integer, intent(IN) :: M, N
real, dimension(M,N), intent(INOUT) :: A
real, dimension(M) , intent(INOUT) :: b
real, dimension(N) , intent(INOUT) :: x
real, dimension(M) :: QTb
real, dimension(M,1) :: v
real, dimension(N,N) :: R
integer :: i, j
real :: v2, gam
R = 0.
QTb = 0.
V = 0.
!we're going to compute the relevant items from the QR factorization
!R is going to be stored in A
!we are also going to calculate Q^T*b for the least squares solve
do i = 1, N
v = 0.
v(i:M,1) = A(i:M,i)
!vi = xi + sign(xi)*x^2
v(i,1 ) = A(i,i) + SIGN( SQRT(dot_product( A(i:M,i),A(i:M,i) )), A(i,i) )
v2 = SQRT(dot_product(v(i:M,1),v(i:M,1)))
v(:,1) = v(:,1)/v2
A(i:M,i:N) = A(i:M,i:N) - 2.*MATMUL( v(i:M,:), MATMUL( TRANSPOSE(v(i:M,:)), A(i:M,i:N) ) )
gam = -2.*dot_product(v(i:M,1), b(i:M))
b(i:M) = b(i:M) + gam*v(i:M,1)
end do
!now we just to do a back-substitution to solve Rx=Q^T*b
do i = N, 1, -1
x(i) = b(i)
do j = i+1, N
x(i) = x(i) - A(i,j)*x(j)
end do
x(i) = x(i)/A(i,i)
end do
end subroutine LSTSQ
subroutine SVD(A, U, S, Vt, N)
!gives the singular value decomposition of A (NxN) = U.S.V*
!uses the DGESVD subroutine in LAPACK
!on mac this is included in the accelerate framework (LDFLAG -framework accelerate)
implicit none
integer, intent(IN) :: N
real, dimension(N,N), intent(IN) :: A
real, dimension(N,N), intent(INOUT) :: U, Vt
real, dimension(N), intent(INOUT) :: S
integer :: info, lwork
!integer ( kind = 4 ), parameter :: lwork
real ( kind = 8 ) work(3*N**2)
lwork = 3*N**2
call DGESVD('A', 'A', N, N, A, N, S, U, N, Vt, N, work, lwork, info)
print *, info
!print *, work
return
end subroutine SVD
subroutine multUSV(U, S, V, C, N)
implicit none
integer, intent(IN) :: N
real, dimension(N,N), intent(IN) :: U,V
real, dimension(N,N), intent(INOUT) :: C
real, dimension(N), intent(IN) :: s
real, dimension(N,N) :: D, E
integer :: i, j
D = 0.
do i = 1,N
D(i,i) = S(i)
end do
do i = 1,N
do j = 1,N
E(i,j) = dot_product(U(i,:), D(:,j))
end do
end do
do i = 1,N
do j = 1,N
C(i,j) = dot_product(E(i,:), V(i,:))
end do
end do
return
end subroutine multUSV
subroutine solve_Axb(A, x, b, L, N)
!solve matrix equation Ax = b for vector x, A is NxN given the cholesky decomposition L of A
implicit none
integer, intent(IN) :: N
real, dimension(N ), intent(IN) :: b
real, dimension(N,N), intent(IN) :: A, L
real, dimension(N) , intent(OUT) :: x
real, dimension(N) :: y
integer :: i, j
!initialize
x = 0.
y = 0.
!first we need to solve Ly=b using forward sub
y(1) = b(1)/L(1,1)
do i = 2,N
y(i) = b(i)
do j = 1, i-1
y(i) = y(i) - L(i,j)*y(j)
end do
y(i) = y(i)/L(i,i)
end do
!now solve L*x = y
x(N) = y(N)/L(N,N)
do i = N-1, 1, -1
x(i) = y(i)
do j =i+1, N
x(i) = x(i) - L(j,i)*x(j)
end do
x(i) = x(i)/L(i,i)
end do
end subroutine solve_Axb
subroutine solve_CZT(C, Z, T, L, N)
!subroutine to solve the matrix equation C.Z* = T* for the matrix Z
!note that the matrix equation is for the transpose of Z and T
!C shoul be NxN, while Z and T are 2xN
implicit none
integer, intent(IN ) :: N
real , dimension(N,N), intent(IN ) :: C, L
real , dimension(2,N), intent(IN ) :: T
real, dimension(2,N), intent(OUT) :: Z
!solve for the first column of Z*
call solve_Axb(C, Z(1, 1:N), T(1, 1:N), L, N)
!solve fore the second column of Z*
call solve_Axb(C, Z(2, 1:N), T(2, 1:N), L, N)
end subroutine solve_CZT
end module linalg