summaryrefslogtreecommitdiff
path: root/src/cormaddp.f90
blob: f9a0f69c929fa861eb03d67b4d71b432bc1934e2 (plain)
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
   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, fresh
      real(kind=dp), dimension(:), allocatable :: U, V, 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