Skip to content
Snippets Groups Projects
Commit 0d9db9dc authored by Tallulah Rytz's avatar Tallulah Rytz
Browse files

change in the README file

parent 6a426ced
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,7 @@ We have provided some simple tests for our methods:
In order to run these tests, use CLion/VSCode or run the following lines in the command line:
enter code here
ctest
## Documentation
......@@ -57,35 +57,38 @@ In order to create the documentation, make sure to have *doxygen* installed, and
## Examples
Given a dense (`Eigen::MatrixXd`A) or sparse (`Eigen::SparseMatrix<double> A` ) matrix A
Given a dense (`Eigen::MatrixXd A`) or sparse (`Eigen::SparseMatrix<double> A` ) matrix A
### PowerMethod
If you want to compute the dominant eigenvalue and all the eigenvalues of the matrix A using the Power Method, you can do the following:
PowerMethod power;
double largest_eigenvalue = power.FindLargestEigenvalue(A);
Eigen::VectorXd all_eigenvalues= power.FindEigenvalues(A);
The user can also select the tolerance and maximum iterations allowed for the power methods:
PowerMethod power;
double largest_eigenvalue = power.FindDominantEigenvalue(A);
Eigen::VectorXd all_eigenvalues = power.FindEigenvalues(A);
You can also customize the tolerance and the maximum number of iterations allowed for the Power Method:
PowerMethod new_power(1e-5, 10);
### InversePowerMethod
If you want to compute the smallest eigenvalue of a matrix A using the Inverse Power Method, you can do the following:
InversePowerMethod inversepower;
double smallest_eigenvalue = inversepower.FindSmallestEigenvalue(A);
Eigen::VectorXd all_eigenvalues= inversepower.FindEigenvalues(A); //Not Recommended
**Note:** It is not recommended to use `FindEigenvalues()` with this method because it is unstable when deflating the matrix.
### PowerMethodShift & InversePowerMethodShift
If you want to compute the eigenvalues of a matrix A using the Power Method with shift or the Inverse Power Method with shift, you can do the following:
PowerMethodShift powershift(0.5); //Shift of 0.5
PowerMethodShift powershift(0.5); // Shift of 0.5
Eigen::VectorXd all_evals = powershift.FindEigenvalues(A);
double shifted_eval = inversepower.FindShiftedEigenvalue(A);
InversePowerMethodShift inversepowershift;
double inverse_shifted_eval = inversepowershift.FindShiftedEigenvalue(A);
Eigen::VectorXd all_evals = inversepowershift.FindEigenvalues(A); //Not Recommended
In addition, the shift can be adjusted to find different eigenvalues:
......@@ -93,7 +96,11 @@ In addition, the shift can be adjusted to find different eigenvalues:
double new_shifted_eval = inversepower.FindShiftedEigenvalue(A);
### QRMethod
If you want to compute all the eigenvalues of the matrix A using the QR Method, you can do the following:
QRMethod qr;
Eigen::VectorXd all_evals = qr.FindEigenvalues(A);
You can also customize the tolerance and the maximum number of iterations allowed for the QR Method:
PowerMethod new_power(1e-5, 10);
\ No newline at end of file
......@@ -107,6 +107,7 @@ void QRMethod::QRDecompositionsparse(const Eigen::SparseMatrix<double>& A, Eigen
// ------------------ Find Eigenvalues -----------------
/**
* @brief Use the QR decomposition to make the matrix A diagonal matrix. Then the eigenvalue are the diagonal of the matrix.
*
......@@ -136,7 +137,7 @@ Eigen::VectorXd QRMethod::FindEigenvalues(Eigen::MatrixXd& A){
return eigenvalues;
}
}
// Throw Error if reach max iterations without converging
throw std::runtime_error("Method did not converge after reaching maximum iterations");
};
......@@ -149,7 +150,6 @@ Eigen::VectorXd QRMethod::FindEigenvalues(Eigen::MatrixXd& A){
* @return eigenvalues of the sparse matrix, in absolute descending order
*/
Eigen::VectorXd QRMethod::FindEigenvalues(Eigen::SparseMatrix<double>& A) {
Eigen::SparseMatrix<double> Ak = A;
Eigen::SparseMatrix<double> Q(A.rows(), A.cols());
......@@ -159,11 +159,13 @@ Eigen::VectorXd QRMethod::FindEigenvalues(Eigen::SparseMatrix<double>& A) {
Ak = Q.transpose() * Ak * Q;
if (isConverged(Ak)) {
// Extract eigenvalues from the diagonal of Ak
Eigen::VectorXd eigenvalues(Ak.rows());
for (int i = 0; i < Ak.rows(); ++i) {
eigenvalues(i) = Ak.coeff(i, i);
}
// Sort the eigenvalues in decreasing order in absolute value
std::sort(eigenvalues.begin(), eigenvalues.end(), [](double a, double b) {
return std::abs(a) > std::abs(b);
});
......@@ -171,6 +173,7 @@ Eigen::VectorXd QRMethod::FindEigenvalues(Eigen::SparseMatrix<double>& A) {
return eigenvalues;
}
}
// Throw Error if reach max iterations without converging
throw std::runtime_error("Method did not converge after reaching maximum iterations");
}
......@@ -15,4 +15,6 @@ target_link_libraries(Test_allMethods gtest gtest_main pthread)
target_include_directories(Test_allMethods PRIVATE
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/eigen
)
\ No newline at end of file
)
add_test(NAME RunAllTests COMMAND Test_allMethods)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment