diff options
Diffstat (limited to 'eigen/doc/examples')
62 files changed, 1313 insertions, 0 deletions
diff --git a/eigen/doc/examples/.krazy b/eigen/doc/examples/.krazy new file mode 100644 index 0000000..00b9940 --- /dev/null +++ b/eigen/doc/examples/.krazy @@ -0,0 +1,2 @@ +EXCLUDE copyright +EXCLUDE license diff --git a/eigen/doc/examples/CMakeLists.txt b/eigen/doc/examples/CMakeLists.txt new file mode 100644 index 0000000..08cf8ef --- /dev/null +++ b/eigen/doc/examples/CMakeLists.txt @@ -0,0 +1,16 @@ +file(GLOB examples_SRCS "*.cpp") + +foreach(example_src ${examples_SRCS}) + get_filename_component(example ${example_src} NAME_WE) + add_executable(${example} ${example_src}) + if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + target_link_libraries(${example} ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) + endif() + add_custom_command( + TARGET ${example} + POST_BUILD + COMMAND ${example} + ARGS >${CMAKE_CURRENT_BINARY_DIR}/${example}.out + ) + add_dependencies(all_examples ${example}) +endforeach(example_src) diff --git a/eigen/doc/examples/DenseBase_middleCols_int.cpp b/eigen/doc/examples/DenseBase_middleCols_int.cpp new file mode 100644 index 0000000..0ebd955 --- /dev/null +++ b/eigen/doc/examples/DenseBase_middleCols_int.cpp @@ -0,0 +1,15 @@ +#include <Eigen/Core> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main(void) +{ + int const N = 5; + MatrixXi A(N,N); + A.setRandom(); + cout << "A =\n" << A << '\n' << endl; + cout << "A(1..3,:) =\n" << A.middleCols(1,3) << endl; + return 0; +} diff --git a/eigen/doc/examples/DenseBase_middleRows_int.cpp b/eigen/doc/examples/DenseBase_middleRows_int.cpp new file mode 100644 index 0000000..a6fe9e8 --- /dev/null +++ b/eigen/doc/examples/DenseBase_middleRows_int.cpp @@ -0,0 +1,15 @@ +#include <Eigen/Core> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main(void) +{ + int const N = 5; + MatrixXi A(N,N); + A.setRandom(); + cout << "A =\n" << A << '\n' << endl; + cout << "A(2..3,:) =\n" << A.middleRows(2,2) << endl; + return 0; +} diff --git a/eigen/doc/examples/DenseBase_template_int_middleCols.cpp b/eigen/doc/examples/DenseBase_template_int_middleCols.cpp new file mode 100644 index 0000000..6191d79 --- /dev/null +++ b/eigen/doc/examples/DenseBase_template_int_middleCols.cpp @@ -0,0 +1,15 @@ +#include <Eigen/Core> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main(void) +{ + int const N = 5; + MatrixXi A(N,N); + A.setRandom(); + cout << "A =\n" << A << '\n' << endl; + cout << "A(:,1..3) =\n" << A.middleCols<3>(1) << endl; + return 0; +} diff --git a/eigen/doc/examples/DenseBase_template_int_middleRows.cpp b/eigen/doc/examples/DenseBase_template_int_middleRows.cpp new file mode 100644 index 0000000..7e8b657 --- /dev/null +++ b/eigen/doc/examples/DenseBase_template_int_middleRows.cpp @@ -0,0 +1,15 @@ +#include <Eigen/Core> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main(void) +{ + int const N = 5; + MatrixXi A(N,N); + A.setRandom(); + cout << "A =\n" << A << '\n' << endl; + cout << "A(1..3,:) =\n" << A.middleRows<3>(1) << endl; + return 0; +} diff --git a/eigen/doc/examples/MatrixBase_cwise_const.cpp b/eigen/doc/examples/MatrixBase_cwise_const.cpp new file mode 100644 index 0000000..23700e0 --- /dev/null +++ b/eigen/doc/examples/MatrixBase_cwise_const.cpp @@ -0,0 +1,18 @@ +#define EIGEN2_SUPPORT +#include <Eigen/Core> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main() +{ + Matrix3i m = Matrix3i::Random(); + cout << "Here is the matrix m:" << endl << m << endl; + Matrix3i n = Matrix3i::Random(); + cout << "And here is the matrix n:" << endl << n << endl; + cout << "The coefficient-wise product of m and n is:" << endl; + cout << m.cwise() * n << endl; + cout << "Taking the cube of the coefficients of m yields:" << endl; + cout << m.cwise().pow(3) << endl; +} diff --git a/eigen/doc/examples/QuickStart_example.cpp b/eigen/doc/examples/QuickStart_example.cpp new file mode 100644 index 0000000..7238c0c --- /dev/null +++ b/eigen/doc/examples/QuickStart_example.cpp @@ -0,0 +1,14 @@ +#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; +} diff --git a/eigen/doc/examples/QuickStart_example2_dynamic.cpp b/eigen/doc/examples/QuickStart_example2_dynamic.cpp new file mode 100644 index 0000000..ff6746e --- /dev/null +++ b/eigen/doc/examples/QuickStart_example2_dynamic.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; +using namespace std; + +int main() +{ + MatrixXd m = MatrixXd::Random(3,3); + m = (m + MatrixXd::Constant(3,3,1.2)) * 50; + cout << "m =" << endl << m << endl; + VectorXd v(3); + v << 1, 2, 3; + cout << "m * v =" << endl << m * v << endl; +} diff --git a/eigen/doc/examples/QuickStart_example2_fixed.cpp b/eigen/doc/examples/QuickStart_example2_fixed.cpp new file mode 100644 index 0000000..d911752 --- /dev/null +++ b/eigen/doc/examples/QuickStart_example2_fixed.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; +using namespace std; + +int main() +{ + Matrix3d m = Matrix3d::Random(); + m = (m + Matrix3d::Constant(1.2)) * 50; + cout << "m =" << endl << m << endl; + Vector3d v(1,2,3); + + cout << "m * v =" << endl << m * v << endl; +} diff --git a/eigen/doc/examples/TemplateKeyword_flexible.cpp b/eigen/doc/examples/TemplateKeyword_flexible.cpp new file mode 100644 index 0000000..9d85292 --- /dev/null +++ b/eigen/doc/examples/TemplateKeyword_flexible.cpp @@ -0,0 +1,22 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; + +template <typename Derived1, typename Derived2> +void copyUpperTriangularPart(MatrixBase<Derived1>& dst, const MatrixBase<Derived2>& src) +{ + /* Note the 'template' keywords in the following line! */ + dst.template triangularView<Upper>() = src.template triangularView<Upper>(); +} + +int main() +{ + MatrixXi m1 = MatrixXi::Ones(5,5); + MatrixXi m2 = MatrixXi::Random(4,4); + std::cout << "m2 before copy:" << std::endl; + std::cout << m2 << std::endl << std::endl; + copyUpperTriangularPart(m2, m1.topLeftCorner(4,4)); + std::cout << "m2 after copy:" << std::endl; + std::cout << m2 << std::endl << std::endl; +} diff --git a/eigen/doc/examples/TemplateKeyword_simple.cpp b/eigen/doc/examples/TemplateKeyword_simple.cpp new file mode 100644 index 0000000..6998c17 --- /dev/null +++ b/eigen/doc/examples/TemplateKeyword_simple.cpp @@ -0,0 +1,20 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; + +void copyUpperTriangularPart(MatrixXf& dst, const MatrixXf& src) +{ + dst.triangularView<Upper>() = src.triangularView<Upper>(); +} + +int main() +{ + MatrixXf m1 = MatrixXf::Ones(4,4); + MatrixXf m2 = MatrixXf::Random(4,4); + std::cout << "m2 before copy:" << std::endl; + std::cout << m2 << std::endl << std::endl; + copyUpperTriangularPart(m2, m1); + std::cout << "m2 after copy:" << std::endl; + std::cout << m2 << std::endl << std::endl; +} diff --git a/eigen/doc/examples/TutorialLinAlgComputeTwice.cpp b/eigen/doc/examples/TutorialLinAlgComputeTwice.cpp new file mode 100644 index 0000000..06ba646 --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgComputeTwice.cpp @@ -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; +} diff --git a/eigen/doc/examples/TutorialLinAlgExComputeSolveError.cpp b/eigen/doc/examples/TutorialLinAlgExComputeSolveError.cpp new file mode 100644 index 0000000..f362fb7 --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgExComputeSolveError.cpp @@ -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; +} diff --git a/eigen/doc/examples/TutorialLinAlgExSolveColPivHouseholderQR.cpp b/eigen/doc/examples/TutorialLinAlgExSolveColPivHouseholderQR.cpp new file mode 100644 index 0000000..3a99a94 --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgExSolveColPivHouseholderQR.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +using namespace Eigen; + +int main() +{ + Matrix3f A; + Vector3f b; + A << 1,2,3, 4,5,6, 7,8,10; + b << 3, 3, 4; + 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:\n" << x << endl; +} diff --git a/eigen/doc/examples/TutorialLinAlgExSolveLDLT.cpp b/eigen/doc/examples/TutorialLinAlgExSolveLDLT.cpp new file mode 100644 index 0000000..f8beacd --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgExSolveLDLT.cpp @@ -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; +} diff --git a/eigen/doc/examples/TutorialLinAlgInverseDeterminant.cpp b/eigen/doc/examples/TutorialLinAlgInverseDeterminant.cpp new file mode 100644 index 0000000..43970ff --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgInverseDeterminant.cpp @@ -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; +}
\ No newline at end of file diff --git a/eigen/doc/examples/TutorialLinAlgRankRevealing.cpp b/eigen/doc/examples/TutorialLinAlgRankRevealing.cpp new file mode 100644 index 0000000..c516507 --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgRankRevealing.cpp @@ -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 +} diff --git a/eigen/doc/examples/TutorialLinAlgSVDSolve.cpp b/eigen/doc/examples/TutorialLinAlgSVDSolve.cpp new file mode 100644 index 0000000..9fbc031 --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgSVDSolve.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +using namespace Eigen; + +int main() +{ + MatrixXf A = MatrixXf::Random(3, 2); + cout << "Here is the matrix A:\n" << A << endl; + VectorXf b = VectorXf::Random(3); + cout << "Here is the right hand side b:\n" << b << endl; + cout << "The least-squares solution is:\n" + << A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << endl; +} diff --git a/eigen/doc/examples/TutorialLinAlgSelfAdjointEigenSolver.cpp b/eigen/doc/examples/TutorialLinAlgSelfAdjointEigenSolver.cpp new file mode 100644 index 0000000..8d1d1ed --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgSelfAdjointEigenSolver.cpp @@ -0,0 +1,18 @@ +#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); + if (eigensolver.info() != Success) abort(); + cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl; + cout << "Here's a matrix whose columns are eigenvectors of A \n" + << "corresponding to these eigenvalues:\n" + << eigensolver.eigenvectors() << endl; +} diff --git a/eigen/doc/examples/TutorialLinAlgSetThreshold.cpp b/eigen/doc/examples/TutorialLinAlgSetThreshold.cpp new file mode 100644 index 0000000..3956b13 --- /dev/null +++ b/eigen/doc/examples/TutorialLinAlgSetThreshold.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +using namespace Eigen; + +int main() +{ + Matrix2d A; + A << 2, 1, + 2, 0.9999999999; + FullPivLU<Matrix2d> lu(A); + cout << "By default, the rank of A is found to be " << lu.rank() << endl; + lu.setThreshold(1e-5); + cout << "With threshold 1e-5, the rank of A is found to be " << lu.rank() << endl; +} diff --git a/eigen/doc/examples/Tutorial_ArrayClass_accessors.cpp b/eigen/doc/examples/Tutorial_ArrayClass_accessors.cpp new file mode 100644 index 0000000..dc720ff --- /dev/null +++ b/eigen/doc/examples/Tutorial_ArrayClass_accessors.cpp @@ -0,0 +1,24 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main() +{ + ArrayXXf m(2,2); + + // assign some values coefficient by coefficient + m(0,0) = 1.0; m(0,1) = 2.0; + m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0); + + // print values to standard output + cout << m << endl << endl; + + // using the comma-initializer is also allowed + m << 1.0,2.0, + 3.0,4.0; + + // print values to standard output + cout << m << endl; +} diff --git a/eigen/doc/examples/Tutorial_ArrayClass_addition.cpp b/eigen/doc/examples/Tutorial_ArrayClass_addition.cpp new file mode 100644 index 0000000..480ffb0 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ArrayClass_addition.cpp @@ -0,0 +1,23 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main() +{ + ArrayXXf a(3,3); + ArrayXXf b(3,3); + a << 1,2,3, + 4,5,6, + 7,8,9; + b << 1,2,3, + 1,2,3, + 1,2,3; + + // Adding two arrays + cout << "a + b = " << endl << a + b << endl << endl; + + // Subtracting a scalar from an array + cout << "a - 2 = " << endl << a - 2 << endl; +} diff --git a/eigen/doc/examples/Tutorial_ArrayClass_cwise_other.cpp b/eigen/doc/examples/Tutorial_ArrayClass_cwise_other.cpp new file mode 100644 index 0000000..d9046c6 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ArrayClass_cwise_other.cpp @@ -0,0 +1,19 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main() +{ + ArrayXf a = ArrayXf::Random(5); + a *= 2; + cout << "a =" << endl + << a << endl; + cout << "a.abs() =" << endl + << a.abs() << endl; + cout << "a.abs().sqrt() =" << endl + << a.abs().sqrt() << endl; + cout << "a.min(a.abs().sqrt()) =" << endl + << a.min(a.abs().sqrt()) << endl; +} diff --git a/eigen/doc/examples/Tutorial_ArrayClass_interop.cpp b/eigen/doc/examples/Tutorial_ArrayClass_interop.cpp new file mode 100644 index 0000000..371f070 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ArrayClass_interop.cpp @@ -0,0 +1,22 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main() +{ + MatrixXf m(2,2); + MatrixXf n(2,2); + MatrixXf result(2,2); + + m << 1,2, + 3,4; + n << 5,6, + 7,8; + + result = (m.array() + 4).matrix() * m; + cout << "-- Combination 1: --" << endl << result << endl << endl; + result = (m.array() * n.array()).matrix() * m; + cout << "-- Combination 2: --" << endl << result << endl << endl; +} diff --git a/eigen/doc/examples/Tutorial_ArrayClass_interop_matrix.cpp b/eigen/doc/examples/Tutorial_ArrayClass_interop_matrix.cpp new file mode 100644 index 0000000..1014275 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ArrayClass_interop_matrix.cpp @@ -0,0 +1,26 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main() +{ + MatrixXf m(2,2); + MatrixXf n(2,2); + MatrixXf result(2,2); + + m << 1,2, + 3,4; + n << 5,6, + 7,8; + + result = m * n; + cout << "-- Matrix m*n: --" << endl << result << endl << endl; + result = m.array() * n.array(); + cout << "-- Array m*n: --" << endl << result << endl << endl; + result = m.cwiseProduct(n); + cout << "-- With cwiseProduct: --" << endl << result << endl << endl; + result = m.array() + 4; + cout << "-- Array m + 4: --" << endl << result << endl << endl; +} diff --git a/eigen/doc/examples/Tutorial_ArrayClass_mult.cpp b/eigen/doc/examples/Tutorial_ArrayClass_mult.cpp new file mode 100644 index 0000000..6cb439f --- /dev/null +++ b/eigen/doc/examples/Tutorial_ArrayClass_mult.cpp @@ -0,0 +1,16 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace Eigen; +using namespace std; + +int main() +{ + ArrayXXf a(2,2); + ArrayXXf b(2,2); + a << 1,2, + 3,4; + b << 5,6, + 7,8; + cout << "a * b = " << endl << a * b << endl; +} diff --git a/eigen/doc/examples/Tutorial_BlockOperations_block_assignment.cpp b/eigen/doc/examples/Tutorial_BlockOperations_block_assignment.cpp new file mode 100644 index 0000000..76f49f2 --- /dev/null +++ b/eigen/doc/examples/Tutorial_BlockOperations_block_assignment.cpp @@ -0,0 +1,18 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace std; +using namespace Eigen; + +int main() +{ + Array22f m; + m << 1,2, + 3,4; + Array44f a = Array44f::Constant(0.6); + cout << "Here is the array a:" << endl << a << endl << endl; + a.block<2,2>(1,1) = m; + cout << "Here is now a with m copied into its central 2x2 block:" << endl << a << endl << endl; + a.block(0,0,2,3) = a.block(2,1,2,3); + cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:" << endl << a << endl << endl; +} diff --git a/eigen/doc/examples/Tutorial_BlockOperations_colrow.cpp b/eigen/doc/examples/Tutorial_BlockOperations_colrow.cpp new file mode 100644 index 0000000..2e7eb00 --- /dev/null +++ b/eigen/doc/examples/Tutorial_BlockOperations_colrow.cpp @@ -0,0 +1,17 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace std; + +int main() +{ + Eigen::MatrixXf m(3,3); + m << 1,2,3, + 4,5,6, + 7,8,9; + cout << "Here is the matrix m:" << endl << m << endl; + cout << "2nd Row: " << m.row(1) << endl; + m.col(2) += 3 * m.col(0); + cout << "After adding 3 times the first column into the third column, the matrix m is:\n"; + cout << m << endl; +} diff --git a/eigen/doc/examples/Tutorial_BlockOperations_corner.cpp b/eigen/doc/examples/Tutorial_BlockOperations_corner.cpp new file mode 100644 index 0000000..3a31507 --- /dev/null +++ b/eigen/doc/examples/Tutorial_BlockOperations_corner.cpp @@ -0,0 +1,17 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace std; + +int main() +{ + Eigen::Matrix4f m; + m << 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10,11,12, + 13,14,15,16; + cout << "m.leftCols(2) =" << endl << m.leftCols(2) << endl << endl; + cout << "m.bottomRows<2>() =" << endl << m.bottomRows<2>() << endl << endl; + m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose(); + cout << "After assignment, m = " << endl << m << endl; +} diff --git a/eigen/doc/examples/Tutorial_BlockOperations_print_block.cpp b/eigen/doc/examples/Tutorial_BlockOperations_print_block.cpp new file mode 100644 index 0000000..edea4ae --- /dev/null +++ b/eigen/doc/examples/Tutorial_BlockOperations_print_block.cpp @@ -0,0 +1,20 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace std; + +int main() +{ + Eigen::MatrixXf m(4,4); + m << 1, 2, 3, 4, + 5, 6, 7, 8, + 9,10,11,12, + 13,14,15,16; + cout << "Block in the middle" << endl; + cout << m.block<2,2>(1,1) << endl << endl; + for (int i = 1; i <= 3; ++i) + { + cout << "Block of size " << i << "x" << i << endl; + cout << m.block(0,0,i,i) << endl << endl; + } +} diff --git a/eigen/doc/examples/Tutorial_BlockOperations_vector.cpp b/eigen/doc/examples/Tutorial_BlockOperations_vector.cpp new file mode 100644 index 0000000..4a0b023 --- /dev/null +++ b/eigen/doc/examples/Tutorial_BlockOperations_vector.cpp @@ -0,0 +1,14 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace std; + +int main() +{ + Eigen::ArrayXf v(6); + v << 1, 2, 3, 4, 5, 6; + cout << "v.head(3) =" << endl << v.head(3) << endl << endl; + cout << "v.tail<3>() = " << endl << v.tail<3>() << endl << endl; + v.segment(1,4) *= 2; + cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl; +} diff --git a/eigen/doc/examples/Tutorial_PartialLU_solve.cpp b/eigen/doc/examples/Tutorial_PartialLU_solve.cpp new file mode 100644 index 0000000..a560879 --- /dev/null +++ b/eigen/doc/examples/Tutorial_PartialLU_solve.cpp @@ -0,0 +1,18 @@ +#include <Eigen/Core> +#include <Eigen/LU> +#include <iostream> + +using namespace std; +using namespace Eigen; + +int main() +{ + Matrix3f A; + 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; + Vector3f x = A.lu().solve(b); + cout << "The solution is:" << endl << x << endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.cpp new file mode 100644 index 0000000..334b4d8 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +using namespace Eigen; + +int main() +{ + Eigen::MatrixXf m(2,4); + Eigen::VectorXf v(2); + + m << 1, 23, 6, 9, + 3, 11, 7, 2; + + v << 2, + 3; + + MatrixXf::Index index; + // find nearest neighbour + (m.colwise() - v).colwise().squaredNorm().minCoeff(&index); + + cout << "Nearest neighbour is column " << index << ":" << endl; + cout << m.col(index) << endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.cpp new file mode 100644 index 0000000..e6c87c6 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.cpp @@ -0,0 +1,21 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +int main() +{ + Eigen::MatrixXf mat(2,4); + Eigen::VectorXf v(2); + + mat << 1, 2, 6, 9, + 3, 1, 7, 2; + + v << 0, + 1; + + //add v to each column of m + mat.colwise() += v; + + std::cout << "Broadcasting result: " << std::endl; + std::cout << mat << std::endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.cpp new file mode 100644 index 0000000..d87c96a --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +int main() +{ + Eigen::MatrixXf mat(2,4); + Eigen::VectorXf v(4); + + mat << 1, 2, 6, 9, + 3, 1, 7, 2; + + v << 0,1,2,3; + + //add v to each row of m + mat.rowwise() += v.transpose(); + + std::cout << "Broadcasting result: " << std::endl; + std::cout << mat << std::endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_colwise.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_colwise.cpp new file mode 100644 index 0000000..df68256 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_colwise.cpp @@ -0,0 +1,13 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +int main() +{ + Eigen::MatrixXf mat(2,4); + mat << 1, 2, 6, 9, + 3, 1, 7, 2; + + std::cout << "Column's maximum: " << std::endl + << mat.colwise().maxCoeff() << std::endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_maxnorm.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_maxnorm.cpp new file mode 100644 index 0000000..049c747 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_maxnorm.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +using namespace Eigen; +int main() +{ + MatrixXf mat(2,4); + mat << 1, 2, 6, 9, + 3, 1, 7, 2; + + MatrixXf::Index maxIndex; + float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex); + + std::cout << "Maximum sum at position " << maxIndex << std::endl; + + std::cout << "The corresponding vector is: " << std::endl; + std::cout << mat.col( maxIndex ) << std::endl; + std::cout << "And its sum is is: " << maxNorm << std::endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp new file mode 100644 index 0000000..0cca37f --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp @@ -0,0 +1,21 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace std; +using namespace Eigen; + +int main() +{ + ArrayXXf a(2,2); + + a << 1,2, + 3,4; + + cout << "(a > 0).all() = " << (a > 0).all() << endl; + cout << "(a > 0).any() = " << (a > 0).any() << endl; + cout << "(a > 0).count() = " << (a > 0).count() << endl; + cout << endl; + cout << "(a > 2).all() = " << (a > 2).all() << endl; + cout << "(a > 2).any() = " << (a > 2).any() << endl; + cout << "(a > 2).count() = " << (a > 2).count() << endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp new file mode 100644 index 0000000..740439f --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp @@ -0,0 +1,28 @@ +#include <Eigen/Dense> +#include <iostream> + +using namespace std; +using namespace Eigen; + +int main() +{ + VectorXf v(2); + MatrixXf m(2,2), n(2,2); + + v << -1, + 2; + + m << 1,-2, + -3,4; + + cout << "v.squaredNorm() = " << v.squaredNorm() << endl; + cout << "v.norm() = " << v.norm() << endl; + cout << "v.lpNorm<1>() = " << v.lpNorm<1>() << endl; + cout << "v.lpNorm<Infinity>() = " << v.lpNorm<Infinity>() << endl; + + cout << endl; + cout << "m.squaredNorm() = " << m.squaredNorm() << endl; + cout << "m.norm() = " << m.norm() << endl; + cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << endl; + cout << "m.lpNorm<Infinity>() = " << m.lpNorm<Infinity>() << endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_rowwise.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_rowwise.cpp new file mode 100644 index 0000000..80427c9 --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_rowwise.cpp @@ -0,0 +1,13 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +int main() +{ + Eigen::MatrixXf mat(2,4); + mat << 1, 2, 6, 9, + 3, 1, 7, 2; + + std::cout << "Row's maximum: " << std::endl + << mat.rowwise().maxCoeff() << std::endl; +} diff --git a/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_visitors.cpp b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_visitors.cpp new file mode 100644 index 0000000..b54e9aa --- /dev/null +++ b/eigen/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_visitors.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +using namespace Eigen; + +int main() +{ + Eigen::MatrixXf m(2,2); + + m << 1, 2, + 3, 4; + + //get location of maximum + MatrixXf::Index maxRow, maxCol; + float max = m.maxCoeff(&maxRow, &maxCol); + + //get location of minimum + MatrixXf::Index minRow, minCol; + float min = m.minCoeff(&minRow, &minCol); + + cout << "Max: " << max << ", at: " << + maxRow << "," << maxCol << endl; + cout << "Min: " << min << ", at: " << + minRow << "," << minCol << endl; +} diff --git a/eigen/doc/examples/Tutorial_simple_example_dynamic_size.cpp b/eigen/doc/examples/Tutorial_simple_example_dynamic_size.cpp new file mode 100644 index 0000000..0f0280e --- /dev/null +++ b/eigen/doc/examples/Tutorial_simple_example_dynamic_size.cpp @@ -0,0 +1,22 @@ +#include <Eigen/Core> +#include <iostream> + +using namespace Eigen; + +int main() +{ + for (int size=1; size<=4; ++size) + { + MatrixXi m(size,size+1); // a (size)x(size+1)-matrix of int's + for (int j=0; j<m.cols(); ++j) // loop over columns + for (int i=0; i<m.rows(); ++i) // loop over rows + m(i,j) = i+j*m.rows(); // to access matrix coefficients, + // use operator()(int,int) + std::cout << m << "\n\n"; + } + + VectorXf v(4); // a vector of 4 float's + // to access vector coefficients, use either operator () or operator [] + v[0] = 1; v[1] = 2; v(2) = 3; v(3) = 4; + std::cout << "\nv:\n" << v << std::endl; +} diff --git a/eigen/doc/examples/Tutorial_simple_example_fixed_size.cpp b/eigen/doc/examples/Tutorial_simple_example_fixed_size.cpp new file mode 100644 index 0000000..bc4f95d --- /dev/null +++ b/eigen/doc/examples/Tutorial_simple_example_fixed_size.cpp @@ -0,0 +1,15 @@ +#include <Eigen/Core> +#include <iostream> + +using namespace Eigen; + +int main() +{ + Matrix3f m3; + m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9; + Matrix4f m4 = Matrix4f::Identity(); + Vector4i v4(1, 2, 3, 4); + + std::cout << "m3\n" << m3 << "\nm4:\n" + << m4 << "\nv4:\n" << v4 << std::endl; +} diff --git a/eigen/doc/examples/class_Block.cpp b/eigen/doc/examples/class_Block.cpp new file mode 100644 index 0000000..ace719a --- /dev/null +++ b/eigen/doc/examples/class_Block.cpp @@ -0,0 +1,27 @@ +#include <Eigen/Core> +#include <iostream> +using namespace Eigen; +using namespace std; + +template<typename Derived> +Eigen::Block<Derived> +topLeftCorner(MatrixBase<Derived>& m, int rows, int cols) +{ + return Eigen::Block<Derived>(m.derived(), 0, 0, rows, cols); +} + +template<typename Derived> +const Eigen::Block<const Derived> +topLeftCorner(const MatrixBase<Derived>& m, int rows, int cols) +{ + return Eigen::Block<const Derived>(m.derived(), 0, 0, rows, cols); +} + +int main(int, char**) +{ + Matrix4d m = Matrix4d::Identity(); + cout << topLeftCorner(4*m, 2, 3) << endl; // calls the const version + topLeftCorner(m, 2, 3) *= 5; // calls the non-const version + cout << "Now the matrix m is:" << endl << m << endl; + return 0; +} diff --git a/eigen/doc/examples/class_CwiseBinaryOp.cpp b/eigen/doc/examples/class_CwiseBinaryOp.cpp new file mode 100644 index 0000000..682af46 --- /dev/null +++ b/eigen/doc/examples/class_CwiseBinaryOp.cpp @@ -0,0 +1,18 @@ +#include <Eigen/Core> +#include <iostream> +using namespace Eigen; +using namespace std; + +// define a custom template binary functor +template<typename Scalar> struct MakeComplexOp { + EIGEN_EMPTY_STRUCT_CTOR(MakeComplexOp) + typedef complex<Scalar> result_type; + complex<Scalar> operator()(const Scalar& a, const Scalar& b) const { return complex<Scalar>(a,b); } +}; + +int main(int, char**) +{ + Matrix4d m1 = Matrix4d::Random(), m2 = Matrix4d::Random(); + cout << m1.binaryExpr(m2, MakeComplexOp<double>()) << endl; + return 0; +} diff --git a/eigen/doc/examples/class_CwiseUnaryOp.cpp b/eigen/doc/examples/class_CwiseUnaryOp.cpp new file mode 100644 index 0000000..a5fcc15 --- /dev/null +++ b/eigen/doc/examples/class_CwiseUnaryOp.cpp @@ -0,0 +1,19 @@ +#include <Eigen/Core> +#include <iostream> +using namespace Eigen; +using namespace std; + +// define a custom template unary functor +template<typename Scalar> +struct CwiseClampOp { + CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {} + const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); } + Scalar m_inf, m_sup; +}; + +int main(int, char**) +{ + Matrix4d m1 = Matrix4d::Random(); + cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << endl; + return 0; +} diff --git a/eigen/doc/examples/class_CwiseUnaryOp_ptrfun.cpp b/eigen/doc/examples/class_CwiseUnaryOp_ptrfun.cpp new file mode 100644 index 0000000..36706d8 --- /dev/null +++ b/eigen/doc/examples/class_CwiseUnaryOp_ptrfun.cpp @@ -0,0 +1,20 @@ +#include <Eigen/Core> +#include <iostream> +using namespace Eigen; +using namespace std; + +// define function to be applied coefficient-wise +double ramp(double x) +{ + if (x > 0) + return x; + else + return 0; +} + +int main(int, char**) +{ + Matrix4d m1 = Matrix4d::Random(); + cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(ptr_fun(ramp)) << endl; + return 0; +} diff --git a/eigen/doc/examples/class_FixedBlock.cpp b/eigen/doc/examples/class_FixedBlock.cpp new file mode 100644 index 0000000..9978b32 --- /dev/null +++ b/eigen/doc/examples/class_FixedBlock.cpp @@ -0,0 +1,27 @@ +#include <Eigen/Core> +#include <iostream> +using namespace Eigen; +using namespace std; + +template<typename Derived> +Eigen::Block<Derived, 2, 2> +topLeft2x2Corner(MatrixBase<Derived>& m) +{ + return Eigen::Block<Derived, 2, 2>(m.derived(), 0, 0); +} + +template<typename Derived> +const Eigen::Block<const Derived, 2, 2> +topLeft2x2Corner(const MatrixBase<Derived>& m) +{ + return Eigen::Block<const Derived, 2, 2>(m.derived(), 0, 0); +} + +int main(int, char**) +{ + Matrix3d m = Matrix3d::Identity(); + cout << topLeft2x2Corner(4*m) << endl; // calls the const version + topLeft2x2Corner(m) *= 2; // calls the non-const version + cout << "Now the matrix m is:" << endl << m << endl; + return 0; +} diff --git a/eigen/doc/examples/class_FixedVectorBlock.cpp b/eigen/doc/examples/class_FixedVectorBlock.cpp new file mode 100644 index 0000000..c88c9fb --- /dev/null +++ b/eigen/doc/examples/class_FixedVectorBlock.cpp @@ -0,0 +1,27 @@ +#include <Eigen/Core> +#include <iostream> +using namespace Eigen; +using namespace std; + +template<typename Derived> +Eigen::VectorBlock<Derived, 2> +firstTwo(MatrixBase<Derived>& v) +{ + return Eigen::VectorBlock<Derived, 2>(v.derived(), 0); +} + +template<typename Derived> +const Eigen::VectorBlock<const Derived, 2> +firstTwo(const MatrixBase<Derived>& v) +{ + return Eigen::VectorBlock<const Derived, 2>(v.derived(), 0); +} + +int main(int, char**) +{ + Matrix<int,1,6> v; v << 1,2,3,4,5,6; + cout << firstTwo(4*v) << endl; // calls the const version + firstTwo(v) *= 2; // calls the non-const version + cout << "Now the vector v is:" << endl << v << endl; + return 0; +} diff --git a/eigen/doc/examples/class_VectorBlock.cpp b/eigen/doc/examples/class_VectorBlock.cpp new file mode 100644 index 0000000..dc213df --- /dev/null +++ b/eigen/doc/examples/class_VectorBlock.cpp @@ -0,0 +1,27 @@ +#include <Eigen/Core> +#include <iostream> +using namespace Eigen; +using namespace std; + +template<typename Derived> +Eigen::VectorBlock<Derived> +segmentFromRange(MatrixBase<Derived>& v, int start, int end) +{ + return Eigen::VectorBlock<Derived>(v.derived(), start, end-start); +} + +template<typename Derived> +const Eigen::VectorBlock<const Derived> +segmentFromRange(const MatrixBase<Derived>& v, int start, int end) +{ + return Eigen::VectorBlock<const Derived>(v.derived(), start, end-start); +} + +int main(int, char**) +{ + Matrix<int,1,6> v; v << 1,2,3,4,5,6; + cout << segmentFromRange(2*v, 2, 4) << endl; // calls the const version + segmentFromRange(v, 1, 3) *= 5; // calls the non-const version + cout << "Now the vector v is:" << endl << v << endl; + return 0; +} diff --git a/eigen/doc/examples/function_taking_eigenbase.cpp b/eigen/doc/examples/function_taking_eigenbase.cpp new file mode 100644 index 0000000..49d94b3 --- /dev/null +++ b/eigen/doc/examples/function_taking_eigenbase.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <Eigen/Core> +using namespace Eigen; + +template <typename Derived> +void print_size(const EigenBase<Derived>& b) +{ + std::cout << "size (rows, cols): " << b.size() << " (" << b.rows() + << ", " << b.cols() << ")" << std::endl; +} + +int main() +{ + Vector3f v; + print_size(v); + // v.asDiagonal() returns a 3x3 diagonal matrix pseudo-expression + print_size(v.asDiagonal()); +} diff --git a/eigen/doc/examples/function_taking_ref.cpp b/eigen/doc/examples/function_taking_ref.cpp new file mode 100644 index 0000000..162a202 --- /dev/null +++ b/eigen/doc/examples/function_taking_ref.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include <Eigen/SVD> +using namespace Eigen; +using namespace std; + +float inv_cond(const Ref<const MatrixXf>& a) +{ + const VectorXf sing_vals = a.jacobiSvd().singularValues(); + return sing_vals(sing_vals.size()-1) / sing_vals(0); +} + +int main() +{ + Matrix4f m = Matrix4f::Random(); + cout << "matrix m:" << endl << m << endl << endl; + cout << "inv_cond(m): " << inv_cond(m) << endl; + cout << "inv_cond(m(1:3,1:3)): " << inv_cond(m.topLeftCorner(3,3)) << endl; + cout << "inv_cond(m+I): " << inv_cond(m+Matrix4f::Identity()) << endl; +} diff --git a/eigen/doc/examples/matrixfree_cg.cpp b/eigen/doc/examples/matrixfree_cg.cpp new file mode 100644 index 0000000..f0631c3 --- /dev/null +++ b/eigen/doc/examples/matrixfree_cg.cpp @@ -0,0 +1,180 @@ +#include <iostream> +#include <Eigen/Core> +#include <Eigen/Dense> +#include <Eigen/IterativeLinearSolvers> + +class MatrixReplacement; +template<typename Rhs> class MatrixReplacement_ProductReturnType; + +namespace Eigen { +namespace internal { + template<> + struct traits<MatrixReplacement> : Eigen::internal::traits<Eigen::SparseMatrix<double> > + {}; + + template <typename Rhs> + struct traits<MatrixReplacement_ProductReturnType<Rhs> > { + // The equivalent plain objet type of the product. This type is used if the product needs to be evaluated into a temporary. + typedef Eigen::Matrix<typename Rhs::Scalar, Eigen::Dynamic, Rhs::ColsAtCompileTime> ReturnType; + }; +} +} + +// Inheriting EigenBase should not be needed in the future. +class MatrixReplacement : public Eigen::EigenBase<MatrixReplacement> { +public: + // Expose some compile-time information to Eigen: + typedef double Scalar; + typedef double RealScalar; + enum { + ColsAtCompileTime = Eigen::Dynamic, + RowsAtCompileTime = Eigen::Dynamic, + MaxColsAtCompileTime = Eigen::Dynamic, + MaxRowsAtCompileTime = Eigen::Dynamic + }; + + Index rows() const { return 4; } + Index cols() const { return 4; } + + void resize(Index a_rows, Index a_cols) + { + // This method should not be needed in the future. + assert(a_rows==0 && a_cols==0 || a_rows==rows() && a_cols==cols()); + } + + // In the future, the return type should be Eigen::Product<MatrixReplacement,Rhs> + template<typename Rhs> + MatrixReplacement_ProductReturnType<Rhs> operator*(const Eigen::MatrixBase<Rhs>& x) const { + return MatrixReplacement_ProductReturnType<Rhs>(*this, x.derived()); + } + +}; + +// The proxy class representing the product of a MatrixReplacement with a MatrixBase<> +template<typename Rhs> +class MatrixReplacement_ProductReturnType : public Eigen::ReturnByValue<MatrixReplacement_ProductReturnType<Rhs> > { +public: + typedef MatrixReplacement::Index Index; + + // The ctor store references to the matrix and right-hand-side object (usually a vector). + MatrixReplacement_ProductReturnType(const MatrixReplacement& matrix, const Rhs& rhs) + : m_matrix(matrix), m_rhs(rhs) + {} + + Index rows() const { return m_matrix.rows(); } + Index cols() const { return m_rhs.cols(); } + + // This function is automatically called by Eigen. It must evaluate the product of matrix * rhs into y. + template<typename Dest> + void evalTo(Dest& y) const + { + y.setZero(4); + + y(0) += 2 * m_rhs(0); y(1) += 1 * m_rhs(0); + y(0) += 1 * m_rhs(1); y(1) += 2 * m_rhs(1); y(2) += 1 * m_rhs(1); + y(1) += 1 * m_rhs(2); y(2) += 2 * m_rhs(2); y(3) += 1 * m_rhs(2); + y(2) += 1 * m_rhs(3); y(3) += 2 * m_rhs(3); + } + +protected: + const MatrixReplacement& m_matrix; + typename Rhs::Nested m_rhs; +}; + + +/*****/ + +// This class simply warp a diagonal matrix as a Jacobi preconditioner. +// In the future such simple and generic wrapper should be shipped within Eigen itsel. +template <typename _Scalar> +class MyJacobiPreconditioner +{ + typedef _Scalar Scalar; + typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> Vector; + typedef typename Vector::Index Index; + + public: + // this typedef is only to export the scalar type and compile-time dimensions to solve_retval + typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> MatrixType; + + MyJacobiPreconditioner() : m_isInitialized(false) {} + + void setInvDiag(const Eigen::VectorXd &invdiag) { + m_invdiag=invdiag; + m_isInitialized=true; + } + + Index rows() const { return m_invdiag.size(); } + Index cols() const { return m_invdiag.size(); } + + template<typename MatType> + MyJacobiPreconditioner& analyzePattern(const MatType& ) { return *this; } + + template<typename MatType> + MyJacobiPreconditioner& factorize(const MatType& mat) { return *this; } + + template<typename MatType> + MyJacobiPreconditioner& compute(const MatType& mat) { return *this; } + + template<typename Rhs, typename Dest> + void _solve(const Rhs& b, Dest& x) const + { + x = m_invdiag.array() * b.array() ; + } + + template<typename Rhs> inline const Eigen::internal::solve_retval<MyJacobiPreconditioner, Rhs> + solve(const Eigen::MatrixBase<Rhs>& b) const + { + eigen_assert(m_isInitialized && "MyJacobiPreconditioner is not initialized."); + eigen_assert(m_invdiag.size()==b.rows() + && "MyJacobiPreconditioner::solve(): invalid number of rows of the right hand side matrix b"); + return Eigen::internal::solve_retval<MyJacobiPreconditioner, Rhs>(*this, b.derived()); + } + + protected: + Vector m_invdiag; + bool m_isInitialized; +}; + +namespace Eigen { +namespace internal { + +template<typename _MatrixType, typename Rhs> +struct solve_retval<MyJacobiPreconditioner<_MatrixType>, Rhs> + : solve_retval_base<MyJacobiPreconditioner<_MatrixType>, Rhs> +{ + typedef MyJacobiPreconditioner<_MatrixType> Dec; + EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs) + + template<typename Dest> void evalTo(Dest& dst) const + { + dec()._solve(rhs(),dst); + } +}; + +} +} + + +/*****/ + + +int main() +{ + MatrixReplacement A; + Eigen::VectorXd b(4), x; + b << 1, 1, 1, 1; + + // solve Ax = b using CG with matrix-free version: + Eigen::ConjugateGradient < MatrixReplacement, Eigen::Lower|Eigen::Upper, MyJacobiPreconditioner<double> > cg; + + Eigen::VectorXd invdiag(4); + invdiag << 1./3., 1./4., 1./4., 1./3.; + + cg.preconditioner().setInvDiag(invdiag); + cg.compute(A); + x = cg.solve(b); + + std::cout << "#iterations: " << cg.iterations() << std::endl; + std::cout << "estimated error: " << cg.error() << std::endl; +} diff --git a/eigen/doc/examples/tut_arithmetic_add_sub.cpp b/eigen/doc/examples/tut_arithmetic_add_sub.cpp new file mode 100644 index 0000000..e97477b --- /dev/null +++ b/eigen/doc/examples/tut_arithmetic_add_sub.cpp @@ -0,0 +1,22 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; + +int main() +{ + Matrix2d a; + a << 1, 2, + 3, 4; + MatrixXd b(2,2); + b << 2, 3, + 1, 4; + std::cout << "a + b =\n" << a + b << std::endl; + std::cout << "a - b =\n" << a - b << std::endl; + std::cout << "Doing a += b;" << std::endl; + a += b; + std::cout << "Now a =\n" << a << std::endl; + Vector3d v(1,2,3); + Vector3d w(1,0,0); + std::cout << "-v + w - v =\n" << -v + w - v << std::endl; +} diff --git a/eigen/doc/examples/tut_arithmetic_dot_cross.cpp b/eigen/doc/examples/tut_arithmetic_dot_cross.cpp new file mode 100644 index 0000000..631c9a5 --- /dev/null +++ b/eigen/doc/examples/tut_arithmetic_dot_cross.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; +using namespace std; +int main() +{ + Vector3d v(1,2,3); + Vector3d w(0,1,2); + + cout << "Dot product: " << v.dot(w) << endl; + double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar + cout << "Dot product via a matrix product: " << dp << endl; + cout << "Cross product:\n" << v.cross(w) << endl; +} diff --git a/eigen/doc/examples/tut_arithmetic_matrix_mul.cpp b/eigen/doc/examples/tut_arithmetic_matrix_mul.cpp new file mode 100644 index 0000000..f213902 --- /dev/null +++ b/eigen/doc/examples/tut_arithmetic_matrix_mul.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; +int main() +{ + Matrix2d mat; + mat << 1, 2, + 3, 4; + Vector2d u(-1,1), v(2,0); + std::cout << "Here is mat*mat:\n" << mat*mat << std::endl; + std::cout << "Here is mat*u:\n" << mat*u << std::endl; + std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl; + std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl; + std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl; + std::cout << "Let's multiply mat by itself" << std::endl; + mat = mat*mat; + std::cout << "Now mat is mat:\n" << mat << std::endl; +} diff --git a/eigen/doc/examples/tut_arithmetic_redux_basic.cpp b/eigen/doc/examples/tut_arithmetic_redux_basic.cpp new file mode 100644 index 0000000..5632fb5 --- /dev/null +++ b/eigen/doc/examples/tut_arithmetic_redux_basic.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace std; +int main() +{ + Eigen::Matrix2d mat; + mat << 1, 2, + 3, 4; + cout << "Here is mat.sum(): " << mat.sum() << endl; + cout << "Here is mat.prod(): " << mat.prod() << endl; + cout << "Here is mat.mean(): " << mat.mean() << endl; + cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl; + cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl; + cout << "Here is mat.trace(): " << mat.trace() << endl; +} diff --git a/eigen/doc/examples/tut_arithmetic_scalar_mul_div.cpp b/eigen/doc/examples/tut_arithmetic_scalar_mul_div.cpp new file mode 100644 index 0000000..d5f65b5 --- /dev/null +++ b/eigen/doc/examples/tut_arithmetic_scalar_mul_div.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; + +int main() +{ + Matrix2d a; + a << 1, 2, + 3, 4; + Vector3d v(1,2,3); + std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl; + std::cout << "0.1 * v =\n" << 0.1 * v << std::endl; + std::cout << "Doing v *= 2;" << std::endl; + v *= 2; + std::cout << "Now v =\n" << v << std::endl; +} diff --git a/eigen/doc/examples/tut_matrix_coefficient_accessors.cpp b/eigen/doc/examples/tut_matrix_coefficient_accessors.cpp new file mode 100644 index 0000000..c2da171 --- /dev/null +++ b/eigen/doc/examples/tut_matrix_coefficient_accessors.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; + +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 << "Here is the matrix m:\n" << m << std::endl; + VectorXd v(2); + v(0) = 4; + v(1) = v(0) - 1; + std::cout << "Here is the vector v:\n" << v << std::endl; +} diff --git a/eigen/doc/examples/tut_matrix_resize.cpp b/eigen/doc/examples/tut_matrix_resize.cpp new file mode 100644 index 0000000..0392c3a --- /dev/null +++ b/eigen/doc/examples/tut_matrix_resize.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; + +int main() +{ + MatrixXd m(2,5); + m.resize(4,3); + std::cout << "The matrix m is of size " + << m.rows() << "x" << m.cols() << std::endl; + std::cout << "It has " << m.size() << " coefficients" << std::endl; + VectorXd v(2); + v.resize(5); + std::cout << "The vector v is of size " << v.size() << std::endl; + std::cout << "As a matrix, v is of size " + << v.rows() << "x" << v.cols() << std::endl; +} diff --git a/eigen/doc/examples/tut_matrix_resize_fixed_size.cpp b/eigen/doc/examples/tut_matrix_resize_fixed_size.cpp new file mode 100644 index 0000000..dcbdfa7 --- /dev/null +++ b/eigen/doc/examples/tut_matrix_resize_fixed_size.cpp @@ -0,0 +1,12 @@ +#include <iostream> +#include <Eigen/Dense> + +using namespace Eigen; + +int main() +{ + Matrix4d m; + m.resize(4,4); // no operation + std::cout << "The matrix m is of size " + << m.rows() << "x" << m.cols() << std::endl; +} |