1
+
1
2
#include " 00_main.h"
2
3
3
4
// Method of alternating projections (Halperin)
@@ -11,9 +12,8 @@ void center_variables_(Mat<double> &V, const Col<double> &w, const list &klist,
11
12
const double inv_sw = 1.0 / accu (w);
12
13
13
14
// Auxiliary variables (storage)
14
- size_t iter, j, k, p, J, interrupt_iter = 1000 ;
15
+ size_t iter, j, k, J, interrupt_iter = 1000 ;
15
16
double meanj, ratio;
16
- Col<double > x (N), x0 (N);
17
17
18
18
// Precompute group indices and weights
19
19
field<field<uvec>> group_indices (K);
@@ -30,16 +30,19 @@ void center_variables_(Mat<double> &V, const Col<double> &w, const list &klist,
30
30
}
31
31
}
32
32
33
- // Halperin projections
34
- // #ifdef _OPENMP
35
- // #pragma omp parallel for schedule(dynamic) private(x, x0, iter, k, j,
36
- // meanj, J, ratio) #endif
37
- for (p = 0 ; p < P; ++p) {
33
+ // Halperin projections
34
+ #ifdef _OPENMP
35
+ #pragma omp parallel for schedule(dynamic) private( \
36
+ iter, k, j, J, meanj, ratio) shared (V, w, group_indices, group_weights)
37
+ #endif
38
+ for (size_t p = 0 ; p < P; ++p) {
38
39
// Center each variable
39
- x = V.col (p);
40
+ Col<double > x = V.col (p);
41
+ Col<double > x0 (N);
40
42
41
43
for (iter = 0 ; iter < I; ++iter) {
42
44
if (iter == interrupt_iter) {
45
+ #pragma omp critical
43
46
check_user_interrupt ();
44
47
interrupt_iter += 1000 ;
45
48
}
@@ -49,7 +52,7 @@ void center_variables_(Mat<double> &V, const Col<double> &w, const list &klist,
49
52
50
53
// Alternate between categories
51
54
for (k = 0 ; k < K; ++k) {
52
- // Substract the weighted group means of category 'k'
55
+ // Subtract the weighted group means of category 'k'
53
56
J = group_indices (k).size ();
54
57
if (J == 0 )
55
58
continue ; // Skip empty groups
@@ -64,15 +67,15 @@ void center_variables_(Mat<double> &V, const Col<double> &w, const list &klist,
64
67
65
68
// Break loop if convergence is reached
66
69
ratio = accu (abs (x - x0) / (1.0 + abs (x0)) % w) * inv_sw;
67
- // ratio = norm(x - x0, 2) * inv_sw;
68
70
if (ratio < tol)
69
71
break ;
70
72
}
71
73
V.col (p) = x;
72
74
}
73
75
}
74
76
75
- [[cpp11::register ]] doubles_matrix<> center_variables_r_ (const doubles_matrix<> &V_r, const doubles &w_r,
77
+ [[cpp11::register ]] doubles_matrix<>
78
+ center_variables_r_ (const doubles_matrix<> &V_r, const doubles &w_r,
76
79
const list &klist, const double &tol, const int &maxiter) {
77
80
Mat<double > V = as_Mat (V_r);
78
81
Col<double > w = as_Col (w_r);
0 commit comments