| 51 | } |
| 52 | |
| 53 | Eigen::MatrixXd FastICA::fast_ica_parallel_compute (const Eigen::MatrixXd &X) |
| 54 | { |
| 55 | int cols = (int)X.cols (); |
| 56 | Eigen::MatrixXd W (num_components, num_components); |
| 57 | random_normal (W); |
| 58 | |
| 59 | // sW <- La.svd(W) |
| 60 | Eigen::BDCSVD<Eigen::MatrixXd> sW (W, Eigen::ComputeThinU | Eigen::ComputeThinV); |
| 61 | // W <- sW$u %*% Diag(1/sW$d) %*% t(sW$u) %*% W |
| 62 | W = sW.matrixU () * (sW.singularValues ().array ().inverse ()).matrix ().asDiagonal () * |
| 63 | sW.matrixU ().transpose () * W; |
| 64 | Eigen::MatrixXd W1 = W; |
| 65 | // lim <- rep(1000, maxit) |
| 66 | std::vector<double> lim (max_it, 1000); |
| 67 | // iteration counter |
| 68 | int it = 0; |
| 69 | |
| 70 | while (lim[it] > tol && it < (max_it - 1)) |
| 71 | { |
| 72 | // wx <- W %*% X |
| 73 | // gwx <- tanh(alpha * wx) |
| 74 | // alpha = 1 , so ignore |
| 75 | Eigen::MatrixXd gwx = (W * X).array ().tanh ().matrix (); |
| 76 | // v1 <- gwx %*% t(X)/cols |
| 77 | Eigen::MatrixXd v1 = gwx * (X.array () / cols).matrix ().transpose (); |
| 78 | // g.wx <- alpha * (1 - (gwx)^2) |
| 79 | // nb alpha == 1 |
| 80 | gwx = 1 - gwx.array ().square (); |
| 81 | // v2 <- Diag(apply(g.wx, 1, FUN = mean)) %*% W |
| 82 | Eigen::MatrixXd v2 = gwx.array ().rowwise ().mean ().matrix ().asDiagonal () * W; |
| 83 | // W1 <- v1 - v2 |
| 84 | W1 = v1 - v2; |
| 85 | // sW1 <- La.svd(W1) |
| 86 | Eigen::BDCSVD<Eigen::MatrixXd> sW1 (W1, Eigen::ComputeThinU | Eigen::ComputeThinV); |
| 87 | // W1 <- sW1$u %*% Diag(1/sW1$d) %*% t(sW1$u) %*% W1 |
| 88 | W1 = sW1.matrixU () * (sW1.singularValues ().array ().inverse ()).matrix ().asDiagonal () * |
| 89 | sW1.matrixU ().transpose () * W1; |
| 90 | // lim[it + 1] <- max( Mod( Mod( diag(W1 %*% t(W) ) ) - 1 ) ) |
| 91 | lim[it + 1] = ((W1 * W.transpose ()).diagonal ().array ().abs () - 1).abs ().maxCoeff (); |
| 92 | // W <- W1 |
| 93 | W = W1; |
| 94 | ++it; |
| 95 | } |
| 96 | |
| 97 | return W; |
| 98 | } |
| 99 | |
| 100 | void FastICA::scale (Eigen::Ref<Eigen::MatrixXd> M, bool center, bool normalize, |
| 101 | bool ignore_invariants, std::vector<int> *zeros) |
nothing calls this directly
no outgoing calls
no test coverage detected