get rid of using namespace Eigen in sample code

This commit is contained in:
Erik Schultheis 2021-12-07 19:57:38 +00:00 committed by Rasmus Munk Larsen
parent e4c40b092a
commit 39a6aff16c
58 changed files with 274 additions and 399 deletions

View File

@ -1,9 +1,8 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <unsupported/Eigen/SpecialFunctions> #include <unsupported/Eigen/SpecialFunctions>
#include <iostream> #include <iostream>
using namespace Eigen;
int main() int main()
{ {
Array4d v(-0.5,2,0,-7); Eigen::Array4d v(-0.5,2,0,-7);
std::cout << v.erf() << std::endl; std::cout << v.erf() << std::endl;
} }

View File

@ -1,9 +1,8 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <unsupported/Eigen/SpecialFunctions> #include <unsupported/Eigen/SpecialFunctions>
#include <iostream> #include <iostream>
using namespace Eigen;
int main() int main()
{ {
Array4d v(-0.5,2,0,-7); Eigen::Array4d v(-0.5,2,0,-7);
std::cout << v.erfc() << std::endl; std::cout << v.erfc() << std::endl;
} }

View File

@ -1,9 +1,8 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <unsupported/Eigen/SpecialFunctions> #include <unsupported/Eigen/SpecialFunctions>
#include <iostream> #include <iostream>
using namespace Eigen;
int main() int main()
{ {
Array4d v(0.5,10,0,-1); Eigen::Array4d v(0.5,10,0,-1);
std::cout << v.lgamma() << std::endl; std::cout << v.lgamma() << std::endl;
} }

View File

@ -1,15 +1,12 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen; int main()
using namespace std;
int main(void)
{ {
int const N = 5; int const N = 5;
MatrixXi A(N,N); Eigen::MatrixXi A(N,N);
A.setRandom(); A.setRandom();
cout << "A =\n" << A << '\n' << endl; std::cout << "A =\n" << A << '\n' << std::endl;
cout << "A(1..3,:) =\n" << A.middleCols(1,3) << endl; std::cout << "A(1..3,:) =\n" << A.middleCols(1,3) << std::endl;
return 0; return 0;
} }

View File

@ -1,15 +1,12 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen; int main()
using namespace std;
int main(void)
{ {
int const N = 5; int const N = 5;
MatrixXi A(N,N); Eigen::MatrixXi A(N,N);
A.setRandom(); A.setRandom();
cout << "A =\n" << A << '\n' << endl; std::cout << "A =\n" << A << '\n' << std::endl;
cout << "A(2..3,:) =\n" << A.middleRows(2,2) << endl; std::cout << "A(2..3,:) =\n" << A.middleRows(2,2) << std::endl;
return 0; return 0;
} }

View File

@ -1,15 +1,12 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen; int main()
using namespace std;
int main(void)
{ {
int const N = 5; int const N = 5;
MatrixXi A(N,N); Eigen::MatrixXi A(N,N);
A.setRandom(); A.setRandom();
cout << "A =\n" << A << '\n' << endl; std::cout << "A =\n" << A << '\n' << std::endl;
cout << "A(:,1..3) =\n" << A.middleCols<3>(1) << endl; std::cout << "A(:,1..3) =\n" << A.middleCols<3>(1) << std::endl;
return 0; return 0;
} }

View File

@ -1,15 +1,12 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen; int main()
using namespace std;
int main(void)
{ {
int const N = 5; int const N = 5;
MatrixXi A(N,N); Eigen::MatrixXi A(N,N);
A.setRandom(); A.setRandom();
cout << "A =\n" << A << '\n' << endl; std::cout << "A =\n" << A << '\n' << std::endl;
cout << "A(1..3,:) =\n" << A.middleRows<3>(1) << endl; std::cout << "A(1..3,:) =\n" << A.middleRows<3>(1) << std::endl;
return 0; return 0;
} }

View File

@ -1,15 +1,15 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen; using Eigen::MatrixXd;
using namespace std; using Eigen::VectorXd;
int main() int main()
{ {
MatrixXd m = MatrixXd::Random(3,3); MatrixXd m = MatrixXd::Random(3,3);
m = (m + MatrixXd::Constant(3,3,1.2)) * 50; m = (m + MatrixXd::Constant(3,3,1.2)) * 50;
cout << "m =" << endl << m << endl; std::cout << "m =" << std::endl << m << std::endl;
VectorXd v(3); VectorXd v(3);
v << 1, 2, 3; v << 1, 2, 3;
cout << "m * v =" << endl << m * v << endl; std::cout << "m * v =" << std::endl << m * v << std::endl;
} }

View File

@ -1,15 +1,15 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen; using Eigen::Matrix3d;
using namespace std; using Eigen::Vector3d;
int main() int main()
{ {
Matrix3d m = Matrix3d::Random(); Matrix3d m = Matrix3d::Random();
m = (m + Matrix3d::Constant(1.2)) * 50; m = (m + Matrix3d::Constant(1.2)) * 50;
cout << "m =" << endl << m << endl; std::cout << "m =" << std::endl << m << std::endl;
Vector3d v(1,2,3); Vector3d v(1,2,3);
cout << "m * v =" << endl << m * v << endl; std::cout << "m * v =" << std::endl << m * v << std::endl;
} }

View File

