initial commit
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
EXCLUDE copyright
|
||||
EXCLUDE license
|
||||
@@ -0,0 +1,4 @@
|
||||
Matrix3f m;
|
||||
m = AngleAxisf(0.25 * EIGEN_PI, Vector3f::UnitX()) * AngleAxisf(0.5 * EIGEN_PI, Vector3f::UnitY()) *
|
||||
AngleAxisf(0.33 * EIGEN_PI, Vector3f::UnitZ());
|
||||
cout << m << endl << "is unitary: " << m.isUnitary() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
ArrayXXi a{{1, 2, 3}, {3, 4, 5}};
|
||||
cout << a << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array<int, Dynamic, 1> v{{1, 2, 3, 4, 5}};
|
||||
cout << v << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
Array<int, 1, 6> a(1, 2, 3, 4, 5, 6);
|
||||
Array<int, 3, 1> b{1, 2, 3};
|
||||
cout << a << "\n\n" << b << endl;
|
||||
@@ -0,0 +1,11 @@
|
||||
int n = 10000;
|
||||
VectorXd x(n), b(n);
|
||||
SparseMatrix<double> A(n, n);
|
||||
/* ... fill A and b ... */
|
||||
BiCGSTAB<SparseMatrix<double> > solver;
|
||||
solver.compute(A);
|
||||
x = solver.solve(b);
|
||||
std::cout << "#iterations: " << solver.iterations() << std::endl;
|
||||
std::cout << "estimated error: " << solver.error() << std::endl;
|
||||
/* ... update b ... */
|
||||
x = solver.solve(b); // solve again
|
||||
@@ -0,0 +1,14 @@
|
||||
int n = 10000;
|
||||
VectorXd x(n), b(n);
|
||||
SparseMatrix<double> A(n, n);
|
||||
/* ... fill A and b ... */
|
||||
BiCGSTAB<SparseMatrix<double> > solver(A);
|
||||
// start from a random solution
|
||||
x = VectorXd::Random(n);
|
||||
solver.setMaxIterations(1);
|
||||
int i = 0;
|
||||
do {
|
||||
x = solver.solveWithGuess(b, x);
|
||||
std::cout << i << " : " << solver.error() << std::endl;
|
||||
++i;
|
||||
} while (solver.info() != Success && i < 100);
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
file(GLOB snippets_SRCS "*.cpp")
|
||||
|
||||
add_custom_target(all_snippets)
|
||||
|
||||
foreach(snippet_src ${snippets_SRCS})
|
||||
get_filename_component(snippet ${snippet_src} NAME_WE)
|
||||
set(compile_snippet_target compile_${snippet})
|
||||
set(compile_snippet_src ${compile_snippet_target}.cpp)
|
||||
|
||||
file(READ ${snippet_src} snippet_source_code)
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/compile_snippet.cpp.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src})
|
||||
add_executable(${compile_snippet_target}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src})
|
||||
if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO)
|
||||
target_link_libraries(${compile_snippet_target} ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO})
|
||||
endif()
|
||||
target_link_libraries(${compile_snippet_target} Eigen3::Eigen)
|
||||
|
||||
if(${snippet_src} MATCHES "deprecated")
|
||||
set_target_properties(${compile_snippet_target} PROPERTIES COMPILE_FLAGS "-DEIGEN_NO_DEPRECATED_WARNING")
|
||||
endif()
|
||||
add_custom_command(
|
||||
TARGET ${compile_snippet_target}
|
||||
POST_BUILD
|
||||
COMMAND ${compile_snippet_target}
|
||||
ARGS >${CMAKE_CURRENT_BINARY_DIR}/${snippet}.out
|
||||
)
|
||||
add_dependencies(all_snippets ${compile_snippet_target})
|
||||
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src}
|
||||
PROPERTIES OBJECT_DEPENDS ${snippet_src})
|
||||
endforeach()
|
||||
@@ -0,0 +1,8 @@
|
||||
Matrix3f m = Matrix3f::Random();
|
||||
Matrix3f y = Matrix3f::Random();
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
cout << "Here is the matrix y:" << endl << y << endl;
|
||||
Matrix3f x;
|
||||
x = m.colPivHouseholderQr().solve(y);
|
||||
assert(y.isApprox(m* x));
|
||||
cout << "Here is a solution x to the equation mx=y:" << endl << x << endl;
|
||||
@@ -0,0 +1,16 @@
|
||||
MatrixXcf A = MatrixXcf::Random(4, 4);
|
||||
cout << "Here is a random 4x4 matrix, A:" << endl << A << endl << endl;
|
||||
|
||||
ComplexEigenSolver<MatrixXcf> ces;
|
||||
ces.compute(A);
|
||||
cout << "The eigenvalues of A are:" << endl << ces.eigenvalues() << endl;
|
||||
cout << "The matrix of eigenvectors, V, is:" << endl << ces.eigenvectors() << endl << endl;
|
||||
|
||||
complex<float> lambda = ces.eigenvalues()[0];
|
||||
cout << "Consider the first eigenvalue, lambda = " << lambda << endl;
|
||||
VectorXcf v = ces.eigenvectors().col(0);
|
||||
cout << "If v is the corresponding eigenvector, then lambda * v = " << endl << lambda * v << endl;
|
||||
cout << "... and A * v = " << endl << A * v << endl << endl;
|
||||
|
||||
cout << "Finally, V * D * V^(-1) = " << endl
|
||||
<< ces.eigenvectors() * ces.eigenvalues().asDiagonal() * ces.eigenvectors().inverse() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
MatrixXcf ones = MatrixXcf::Ones(3, 3);
|
||||
ComplexEigenSolver<MatrixXcf> ces(ones, /* computeEigenvectors = */ false);
|
||||
cout << "The eigenvalues of the 3x3 matrix of ones are:" << endl << ces.eigenvalues() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
MatrixXcf ones = MatrixXcf::Ones(3, 3);
|
||||
ComplexEigenSolver<MatrixXcf> ces(ones);
|
||||
cout << "The first eigenvector of the 3x3 matrix of ones is:" << endl << ces.eigenvectors().col(0) << endl;
|
||||
@@ -0,0 +1,6 @@
|
||||
MatrixXcf A = MatrixXcf::Random(4, 4);
|
||||
ComplexSchur<MatrixXcf> schur(4);
|
||||
schur.compute(A);
|
||||
cout << "The matrix T in the decomposition of A is:" << endl << schur.matrixT() << endl;
|
||||
schur.compute(A.inverse());
|
||||
cout << "The matrix T in the decomposition of A^(-1) is:" << endl << schur.matrixT() << endl;
|
||||
@@ -0,0 +1,4 @@
|
||||
MatrixXcf A = MatrixXcf::Random(4, 4);
|
||||
cout << "Here is a random 4x4 matrix, A:" << endl << A << endl << endl;
|
||||
ComplexSchur<MatrixXcf> schurOfA(A, false); // false means do not compute U
|
||||
cout << "The triangular matrix T is:" << endl << schurOfA.matrixT() << endl;
|
||||
@@ -0,0 +1,4 @@
|
||||
MatrixXcf A = MatrixXcf::Random(4, 4);
|
||||
cout << "Here is a random 4x4 matrix, A:" << endl << A << endl << endl;
|
||||
ComplexSchur<MatrixXcf> schurOfA(A);
|
||||
cout << "The unitary matrix U is:" << endl << schurOfA.matrixU() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, -2, -3);
|
||||
cout << v.abs() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, -2, -3);
|
||||
cout << v.abs2() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(0, sqrt(2.) / 2, 1);
|
||||
cout << v.acos() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
ArrayXcf v = ArrayXcf::Random(3);
|
||||
cout << v << endl << endl;
|
||||
cout << arg(v) << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
Array<double, 1, 3> x(8, -25, 3), y(1. / 3., 0.5, -2.);
|
||||
cout << "atan2([" << x << "], [" << y << "]) = " << x.atan2(y) << endl; // using ArrayBase::pow
|
||||
cout << "atan2([" << x << "], [" << y << "] = " << atan2(x, y) << endl; // using Eigen::pow
|
||||
@@ -0,0 +1,3 @@
|
||||
Array<double, 1, 3> x(8, 25, 3), e(1. / 3., 0.5, 2.);
|
||||
cout << "[" << x << "]^[" << e << "] = " << x.pow(e) << endl; // using ArrayBase::pow
|
||||
cout << "[" << x << "]^[" << e << "] = " << pow(x, e) << endl; // using Eigen::pow
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(0, sqrt(2.) / 2, 1);
|
||||
cout << v.asin() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(5, 0, 1);
|
||||
cout << v.atan() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(-1, 2, 1), w(-3, 2, 3);
|
||||
cout << ((v < w) && (v < 0)) << endl;
|
||||
@@ -0,0 +1,5 @@
|
||||
Array3d v(1, 2, 3);
|
||||
v(1) *= 0.0 / 0.0;
|
||||
v(2) /= 0.0;
|
||||
cout << v << endl << endl;
|
||||
cout << !isfinite(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(-1, 2, 1), w(-3, 2, 3);
|
||||
cout << ((v < w) || (v < 0)) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 4);
|
||||
cout << v.cbrt() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(7, -2, 2);
|
||||
cout << v << endl << endl;
|
||||
cout << ceil(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(EIGEN_PI, EIGEN_PI / 2, EIGEN_PI / 3);
|
||||
cout << v.cos() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(5, 0, 1);
|
||||
cout << cosh(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(2, 3, 4);
|
||||
cout << v.cube() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3), w(3, 2, 1);
|
||||
cout << (v == w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3);
|
||||
cout << v.exp() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(7, -2, 2);
|
||||
cout << v << endl << endl;
|
||||
cout << floor(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3), w(3, 2, 1);
|
||||
cout << (v > w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3), w(3, 2, 1);
|
||||
cout << (v >= w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(2, 3, 4);
|
||||
cout << v.inverse() << endl;
|
||||
@@ -0,0 +1,5 @@
|
||||
Array3d v(1, 2, 3);
|
||||
v(1) *= 0.0 / 0.0;
|
||||
v(2) /= 0.0;
|
||||
cout << v << endl << endl;
|
||||
cout << isfinite(v) << endl;
|
||||
@@ -0,0 +1,5 @@
|
||||
Array3d v(1, 2, 3);
|
||||
v(1) *= 0.0 / 0.0;
|
||||
v(2) /= 0.0;
|
||||
cout << v << endl << endl;
|
||||
cout << isinf(v) << endl;
|
||||
@@ -0,0 +1,5 @@
|
||||
Array3d v(1, 2, 3);
|
||||
v(1) *= 0.0 / 0.0;
|
||||
v(2) /= 0.0;
|
||||
cout << v << endl << endl;
|
||||
cout << isnan(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3), w(3, 2, 1);
|
||||
cout << (v < w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3), w(3, 2, 1);
|
||||
cout << (v <= w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3);
|
||||
cout << v.log() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array4d v(-1, 0, 1, 2);
|
||||
cout << log10(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(2, 3, 4), w(4, 2, 3);
|
||||
cout << v.max(w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(2, 3, 4), w(4, 2, 3);
|
||||
cout << v.min(w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3);
|
||||
cout << v - 5 << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
Array3d v(1, 2, 3);
|
||||
v -= 5;
|
||||
cout << v << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3), w(3, 2, 1);
|
||||
cout << (v != w) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 3);
|
||||
cout << v + 5 << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
Array3d v(1, 2, 3);
|
||||
v += 5;
|
||||
cout << v << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(8, 27, 64);
|
||||
cout << v.pow(0.333333) << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
Array33i a = Array33i::Random(), b = Array33i::Random();
|
||||
Array33i c = a * b;
|
||||
cout << "a:\n" << a << "\nb:\n" << b << "\nc:\n" << c << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(2, 3, 4), w(4, 2, 3);
|
||||
cout << v / w << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(7, -2, 2);
|
||||
cout << v << endl << endl;
|
||||
cout << rint(v) << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(7, -2, 2);
|
||||
cout << v << endl << endl;
|
||||
cout << round(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array<double, 1, 3> e(2, -3, 1. / 3.);
|
||||
cout << "10^[" << e << "] = " << pow(10, e) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(-3, 5, 0);
|
||||
cout << v.sign() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(EIGEN_PI, EIGEN_PI / 2, EIGEN_PI / 3);
|
||||
cout << v.sin() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(5, 0, 1);
|
||||
cout << sinh(v) << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
Array3d v(3, 2, 4), w(5, 4, 2);
|
||||
v /= w;
|
||||
cout << v << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(1, 2, 4);
|
||||
cout << v.sqrt() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(2, 3, 4);
|
||||
cout << v.square() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
Array3d v(EIGEN_PI, EIGEN_PI / 2, EIGEN_PI / 3);
|
||||
cout << v.tan() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(5, 0, 1);
|
||||
cout << tanh(v) << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
Array3d v(1, 2, 3), w(2, 3, 0);
|
||||
v *= w;
|
||||
cout << v << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
ArrayXd v = ArrayXd::LinSpaced(7, -2, 2);
|
||||
cout << v << endl << endl;
|
||||
cout << trunc(v) << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
cout << VectorXi::LinSpaced(4, 7, 10).transpose() << endl;
|
||||
cout << VectorXd::LinSpaced(5, 0.0, 1.0).transpose() << endl;
|
||||
@@ -0,0 +1,8 @@
|
||||
cout << "Even spacing inputs:" << endl;
|
||||
cout << VectorXi::LinSpaced(8, 1, 4).transpose() << endl;
|
||||
cout << VectorXi::LinSpaced(8, 1, 8).transpose() << endl;
|
||||
cout << VectorXi::LinSpaced(8, 1, 15).transpose() << endl;
|
||||
cout << "Uneven spacing inputs:" << endl;
|
||||
cout << VectorXi::LinSpaced(8, 1, 7).transpose() << endl;
|
||||
cout << VectorXi::LinSpaced(8, 1, 9).transpose() << endl;
|
||||
cout << VectorXi::LinSpaced(8, 1, 16).transpose() << endl;
|
||||
@@ -0,0 +1,2 @@
|
||||
cout << VectorXi::LinSpaced(Sequential, 4, 7, 10).transpose() << endl;
|
||||
cout << VectorXd::LinSpaced(Sequential, 5, 0.0, 1.0).transpose() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
VectorXf v;
|
||||
v.setLinSpaced(5, 0.5f, 1.5f);
|
||||
cout << v << endl;
|
||||
@@ -0,0 +1,6 @@
|
||||
Matrix4Xd M = Matrix4Xd::Random(4, 5);
|
||||
Projective3d P(Matrix4d::Random());
|
||||
cout << "The matrix M is:" << endl << M << endl << endl;
|
||||
cout << "M.colwise().hnormalized():" << endl << M.colwise().hnormalized() << endl << endl;
|
||||
cout << "P*M:" << endl << P * M << endl << endl;
|
||||
cout << "(P*M).colwise().hnormalized():" << endl << (P * M).colwise().hnormalized() << endl << endl;
|
||||
@@ -0,0 +1,4 @@
|
||||
MatrixXi m = MatrixXi::Random(2, 3);
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
cout << "m.colwise().replicate<3>() = ..." << endl;
|
||||
cout << m.colwise().replicate<3>() << endl;
|
||||
@@ -0,0 +1,4 @@
|
||||
Vector3i v = Vector3i::Random();
|
||||
cout << "Here is the vector v:" << endl << v << endl;
|
||||
cout << "v.rowwise().replicate(5) = ..." << endl;
|
||||
cout << v.rowwise().replicate(5) << endl;
|
||||
@@ -0,0 +1,16 @@
|
||||
MatrixXd A = MatrixXd::Random(6, 6);
|
||||
cout << "Here is a random 6x6 matrix, A:" << endl << A << endl << endl;
|
||||
|
||||
EigenSolver<MatrixXd> es(A);
|
||||
cout << "The eigenvalues of A are:" << endl << es.eigenvalues() << endl;
|
||||
cout << "The matrix of eigenvectors, V, is:" << endl << es.eigenvectors() << endl << endl;
|
||||
|
||||
complex<double> lambda = es.eigenvalues()[0];
|
||||
cout << "Consider the first eigenvalue, lambda = " << lambda << endl;
|
||||
VectorXcd v = es.eigenvectors().col(0);
|
||||
cout << "If v is the corresponding eigenvector, then lambda * v = " << endl << lambda * v << endl;
|
||||
cout << "... and A * v = " << endl << A.cast<complex<double> >() * v << endl << endl;
|
||||
|
||||
MatrixXcd D = es.eigenvalues().asDiagonal();
|
||||
MatrixXcd V = es.eigenvectors();
|
||||
cout << "Finally, V * D * V^(-1) = " << endl << V * D * V.inverse() << endl;
|
||||
@@ -0,0 +1,6 @@
|
||||
EigenSolver<MatrixXf> es;
|
||||
MatrixXf A = MatrixXf::Random(4, 4);
|
||||
es.compute(A, /* computeEigenvectors = */ false);
|
||||
cout << "The eigenvalues of A are: " << es.eigenvalues().transpose() << endl;
|
||||
es.compute(A + MatrixXf::Identity(4, 4), false); // re-use es to compute eigenvalues of A+I
|
||||
cout << "The eigenvalues of A+I are: " << es.eigenvalues().transpose() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
MatrixXd ones = MatrixXd::Ones(3, 3);
|
||||
EigenSolver<MatrixXd> es(ones, false);
|
||||
cout << "The eigenvalues of the 3x3 matrix of ones are:" << endl << es.eigenvalues() << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
MatrixXd ones = MatrixXd::Ones(3, 3);
|
||||
EigenSolver<MatrixXd> es(ones);
|
||||
cout << "The first eigenvector of the 3x3 matrix of ones is:" << endl << es.eigenvectors().col(0) << endl;
|
||||
@@ -0,0 +1,9 @@
|
||||
MatrixXd A = MatrixXd::Random(6, 6);
|
||||
cout << "Here is a random 6x6 matrix, A:" << endl << A << endl << endl;
|
||||
|
||||
EigenSolver<MatrixXd> es(A);
|
||||
MatrixXd D = es.pseudoEigenvalueMatrix();
|
||||
MatrixXd V = es.pseudoEigenvectors();
|
||||
cout << "The pseudo-eigenvalue matrix D is:" << endl << D << endl;
|
||||
cout << "The pseudo-eigenvector matrix V is:" << endl << V << endl;
|
||||
cout << "Finally, V * D * V^(-1) = " << endl << V * D * V.inverse() << endl;
|
||||
@@ -0,0 +1,8 @@
|
||||
Matrix3f m = Matrix3f::Random();
|
||||
Matrix3f y = Matrix3f::Random();
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
cout << "Here is the matrix y:" << endl << y << endl;
|
||||
Matrix3f x;
|
||||
x = m.fullPivHouseholderQr().solve(y);
|
||||
assert(y.isApprox(m* x));
|
||||
cout << "Here is a solution x to the equation mx=y:" << endl << x << endl;
|
||||
@@ -0,0 +1,7 @@
|
||||
Matrix3d m;
|
||||
m << 1, 1, 0, 1, 3, 2, 0, 1, 1;
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
cout << "Notice that the middle column is the sum of the two others, so the "
|
||||
<< "columns are linearly dependent." << endl;
|
||||
cout << "Here is a matrix whose columns have the same span but are linearly independent:" << endl
|
||||
<< m.fullPivLu().image(m) << endl;
|
||||
@@ -0,0 +1,5 @@
|
||||
MatrixXf m = MatrixXf::Random(3, 5);
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
MatrixXf ker = m.fullPivLu().kernel();
|
||||
cout << "Here is a matrix whose columns form a basis of the kernel of m:" << endl << ker << endl;
|
||||
cout << "By definition of the kernel, m*ker is zero:" << endl << m * ker << endl;
|
||||
@@ -0,0 +1,9 @@
|
||||
Matrix<float, 2, 3> m = Matrix<float, 2, 3>::Random();
|
||||
Matrix2f y = Matrix2f::Random();
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
cout << "Here is the matrix y:" << endl << y << endl;
|
||||
Matrix<float, 3, 2> x = m.fullPivLu().solve(y);
|
||||
if ((m * x).isApprox(y)) {
|
||||
cout << "Here is a solution x to the equation mx=y:" << endl << x << endl;
|
||||
} else
|
||||
cout << "The equation mx=y does not have any solution." << endl;
|
||||
@@ -0,0 +1,7 @@
|
||||
GeneralizedEigenSolver<MatrixXf> ges;
|
||||
MatrixXf A = MatrixXf::Random(4, 4);
|
||||
MatrixXf B = MatrixXf::Random(4, 4);
|
||||
ges.compute(A, B);
|
||||
cout << "The (complex) numerators of the generalzied eigenvalues are: " << ges.alphas().transpose() << endl;
|
||||
cout << "The (real) denominatore of the generalzied eigenvalues are: " << ges.betas().transpose() << endl;
|
||||
cout << "The (complex) generalzied eigenvalues are (alphas./beta): " << ges.eigenvalues().transpose() << endl;
|
||||
@@ -0,0 +1,6 @@
|
||||
MatrixXcf A = MatrixXcf::Random(4, 4);
|
||||
HessenbergDecomposition<MatrixXcf> hd(4);
|
||||
hd.compute(A);
|
||||
cout << "The matrix H in the decomposition of A is:" << endl << hd.matrixH() << endl;
|
||||
hd.compute(2 * A); // re-use hd to compute and store decomposition of 2A
|
||||
cout << "The matrix H in the decomposition of 2A is:" << endl << hd.matrixH() << endl;
|
||||
@@ -0,0 +1,8 @@
|
||||
Matrix4f A = MatrixXf::Random(4, 4);
|
||||
cout << "Here is a random 4x4 matrix:" << endl << A << endl;
|
||||
HessenbergDecomposition<MatrixXf> hessOfA(A);
|
||||
MatrixXf H = hessOfA.matrixH();
|
||||
cout << "The Hessenberg matrix H is:" << endl << H << endl;
|
||||
MatrixXf Q = hessOfA.matrixQ();
|
||||
cout << "The orthogonal matrix Q is:" << endl << Q << endl;
|
||||
cout << "Q H Q^T is:" << endl << Q * H * Q.transpose() << endl;
|
||||
@@ -0,0 +1,8 @@
|
||||
Matrix4d A = Matrix4d::Random(4, 4);
|
||||
cout << "Here is a random 4x4 matrix:" << endl << A << endl;
|
||||
HessenbergDecomposition<Matrix4d> hessOfA(A);
|
||||
Matrix4d pm = hessOfA.packedMatrix();
|
||||
cout << "The packed matrix M is:" << endl << pm << endl;
|
||||
cout << "The upper Hessenberg part corresponds to the matrix H, which is:" << endl << hessOfA.matrixH() << endl;
|
||||
Vector3d hc = hessOfA.householderCoefficients();
|
||||
cout << "The vector of Householder coefficients is:" << endl << hc << endl;
|
||||
@@ -0,0 +1,7 @@
|
||||
MatrixXf A(MatrixXf::Random(5, 3)), thinQ(MatrixXf::Identity(5, 3)), Q;
|
||||
A.setRandom();
|
||||
HouseholderQR<MatrixXf> qr(A);
|
||||
Q = qr.householderQ();
|
||||
thinQ = qr.householderQ() * thinQ;
|
||||
std::cout << "The complete unitary matrix Q is:\n" << Q << "\n\n";
|
||||
std::cout << "The thin matrix Q is:\n" << thinQ << "\n\n";
|
||||
@@ -0,0 +1,9 @@
|
||||
typedef Matrix<float, 3, 3> Matrix3x3;
|
||||
Matrix3x3 m = Matrix3x3::Random();
|
||||
Matrix3f y = Matrix3f::Random();
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
cout << "Here is the matrix y:" << endl << y << endl;
|
||||
Matrix3f x;
|
||||
x = m.householderQr().solve(y);
|
||||
assert(y.isApprox(m* x));
|
||||
cout << "Here is a solution x to the equation mx=y:" << endl << x << endl;
|
||||
@@ -0,0 +1,31 @@
|
||||
Matrix3d v = Matrix3d::Random();
|
||||
cout << "The matrix v is:" << endl;
|
||||
cout << v << endl;
|
||||
|
||||
Vector3d v0(1, v(1, 0), v(2, 0));
|
||||
cout << "The first Householder vector is: v_0 = " << v0.transpose() << endl;
|
||||
Vector3d v1(0, 1, v(2, 1));
|
||||
cout << "The second Householder vector is: v_1 = " << v1.transpose() << endl;
|
||||
Vector3d v2(0, 0, 1);
|
||||
cout << "The third Householder vector is: v_2 = " << v2.transpose() << endl;
|
||||
|
||||
Vector3d h = Vector3d::Random();
|
||||
cout << "The Householder coefficients are: h = " << h.transpose() << endl;
|
||||
|
||||
Matrix3d H0 = Matrix3d::Identity() - h(0) * v0 * v0.adjoint();
|
||||
cout << "The first Householder reflection is represented by H_0 = " << endl;
|
||||
cout << H0 << endl;
|
||||
Matrix3d H1 = Matrix3d::Identity() - h(1) * v1 * v1.adjoint();
|
||||
cout << "The second Householder reflection is represented by H_1 = " << endl;
|
||||
cout << H1 << endl;
|
||||
Matrix3d H2 = Matrix3d::Identity() - h(2) * v2 * v2.adjoint();
|
||||
cout << "The third Householder reflection is represented by H_2 = " << endl;
|
||||
cout << H2 << endl;
|
||||
cout << "Their product is H_0 H_1 H_2 = " << endl;
|
||||
cout << H0 * H1 * H2 << endl;
|
||||
|
||||
HouseholderSequence<Matrix3d, Vector3d> hhSeq(v, h);
|
||||
Matrix3d hhSeqAsMatrix(hhSeq);
|
||||
cout << "If we construct a HouseholderSequence from v and h" << endl;
|
||||
cout << "and convert it to a matrix, we get:" << endl;
|
||||
cout << hhSeqAsMatrix << endl;
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
std::string sep = "\n----------------------------------------\n";
|
||||
Matrix3d m1;
|
||||
m1 << 1.111111, 2, 3.33333, 4, 5, 6, 7, 8.888888, 9;
|
||||
|
||||
IOFormat CommaInitFmt(StreamPrecision, DontAlignCols, ", ", ", ", "", "", " << ", ";");
|
||||
IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
|
||||
IOFormat OctaveFmt(StreamPrecision, 0, ", ", ";\n", "", "", "[", "]");
|
||||
IOFormat HeavyFmt(FullPrecision, 0, ", ", ";\n", "[", "]", "[", "]");
|
||||
|
||||
std::cout << m1 << sep;
|
||||
std::cout << m1.format(CommaInitFmt) << sep;
|
||||
std::cout << m1.format(CleanFmt) << sep;
|
||||
std::cout << m1.format(OctaveFmt) << sep;
|
||||
std::cout << m1.format(HeavyFmt) << sep;
|
||||
@@ -0,0 +1,9 @@
|
||||
MatrixXf m = MatrixXf::Random(3, 2);
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
JacobiSVD<MatrixXf, ComputeThinU | ComputeThinV> svd(m);
|
||||
cout << "Its singular values are:" << endl << svd.singularValues() << endl;
|
||||
cout << "Its left singular vectors are the columns of the thin U matrix:" << endl << svd.matrixU() << endl;
|
||||
cout << "Its right singular vectors are the columns of the thin V matrix:" << endl << svd.matrixV() << endl;
|
||||
Vector3f rhs(1, 0, 0);
|
||||
cout << "Now consider this rhs vector:" << endl << rhs << endl;
|
||||
cout << "A least-squares solution of m*x = rhs is:" << endl << svd.solve(rhs) << endl;
|
||||
@@ -0,0 +1,6 @@
|
||||
Vector2f v = Vector2f::Random();
|
||||
JacobiRotation<float> G;
|
||||
G.makeGivens(v.x(), v.y());
|
||||
cout << "Here is the vector v:" << endl << v << endl;
|
||||
v.applyOnTheLeft(0, 1, G.adjoint());
|
||||
cout << "Here is the vector J' * v:" << endl << v << endl;
|
||||
@@ -0,0 +1,8 @@
|
||||
Matrix2f m = Matrix2f::Random();
|
||||
m = (m + m.adjoint()).eval();
|
||||
JacobiRotation<float> J;
|
||||
J.makeJacobi(m, 0, 1);
|
||||
cout << "Here is the matrix m:" << endl << m << endl;
|
||||
m.applyOnTheLeft(0, 1, J.adjoint());
|
||||
m.applyOnTheRight(0, 1, J);
|
||||
cout << "Here is the matrix J' * m * J:" << endl << m << endl;
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
MatrixXd A(3, 3);
|
||||
A << 4, -1, 2, -1, 6, 0, 2, 0, 5;
|
||||
cout << "The matrix A is" << endl << A << endl;
|
||||
|
||||
LLT<MatrixXd> lltOfA(A); // compute the Cholesky decomposition of A
|
||||
MatrixXd L = lltOfA.matrixL(); // retrieve factor L in the decomposition
|
||||
// The previous two lines can also be written as "L = A.llt().matrixL()"
|
||||
|
||||
cout << "The Cholesky factor L is" << endl << L << endl;
|
||||
cout << "To check this, let us compute L * L.transpose()" << endl;
|
||||
cout << L * L.transpose() << endl;
|
||||
cout << "This should equal the matrix A" << endl;
|
||||
@@ -0,0 +1,7 @@
|
||||
typedef Matrix<float, Dynamic, 2> DataMatrix;
|
||||
// let's generate some samples on the 3D plane of equation z = 2x+3y (with some noise)
|
||||
DataMatrix samples = DataMatrix::Random(12, 2);
|
||||
VectorXf elevations = 2 * samples.col(0) + 3 * samples.col(1) + VectorXf::Random(12) * 0.1;
|
||||
// and let's solve samples * [x y]^T = elevations in least square sense:
|
||||
Matrix<float, 2, 1> xy = (samples.adjoint() * samples).llt().solve((samples.adjoint() * elevations));
|
||||
cout << xy << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
MatrixXf A = MatrixXf::Random(3, 2);
|
||||
VectorXf b = VectorXf::Random(3);
|
||||
cout << "The solution using normal equations is:\n" << (A.transpose() * A).ldlt().solve(A.transpose() * b) << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
MatrixXf A = MatrixXf::Random(3, 2);
|
||||
VectorXf b = VectorXf::Random(3);
|
||||
cout << "The solution using the QR decomposition is:\n" << A.colPivHouseholderQr().solve(b) << endl;
|
||||
@@ -0,0 +1,3 @@
|
||||
int array[24];
|
||||
for (int i = 0; i < 24; ++i) array[i] = i;
|
||||
cout << Map<MatrixXi, 0, Stride<Dynamic, 2> >(array, 3, 3, Stride<Dynamic, 2>(8, 2)) << endl;
|
||||
@@ -0,0 +1,4 @@
|
||||
int array[12];
|
||||
for (int i = 0; i < 12; ++i) array[i] = i;
|
||||
cout << Map<VectorXi, 0, InnerStride<2> >(array, 6) // the inner stride has already been passed as template parameter
|
||||
<< endl;
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user