summaryrefslogtreecommitdiff
path: root/src/cormad_ptr.c
blob: 670a71a0aff573f3c10705cafc8751df51568391 (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
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
#include "RSCdefines.h"
#include <math.h>
#include <stdio.h>
#ifdef _OPENMP
#include <omp.h>
#endif

#define SQRT2 (sqrt(2))
#define CONST 1.4826

void cormad(double *matrix, int n_row, int n_col, double *output,
            int evencorrect) {

  int k = n_row / 2; // position of the median
  int output_size = (n_col - 1) * n_col / 2;
  double med, mad;           // store variables for medians
  double U[n_row], V[n_row]; // help vectors

  /* Transform matrix columns (CORMAD part 1)
   * each column has n entries;
   * matrix is assumed to be streamed in onevector
   */
  int i = 0;
  for (int l = 0; l < n_col * n_row; l++) {
    if (i == (n_row - 1)) {
      U[i] = *matrix;
      med = quickselect_recursive(U, n_row, k);
      if (evencorrect == 1) { // handle even correction
        med = (med + quickselect_recursive(U, n_row, k - 1)) / 2;
      }
      for (int j = 0; j < n_row; j++) {
        U[j] = *(matrix - (n_row - 1) + j) - med;
        V[j] = fabs(U[j]);
      }
      med = quickselect_recursive(V, n_row, k);
      if (evencorrect == 1) { // handle even correction
        med = (med + quickselect_recursive(V, n_row, k - 1)) / 2;
      }
      for (int j = 0; j < n_row; j++) // reassign
        *(matrix - (n_row - 1) + j) = U[j] / (SQRT2 * CONST * med);
      // prepare next iter
      matrix++;
      i = 0;
    } else {
      U[i] = *matrix;
      // prepare next iter
      matrix++;
      i++;
    }
  }

  matrix = matrix - (n_col * n_row); /* reset pointer */

  /* Operate on columns pairs (CORMAD part 2) */
  double *matrix_2 = matrix; // used to point at second column
  int first_col = 0;         /* Running first column */
  int second_col = 0;        /* Running second column */
  for (int l = 0; l < output_size; l++) {
    if (second_col == n_col - 1) {
      first_col++;
      second_col = first_col;
      /* set pointers to columns */
      matrix += n_row;
      matrix_2 = matrix;
    }
    second_col++;
    matrix_2 += n_row;

    for (int i = 0; i < n_row; i++) { // auxiliary vectors from matrix
      U[i] = *(matrix + i) + *(matrix_2 + i);
      V[i] = -*(matrix + i) + *(matrix_2 + i);
    }
    mad = quickselect_recursive(U, n_row, k);
    med = quickselect_recursive(V, n_row, k);
    if (evencorrect == 1) {
      mad = (mad + quickselect_recursive(U, n_row, k - 1)) / 2;
      med = (med + quickselect_recursive(V, n_row, k - 1)) / 2;
    }
    for (int i = 0; i < n_row; i++) { // reassign for new medians
      U[i] = fabs(U[i] - mad);
      V[i] = fabs(V[i] - med);
    }
    mad = quickselect_recursive(U, n_row, k);
    med = quickselect_recursive(V, n_row, k);
    if (evencorrect == 1) {
      mad = (mad + quickselect_recursive(U, n_row, k - 1)) / 2;
      med = (med + quickselect_recursive(V, n_row, k - 1)) / 2;
    }
    mad = pow(CONST * mad, 2);
    med = pow(CONST * med, 2);

    // Assign output
    *output = (mad - med) / (mad + med);
    output++;
  }
}

#ifdef _OPENMP
void cormad_parallel(double *matrix, int n_row, int n_col, double *output,
                     int evencorrect, int num_threads) {
  int k = n_row / 2; // position of the median
  int output_size = (n_col - 1) * n_col / 2;
  double med, mad;           // store variables for medians
  double U[n_row], V[n_row]; // help vectors

  /* Transform matrix columns (CORMAD part 1) */
  double *help_matrix = matrix; /*  help pointer for matrix */
#pragma omp parallel for private(med, mad, U, V, help_matrix)                  \
    num_threads(num_threads)
  for (int j = 0; j < n_col; j++) {   // iterate on cols
    help_matrix = matrix + n_row * j; /* set pointer at beg of col */
    for (int i = 0; i < n_row; i++)
      U[i] = *(help_matrix + i);
    med = quickselect_recursive(U, n_row, k);
    if (evencorrect == 1) { // handle even correction
      med = (med + quickselect_recursive(U, n_row, k - 1)) / 2;
    }
    for (int i = 0; i < n_row; i++) {
      U[i] = *(help_matrix + i) - med;
      V[i] = fabs(U[i]);
    }
    med = quickselect_recursive(V, n_row, k);
    if (evencorrect == 1) { // handle even correction
      med = (med + quickselect_recursive(V, n_row, k - 1)) / 2;
    }
    for (int i = 0; i < n_row; i++) // reassign
      *(help_matrix + i) = U[i] / (SQRT2 * CONST * med);
  }

  // int l = 0; // used to iterate over output
  /* Operate on columns pairs (CORMAD part 2) */
  help_matrix = matrix; /* help pointers for matrix */
  double *help_matrix_2 = matrix;
#pragma omp parallel for num_threads(num_threads) private(                     \
    med, mad, U, V, help_matrix, help_matrix_2)
  for (int l = 0; l < output_size; l++) {
    /*  Detrmine columns pairs */
    int col1 = 0, col2 = 0;
    int copy_l = l + 1;
    for (int last_elem = n_col - 1; last_elem > 0; last_elem--) {
      copy_l -= last_elem;
      if (copy_l <= 0) {
        col2 = (n_col - 1) + copy_l;
        break;
      } else {
        col1++;
      }
    }
    /* Set pointers to columns */
    help_matrix = matrix + n_row * col1;   // col1
    help_matrix_2 = matrix + n_row * col2; // col2

    for (int i = 0; i < n_row; i++) { // auxiliary vectors from matrix
      U[i] = *(help_matrix + i) + *(help_matrix_2 + i);
      V[i] = -*(help_matrix + i) + *(help_matrix_2 + i);
    }
    mad = quickselect_recursive(U, n_row, k);
    med = quickselect_recursive(V, n_row, k);
    if (evencorrect == 1) {
      mad = (mad + quickselect_recursive(U, n_row, k - 1)) / 2;
      med = (med + quickselect_recursive(V, n_row, k - 1)) / 2;
    }
    for (int i = 0; i < n_row; i++) { // reassign for new medians
      U[i] = fabs(U[i] - mad);
      V[i] = fabs(V[i] - med);
    }
    mad = quickselect_recursive(U, n_row, k);
    med = quickselect_recursive(V, n_row, k);
    if (evencorrect == 1) {
      mad = (mad + quickselect_recursive(U, n_row, k - 1)) / 2;
      med = (med + quickselect_recursive(V, n_row, k - 1)) / 2;
    }
    mad = pow(CONST * mad, 2);
    med = pow(CONST * med, 2);

    // Assign output
    *(output + l) = (mad - med) / (mad + med);
  }
}
#endif