@ -1,19 +1,17 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen;
template <typename Derived1, typename Derived2> template <typename Derived1, typename Derived2>
void copyUpperTriangularPart(MatrixBase<Derived1>& dst, const MatrixBase<Derived2>& src) void copyUpperTriangularPart(Eigen::MatrixBase<Derived1>& dst, const Eigen::MatrixBase<Derived2>& src)
{ {
/* Note the 'template' keywords in the following line! */ /* Note the 'template' keywords in the following line! */
dst.template triangularView<Upper>() = src.template triangularView<Upper>(); dst.template triangularView<Eigen::Upper>() = src.template triangularView<Eigen::Upper>();
} }
int main() int main()
{ {
MatrixXi m1 = MatrixXi::Ones(5,5); Eigen::MatrixXi m1 = Eigen::MatrixXi::Ones(5,5);
MatrixXi m2 = MatrixXi::Random(4,4); Eigen::MatrixXi m2 = Eigen::MatrixXi::Random(4,4);
std::cout << "m2 before copy:" << std::endl; std::cout << "m2 before copy:" << std::endl;
std::cout << m2 << std::endl << std::endl; std::cout << m2 << std::endl << std::endl;
copyUpperTriangularPart(m2, m1.topLeftCorner(4,4)); copyUpperTriangularPart(m2, m1.topLeftCorner(4,4));

View File

@ -1,11 +1,11 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen; using Eigen::MatrixXf;
void copyUpperTriangularPart(MatrixXf& dst, const MatrixXf& src) void copyUpperTriangularPart(MatrixXf& dst, const MatrixXf& src)
{ {
dst.triangularView<Upper>() = src.triangularView<Upper>(); dst.triangularView<Eigen::Upper>() = src.triangularView<Eigen::Upper>();
} }
int main() int main()

View File

@ -1,61 +1,57 @@
#include <iostream> #include <iostream>
struct init { struct init {
init() { std::cout << "[" << "init" << "]" << std::endl; } init() { std::cout << "[init]\n"; }
}; };
init init_obj; init init_obj;
// [init] // [init]
#include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
MatrixXd A(2,2); Eigen::MatrixXd A(2,2);
A << 2, -1, 1, 3; A << 2, -1, 1, 3;
cout << "Here is the input matrix A before decomposition:\n" << A << endl; std::cout << "Here is the input matrix A before decomposition:\n" << A << "\n";
cout << "[init]" << endl; std::cout << "[init]\n";
cout << "[declaration]" << endl; std::cout << "[declaration]\n";
PartialPivLU<Ref<MatrixXd> > lu(A); Eigen::PartialPivLU<Eigen::Ref<Eigen::MatrixXd> > lu(A);
cout << "Here is the input matrix A after decomposition:\n" << A << endl; std::cout << "Here is the input matrix A after decomposition:\n" << A << "\n";
cout << "[declaration]" << endl; std::cout << "[declaration]\n";
cout << "[matrixLU]" << endl; std::cout << "[matrixLU]\n";
cout << "Here is the matrix storing the L and U factors:\n" << lu.matrixLU() << endl; std::cout << "Here is the matrix storing the L and U factors:\n" << lu.matrixLU() << "\n";
cout << "[matrixLU]" << endl; std::cout << "[matrixLU]\n";
cout << "[solve]" << endl; std::cout << "[solve]\n";
MatrixXd A0(2,2); A0 << 2, -1, 1, 3; Eigen::MatrixXd A0(2,2); A0 << 2, -1, 1, 3;
VectorXd b(2); b << 1, 2; Eigen::VectorXd b(2); b << 1, 2;
VectorXd x = lu.solve(b); Eigen::VectorXd x = lu.solve(b);
cout << "Residual: " << (A0 * x - b).norm() << endl; std::cout << "Residual: " << (A0 * x - b).norm() << "\n";
cout << "[solve]" << endl; std::cout << "[solve]\n";
cout << "[modifyA]" << endl; std::cout << "[modifyA]\n";
A << 3, 4, -2, 1; A << 3, 4, -2, 1;
x = lu.solve(b); x = lu.solve(b);
cout << "Residual: " << (A0 * x - b).norm() << endl; std::cout << "Residual: " << (A0 * x - b).norm() << "\n";
cout << "[modifyA]" << endl; std::cout << "[modifyA]\n";
cout << "[recompute]" << endl; std::cout << "[recompute]\n";
A0 = A; // save A A0 = A; // save A
lu.compute(A); lu.compute(A);
x = lu.solve(b); x = lu.solve(b);
cout << "Residual: " << (A0 * x - b).norm() << endl; std::cout << "Residual: " << (A0 * x - b).norm() << "\n";
cout << "[recompute]" << endl; std::cout << "[recompute]\n";
cout << "[recompute_bis0]" << endl; std::cout << "[recompute_bis0]\n";
MatrixXd A1(2,2); Eigen::MatrixXd A1(2,2);
A1 << 5,-2,3,4; A1 << 5,-2,3,4;
lu.compute(A1); lu.compute(A1);
cout << "Here is the input matrix A1 after decomposition:\n" << A1 << endl; std::cout << "Here is the input matrix A1 after decomposition:\n" << A1 << "\n";
cout << "[recompute_bis0]" << endl; std::cout << "[recompute_bis0]\n";
cout << "[recompute_bis1]" << endl; std::cout << "[recompute_bis1]\n";
x = lu.solve(b); x = lu.solve(b);
cout << "Residual: " << (A1 * x - b).norm() << endl; std::cout << "Residual: " << (A1 * x - b).norm() << "\n";
cout << "[recompute_bis1]" << endl; std::cout << "[recompute_bis1]\n";
} }

View File

@ -1,23 +1,20 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix2f A, b; Eigen::Matrix2f A, b;
LLT<Matrix2f> llt; Eigen::LLT<Eigen::Matrix2f> llt;
A << 2, -1, -1, 3; A << 2, -1, -1, 3;
b << 1, 2, 3, 1; b << 1, 2, 3, 1;
cout << "Here is the matrix A:\n" << A << endl; std::cout << "Here is the matrix A:\n" << A << std::endl;
cout << "Here is the right hand side b:\n" << b << endl; std::cout << "Here is the right hand side b:\n" << b << std::endl;
cout << "Computing LLT decomposition..." << endl; std::cout << "Computing LLT decomposition..." << std::endl;
llt.compute(A); llt.compute(A);
cout << "The solution is:\n" << llt.solve(b) << endl; std::cout << "The solution is:\n" << llt.solve(b) << std::endl;
A(1,1)++; A(1,1)++;
cout << "The matrix A is now:\n" << A << endl; std::cout << "The matrix A is now:\n" << A << std::endl;
cout << "Computing LLT decomposition..." << endl; std::cout << "Computing LLT decomposition..." << std::endl;
llt.compute(A); llt.compute(A);
cout << "The solution is now:\n" << llt.solve(b) << endl; std::cout << "The solution is now:\n" << llt.solve(b) << std::endl;
} }

View File

@ -1,8 +1,7 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std; using Eigen::MatrixXd;
using namespace Eigen;
int main() int main()
{ {
@ -10,5 +9,5 @@ int main()
MatrixXd b = MatrixXd::Random(100,50); MatrixXd b = MatrixXd::Random(100,50);
MatrixXd x = A.fullPivLu().solve(b); MatrixXd x = A.fullPivLu().solve(b);
double relative_error = (A*x - b).norm() / b.norm(); // norm() is L2 norm double relative_error = (A*x - b).norm() / b.norm(); // norm() is L2 norm
cout << "The relative error is:\n" << relative_error << endl; std::cout << "The relative error is:\n" << relative_error << std::endl;
} }

View File

@ -1,17 +1,14 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix3f A; Eigen::Matrix3f A;
Vector3f b; Eigen::Vector3f b;
A << 1,2,3, 4,5,6, 7,8,10; A << 1,2,3, 4,5,6, 7,8,10;
b << 3, 3, 4; b << 3, 3, 4;
cout << "Here is the matrix A:\n" << A << endl; std::cout << "Here is the matrix A:\n" << A << std::endl;
cout << "Here is the vector b:\n" << b << endl; std::cout << "Here is the vector b:\n" << b << std::endl;
Vector3f x = A.colPivHouseholderQr().solve(b); Eigen::Vector3f x = A.colPivHouseholderQr().solve(b);
cout << "The solution is:\n" << x << endl; std::cout << "The solution is:\n" << x << std::endl;
} }

View File

@ -1,16 +1,13 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix2f A, b; Eigen::Matrix2f A, b;
A << 2, -1, -1, 3; A << 2, -1, -1, 3;
b << 1, 2, 3, 1; b << 1, 2, 3, 1;
cout << "Here is the matrix A:\n" << A << endl; std::cout << "Here is the matrix A:\n" << A << std::endl;
cout << "Here is the right hand side b:\n" << b << endl; std::cout << "Here is the right hand side b:\n" << b << std::endl;
Matrix2f x = A.ldlt().solve(b); Eigen::Matrix2f x = A.ldlt().solve(b);
cout << "The solution is:\n" << x << endl; std::cout << "The solution is:\n" << x << std::endl;
} }

View File

