void SpectClust::normalizeSum(SymmetricMatrix &A) { RowVector Ones(A.Ncols()); Ones = 1.0; // Calculate the sum of each row matrix style. Could this be a Diagonal Matrix instead? Matrix RowSum = Ones * (A.t()); DiagonalMatrix D(A.Ncols()); D = 0; // Take the inverse square root for(int i = 1; i <= A.Ncols(); i++) { D(i,i) = 1/sqrt(RowSum(1,i)); } Matrix X = D * (A * D); A << X; }
void Print(const SymmetricMatrix& X) { ++PCN; cout << "\nMatrix type: " << X.Type().Value() << " ("; cout << X.Nrows() << ", "; cout << X.Ncols() << ")\n\n"; if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; } int nr=X.Nrows(); int nc=X.Ncols(); for (int i=1; i<=nr; i++) { int j; for (j=1; j<i; j++) cout << X(j,i) << "\t"; for (j=i; j<=nc; j++) cout << X(i,j) << "\t"; cout << "\n"; } cout << flush; ++PCZ; }
bool SpectClust::findNLargestSymEvals(const SymmetricMatrix &W, int numLamda, std::vector<Numeric> &eVals, Matrix &EVec) { bool converged = false; eVals.clear(); eVals.reserve(numLamda); DiagonalMatrix D(W.Ncols()); Matrix E; try { EigenValues(W, D, E); converged = true; EVec.ReSize(W.Ncols(), numLamda); int count = 0; for(int i = W.Ncols(); i > W.Ncols() - numLamda; i--) { eVals.push_back(D(i)); EVec.Column(++count) << E.Column(i); } } catch(const Exception &e) { Err::errAbort("Exception: " + ToStr(e.what())); } catch(...) { Err::errAbort("Yikes couldn't calculate eigen vectors."); } return converged; }