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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
module selectionalgo
use, intrinsic :: iso_fortran_env
implicit none
real, parameter :: cost=1.4826, sqrt2=sqrt(2.)
integer, parameter :: sp = real32, dp=real64
interface qselect
procedure quickselectdp, quickselectscalardp
end interface qselect
interface iselect
procedure introselectdp, introselectscalardp
end interface iselect
contains
!!!!!!! DOUBLE PRECISION
recursive subroutine quickselectrecursivedp(invector,tovector,k,output)
real(kind=dp), dimension(:), allocatable, target, intent(inout) :: invector
real(kind=dp), intent(inout), pointer, contiguous :: tovector(:)
integer, intent(inout) :: k
real(kind=dp), intent(out) :: output
real(kind=dp) :: swapper, pvt
integer :: i, r, subst
real(kind=dp), pointer :: a,b,c
r=size(tovector)
select case(r)
case(1)
output=tovector(1)
return
case(2)
select case(k)
case(1)
output=minval(tovector)
case(2)
output=maxval(tovector)
end select
return
case(3)
select case(k)
case(1)
output=minval(tovector)
case(3)
output=maxval(tovector)
case(2)
i=r/2+1
output=sum(tovector)-minval(tovector)-maxval(tovector)
end select
return
case default
i=r/2+1
!pivoting section (pivot of 3)
a=>tovector(1)
b=>tovector(i)
c=>tovector(r)
pvt=a+b+c
swapper=max(a,b,c)
b=swapper
pvt=pvt-swapper
swapper=min(a,b,c)
a=swapper
pvt=pvt-swapper
c=pvt
!one pass section
subst=1
b=>tovector(subst)
do i=1,(r-1-3),3
a => tovector(i)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
a => tovector(i+1)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
a => tovector(i+2)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
end do
do i=i,(r-1)
a => tovector(i)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
end do
tovector(r)=tovector(subst)
tovector(subst)=pvt
!decide next step
select case(subst-k)
case(0)
output=tovector(k)
case(1:)
tovector => tovector(1:subst-1)
call quickselectrecursivedp(invector,tovector,k, output)
case(:-1)
k=k-subst
tovector => tovector(subst+1:)
call quickselectrecursivedp(invector,tovector,k, output)
end select
end select
end subroutine quickselectrecursivedp
recursive subroutine introselectrecursivedp(invector,tovector,k,output)
real(kind=dp), dimension(:), allocatable, target, intent(inout) :: invector
real(kind=dp), intent(inout), pointer, contiguous :: tovector(:)
integer, intent(inout) :: k
real(kind=dp), intent(out) :: output
real(kind=dp) :: swapper, pvt
integer, save :: switch
integer :: i, r, subst, r_min, switch_after
real(kind=dp), pointer :: a,b,c
r=ubound(tovector,1)
r_min = 3000 ! at least this bigger to switch to median of medians
switch_after = 5 ! at least fails this amount of time to reorder
! one third of the vector to consider med of med
select case(r)
case(1)
output=tovector(1)
switch=0
return
case(2)
select case(k)
case(1)
output=minval(tovector)
case(2)
output=maxval(tovector)
end select
switch=0
return
case(3)
select case(k)
case(1)
output=minval(tovector)
case(3)
output=maxval(tovector)
case(2)
i=r/2+1
output=sum(tovector)-minval(tovector)-maxval(tovector)
end select
switch=0
return
case default
i=r/2+1
!pivoting section (pivot of 3)
a=>tovector(1)
b=>tovector(i)
c=>tovector(r)
pvt=a+b+c
swapper=max(a,b,c)
b=swapper
pvt=pvt-swapper
swapper=min(a,b,c)
a=swapper
pvt=pvt-swapper
c=pvt
!one pass section
subst=1
b=>tovector(subst)
do i=1,(r-1-3),3
a => tovector(i)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
a => tovector(i+1)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
a => tovector(i+2)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
end do
do i=i,(r-1)
a => tovector(i)
if (a<pvt) then
swapper=b
b=a
a=swapper
subst=subst+1
b=>tovector(subst)
end if
end do
tovector(r)=tovector(subst)
tovector(subst)=pvt
!decide next step
select case(subst-k)
case(0)
output=tovector(k)
switch=0
case(1:)
tovector => tovector(1:subst-1)
if ((subst-1)>r*2/3) then
switch=switch+1
if (switch==switch_after .and. r>r_min) then
!reset switch and call medofmeddp
switch=0
call medofmeddp(invector,tovector,k, output)
else
call introselectrecursivedp(invector,tovector,k, output)
end if
else
switch=0
call introselectrecursivedp(invector,tovector,k, output)
end if
case(:-1)
k=k-subst
tovector => tovector(subst+1:)
if ((r-subst)>r*2/3) then
switch=switch+1
if (switch==switch_after .and. r>r_min) then
!reset switch and call medofmeddp
switch=0
call medofmeddp(invector,tovector,k, output)
else
call introselectrecursivedp(invector,tovector,k, output)
end if
else
switch=0
call introselectrecursivedp(invector,tovector,k, output)
end if
end select
end select
end subroutine introselectrecursivedp
recursive subroutine medofmeddp(invector,tovector,k,output)
real(kind=dp), dimension(:), allocatable, target, intent(inout) :: invector
real(kind=dp), intent(inout), pointer, contiguous :: tovector(:)
real(kind=dp), intent(out) :: output
integer, intent(inout) :: k
real(kind=dp), pointer, contiguous :: subsec(:)
real(kind=dp), dimension(:),allocatable :: meds
integer :: i,subst,r
real(kind=dp) :: pvt
!real(kind=dp) :: swapper
!real(kind=dp), pointer :: a,b
r=ubound(tovector,1)
select case(mod(r,5))
case(0)
allocate(meds(r/5))
do i=1,r-4,5
subsec=>tovector(i:i+4)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
subst=minloc(subsec(2:),1)+1
pvt=subsec(subst)
subsec(subst)=subsec(2)
subsec(2)=pvt
subst=minloc(subsec(3:),1)+2
pvt=subsec(subst)
subsec(subst)=subsec(3)
subsec(3)=pvt
meds(int(i/5.0)+1)=pvt
end do
case(1)
allocate(meds(r/5+1))
do i=1,r-4,5
subsec=>tovector(i:i+4)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
subst=minloc(subsec(2:),1)+1
pvt=subsec(subst)
subsec(subst)=subsec(2)
subsec(2)=pvt
subst=minloc(subsec(3:),1)+2
pvt=subsec(subst)
subsec(subst)=subsec(3)
subsec(3)=pvt
meds(int(i/5.0)+1)=pvt
end do
meds(r/5+1)=tovector(i)
case(2)
allocate(meds(r/5+1))
do i=1,r-4,5
subsec=>tovector(i:i+4)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
subst=minloc(subsec(2:),1)+1
pvt=subsec(subst)
subsec(subst)=subsec(2)
subsec(2)=pvt
subst=minloc(subsec(3:),1)+2
pvt=subsec(subst)
subsec(subst)=subsec(3)
subsec(3)=pvt
meds(int(i/5.0)+1)=pvt
end do
subsec=>tovector(i:)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
meds(r/5+1)=subsec(2)
case(3)
allocate(meds(r/5+1))
do i=1,r-4,5
subsec=>tovector(i:i+4)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
subst=minloc(subsec(2:),1)+1
pvt=subsec(subst)
subsec(subst)=subsec(2)
subsec(2)=pvt
subst=minloc(subsec(3:),1)+2
pvt=subsec(subst)
subsec(subst)=subsec(3)
subsec(3)=pvt
meds(int(i/5.0)+1)=pvt
end do
subsec=>tovector(i:)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
subst=minloc(subsec(2:),1)+1
pvt=subsec(subst)
subsec(subst)=subsec(2)
subsec(2)=pvt
meds(r/5+1)=pvt
case(4)
allocate(meds(r/5+1))
do i=1,r-4,5
subsec=>tovector(i:i+4)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
subst=minloc(subsec(2:),1)+1
pvt=subsec(subst)
subsec(subst)=subsec(2)
subsec(2)=pvt
subst=minloc(subsec(3:),1)+2
pvt=subsec(subst)
subsec(subst)=subsec(3)
subsec(3)=pvt
meds(int(i/5.0)+1)=pvt
end do
subsec=>tovector(i:)
subst=minloc(subsec,1)
pvt=subsec(subst)
subsec(subst)=subsec(1)
subsec(1)=pvt
subst=minloc(subsec(2:),1)+1
pvt=subsec(subst)
subsec(subst)=subsec(2)
subsec(2)=pvt
subst=minloc(subsec(3:),1)+2
pvt=subsec(subst)
subsec(subst)=subsec(3)
subsec(3)=pvt
meds(r/5+1)=pvt
end select
!set pivot
pvt=qselect(meds)
do i=1,r-3,3
if (tovector(i)==pvt) then
tovector(i)=tovector(r)
tovector(r)=pvt
exit
else if (tovector(i+1)==pvt) then
tovector(i+1)=tovector(r)
tovector(r)=pvt
exit
else if (tovector(i+2)==pvt) then
tovector(i+2)=tovector(r)
tovector(r)=pvt
exit
else
cycle
end if
end do
do i=i,r
if (tovector(i)==pvt) then
tovector(i)=tovector(r)
tovector(r)=pvt
exit
else
cycle
end if
end do
call introselectrecursivedp(invector,tovector,k, output)
end subroutine medofmeddp
function introselectdp(invector,ord,evencorrection)
real(kind=dp), intent(in), dimension(:) :: invector
logical, optional, intent(in) :: evencorrection
integer, intent(in), optional :: ord
real(kind=dp) :: introselectdp
real(kind=dp), dimension(:), allocatable, target :: vector
real(kind=dp), pointer, contiguous :: tovector(:) => null()
integer :: k1, k
k1=size(invector)
allocate(vector(k1))
vector=invector
tovector => vector
k1=ubound(invector,1)
if(present(ord)) then
k=ord
else
k=k1/2+1
end if
if(present(evencorrection)) then
if (k/=k1/2+1 .or. mod(k1,2)/=0) then
!write(*,'(a)') 'Warning: evencorrection argument ignored.'
k1=0
else
k1=0
if (evencorrection) k1=k-1
end if
else
k1=0
end if
call introselectrecursivedp(vector,tovector,k, introselectdp)
if (k1/=0) then
block
real(kind=dp) :: temp
temp=introselectdp
!tovector=>vector(1:k1+1)
tovector=>vector
call introselectrecursivedp(vector,tovector,k1, introselectdp)
introselectdp=(introselectdp+temp)/2.0
end block
end if
end function introselectdp
function quickselectdp(invector,ord,evencorrection)
real(kind=dp), intent(in), dimension(:) :: invector
logical, optional, intent(in) :: evencorrection
integer, intent(in), optional :: ord
real(kind=dp) :: quickselectdp
real(kind=dp), dimension(:), allocatable, target :: vector
real(kind=dp), pointer, contiguous :: tovector(:) => null()
integer :: k1, k
k1=size(invector)
allocate(vector(k1))
vector=invector
tovector => vector
k1=ubound(invector,1)
if(present(ord)) then
k=ord
else
k=k1/2+1
end if
if(present(evencorrection)) then
if (k/=k1/2+1 .or. mod(k1,2)/=0) then
!write(*,'(a)') 'Warning: evencorrection argument ignored.'
k1=0
else
k1=0
if (evencorrection) k1=k-1
end if
else
k1=0
end if
call quickselectrecursivedp(vector,tovector,k, quickselectdp)
if (k1/=0) then
block
real(kind=dp) :: temp
temp=quickselectdp
tovector=>vector(1:k1+1)
call quickselectrecursivedp(vector,tovector,k1, quickselectdp)
quickselectdp=(quickselectdp+temp)/2.0
end block
end if
end function quickselectdp
!deal with scalar input
function introselectscalardp(invector,ord, evencorrection)
real(kind=dp), intent(in) :: invector
integer, intent(in), optional :: ord
logical, optional, intent(in) :: evencorrection
real(kind=dp) :: introselectscalardp
if (present(evencorrection) .or. present(ord)) then
continue
end if
introselectscalardp=invector
end function introselectscalardp
function quickselectscalardp(invector,ord, evencorrection)
real(kind=dp), intent(in) :: invector
integer, intent(in), optional :: ord
logical, optional, intent(in) :: evencorrection
real(kind=dp) :: quickselectscalardp
if (present(evencorrection) .or. present(ord)) then
continue
end if
quickselectscalardp=invector
end function quickselectscalardp
end module selectionalgo
subroutine cormadvecdp(matrix,nrow,ncol,res,ressize,correcteven)
use selectionalgo
implicit none
!note kind dp is defined in selectionalgo
integer, intent(in) :: nrow, ncol,ressize, correcteven !.Fortran R function will not deal with deferred size arrays
real(kind=dp), dimension(nrow,ncol), intent(inout) :: matrix
real(kind=dp), dimension(ressize), intent(out) :: res
integer :: i, j, n, p
real(kind=dp) :: med,mad
!real(kind=dp) :: fresh
real(kind=dp), dimension(:), allocatable :: U, V
!real(kind=dp), dimension(:), allocatable :: A, B
p=ubound(matrix,2)
n=ubound(matrix,1)
allocate(U(n),V(n))
if (correcteven==1) then
do i=1,p-3,3
U=matrix(:,i)
med=iselect(U,evencorrection=.true.)
matrix(:,i)=(U-med)/(sqrt2*cost*iselect(abs(U-med),evencorrection=.true.))
U=matrix(:,i+1)
med=iselect(U,evencorrection=.true.)
matrix(:,i+1)=(U-med)/(sqrt2*cost*iselect(abs(U-med),evencorrection=.true.))
U=matrix(:,i+2)
med=iselect(U,evencorrection=.true.)
matrix(:,i+2)=(U-med)/(sqrt2*cost*iselect(abs(U-med),evencorrection=.true.))
end do
do i=i,p
U=matrix(:,i)
med=iselect(U,evencorrection=.true.)
matrix(:,i)=(U-med)/(sqrt2*cost*iselect(abs(U-med),evencorrection=.true.))
end do
!unrolled loops
n=1
do i=1,p-1
do j=i+1,p
U=matrix(:,i)+matrix(:,j)
V=-matrix(:,i)+matrix(:,j)
mad=(cost*iselect(abs(U-iselect(U,evencorrection=.true.)),evencorrection=.true.))**2
med=(cost*iselect(abs(V-iselect(V,evencorrection=.true.)),evencorrection=.true.))**2
res(n)=(mad-med)/(mad+med)
n=n+1
end do
end do
else
do i=1,p-3,3
U=matrix(:,i)
med=iselect(U)
matrix(:,i)=(U-med)/(sqrt2*cost*iselect(abs(U-med)))
U=matrix(:,i+1)
med=iselect(U)
matrix(:,i+1)=(U-med)/(sqrt2*cost*iselect(abs(U-med)))
U=matrix(:,i+2)
med=iselect(U)
matrix(:,i+2)=(U-med)/(sqrt2*cost*iselect(abs(U-med)))
end do
do i=i,p
U=matrix(:,i)
med=iselect(U)
matrix(:,i)=(U-med)/(sqrt2*cost*iselect(abs(U-med)))
end do
!unrolled loops
n=1
do i=1,p-1
do j=i+1,p
U=matrix(:,i)+matrix(:,j)
V=-matrix(:,i)+matrix(:,j)
mad=(cost*iselect(abs(U-iselect(U))))**2
med=(cost*iselect(abs(V-iselect(V))))**2
res(n)=(mad-med)/(mad+med)
n=n+1
end do
end do
end if
end subroutine cormadvecdp
|