@ -1,16 +1,13 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix3f A; Eigen::Matrix3f A;
A << 1, 2, 1, A << 1, 2, 1,
2, 1, 0, 2, 1, 0,
-1, 1, 2; -1, 1, 2;
cout << "Here is the matrix A:\n" << A << endl; std::cout << "Here is the matrix A:\n" << A << std::endl;
cout << "The determinant of A is " << A.determinant() << endl; std::cout << "The determinant of A is " << A.determinant() << std::endl;
cout << "The inverse of A is:\n" << A.inverse() << endl; std::cout << "The inverse of A is:\n" << A.inverse() << std::endl;
} }

View File

@ -1,20 +1,17 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix3f A; Eigen::Matrix3f A;
A << 1, 2, 5, A << 1, 2, 5,
2, 1, 4, 2, 1, 4,
3, 0, 3; 3, 0, 3;
cout << "Here is the matrix A:\n" << A << endl; std::cout << "Here is the matrix A:\n" << A << std::endl;
FullPivLU<Matrix3f> lu_decomp(A); Eigen::FullPivLU<Eigen::Matrix3f> lu_decomp(A);
cout << "The rank of A is " << lu_decomp.rank() << endl; std::cout << "The rank of A is " << lu_decomp.rank() << std::endl;
cout << "Here is a matrix whose columns form a basis of the null-space of A:\n" std::cout << "Here is a matrix whose columns form a basis of the null-space of A:\n"
<< lu_decomp.kernel() << endl; << lu_decomp.kernel() << std::endl;
cout << "Here is a matrix whose columns form a basis of the column-space of A:\n" std::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 << lu_decomp.image(A) << std::endl; // yes, have to pass the original A
} }

View File

@ -1,15 +1,12 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
MatrixXf A = MatrixXf::Random(3, 2); Eigen::MatrixXf A = Eigen::MatrixXf::Random(3, 2);
cout << "Here is the matrix A:\n" << A << endl; std::cout << "Here is the matrix A:\n" << A << std::endl;
VectorXf b = VectorXf::Random(3); Eigen::VectorXf b = Eigen::VectorXf::Random(3);
cout << "Here is the right hand side b:\n" << b << endl; std::cout << "Here is the right hand side b:\n" << b << std::endl;
cout << "The least-squares solution is:\n" std::cout << "The least-squares solution is:\n"
<< A.bdcSvd(ComputeThinU | ComputeThinV).solve(b) << endl; << A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b) << std::endl;
} }

View File

@ -1,18 +1,15 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix2f A; Eigen::Matrix2f A;
A << 1, 2, 2, 3; A << 1, 2, 2, 3;
cout << "Here is the matrix A:\n" << A << endl; std::cout << "Here is the matrix A:\n" << A << std::endl;
SelfAdjointEigenSolver<Matrix2f> eigensolver(A); Eigen::SelfAdjointEigenSolver<Eigen::Matrix2f> eigensolver(A);
if (eigensolver.info() != Success) abort(); if (eigensolver.info() != Eigen::Success) abort();
cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl; std::cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << std::endl;
cout << "Here's a matrix whose columns are eigenvectors of A \n" std::cout << "Here's a matrix whose columns are eigenvectors of A \n"
<< "corresponding to these eigenvalues:\n" << "corresponding to these eigenvalues:\n"
<< eigensolver.eigenvectors() << endl; << eigensolver.eigenvectors() << std::endl;
} }

View File

@ -1,16 +1,13 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix2d A; Eigen::Matrix2d A;
A << 2, 1, A << 2, 1,
2, 0.9999999999; 2, 0.9999999999;
FullPivLU<Matrix2d> lu(A); Eigen::FullPivLU<Eigen::Matrix2d> lu(A);
cout << "By default, the rank of A is found to be " << lu.rank() << endl; std::cout << "By default, the rank of A is found to be " << lu.rank() << std::endl;
lu.setThreshold(1e-5); lu.setThreshold(1e-5);
cout << "With threshold 1e-5, the rank of A is found to be " << lu.rank() << endl; std::cout << "With threshold 1e-5, the rank of A is found to be " << lu.rank() << std::endl;
} }

View File

@ -1,24 +1,21 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
int main() int main()
{ {
ArrayXXf m(2,2); Eigen::ArrayXXf m(2,2);
// assign some values coefficient by coefficient // assign some values coefficient by coefficient
m(0,0) = 1.0; m(0,1) = 2.0; m(0,0) = 1.0; m(0,1) = 2.0;
m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0); m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);
// print values to standard output // print values to standard output
cout << m << endl << endl; std::cout << m << std::endl << std::endl;
// using the comma-initializer is also allowed // using the comma-initializer is also allowed
m << 1.0,2.0, m << 1.0,2.0,
3.0,4.0; 3.0,4.0;
// print values to standard output // print values to standard output
cout << m << endl; std::cout << m << std::endl;
} }

View File

@ -1,13 +1,10 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
int main() int main()
{ {
ArrayXXf a(3,3); Eigen::ArrayXXf a(3,3);
ArrayXXf b(3,3); Eigen::ArrayXXf b(3,3);
a << 1,2,3, a << 1,2,3,
4,5,6, 4,5,6,
7,8,9; 7,8,9;
@ -16,8 +13,8 @@ int main()
1,2,3; 1,2,3;
// Adding two arrays // Adding two arrays
cout << "a + b = " << endl << a + b << endl << endl; std::cout << "a + b = " << std::endl << a + b << std::endl << std::endl;
// Subtracting a scalar from an array // Subtracting a scalar from an array
cout << "a - 2 = " << endl << a - 2 << endl; std::cout << "a - 2 = " << std::endl << a - 2 << std::endl;
} }

View File

@ -1,19 +1,16 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
int main() int main()
{ {
ArrayXf a = ArrayXf::Random(5); Eigen::ArrayXf a = Eigen::ArrayXf::Random(5);
a *= 2; a *= 2;
cout << "a =" << endl std::cout << "a =" << std::endl
<< a << endl; << a << std::endl;
cout << "a.abs() =" << endl std::cout << "a.abs() =" << std::endl
<< a.abs() << endl; << a.abs() << std::endl;
cout << "a.abs().sqrt() =" << endl std::cout << "a.abs().sqrt() =" << std::endl
<< a.abs().sqrt() << endl; << a.abs().sqrt() << std::endl;
cout << "a.min(a.abs().sqrt()) =" << endl std::cout << "a.min(a.abs().sqrt()) =" << std::endl
<< a.min(a.abs().sqrt()) << endl; << a.min(a.abs().sqrt()) << std::endl;
} }

View File

@ -1,8 +1,7 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen; using Eigen::MatrixXf;
using namespace std;
int main() int main()
{ {
@ -16,7 +15,7 @@ int main()
7,8; 7,8;
result = (m.array() + 4).matrix() * m; result = (m.array() + 4).matrix() * m;
cout << "-- Combination 1: --" << endl << result << endl << endl; std::cout << "-- Combination 1: --\n" << result << "\n\n";
result = (m.array() * n.array()).matrix() * m; result = (m.array() * n.array()).matrix() * m;
cout << "-- Combination 2: --" << endl << result << endl << endl; std::cout << "-- Combination 2: --\n" << result << "\n\n";
} }

View File

