Skip to content

Commit

Permalink
Updates to improve speed; toggle sign choice
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin D. Weinberg committed Feb 13, 2025
1 parent 46161f9 commit 8661d07
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
36 changes: 19 additions & 17 deletions expui/SvdSignChoice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,37 @@ namespace MSSA {
sL.setZero();
sR.setZero();

// Working, non-const instance
auto S1 = S;

auto sgn = [](double val) -> int
{
return (0.0 < val) - (val < 0.0);
};

// Get projects from left and right singular vectors onto data matrix
// Get projections from left and right singular vectors onto data
// matrix
//
for (int k=0; k<K; k++) {

// Remove all but target dimension for numerical stability
S1(k) = 0.0;
auto Y = X - U * S1.asDiagonal() * V.transpose();

// Restore the value
S1(k) = S(k);

// d_j = U^T_k * Y_j
for (int j=0; j<Y.cols(); j++) {
double d = U.col(k).dot(Y.col(j));
sL(k) += sgn(d) * d*d;
}
Eigen::VectorXd dL = Y.transpose() * U.col(k);

// sum of sgn(dL_j)*dL_j^2
sL(k) += dL.dot(dL.cwiseAbs());

// d_i = V^T_k * (Y^T)_i
for (int i=0; i<Y.rows(); i++) {
double d = V.col(k).dot(Y.row(i));
sR(k) += sgn(d) * d*d;
}
Eigen::VectorXd dR = Y * V.col(k);

// sum of sgn(dR_i)*dR_i^2
sR(k) += dR.dot(dR.cwiseAbs());
}

auto sgn = [](double val) -> int
{
return (0.0 < val) - (val < 0.0);
};

// Determine and apply the sign correction
//
Expand Down
3 changes: 2 additions & 1 deletion expui/expMSSA.H
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace MSSA
//! Primary MSSA analysis
void mssa_analysis();

bool computed, reconstructed, trajectory;
//! MSSA control flags
bool computed, reconstructed, trajectory, useSignChoice;

//! The reconstructed coefficients for each PC
std::map<Key, Eigen::MatrixXd, mSSAkeyCompare> RC;
Expand Down
8 changes: 4 additions & 4 deletions expui/expMSSA.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@

namespace MSSA {

const bool useSignChoice = true;

void SvdSignChoice
(const Eigen::MatrixXd& X,
Eigen::MatrixXd& U, const Eigen::VectorXd& S, Eigen::MatrixXd& V);
Expand Down Expand Up @@ -1281,6 +1279,7 @@ namespace MSSA {
"Traj",
"RedSym",
"rank",
"Sign",
"allchan",
"distance",
"flip",
Expand Down Expand Up @@ -1841,7 +1840,7 @@ namespace MSSA {
}


expMSSA::expMSSA(const mssaConfig& config, int nW, int nPC, const std::string flags) : numW(nW), npc(nPC), trajectory(true)
expMSSA::expMSSA(const mssaConfig& config, int nW, int nPC, const std::string flags) : numW(nW), npc(nPC), trajectory(true), useSignChoice(false)
{
// Parse the YAML string
//
Expand All @@ -1860,7 +1859,8 @@ namespace MSSA {

// Set the SVD strategy for mSSA
//
if (params["Traj"]) trajectory = params["Traj"].as<bool>();
if (params["Traj"]) trajectory = params["Traj"].as<bool>();
if (params["Sign"]) useSignChoice = params["Sign"].as<bool>();

// std::cout << "Trajectory is " << std::boolalpha << trajectory
// << std::endl;
Expand Down

0 comments on commit 8661d07

Please sign in to comment.