void ctms_decompositions() { const int maxSize = 16; const int size = 12; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, 0, maxSize, maxSize> Matrix; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, 0, maxSize, 1> Vector; typedef Eigen::Matrix<std::complex<Scalar>, Eigen::Dynamic, Eigen::Dynamic, 0, maxSize, maxSize> ComplexMatrix; const Matrix A(Matrix::Random(size, size)); const ComplexMatrix complexA(ComplexMatrix::Random(size, size)); const Matrix saA = A.adjoint() * A; // Cholesky module Eigen::LLT<Matrix> LLT; LLT.compute(A); Eigen::LDLT<Matrix> LDLT; LDLT.compute(A); // Eigenvalues module Eigen::HessenbergDecomposition<ComplexMatrix> hessDecomp; hessDecomp.compute(complexA); Eigen::ComplexSchur<ComplexMatrix> cSchur(size); cSchur.compute(complexA); Eigen::ComplexEigenSolver<ComplexMatrix> cEigSolver; cEigSolver.compute(complexA); Eigen::EigenSolver<Matrix> eigSolver; eigSolver.compute(A); Eigen::SelfAdjointEigenSolver<Matrix> saEigSolver(size); saEigSolver.compute(saA); Eigen::Tridiagonalization<Matrix> tridiag; tridiag.compute(saA); // LU module Eigen::PartialPivLU<Matrix> ppLU; ppLU.compute(A); Eigen::FullPivLU<Matrix> fpLU; fpLU.compute(A); // QR module Eigen::HouseholderQR<Matrix> hQR; hQR.compute(A); Eigen::ColPivHouseholderQR<Matrix> cpQR; cpQR.compute(A); Eigen::FullPivHouseholderQR<Matrix> fpQR; fpQR.compute(A); // SVD module Eigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV); }
static const Eigen::MatrixXcd executeEigenSolver(const QList< Analitza::Expression >& args, bool computeEigenvectors, QStringList &errors) { const int nargs = args.size(); if (nargs != 1) { errors.append(QCoreApplication::tr("Invalid parameter count for '%1'. Should have 1 parameter").arg(EigenvaluesCommand::id)); return Eigen::MatrixXcd(); } const Analitza::Matrix *matrix = static_cast<const Analitza::Matrix*>(args.first().tree()); if (!matrix->isSquare()) { errors.append(QCoreApplication::tr("To use '%1' command the input matrix must be square").arg(EigenvaluesCommand::id)); return Eigen::MatrixXcd(); } const int m = matrix->rowCount(); const int n = matrix->columnCount(); Eigen::MatrixXd realmatrix(m, n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix->at(i,j)->type() == Analitza::Object::value) { const Analitza::Cn *entry = static_cast<const Analitza::Cn*>(matrix->at(i,j)); const Analitza::Cn::ValueFormat entryformat = entry->format(); //Don't allow complex numbers if (entryformat == Analitza::Cn::Char || entryformat == Analitza::Cn::Real || entryformat == Analitza::Cn::Integer || entryformat == Analitza::Cn::Boolean) { realmatrix(i,j) = entry->value(); } else { errors.append(QCoreApplication::tr("Invalid parameter type in matrix entry (%1,%2) for '%3', it must be a number value") .arg(i).arg(j).arg(EigenvaluesCommand::id)); return Eigen::MatrixXcd(); } } else { errors.append(QCoreApplication::tr("Invalid parameter type in matrix entry (%1,%2) for '%3', it must be a number value") .arg(i).arg(j).arg(EigenvaluesCommand::id)); return Eigen::MatrixXcd(); } } } Q_ASSERT(nargs > 0); Eigen::EigenSolver<Eigen::MatrixXd> eigensolver; eigensolver.compute(realmatrix, computeEigenvectors); Eigen::MatrixXcd ret; if (computeEigenvectors) ret = eigensolver.eigenvectors(); else ret = eigensolver.eigenvalues(); return ret; }
// partial reorthogonalization double EigenTriangle::solve(int maxIter, double tol) { int n = graph -> VertexCount(); srand(time(0)); if (maxIter > n) maxIter = n; double na = adjMatrix.norm(); double phi = tol * na; double delta = tol * sqrt(na); vector<Triplet<double> > omega_vector; //MatrixXd omega = MatrixXd::Zero(n + 2, n + 2); for (int i = 0; i < n + 2; i ++) { //omega(i, i - 1) = phi; if (i > 0) omega_vector.push_back(Triplet<double>(i, i - 1, phi)); omega_vector.push_back(Triplet<double>(i, i, 1)); } omega.resize(n + 2, n + 2); omega.setFromTriplets(omega_vector.begin(), omega_vector.end()); //std::cout << omega << std::endl; vector<SparseVector<double> > v; //v.push_back(SparseVector::Random(n)); SparseVector<double> firstV(n); for (int i = 0; i < 100; i ++) firstV.coeffRef(i % n) = i; v.push_back(firstV); v[0] /= v[0].norm(); VectorXd alpha(n); VectorXd beta(n + 1); beta[0] = 0; SparseVector<double> w; bool flag = false; int num = 0; // reorthogonalization times int last = -1; for (int i = 0; i < maxIter; i ++) { if ((i + 1) * 100 / maxIter > last) { last = (i + 1) * 100 / maxIter; std::cout << "\r" << "[EigenTriangle] Processing " << last << "% ..." << std::flush; } //printf("== Iter %d ===\n", i); w = adjMatrix * v[i]; alpha.coeffRef(i) = w.dot(v[i]); if (i == maxIter - 1) break; w -= alpha[i] * v[i]; if (i > 0) w -= beta[i] * v[i - 1]; beta[i + 1] = w.norm(); v.push_back(w / beta[i + 1]); if (flag) { flag = false; for (int j = 0; j <= i; j ++) v[i + 1] -= v[j].dot(v[i + 1]) * v[j]; for (int j = 0; j <= i; j ++) omega.coeffRef(i + 1, j) = phi; //for (int j = 0; j <= i; j ++) // printf("%.5lf\n", v[i + 1].dot(v[j])); } else { omega.coeffRef(i + 1, 0) = 0.0; if (i > 0) { omega.coeffRef(i + 1, 0) = 1.0 / beta(i) * ((alpha(0) - alpha(i)) * omega.coeffRef(i, 0) - beta(i - 1) * omega.coeffRef(i - 1, 0)) + delta; } for (int j = 1; j <= i; j ++) { omega.coeffRef(i + 1, j) = 1.0 / beta(i) * (beta(j) * omega.coeffRef(i, j + 1) + (alpha(j) - alpha(i)) * omega.coeffRef(i, j) - beta(j - 1) * omega.coeffRef(i, j - 1) - beta(i - 1) * omega.coeffRef(i - 1, j)) + delta; } } double mx = 0.0; for (int j = 0; j <= i; j ++) if (mx < fabs(omega.coeffRef(i + 1, j))) mx = fabs(omega.coeffRef(i + 1, j)); if (mx > sqrt(tol)) { for (int j = 0; j <= i; j ++) omega.coeffRef(i + 1, j) = phi; num ++; for (int j = 0; j <= i; j ++) v[i + 1] -= v[i + 1].dot(v[j]) * v[j]; flag = true; } } printf("\n"); int k = maxIter; MatrixXd T = MatrixXd::Zero(k, k); for (int i = 0; i < k; i ++) { T(i, i) = alpha[i]; if (i < k - 1) T(i, i + 1) = beta[i + 1]; if (i > 0) T(i, i - 1) = beta[i]; } //std::cout << T << std::endl; Eigen::EigenSolver<MatrixXd> eigenSolver; eigenSolver.compute(T, false); Eigen::VectorXcd eigens = eigenSolver.eigenvalues(); double res = 0; for (int i = 0; i < eigens.size(); i ++) { std::complex<double> tmp = eigens[i]; res += pow(tmp.real(), 3) / 6; std::cout << i << ": " << tmp << std::endl; } //res /= 6; return res; }
// full reorthogonalization double EigenTriangle::solve(int maxIter) { int n = graph -> VertexCount(); srand(time(0)); // check if rows == cols() if (maxIter > n) maxIter = n; vector<VectorXd> v; v.push_back(VectorXd::Random(n)); v[0] = v[0] / v[0].norm(); VectorXd alpha(n); VectorXd beta(n + 1); beta[0] = 0; VectorXd w; int last = 0; printf("%d\n", maxIter); for (int i = 0; i < maxIter; i ++) { if (i * 100 / maxIter > last + 10 || i == maxIter - 1) { last = (i + 1) * 100 / maxIter; std::cout << "\r" << "[EigenTriangle] Processing " << last << "% ..." << std::flush; } w = adjMatrix * v[i]; alpha[i] = w.dot(v[i]); if (i == maxIter - 1) break; w -= alpha[i] * v[i]; if (i > 0) w -= beta[i] * v[i - 1]; beta[i + 1] = w.norm(); v.push_back(w / beta[i + 1]); for (int j = 0; j <= i; j ++) { v[i + 1] = v[i + 1] - v[i + 1].dot(v[j]) * v[j]; } } printf("\n"); int k = maxIter; MatrixXd T = MatrixXd::Zero(k, k); for (int i = 0; i < k; i ++) { T(i, i) = alpha[i]; if (i < k - 1) T(i, i + 1) = beta[i + 1]; if (i > 0) T(i, i - 1) = beta[i]; } //std::cout << T << std::endl; Eigen::EigenSolver<MatrixXd> eigenSolver; eigenSolver.compute(T, /* computeEigenvectors = */ false); Eigen::VectorXcd eigens = eigenSolver.eigenvalues(); double res = 0; for (int i = 0; i < eigens.size(); i ++) { std::complex<double> tmp = eigens[i]; res += pow(tmp.real(), 3); printf("%.5lf\n", tmp.real()); } res /= 6; return res; }