mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
nearly complete page 6 / linear algebra + examples
fix the previous/next links
This commit is contained in:
23
doc/examples/TutorialLinAlgComputeTwice.cpp
Normal file
23
doc/examples/TutorialLinAlgComputeTwice.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
|
||||
int main()
|
||||
{
|
||||
Matrix2f A, b;
|
||||
LLT<Matrix2f> llt;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
cout << "Here is the matrix A:\n" << A << endl;
|
||||
cout << "Here is the right hand side b:\n" << b << endl;
|
||||
cout << "Computing LLT decomposition..." << endl;
|
||||
llt.compute(A);
|
||||
cout << "The solution is:\n" << llt.solve(b) << endl;
|
||||
A(1,1)++;
|
||||
cout << "The matrix A is now:\n" << A << endl;
|
||||
cout << "Computing LLT decomposition..." << endl;
|
||||
llt.compute(A);
|
||||
cout << "The solution is now:\n" << llt.solve(b) << endl;
|
||||
}
|
||||
14
doc/examples/TutorialLinAlgExComputeSolveError.cpp
Normal file
14
doc/examples/TutorialLinAlgExComputeSolveError.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
|
||||
int main()
|
||||
{
|
||||
MatrixXd A = MatrixXd::Random(100,100);
|
||||
MatrixXd b = MatrixXd::Random(100,50);
|
||||
MatrixXd x = A.fullPivLu().solve(b);
|
||||
double relative_error = (A*x - b).norm() / b.norm(); // norm() is L2 norm
|
||||
cout << "The relative error is:\n" << relative_error << endl;
|
||||
}
|
||||
@@ -10,8 +10,8 @@ int main()
|
||||
Vector3f b;
|
||||
A << 1,2,3, 4,5,6, 7,8,10;
|
||||
b << 3, 3, 4;
|
||||
cout << "Here is the matrix A:" << endl << A << endl;
|
||||
cout << "Here is the vector b:" << endl << b << endl;
|
||||
cout << "Here is the matrix A:\n" << A << endl;
|
||||
cout << "Here is the vector b:\n" << b << endl;
|
||||
Vector3f x = A.colPivHouseholderQr().solve(b);
|
||||
cout << "The solution is:" << endl << x << endl;
|
||||
cout << "The solution is:\n" << x << endl;
|
||||
}
|
||||
|
||||
16
doc/examples/TutorialLinAlgExSolveLDLT.cpp
Normal file
16
doc/examples/TutorialLinAlgExSolveLDLT.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
|
||||
int main()
|
||||
{
|
||||
Matrix2f A, b;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
cout << "Here is the matrix A:\n" << A << endl;
|
||||
cout << "Here is the right hand side b:\n" << b << endl;
|
||||
Matrix2f x = A.ldlt().solve(b);
|
||||
cout << "The solution is:\n" << x << endl;
|
||||
}
|
||||
16
doc/examples/TutorialLinAlgInverseDeterminant.cpp
Normal file
16
doc/examples/TutorialLinAlgInverseDeterminant.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
|
||||
int main()
|
||||
{
|
||||
Matrix3f A;
|
||||
A << 1, 2, 1,
|
||||
2, 1, 0,
|
||||
-1, 1, 2;
|
||||
cout << "Here is the matrix A:\n" << A << endl;
|
||||
cout << "The determinant of A is " << A.determinant() << endl;
|
||||
cout << "The inverse of A is:\n" << A.inverse() << endl;
|
||||
}
|
||||
20
doc/examples/TutorialLinAlgRankRevealing.cpp
Normal file
20
doc/examples/TutorialLinAlgRankRevealing.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
|
||||
int main()
|
||||
{
|
||||
Matrix3f A;
|
||||
A << 1, 2, 5,
|
||||
2, 1, 4,
|
||||
3, 0, 3;
|
||||
cout << "Here is the matrix A:\n" << A << endl;
|
||||
FullPivLU<Matrix3f> lu_decomp(A);
|
||||
cout << "The rank of A is " << lu_decomp.rank() << endl;
|
||||
cout << "Here is a matrix whose columns form a basis of the null-space of A:\n"
|
||||
<< lu_decomp.kernel() << endl;
|
||||
cout << "Here is a matrix whose columns form a basis of the column-space of A:\n"
|
||||
<< lu_decomp.image(A) << endl; // yes, have to pass the original A
|
||||
}
|
||||
17
doc/examples/TutorialLinAlgSelfAdjointEigenSolver.cpp
Normal file
17
doc/examples/TutorialLinAlgSelfAdjointEigenSolver.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
|
||||
int main()
|
||||
{
|
||||
Matrix2f A;
|
||||
A << 1, 2, 2, 3;
|
||||
cout << "Here is the matrix A:\n" << A << endl;
|
||||
SelfAdjointEigenSolver<Matrix2f> eigensolver(A);
|
||||
cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl;
|
||||
cout << "Here's a matrix whose columns are eigenvectors of A "
|
||||
<< "corresponding to these eigenvalues:\n"
|
||||
<< eigensolver.eigenvectors() << endl;
|
||||
}
|
||||
19
doc/examples/TutorialLinAlgSetThreshold.cpp
Normal file
19
doc/examples/TutorialLinAlgSetThreshold.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
|
||||
int main()
|
||||
{
|
||||
Matrix2d A;
|
||||
FullPivLU<Matrix2d> lu;
|
||||
A << 2, 1,
|
||||
2, 0.9999999999;
|
||||
lu.compute(A);
|
||||
cout << "By default, the rank of A is found to be " << lu.rank() << endl;
|
||||
cout << "Now recomputing the LU decomposition with threshold 1e-5" << endl;
|
||||
lu.setThreshold(1e-5);
|
||||
lu.compute(A);
|
||||
cout << "The rank of A is found to be " << lu.rank() << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user