@ -1,8 +1,7 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen; using Eigen::MatrixXf;
using namespace std;
int main() int main()
{ {
@ -16,11 +15,11 @@ int main()
7,8; 7,8;
result = m * n; result = m * n;
cout << "-- Matrix m*n: --" << endl << result << endl << endl; std::cout << "-- Matrix m*n: --\n" << result << "\n\n";
result = m.array() * n.array(); result = m.array() * n.array();
cout << "-- Array m*n: --" << endl << result << endl << endl; std::cout << "-- Array m*n: --\n" << result << "\n\n";
result = m.cwiseProduct(n); result = m.cwiseProduct(n);
cout << "-- With cwiseProduct: --" << endl << result << endl << endl; std::cout << "-- With cwiseProduct: --\n" << result << "\n\n";
result = m.array() + 4; result = m.array() + 4;
cout << "-- Array m + 4: --" << endl << result << endl << endl; std::cout << "-- Array m + 4: --\n" << result << "\n\n";
} }

View File

@ -1,16 +1,13 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
int main() int main()
{ {
ArrayXXf a(2,2); Eigen::ArrayXXf a(2,2);
ArrayXXf b(2,2); Eigen::ArrayXXf b(2,2);
a << 1,2, a << 1,2,
3,4; 3,4;
b << 5,6, b << 5,6,
7,8; 7,8;
cout << "a * b = " << endl << a * b << endl; std::cout << "a * b = " << std::endl << a * b << std::endl;
} }

View File

@ -1,18 +1,15 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Array22f m; Eigen::Array22f m;
m << 1,2, m << 1,2,
3,4; 3,4;
Array44f a = Array44f::Constant(0.6); Eigen::Array44f a = Eigen::Array44f::Constant(0.6);
cout << "Here is the array a:" << endl << a << endl << endl; std::cout << "Here is the array a:\n" << a << "\n\n";
a.block<2,2>(1,1) = m; a.block<2,2>(1,1) = m;
cout << "Here is now a with m copied into its central 2x2 block:" << endl << a << endl << endl; std::cout << "Here is now a with m copied into its central 2x2 block:\n" << a << "\n\n";
a.block(0,0,2,3) = a.block(2,1,2,3); 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 2x3 block:" << endl << a << endl << endl; std::cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:\n" << a << "\n\n";
} }

View File

@ -2,17 +2,14 @@
#include <Eigen/LU> #include <Eigen/LU>
#include <iostream> #include <iostream>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Matrix3f A; Eigen::Matrix3f A;
Vector3f b; Eigen::Vector3f b;
A << 1,2,3, 4,5,6, 7,8,10; A << 1,2,3, 4,5,6, 7,8,10;
b << 3, 3, 4; b << 3, 3, 4;
cout << "Here is the matrix A:" << endl << A << endl; std::cout << "Here is the matrix A:" << std::endl << A << std::endl;
cout << "Here is the vector b:" << endl << b << endl; std::cout << "Here is the vector b:" << std::endl << b << std::endl;
Vector3f x = A.lu().solve(b); Eigen::Vector3f x = A.lu().solve(b);
cout << "The solution is:" << endl << x << endl; std::cout << "The solution is:" << std::endl << x << std::endl;
} }

View File

@ -1,9 +1,6 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Eigen::MatrixXf m(2,4); Eigen::MatrixXf m(2,4);
@ -15,10 +12,10 @@ int main()
v << 2, v << 2,
3; 3;
MatrixXf::Index index; Eigen::Index index;
// find nearest neighbour // find nearest neighbour
(m.colwise() - v).colwise().squaredNorm().minCoeff(&index); (m.colwise() - v).colwise().squaredNorm().minCoeff(&index);
cout << "Nearest neighbour is column " << index << ":" << endl; std::cout << "Nearest neighbour is column " << index << ":" << std::endl;
cout << m.col(index) << endl; std::cout << m.col(index) << std::endl;
} }

View File

@ -1,15 +1,13 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
MatrixXf mat(2,4); Eigen::MatrixXf mat(2,4);
mat << 1, 2, 6, 9, mat << 1, 2, 6, 9,
3, 1, 7, 2; 3, 1, 7, 2;
MatrixXf::Index maxIndex; Eigen::Index maxIndex;
float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex); float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex);
std::cout << "Maximum sum at position " << maxIndex << std::endl; std::cout << "Maximum sum at position " << maxIndex << std::endl;

View File

@ -1,21 +1,18 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
ArrayXXf a(2,2); Eigen::ArrayXXf a(2,2);
a << 1,2, a << 1,2,
3,4; 3,4;
cout << "(a > 0).all() = " << (a > 0).all() << endl; std::cout << "(a > 0).all() = " << (a > 0).all() << std::endl;
cout << "(a > 0).any() = " << (a > 0).any() << endl; std::cout << "(a > 0).any() = " << (a > 0).any() << std::endl;
cout << "(a > 0).count() = " << (a > 0).count() << endl; std::cout << "(a > 0).count() = " << (a > 0).count() << std::endl;
cout << endl; std::cout << std::endl;
cout << "(a > 2).all() = " << (a > 2).all() << endl; std::cout << "(a > 2).all() = " << (a > 2).all() << std::endl;
cout << "(a > 2).any() = " << (a > 2).any() << endl; std::cout << "(a > 2).any() = " << (a > 2).any() << std::endl;
cout << "(a > 2).count() = " << (a > 2).count() << endl; std::cout << "(a > 2).count() = " << (a > 2).count() << std::endl;
} }

View File

@ -1,13 +1,10 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
VectorXf v(2); Eigen::VectorXf v(2);
MatrixXf m(2,2), n(2,2); Eigen::MatrixXf m(2,2), n(2,2);
v << -1, v << -1,
2; 2;
@ -15,14 +12,14 @@ int main()
m << 1,-2, m << 1,-2,
-3,4; -3,4;
cout << "v.squaredNorm() = " << v.squaredNorm() << endl; std::cout << "v.squaredNorm() = " << v.squaredNorm() << std::endl;
cout << "v.norm() = " << v.norm() << endl; std::cout << "v.norm() = " << v.norm() << std::endl;
cout << "v.lpNorm<1>() = " << v.lpNorm<1>() << endl; std::cout << "v.lpNorm<1>() = " << v.lpNorm<1>() << std::endl;
cout << "v.lpNorm<Infinity>() = " << v.lpNorm<Infinity>() << endl; std::cout << "v.lpNorm<Infinity>() = " << v.lpNorm<Eigen::Infinity>() << std::endl;
cout << endl; std::cout << std::endl;
cout << "m.squaredNorm() = " << m.squaredNorm() << endl; std::cout << "m.squaredNorm() = " << m.squaredNorm() << std::endl;
cout << "m.norm() = " << m.norm() << endl; std::cout << "m.norm() = " << m.norm() << std::endl;
cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << endl; std::cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << std::endl;
cout << "m.lpNorm<Infinity>() = " << m.lpNorm<Infinity>() << endl; std::cout << "m.lpNorm<Infinity>() = " << m.lpNorm<Eigen::Infinity>() << std::endl;
} }

View File

