-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmo_select_distant_entries.f90
319 lines (269 loc) · 13.1 KB
/
mo_select_distant_entries.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
!> \file mo_select_distant_entries.f90
!> \brief Selects elements of distance matrix with most largest distance to each other
!> \details Selects from a distance matrix (symmetric and positive) a specified
!> number (N) of elements which have large distances to each other.
!> \authors Juliane Mai
!> \date November 2014
module mo_select_distant_entries
! Written November 2014, Juliane Mai
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2014 Juliane Mai
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
use mo_kind, only: i4, sp, dp
use mo_orderpack, only: sort
use mo_xor4096, only: get_timeseed, n_save_state, xor4096
use mo_xor4096_apps, only: xor4096_range
implicit none
public :: select_distant_entries ! selects elements of distance matrix with most largest distance to each other
! ------------------------------------------------------------------
! NAME
! select_distant_entries
! PURPOSE
! Selects from a distance matrix (symmetric and positive) a specified
! number (N) of elements which have large distances to each other.
!
! Indexes are chosen iteratively. Therefore, it might be possible to
! find selections which have an overall larger distance. This is only
! a good GUESS of such a selection.
!
! There are two ways implemented how the first pair is chosen.
! (1) Default
! The first pair is the one with the overall largest distance,
! i.e. largest entry in distance matrix
! Therfore, the selection is deterministic.
! (2) The first pair is randomly chosen.
! Therefore, the returened selection is random.
! CALLING SEQUENCE
! indexes = select_distant_entries( distance_matrix, N, first=0 )
! INTENT(IN)
! real(sp/dp) :: distance_matrix 2D array of distances between element i and j
! integer(i4) :: n_elements number of elements to return
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! integer(i4) :: first method how first pair is chosen
! 0 : first pair is the one with the overall
! largest distance,
! i.e. result is deterministic
! 1 : first pair is randomly chosen,
! i.e. result is random
! DEFAULT: 0
! integer(i4) :: seed seed used for random number generator
! (only considered if first=1)
! DEFAULT: timeseed
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
! integer(i4) :: select_distant_entries(n_elements) indexes of elements with largest distance to each other
! RESTRICTIONS
! There is no initial mask of distance matrix allowed.
! EXAMPLE
! -> see also example in test directory
!
! LITERATURE
! None
!
! HISTORY
! Written, Juliane Mai, Nov 2014
interface select_distant_entries
module procedure select_distant_entries_dp, select_distant_entries_sp
end interface select_distant_entries
private
contains
function select_distant_entries_dp( distance_matrix, n_elements, first, seed )
implicit none
real(dp), dimension(:,:), intent(in) :: distance_matrix ! square matrix with distance between element i and j
integer(i4), intent(in) :: n_elements ! number of elements to return
integer(i4), optional, intent(in) :: first ! method how first pair is chosen
! ! 0 : first pair is the one with the overall
! ! largest distance,
! ! i.e. result is deterministic
! ! 1 : first pair is randomly chosen,
! ! i.e. result is random
! ! DEFAULT: 0
integer(i4), optional, intent(in) :: seed ! seed used for random number generator
! ! (only considered if first=1)
! ! DEFAULT: timeseed
integer(i4), dimension(n_elements) :: select_distant_entries_dp ! indexes of elements with largest distance to each other
! local variables
integer(i4) :: ii, jj, kk
integer(i4) :: ifirst ! method how first pair is chosen
integer(i4) :: iseed ! seed for random number generator
integer(i4), dimension(n_save_state) :: save_state
integer(i4) :: n_total ! number of elements --> number of rows/cols of distance_matrix
integer(i4) :: maxpos(2)
logical, dimension(:,:), allocatable :: mask
! handling of optionals
if ( present(first) ) then
ifirst = first
else
ifirst = 0
end if
! handling of optionals
if ( present(seed) ) then
iseed = seed
else
call get_timeseed(iseed)
end if
n_total = size(distance_matrix,1)
! initialization
select_distant_entries_dp(:) = 0
if (ifirst .eq. 0) then
! first pair is the one with overall maximal distance
maxpos = maxloc(distance_matrix)
select_distant_entries_dp(1) = maxpos(1)
select_distant_entries_dp(2) = maxpos(2)
else
! first pair is chosen randomly
! (a) initialize random stream
call xor4096(iseed, maxpos(1), save_state=save_state)
iseed = 0
! (b) generate first index
call xor4096_range((/1_i4, n_total/), maxpos(1), save_state=save_state)
! (c) generate second index
call xor4096_range((/1_i4, n_total/), maxpos(2), save_state=save_state)
do while (maxpos(1) .eq. maxpos(2))
call xor4096_range((/1_i4, n_total/), maxpos(2), save_state=save_state)
end do
select_distant_entries_dp(1) = maxpos(1)
select_distant_entries_dp(2) = maxpos(2)
end if
do ii = 3, n_elements
allocate( mask(n_total,n_total) )
mask = .false.
! select rows and columns
do jj = 1, ii-1
! select row
mask(select_distant_entries_dp(jj),:) = .true.
! select column
mask(:,select_distant_entries_dp(jj)) = .true.
! de-select diagonal elements and already chosen pairs
do kk = 1, jj
mask(select_distant_entries_dp(jj),select_distant_entries_dp(kk)) = .false.
mask(select_distant_entries_dp(kk),select_distant_entries_dp(jj)) = .false.
end do
end do
! find largest distance of selected entries
maxpos = maxloc(distance_matrix, mask=mask)
! either row or column index is already part of the list
! --> add the other one
if ( count(select_distant_entries_dp .eq. maxpos(1)) .eq. 0 ) then
select_distant_entries_dp(ii) = maxpos(1)
else
select_distant_entries_dp(ii) = maxpos(2)
end if
deallocate( mask )
end do
call sort(select_distant_entries_dp)
end function select_distant_entries_dp
function select_distant_entries_sp( distance_matrix, n_elements, first, seed )
implicit none
real(sp), dimension(:,:), intent(in) :: distance_matrix ! square matrix with distance between element i and j
integer(i4), intent(in) :: n_elements ! number of elements to return
integer(i4), optional, intent(in) :: first ! method how first pair is chosen
! ! 0 : first pair is the one with the overall
! ! largest distance,
! ! i.e. result is deterministic
! ! 1 : first pair is randomly chosen,
! ! i.e. result is random
! ! DEFAULT: 0
integer(i4), optional, intent(in) :: seed ! seed used for random number generator
! ! (only considered if first=1)
! ! DEFAULT: timeseed
integer(i4), dimension(n_elements) :: select_distant_entries_sp ! indexes of elements with largest distance to each other
! local variables
integer(i4) :: ii, jj, kk
integer(i4) :: ifirst ! method how first pair is chosen
integer(i4) :: iseed ! seed for random number generator
integer(i4), dimension(n_save_state) :: save_state
integer(i4) :: n_total ! number of elements --> number of rows/cols of distance_matrix
integer(i4) :: maxpos(2)
logical, dimension(:,:), allocatable :: mask
! handling of optionals
if ( present(first) ) then
ifirst = first
else
ifirst = 0
end if
! handling of optionals
if ( present(seed) ) then
iseed = seed
else
call get_timeseed(iseed)
end if
n_total = size(distance_matrix,1)
! initialization
select_distant_entries_sp(:) = 0
if (ifirst .eq. 0) then
! first pair is the one with overall maximal distance
maxpos = maxloc(distance_matrix)
select_distant_entries_sp(1) = maxpos(1)
select_distant_entries_sp(2) = maxpos(2)
else
! first pair is chosen randomly
! (a) initialize random stream
call xor4096(iseed, maxpos(1), save_state=save_state)
iseed = 0
! (b) generate first index
call xor4096_range((/1_i4, n_total/), maxpos(1), save_state=save_state)
! (c) generate second index
call xor4096_range((/1_i4, n_total/), maxpos(2), save_state=save_state)
do while (maxpos(1) .eq. maxpos(2))
call xor4096_range((/1_i4, n_total/), maxpos(2), save_state=save_state)
end do
select_distant_entries_sp(1) = maxpos(1)
select_distant_entries_sp(2) = maxpos(2)
end if
do ii = 3, n_elements
allocate( mask(n_total,n_total) )
mask = .false.
! select rows and columns
do jj = 1, ii-1
! select row
mask(select_distant_entries_sp(jj),:) = .true.
! select column
mask(:,select_distant_entries_sp(jj)) = .true.
! de-select diagonal elements and already chosen pairs
do kk = 1, jj
mask(select_distant_entries_sp(jj),select_distant_entries_sp(kk)) = .false.
mask(select_distant_entries_sp(kk),select_distant_entries_sp(jj)) = .false.
end do
end do
! find largest distance of selected entries
maxpos = maxloc(distance_matrix, mask=mask)
! either row or column index is already part of the list
! --> add the other one
if ( count(select_distant_entries_sp .eq. maxpos(1)) .eq. 0 ) then
select_distant_entries_sp(ii) = maxpos(1)
else
select_distant_entries_sp(ii) = maxpos(2)
end if
deallocate( mask )
end do
call sort(select_distant_entries_sp)
end function select_distant_entries_sp
end module mo_select_distant_entries