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; }
// 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; }
// 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; }
void approx_relpose_generalized ( const Eigen::Matrix<double,6,6> &w1, const Eigen::Matrix<double,6,6> &w2, const Eigen::Matrix<double,6,6> &w3, const Eigen::Matrix<double,6,6> &w4, const Eigen::Matrix<double,6,6> &w5, const Eigen::Matrix<double,6,6> &w6, std::vector<Eigen::Vector3d> &rsolns ) { const Eigen::Matrix<double,15,35> A = computeA(w1,w2,w3,w4,w5,w6); Eigen::Matrix<double,15,35> gbA; gbA << A.col(0),A.col(1),A.col(2),A.col(3),A.col(4),A.col(5),A.col(7),A.col(9),A.col(11),A.col(15),A.col(18),A.col(21),A.col(24),A.col(28),A.col(13),A.col(6),A.col(8),A.col(10),A.col(12),A.col(16),A.col(19),A.col(22),A.col(25),A.col(29),A.col(14),A.col(17),A.col(20),A.col(23),A.col(26),A.col(30),A.col(32),A.col(27),A.col(31),A.col(33),A.col(34); const Eigen::Matrix<double,15,20> G = gbA.block<15,15>(0,0).lu().solve(gbA.block<15,20>(0,15)); Eigen::Matrix<double,20,20> M = Eigen::Matrix<double,20,20>::Zero(); M.block<10,20>(0,0) = -G.block<10,20>(5,0); M(10,4) = 1; M(11,5) = 1; M(12,6) = 1; M(13,7) = 1; M(14,8) = 1; M(15,9) = 1; M(16,13) = 1; M(17,14) = 1; M(18,15) = 1; M(19,18) = 1; const Eigen::EigenSolver< Eigen::Matrix<double,20,20> > eigensolver(M,true); const Eigen::EigenSolver< Eigen::Matrix<double,20,20> >::EigenvalueType evalues = eigensolver.eigenvalues(); const Eigen::EigenSolver< Eigen::Matrix<double,20,20> >::EigenvectorsType evecs = eigensolver.eigenvectors(); rsolns.clear(); rsolns.reserve(evalues.size()); for ( size_t i = 0; i < evalues.size(); i++ ) { if ( evalues[i].imag() != 0 ) continue; const double zsoln = evalues(i).real(); const double xsoln = evecs(16,i).real()/evecs(19,i).real(); const double ysoln = evecs(17,i).real()/evecs(19,i).real(); Eigen::Vector3d rsoln; rsoln << xsoln, ysoln, zsoln; rsolns.push_back(rsoln); } }