@ -1,18 +1,15 @@
#include <Eigen/Dense> #include <Eigen/Dense>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
int main() int main()
{ {
MatrixXf m(2,2); Eigen::MatrixXf m(2,2);
m << 1,-2, m << 1,-2,
-3,4; -3,4;
cout << "1-norm(m) = " << m.cwiseAbs().colwise().sum().maxCoeff() std::cout << "1-norm(m) = " << m.cwiseAbs().colwise().sum().maxCoeff()
<< " == " << m.colwise().lpNorm<1>().maxCoeff() << endl; << " == " << m.colwise().lpNorm<1>().maxCoeff() << std::endl;
cout << "infty-norm(m) = " << m.cwiseAbs().rowwise().sum().maxCoeff() std::cout << "infty-norm(m) = " << m.cwiseAbs().rowwise().sum().maxCoeff()
<< " == " << m.rowwise().lpNorm<1>().maxCoeff() << endl; << " == " << m.rowwise().lpNorm<1>().maxCoeff() << std::endl;
} }

View File

@ -1,9 +1,6 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main() int main()
{ {
Eigen::MatrixXf m(2,2); Eigen::MatrixXf m(2,2);
@ -12,15 +9,15 @@ int main()
3, 4; 3, 4;
//get location of maximum //get location of maximum
MatrixXf::Index maxRow, maxCol; Eigen::Index maxRow, maxCol;
float max = m.maxCoeff(&maxRow, &maxCol); float max = m.maxCoeff(&maxRow, &maxCol);
//get location of minimum //get location of minimum
MatrixXf::Index minRow, minCol; Eigen::Index minRow, minCol;
float min = m.minCoeff(&minRow, &minCol); float min = m.minCoeff(&minRow, &minCol);
cout << "Max: " << max << ", at: " << std::cout << "Max: " << max << ", at: " <<
maxRow << "," << maxCol << endl; maxRow << "," << maxCol << std::endl;
cout << "Min: " << min << ", at: " << std:: cout << "Min: " << min << ", at: " <<
minRow << "," << minCol << endl; minRow << "," << minCol << std::endl;
} }

View File

