-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunify_eigvec.f90
242 lines (197 loc) · 7.06 KB
/
unify_eigvec.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
module m_unique_eigvec
interface linear_independency
module procedure linear_independency_cmplx, linear_independency_real
end interface linear_independency
contains
subroutine unique_eigvec(eigval, z, ierr)
use omp_lib
implicit none
real(kind=8), intent(in) :: eigval(:)
real(kind=8) :: z(:,:)
integer, intent(out) :: ierr
integer :: beg_group, end_group, n_g
integer, allocatable :: groups(:)
groups = make_groups(eigval)
do n_g = 1, size(groups)
beg_group = sum(groups(1:n_g-1)) + 1
end_group = sum(groups(1:n_g))
if(end_group <= size(z,2))then
call unify_group_operator(beg_group, end_group, z, ierr)
endif
enddo
end subroutine unique_eigvec
subroutine unify_group_operator(beg_group, end_group, z, ierr)
implicit none
integer, intent(in) :: beg_group, end_group
real(kind=8), intent(inout) :: z(:,:)
integer, intent(out) :: ierr
real(kind=8), allocatable :: mtx(:,:), eigval(:), work(:), rq_mat(:,:), tau(:), Q(:,:), tmp(:,:)
integer :: dim, lwork, info, i, j
real(kind=8) :: work_size(1)
real(kind=8), parameter :: zero = 0.0, one = 1.0
dim = end_group - beg_group + 1
allocate(rq_mat(dim, dim), tau(dim),Q(dim,dim))
call make_rq_mat(z(:,beg_group:end_group), rq_mat, ierr)
if(ierr /= 0) return
rq_mat = transpose(rq_mat)
call dgeqrfp(dim, dim, rq_mat, dim, tau, work_size, -1, info)
if(info /= 0) then
write (*,*) "Problem setting up dgeqrfp"
ierr = 2
return
endif
lwork = int(work_size(1))
allocate(work(lwork))
call dgeqrfp(dim, dim, rq_mat, dim, tau, work, lwork, info)
if(info /= 0) then
write (*,*) "Problem executing dgeqrfp"
ierr = 4
return
endif
deallocate(work)
q = rq_mat
do i =1,3
do j=i,dim
q(i,j) = 0.0
enddo
enddo
call dorgqr(dim, dim, dim, Q, dim, tau, work_size, -1, info)
if(info /= 0) then
write (*,*) "Problem setting up dorgqr"
ierr = 8
return
endif
lwork = int(work_size(1))
allocate(work(lwork))
call dorgqr(dim, dim, dim, Q, dim, tau, work, lwork, info)
if(info /= 0) then
write (*,*) "Problem executing dorgqr"
ierr = 16
return
endif
tmp = z(:,beg_group:end_group)
! z(:,beg_group:end_group) = matmul(z(:,beg_group:end_group), q)
call dgemm("N", "N", size(z,1), dim, dim, one, tmp, size(z,1), q, dim, zero, z(:,beg_group:end_group), size(z,1))
end subroutine unify_group_operator
function make_groups(eigval) result(groups)
implicit none
real(8), intent(in) :: eigval(:)
integer, allocatable :: groups(:)
integer :: color(size(eigval))
integer :: i, beg, g_cnt, n_groups
g_cnt = 1
beg = 1
do while (beg <= size(eigval))
i = beg
do while (abs(eigval(i) - eigval(beg)) < 1e-8 )
color(i) = g_cnt
i = i + 1
if(i > size(eigval)) exit
enddo
g_cnt = g_cnt + 1
beg = i
enddo
n_groups = color(size(eigval))
allocate(groups(n_groups))
do i = 1,n_groups
groups(i) = count(i == color)
enddo
end function make_groups
subroutine make_rq_mat(eigvecs, rq_mat, ierr)
implicit none
real(kind=8), intent(in) :: eigvecs(:,:)
real(kind=8), intent(inout), allocatable :: rq_mat(:,:)
integer, intent(out) :: ierr
integer :: n, i, j, best_j
logical :: l_full_search
real(kind=8) :: cutoff, lindep, best_lindep
real(kind=8), allocatable :: tmp(:,:)
n = size(eigvecs,2)
cutoff = 1e-6 * sqrt(1.0/size(eigvecs,1))
if(allocated(rq_mat)) deallocate(rq_mat)
allocate(rq_mat(n,n), tmp(n,n))
rq_mat = 0.0
lindep = 0.0
j = 0
do i = 1,n
if(allocated(tmp)) deallocate(tmp)
allocate(tmp(i,n))
tmp = 0.0
lindep = 0.0
l_full_search = .False.
best_j = 0
best_lindep = 0.0
do while(lindep < cutoff)
j = j + 1
if(j > size(eigvecs,1)) then
rq_mat(i,:) = eigvecs(best_j,:)
if(l_full_search) then
exit
else
j = 1
l_full_search = .True.
endif
endif
rq_mat(i,:) = eigvecs(j,:)
tmp = rq_mat(1:i,:)
lindep = linear_independency(tmp)
if(lindep > best_lindep) then
best_j = j
best_lindep = lindep
endif
enddo
enddo
if(lindep < 1e-9) then
write (*,*) "RQ matrix seems linear dependent", lindep
ierr = 1
else
ierr = 0
endif
end subroutine make_rq_mat
function linear_independency_real(mat)
implicit none
real(kind=8), intent(inout) :: mat(:,:)
real(kind=8) :: linear_independency_real
integer :: info, lwork, iwork(8*minval(shape(mat))), ldmat, m, n
integer, parameter :: ldu = 1, ldvt = 1
real(kind=8) :: s(minval(shape(mat))), u(1,1), vt(1,1), work_size(1)
real(kind=8), allocatable :: work(:)
ldmat = size(mat, 1)
m = size(mat,1)
n = size(mat,2)
!call dgesdd(jobz,m,n, a, lda, s, u, ldu, vt, ldvt, work,lwork, iwork, info)
call dgesdd("N", m, n, mat, ldmat, s, u, ldu, vt, ldvt, work_size, -1, iwork, info)
lwork = int(work_size(1))
allocate(work(lwork))
call dgesdd("N", m, n, mat, ldmat, s, u, ldu, vt, ldvt, work, lwork, iwork, info)
linear_independency_real = s(size(s))
end function linear_independency_real
function linear_independency_cmplx(mat)
implicit none
complex(kind=8), intent(inout) :: mat(:,:)
real(kind=8) :: linear_independency_cmplx
integer :: info, lwork, iwork(8*minval(shape(mat))), ldmat, m, n
integer, parameter :: ldu = 1, ldvt = 1
real(kind=8) :: s(minval(shape(mat))), rwork(7*minval(shape(mat)))
complex(kind=8) :: u(1,1), vt(1,1), work_size(1)
complex(kind=8), allocatable :: work(:)
ldmat = size(mat, 1)
m = size(mat,1)
n = size(mat,2)
!call zgesdd(jobz, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, rwork, iwork, info )
call zgesdd("N", m, n, mat, ldmat, s, u, ldu, vt, ldvt, work_size, -1, rwork, iwork, info)
lwork = int(work_size(1))
allocate(work(lwork))
call zgesdd("N", m, n, mat, ldmat, s, u, ldu, vt, ldvt, work, lwork, rwork, iwork, info)
linear_independency_cmplx = s(size(s))
end function linear_independency_cmplx
subroutine print_mtx(mtx)
implicit none
real(kind=8), intent(in) :: mtx(:,:)
integer ::i
do i = 1, size(mtx,1)
write (*,*) mtx(i,:)
enddo
write (*,*) "####"
end subroutine print_mtx
end module m_unique_eigvec