-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5218c9c
commit e4f0b66
Showing
6 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <iostream> | ||
#include <Eigen/Dense> | ||
|
||
using namespace Eigen; | ||
using namespace std; | ||
|
||
int main() { | ||
// Define the matrix A | ||
MatrixXd A(3, 3); | ||
/*A << 1, 2, 3, | ||
4, 5, 6, | ||
7, 8, 9; | ||
*/ | ||
A << 2.0, -1.0, 0.0, | ||
-1.0, 2.0, -1.0, | ||
0.0, -1.0, 2.0; | ||
|
||
// Initial guess for the eigenvector | ||
VectorXd v = VectorXd::Random(3); | ||
|
||
// Power iteration method | ||
int iterations = 1000; // Maximum number of iterations | ||
double tolerance = 1e-3; // Tolerance for convergence | ||
|
||
for (int i = 0; i < iterations; ++i) { | ||
VectorXd Av = A * v; | ||
double lambda = v.dot(Av) / v.dot(v); // Approximation of the dominant eigenvalue | ||
VectorXd v_new = Av / Av.norm(); // Normalized eigenvector | ||
|
||
if ((v_new - v).norm() < tolerance) { | ||
cout << "Dominant eigenvalue: " << lambda << endl; | ||
cout << "Eigenvector: \n" << v_new << endl; | ||
break; // Convergence achieved | ||
} | ||
|
||
v = v_new; | ||
} | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import numpy as np | ||
|
||
# Define the matrix A | ||
A = np.array([ | ||
[2.0, -1.0, 0.0], | ||
[-1.0, 2.0, -1.0], | ||
[0.0, -1.0, 2.0] | ||
]) | ||
|
||
|
||
|
||
# Choose the initial vector x | ||
x = np.array([[1, 2]]).T | ||
|
||
# Define the tolerance for the eigenvalue | ||
# and eigenvector approximations | ||
# (i.e. the maximum allowed difference between | ||
# the approximations and the actual values) | ||
tol = 1e-6 | ||
|
||
# Define the maximum number of iterations | ||
max_iter = 100 | ||
|
||
# Define the variable lam_prev to store the | ||
# previous approximation for the largest eigenvalue | ||
lam_prev = 0 | ||
|
||
# Iteratively improve the approximations | ||
# for the largest eigenvalue and eigenvector | ||
# using the power method | ||
for i in range(max_iter): | ||
# Compute the updated approximation for the eigenvector | ||
x = A @ x / np.linalg.norm(A @ x) | ||
|
||
# Compute the updated approximation for the largest eigenvalue | ||
lam = (x.T @ A @ x) / (x.T @ x) | ||
|
||
# Check if the approximations have converged | ||
if np.abs(lam - lam_prev) < tol: | ||
break | ||
|
||
# Store the current approximation for the largest eigenvalue | ||
lam_prev = lam | ||
|
||
# Print the approximations for the | ||
# largest eigenvalue and eigenvector | ||
print(float(lam)) | ||
print(x) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <iostream> | ||
#include <Eigen/Dense> | ||
|
||
using Eigen::MatrixXd; | ||
|
||
int main() | ||
{ | ||
MatrixXd m(2,2); | ||
m(0,0) = 3; | ||
m(1,0) = 2.5; | ||
m(0,1) = -1; | ||
m(1,1) = m(1,0) + m(0,1); | ||
std::cout << m << std::endl; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
#include <openGPMP/linalg/eigen.hpp> | ||
#include <vector> | ||
|
||
const double TOLERANCE = 1e-3; | ||
|
||
int main () { | ||
std::vector<std::vector<double>> squareMat = {{2.0, -1.0, 0.0}, | ||
{-1.0, 2.0, -1.0}, | ||
{0.0, -1.0, 2.0}}; | ||
gpmp::linalg::Eigen eigenSquare(squareMat); | ||
|
||
double eigenvalueSquare = eigenSquare.power_iter(1000, TOLERANCE); | ||
|
||
std::cout << "Eigenvalue square: " << eigenvalueSquare << std::endl; | ||
|
||
} |