@ -1,13 +1,11 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
int main() int main()
{ {
for (int size=1; size<=4; ++size) for (int size=1; size<=4; ++size)
{ {
MatrixXi m(size,size+1); // a (size)x(size+1)-matrix of int's Eigen::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 j=0; j<m.cols(); ++j) // loop over columns
for (int i=0; i<m.rows(); ++i) // loop over rows for (int i=0; i<m.rows(); ++i) // loop over rows
m(i,j) = i+j*size; // to access matrix coefficients, m(i,j) = i+j*size; // to access matrix coefficients,
@ -15,7 +13,7 @@ int main()
std::cout << m << "\n\n"; std::cout << m << "\n\n";
} }
VectorXf v(4); // a vector of 4 float's Eigen::VectorXf v(4); // a vector of 4 float's
// to access vector coefficients, use either operator () or operator [] // to access vector coefficients, use either operator () or operator []
v[0] = 1; v[1] = 2; v(2) = 3; v(3) = 4; v[0] = 1; v[1] = 2; v(2) = 3; v(3) = 4;
std::cout << "\nv:\n" << v << std::endl; std::cout << "\nv:\n" << v << std::endl;

View File

@ -1,14 +1,12 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
int main() int main()
{ {
Matrix3f m3; Eigen::Matrix3f m3;
m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9; m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
Matrix4f m4 = Matrix4f::Identity(); Eigen::Matrix4f m4 = Eigen::Matrix4f::Identity();
Vector4i v4(1, 2, 3, 4); Eigen::Vector4i v4(1, 2, 3, 4);
std::cout << "m3\n" << m3 << "\nm4:\n" std::cout << "m3\n" << m3 << "\nm4:\n"
<< m4 << "\nv4:\n" << v4 << std::endl; << m4 << "\nv4:\n" << v4 << std::endl;

View File

@ -1,27 +1,25 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
template<typename Derived> template<typename Derived>
Eigen::Block<Derived> Eigen::Block<Derived>
topLeftCorner(MatrixBase<Derived>& m, int rows, int cols) topLeftCorner(Eigen::MatrixBase<Derived>& m, int rows, int cols)
{ {
return Eigen::Block<Derived>(m.derived(), 0, 0, rows, cols); return Eigen::Block<Derived>(m.derived(), 0, 0, rows, cols);
} }
template<typename Derived> template<typename Derived>
const Eigen::Block<const Derived> const Eigen::Block<const Derived>
topLeftCorner(const MatrixBase<Derived>& m, int rows, int cols) topLeftCorner(const Eigen::MatrixBase<Derived>& m, int rows, int cols)
{ {
return Eigen::Block<const Derived>(m.derived(), 0, 0, rows, cols); return Eigen::Block<const Derived>(m.derived(), 0, 0, rows, cols);
} }
int main(int, char**) int main(int, char**)
{ {
Matrix4d m = Matrix4d::Identity(); Eigen::Matrix4d m = Eigen::Matrix4d::Identity();
cout << topLeftCorner(4*m, 2, 3) << endl; // calls the const version std::cout << topLeftCorner(4*m, 2, 3) << std::endl; // calls the const version
topLeftCorner(m, 2, 3) *= 5; // calls the non-const version topLeftCorner(m, 2, 3) *= 5; // calls the non-const version
cout << "Now the matrix m is:" << endl << m << endl; std::cout << "Now the matrix m is:" << std::endl << m << std::endl;
return 0; return 0;
} }

View File

@ -1,18 +1,18 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std; using Eigen::Matrix4d;
// define a custom template binary functor // define a custom template binary functor
template<typename Scalar> struct MakeComplexOp { template<typename Scalar> struct MakeComplexOp {
EIGEN_EMPTY_STRUCT_CTOR(MakeComplexOp) EIGEN_EMPTY_STRUCT_CTOR(MakeComplexOp)
typedef complex<Scalar> result_type; typedef std::complex<Scalar> result_type;
complex<Scalar> operator()(const Scalar& a, const Scalar& b) const { return complex<Scalar>(a,b); } result_type operator()(const Scalar& a, const Scalar& b) const { return result_type(a,b); }
}; };
int main(int, char**) int main(int, char**)
{ {
Matrix4d m1 = Matrix4d::Random(), m2 = Matrix4d::Random(); Matrix4d m1 = Matrix4d::Random(), m2 = Matrix4d::Random();
cout << m1.binaryExpr(m2, MakeComplexOp<double>()) << endl; std::cout << m1.binaryExpr(m2, MakeComplexOp<double>()) << std::endl;
return 0; return 0;
} }

View File

@ -1,7 +1,5 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
// define a custom template unary functor // define a custom template unary functor
template<typename Scalar> template<typename Scalar>
@ -13,7 +11,7 @@ struct CwiseClampOp {
int main(int, char**) int main(int, char**)
{ {
Matrix4d m1 = Matrix4d::Random(); Eigen::Matrix4d m1 = Eigen::Matrix4d::Random();
cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << endl; std::cout << m1 << std::endl << "becomes: " << std::endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << std::endl;
return 0; return 0;
} }

View File

@ -1,7 +1,5 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
// define function to be applied coefficient-wise // define function to be applied coefficient-wise
double ramp(double x) double ramp(double x)
@ -14,7 +12,7 @@ double ramp(double x)
int main(int, char**) int main(int, char**)
{ {
Matrix4d m1 = Matrix4d::Random(); Eigen::Matrix4d m1 = Eigen::Matrix4d::Random();
cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(ptr_fun(ramp)) << endl; std::cout << m1 << std::endl << "becomes: " << std::endl << m1.unaryExpr(std::ptr_fun(ramp)) << std::endl;
return 0; return 0;
} }

View File

@ -1,27 +1,25 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
template<typename Derived> template<typename Derived>
Eigen::Block<Derived, 2, 2> Eigen::Block<Derived, 2, 2>
topLeft2x2Corner(MatrixBase<Derived>& m) topLeft2x2Corner(Eigen::MatrixBase<Derived>& m)
{ {
return Eigen::Block<Derived, 2, 2>(m.derived(), 0, 0); return Eigen::Block<Derived, 2, 2>(m.derived(), 0, 0);
} }
template<typename Derived> template<typename Derived>
const Eigen::Block<const Derived, 2, 2> const Eigen::Block<const Derived, 2, 2>
topLeft2x2Corner(const MatrixBase<Derived>& m) topLeft2x2Corner(const Eigen::MatrixBase<Derived>& m)
{ {
return Eigen::Block<const Derived, 2, 2>(m.derived(), 0, 0); return Eigen::Block<const Derived, 2, 2>(m.derived(), 0, 0);
} }
int main(int, char**) int main(int, char**)
{ {
Matrix3d m = Matrix3d::Identity(); Eigen::Matrix3d m = Eigen::Matrix3d::Identity();
cout << topLeft2x2Corner(4*m) << endl; // calls the const version std::cout << topLeft2x2Corner(4*m) << std::endl; // calls the const version
topLeft2x2Corner(m) *= 2; // calls the non-const version topLeft2x2Corner(m) *= 2; // calls the non-const version
cout << "Now the matrix m is:" << endl << m << endl; std::cout << "Now the matrix m is:" << std::endl << m << std::endl;
return 0; return 0;
} }

View File

@ -1,22 +1,20 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
template<typename Derived> template<typename Derived>
Eigen::Reshaped<Derived, 4, 2> Eigen::Reshaped<Derived, 4, 2>
reshape_helper(MatrixBase<Derived>& m) reshape_helper(Eigen::MatrixBase<Derived>& m)
{ {
return Eigen::Reshaped<Derived, 4, 2>(m.derived()); return Eigen::Reshaped<Derived, 4, 2>(m.derived());
} }
int main(int, char**) int main(int, char**)
{ {
MatrixXd m(2, 4); Eigen::MatrixXd m(2, 4);
m << 1, 2, 3, 4, m << 1, 2, 3, 4,
5, 6, 7, 8; 5, 6, 7, 8;
MatrixXd n = reshape_helper(m); Eigen::MatrixXd n = reshape_helper(m);
cout << "matrix m is:" << endl << m << endl; std::cout << "matrix m is:" << std::endl << m << std::endl;
cout << "matrix n is:" << endl << n << endl; std::cout << "matrix n is:" << std::endl << n << std::endl;
return 0; return 0;
} }

View File

@ -1,27 +1,25 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
template<typename Derived> template<typename Derived>
Eigen::VectorBlock<Derived, 2> Eigen::VectorBlock<Derived, 2>
firstTwo(MatrixBase<Derived>& v) firstTwo(Eigen::MatrixBase<Derived>& v)
{ {
return Eigen::VectorBlock<Derived, 2>(v.derived(), 0); return Eigen::VectorBlock<Derived, 2>(v.derived(), 0);
} }
template<typename Derived> template<typename Derived>
const Eigen::VectorBlock<const Derived, 2> const Eigen::VectorBlock<const Derived, 2>
firstTwo(const MatrixBase<Derived>& v) firstTwo(const Eigen::MatrixBase<Derived>& v)
{ {
return Eigen::VectorBlock<const Derived, 2>(v.derived(), 0); return Eigen::VectorBlock<const Derived, 2>(v.derived(), 0);
} }
int main(int, char**) int main(int, char**)
{ {
Matrix<int,1,6> v; v << 1,2,3,4,5,6; Eigen::Matrix<int,1,6> v; v << 1,2,3,4,5,6;
cout << firstTwo(4*v) << endl; // calls the const version std::cout << firstTwo(4*v) << std::endl; // calls the const version
firstTwo(v) *= 2; // calls the non-const version firstTwo(v) *= 2; // calls the non-const version
cout << "Now the vector v is:" << endl << v << endl; std::cout << "Now the vector v is:" << std::endl << v << std::endl;
return 0; return 0;
} }

View File

@ -1,23 +1,21 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace std;
using namespace Eigen;
template<typename Derived> template<typename Derived>
const Reshaped<const Derived> const Eigen::Reshaped<const Derived>
reshape_helper(const MatrixBase<Derived>& m, int rows, int cols) reshape_helper(const Eigen::MatrixBase<Derived>& m, int rows, int cols)
{ {
return Reshaped<const Derived>(m.derived(), rows, cols); return Eigen::Reshaped<const Derived>(m.derived(), rows, cols);
} }
int main(int, char**) int main(int, char**)
{ {
MatrixXd m(3, 4); Eigen::MatrixXd m(3, 4);
m << 1, 4, 7, 10, m << 1, 4, 7, 10,
2, 5, 8, 11, 2, 5, 8, 11,
3, 6, 9, 12; 3, 6, 9, 12;
cout << m << endl; std::cout << m << std::endl;
Ref<const MatrixXd> n = reshape_helper(m, 2, 6); Eigen::Ref<const Eigen::MatrixXd> n = reshape_helper(m, 2, 6);
cout << "Matrix m is:" << endl << m << endl; std::cout << "Matrix m is:" << std::endl << m << std::endl;
cout << "Matrix n is:" << endl << n << endl; std::cout << "Matrix n is:" << std::endl << n << std::endl;
} }

View File

@ -1,27 +1,25 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
using namespace std;
template<typename Derived> template<typename Derived>
Eigen::VectorBlock<Derived> Eigen::VectorBlock<Derived>
segmentFromRange(MatrixBase<Derived>& v, int start, int end) segmentFromRange(Eigen::MatrixBase<Derived>& v, int start, int end)
{ {
return Eigen::VectorBlock<Derived>(v.derived(), start, end-start); return Eigen::VectorBlock<Derived>(v.derived(), start, end-start);
} }
template<typename Derived> template<typename Derived>
const Eigen::VectorBlock<const Derived> const Eigen::VectorBlock<const Derived>
segmentFromRange(const MatrixBase<Derived>& v, int start, int end) segmentFromRange(const Eigen::MatrixBase<Derived>& v, int start, int end)
{ {
return Eigen::VectorBlock<const Derived>(v.derived(), start, end-start); return Eigen::VectorBlock<const Derived>(v.derived(), start, end-start);
} }
int main(int, char**) int main(int, char**)
{ {
Matrix<int,1,6> v; v << 1,2,3,4,5,6; Eigen::Matrix<int,1,6> v; v << 1,2,3,4,5,6;
cout << segmentFromRange(2*v, 2, 4) << endl; // calls the const version std::cout << segmentFromRange(2*v, 2, 4) << std::endl; // calls the const version
segmentFromRange(v, 1, 3) *= 5; // calls the non-const version segmentFromRange(v, 1, 3) *= 5; // calls the non-const version
cout << "Now the vector v is:" << endl << v << endl; std::cout << "Now the vector v is:" << std::endl << v << std::endl;
return 0; return 0;
} }

View File

@ -1,9 +1,8 @@
#include <iostream> #include <iostream>
#include <Eigen/Core> #include <Eigen/Core>
using namespace Eigen;
template <typename Derived> template <typename Derived>
void print_size(const EigenBase<Derived>& b) void print_size(const Eigen::EigenBase<Derived>& b)
{ {
std::cout << "size (rows, cols): " << b.size() << " (" << b.rows() std::cout << "size (rows, cols): " << b.size() << " (" << b.rows()
<< ", " << b.cols() << ")" << std::endl; << ", " << b.cols() << ")" << std::endl;
@ -11,7 +10,7 @@ void print_size(const EigenBase<Derived>& b)
int main() int main()
{ {
Vector3f v; Eigen::Vector3f v;
print_size(v); print_size(v);
// v.asDiagonal() returns a 3x3 diagonal matrix pseudo-expression // v.asDiagonal() returns a 3x3 diagonal matrix pseudo-expression
print_size(v.asDiagonal()); print_size(v.asDiagonal());

View File

@ -1,19 +1,17 @@
#include <iostream> #include <iostream>
#include <Eigen/SVD> #include <Eigen/SVD>
using namespace Eigen;
using namespace std;
float inv_cond(const Ref<const MatrixXf>& a) float inv_cond(const Eigen::Ref<const Eigen::MatrixXf>& a)
{ {
const VectorXf sing_vals = a.jacobiSvd().singularValues(); const Eigen::VectorXf sing_vals = a.jacobiSvd().singularValues();
return sing_vals(sing_vals.size()-1) / sing_vals(0); return sing_vals(sing_vals.size()-1) / sing_vals(0);
} }
int main() int main()
{ {
Matrix4f m = Matrix4f::Random(); Eigen::MatrixXf m = Eigen::MatrixXf::Random(4, 4);
cout << "matrix m:" << endl << m << endl << endl; std::cout << "matrix m:\n" << m << "\n\n";
cout << "inv_cond(m): " << inv_cond(m) << endl; std::cout << "inv_cond(m): " << inv_cond(m) << "\n";
cout << "inv_cond(m(1:3,1:3)): " << inv_cond(m.topLeftCorner(3,3)) << endl; std::cout << "inv_cond(m(1:3,1:3)): " << inv_cond(m.topLeftCorner(3,3)) << "\n";
cout << "inv_cond(m+I): " << inv_cond(m+Matrix4f::Identity()) << endl; std::cout << "inv_cond(m+I): " << inv_cond(m+Eigen::MatrixXf::Identity(4, 4)) << "\n";
} }

View File

@ -1,8 +1,6 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
// [circulant_func] // [circulant_func]
template<class ArgType> template<class ArgType>
class circulant_functor { class circulant_functor {
@ -10,8 +8,8 @@ class circulant_functor {
public: public:
circulant_functor(const ArgType& arg) : m_vec(arg) {} circulant_functor(const ArgType& arg) : m_vec(arg) {}
const typename ArgType::Scalar& operator() (Index row, Index col) const { const typename ArgType::Scalar& operator() (Eigen::Index row, Eigen::Index col) const {
Index index = row - col; Eigen::Index index = row - col;
if (index < 0) index += m_vec.size(); if (index < 0) index += m_vec.size();
return m_vec(index); return m_vec(index);
} }
@ -21,10 +19,10 @@ public:
// [square] // [square]
template<class ArgType> template<class ArgType>
struct circulant_helper { struct circulant_helper {
typedef Matrix<typename ArgType::Scalar, typedef Eigen::Matrix<typename ArgType::Scalar,
ArgType::SizeAtCompileTime, ArgType::SizeAtCompileTime,
ArgType::SizeAtCompileTime, ArgType::SizeAtCompileTime,
ColMajor, Eigen::ColMajor,
ArgType::MaxSizeAtCompileTime, ArgType::MaxSizeAtCompileTime,
ArgType::MaxSizeAtCompileTime> MatrixType; ArgType::MaxSizeAtCompileTime> MatrixType;
}; };
@ -32,7 +30,7 @@ struct circulant_helper {
// [makeCirculant] // [makeCirculant]
template <class ArgType> template <class ArgType>
CwiseNullaryOp<circulant_functor<ArgType>, typename circulant_helper<ArgType>::MatrixType> Eigen::CwiseNullaryOp<circulant_functor<ArgType>, typename circulant_helper<ArgType>::MatrixType>
makeCirculant(const Eigen::MatrixBase<ArgType>& arg) makeCirculant(const Eigen::MatrixBase<ArgType>& arg)
{ {
typedef typename circulant_helper<ArgType>::MatrixType MatrixType; typedef typename circulant_helper<ArgType>::MatrixType MatrixType;

View File

@ -1,8 +1,6 @@
#include <Eigen/Core> #include <Eigen/Core>
#include <iostream> #include <iostream>
using namespace Eigen;
// [functor] // [functor]
template<class ArgType, class RowIndexType, class ColIndexType> template<class ArgType, class RowIndexType, class ColIndexType>
class indexing_functor { class indexing_functor {
@ -10,10 +8,10 @@ class indexing_functor {
const RowIndexType &m_rowIndices; const RowIndexType &m_rowIndices;
const ColIndexType &m_colIndices; const ColIndexType &m_colIndices;
public: public:
typedef Matrix<typename ArgType::Scalar, typedef Eigen::Matrix<typename ArgType::Scalar,
RowIndexType::SizeAtCompileTime, RowIndexType::SizeAtCompileTime,
ColIndexType::SizeAtCompileTime, ColIndexType::SizeAtCompileTime,
ArgType::Flags&RowMajorBit?RowMajor:ColMajor, ArgType::Flags&Eigen::RowMajorBit?Eigen::RowMajor:Eigen::ColMajor,
RowIndexType::MaxSizeAtCompileTime, RowIndexType::MaxSizeAtCompileTime,
ColIndexType::MaxSizeAtCompileTime> MatrixType; ColIndexType::MaxSizeAtCompileTime> MatrixType;
@ -21,7 +19,7 @@ public:
: m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices) : m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices)
{} {}
const typename ArgType::Scalar& operator() (Index row, Index col) const { const typename ArgType::Scalar& operator() (Eigen::Index row, Eigen::Index col) const {
return m_arg(m_rowIndices[row], m_colIndices[col]); return m_arg(m_rowIndices[row], m_colIndices[col]);
} }
}; };
@ -29,7 +27,7 @@ public:
// [function] // [function]
template <class ArgType, class RowIndexType, class ColIndexType> template <class ArgType, class RowIndexType, class ColIndexType>
CwiseNullaryOp<indexing_functor<ArgType,RowIndexType,ColIndexType>, typename indexing_functor<ArgType,RowIndexType,ColIndexType>::MatrixType> Eigen::CwiseNullaryOp<indexing_functor<ArgType,RowIndexType,ColIndexType>, typename indexing_functor<ArgType,RowIndexType,ColIndexType>::MatrixType>
mat_indexing(const Eigen::MatrixBase<ArgType>& arg, const RowIndexType& row_indices, const ColIndexType& col_indices) mat_indexing(const Eigen::MatrixBase<ArgType>& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
{ {
typedef indexing_functor<ArgType,RowIndexType,ColIndexType> Func; typedef indexing_functor<ArgType,RowIndexType,ColIndexType> Func;
@ -43,8 +41,8 @@ int main()
{ {
std::cout << "[main1]\n"; std::cout << "[main1]\n";
Eigen::MatrixXi A = Eigen::MatrixXi::Random(4,4); Eigen::MatrixXi A = Eigen::MatrixXi::Random(4,4);
Array3i ri(1,2,1); Eigen::Array3i ri(1,2,1);
ArrayXi ci(6); ci << 3,2,1,0,0,2; Eigen::ArrayXi ci(6); ci << 3,2,1,0,0,2;
Eigen::MatrixXi B = mat_indexing(A, ri, ci); Eigen::MatrixXi B = mat_indexing(A, ri, ci);
std::cout << "A =" << std::endl; std::cout << "A =" << std::endl;
std::cout << A << std::endl << std::endl; std::cout << A << std::endl << std::endl;
@ -56,7 +54,7 @@ int main()
B = mat_indexing(A, ri+1, ci); B = mat_indexing(A, ri+1, ci);
std::cout << "A(ri+1,ci) =" << std::endl; std::cout << "A(ri+1,ci) =" << std::endl;
std::cout << B << std::endl << std::endl; std::cout << B << std::endl << std::endl;
B = mat_indexing(A, ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), ArrayXi::LinSpaced(4,0,3)); B = mat_indexing(A, Eigen::ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), Eigen::ArrayXi::LinSpaced(4,0,3));
std::cout << "A(ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), ArrayXi::LinSpaced(4,0,3)) =" << std::endl; std::cout << "A(ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), ArrayXi::LinSpaced(4,0,3)) =" << std::endl;
std::cout << B << std::endl << std::endl; std::cout << B << std::endl << std::endl;
std::cout << "[main2]\n"; std::cout << "[main2]\n";

View File

@ -1,14 +1,12 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen;
int main() int main()
{ {
Matrix2d a; Eigen::Matrix2d a;
a << 1, 2, a << 1, 2,
3, 4; 3, 4;
MatrixXd b(2,2); Eigen::MatrixXd b(2,2);
b << 2, 3, b << 2, 3,
1, 4; 1, 4;
std::cout << "a + b =\n" << a + b << std::endl; std::cout << "a + b =\n" << a + b << std::endl;
@ -16,7 +14,7 @@ int main()
std::cout << "Doing a += b;" << std::endl; std::cout << "Doing a += b;" << std::endl;
a += b; a += b;
std::cout << "Now a =\n" << a << std::endl; std::cout << "Now a =\n" << a << std::endl;
Vector3d v(1,2,3); Eigen::Vector3d v(1,2,3);
Vector3d w(1,0,0); Eigen::Vector3d w(1,0,0);
std::cout << "-v + w - v =\n" << -v + w - v << std::endl; std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
} }

View File

@ -1,15 +1,13 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main() int main()
{ {
Vector3d v(1,2,3); Eigen::Vector3d v(1,2,3);
Vector3d w(0,1,2); Eigen::Vector3d w(0,1,2);
cout << "Dot product: " << v.dot(w) << endl; std::cout << "Dot product: " << v.dot(w) << std::endl;
double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar
cout << "Dot product via a matrix product: " << dp << endl; std::cout << "Dot product via a matrix product: " << dp << std::endl;
cout << "Cross product:\n" << v.cross(w) << endl; std::cout << "Cross product:\n" << v.cross(w) << std::endl;
} }

View File

@ -1,13 +1,12 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen;
int main() int main()
{ {
Matrix2d mat; Eigen::Matrix2d mat;
mat << 1, 2, mat << 1, 2,
3, 4; 3, 4;
Vector2d u(-1,1), v(2,0); Eigen::Vector2d u(-1,1), v(2,0);
std::cout << "Here is mat*mat:\n" << mat*mat << std::endl; 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 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*mat:\n" << u.transpose()*mat << std::endl;

View File

@ -1,14 +1,12 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen;
int main() int main()
{ {
Matrix2d a; Eigen::Matrix2d a;
a << 1, 2, a << 1, 2,
3, 4; 3, 4;
Vector3d v(1,2,3); Eigen::Vector3d v(1,2,3);
std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl; std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;
std::cout << "0.1 * v =\n" << 0.1 * v << std::endl; std::cout << "0.1 * v =\n" << 0.1 * v << std::endl;
std::cout << "Doing v *= 2;" << std::endl; std::cout << "Doing v *= 2;" << std::endl;

View File

@ -1,17 +1,15 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen;
int main() int main()
{ {
MatrixXd m(2,2); Eigen::MatrixXd m(2,2);
m(0,0) = 3; m(0,0) = 3;
m(1,0) = 2.5; m(1,0) = 2.5;
m(0,1) = -1; m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1); m(1,1) = m(1,0) + m(0,1);
std::cout << "Here is the matrix m:\n" << m << std::endl; std::cout << "Here is the matrix m:\n" << m << std::endl;
VectorXd v(2); Eigen::VectorXd v(2);
v(0) = 4; v(0) = 4;
v(1) = v(0) - 1; v(1) = v(0) - 1;
std::cout << "Here is the vector v:\n" << v << std::endl; std::cout << "Here is the vector v:\n" << v << std::endl;

View File

@ -1,16 +1,14 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen;
int main() int main()
{ {
MatrixXd m(2,5); Eigen::MatrixXd m(2,5);
m.resize(4,3); m.resize(4,3);
std::cout << "The matrix m is of size " std::cout << "The matrix m is of size "
<< m.rows() << "x" << m.cols() << std::endl; << m.rows() << "x" << m.cols() << std::endl;
std::cout << "It has " << m.size() << " coefficients" << std::endl; std::cout << "It has " << m.size() << " coefficients" << std::endl;
VectorXd v(2); Eigen::VectorXd v(2);
v.resize(5); v.resize(5);
std::cout << "The vector v is of size " << v.size() << std::endl; std::cout << "The vector v is of size " << v.size() << std::endl;
std::cout << "As a matrix, v is of size " std::cout << "As a matrix, v is of size "

View File

@ -1,11 +1,9 @@
#include <iostream> #include <iostream>
#include <Eigen/Dense> #include <Eigen/Dense>
using namespace Eigen;
int main() int main()
{ {
Matrix4d m; Eigen::Matrix4d m;
m.resize(4,4); // no operation m.resize(4,4); // no operation
std::cout << "The matrix m is of size " std::cout << "The matrix m is of size "
<< m.rows() << "x" << m.cols() << std::endl; << m.rows() << "x" << m.cols() << std::endl;

View File

@ -2,13 +2,11 @@
#include <iostream> #include <iostream>
#include <random> #include <random>
using namespace Eigen;
int main() { int main() {
std::default_random_engine generator; std::default_random_engine generator;
std::poisson_distribution<int> distribution(4.1); std::poisson_distribution<int> distribution(4.1);
auto poisson = [&] () {return distribution(generator);}; auto poisson = [&] () {return distribution(generator);};
RowVectorXi v = RowVectorXi::NullaryExpr(10, poisson ); Eigen::RowVectorXi v = Eigen::RowVectorXi::NullaryExpr(10, poisson );
std::cout << v << "\n"; std::cout << v << "\n";
} }