mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Clang-format tests, examples, libraries, benchmarks, etc.
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
3252ecc7a4
commit
46e9cdb7fe
@@ -1,28 +1,23 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
class MyVectorType : public Eigen::VectorXd
|
||||
{
|
||||
public:
|
||||
MyVectorType(void):Eigen::VectorXd() {}
|
||||
class MyVectorType : public Eigen::VectorXd {
|
||||
public:
|
||||
MyVectorType(void) : Eigen::VectorXd() {}
|
||||
|
||||
// This constructor allows you to construct MyVectorType from Eigen expressions
|
||||
template<typename OtherDerived>
|
||||
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other)
|
||||
: Eigen::VectorXd(other)
|
||||
{ }
|
||||
// This constructor allows you to construct MyVectorType from Eigen expressions
|
||||
template <typename OtherDerived>
|
||||
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other) : Eigen::VectorXd(other) {}
|
||||
|
||||
// This method allows you to assign Eigen expressions to MyVectorType
|
||||
template<typename OtherDerived>
|
||||
MyVectorType& operator=(const Eigen::MatrixBase <OtherDerived>& other)
|
||||
{
|
||||
this->Eigen::VectorXd::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
// This method allows you to assign Eigen expressions to MyVectorType
|
||||
template <typename OtherDerived>
|
||||
MyVectorType& operator=(const Eigen::MatrixBase<OtherDerived>& other) {
|
||||
this->Eigen::VectorXd::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
MyVectorType v = MyVectorType::Ones(4);
|
||||
v(2) += 10;
|
||||
v = 2 * v;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <Eigen/Core>
|
||||
#include <unsupported/Eigen/SpecialFunctions>
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
Eigen::Array4d v(-0.5,2,0,-7);
|
||||
int main() {
|
||||
Eigen::Array4d v(-0.5, 2, 0, -7);
|
||||
std::cout << v.erf() << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <Eigen/Core>
|
||||
#include <unsupported/Eigen/SpecialFunctions>
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
Eigen::Array4d v(-0.5,2,0,-7);
|
||||
int main() {
|
||||
Eigen::Array4d v(-0.5, 2, 0, -7);
|
||||
std::cout << v.erfc() << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <Eigen/Core>
|
||||
#include <unsupported/Eigen/SpecialFunctions>
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
Eigen::Array4d v(0.5,10,0,-1);
|
||||
int main() {
|
||||
Eigen::Array4d v(0.5, 10, 0, -1);
|
||||
std::cout << v.lgamma() << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N,N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(1..3,:) =\n" << A.middleCols(1,3) << std::endl;
|
||||
return 0;
|
||||
int main() {
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N, N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(1..3,:) =\n" << A.middleCols(1, 3) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N,N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(2..3,:) =\n" << A.middleRows(2,2) << std::endl;
|
||||
return 0;
|
||||
int main() {
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N, N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(2..3,:) =\n" << A.middleRows(2, 2) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N,N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(:,1..3) =\n" << A.middleCols<3>(1) << std::endl;
|
||||
return 0;
|
||||
int main() {
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N, N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(:,1..3) =\n" << A.middleCols<3>(1) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N,N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(1..3,:) =\n" << A.middleRows<3>(1) << std::endl;
|
||||
return 0;
|
||||
int main() {
|
||||
int const N = 5;
|
||||
Eigen::MatrixXi A(N, N);
|
||||
A.setRandom();
|
||||
std::cout << "A =\n" << A << '\n' << std::endl;
|
||||
std::cout << "A(1..3,:) =\n" << A.middleRows<3>(1) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -3,12 +3,11 @@
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -4,10 +4,9 @@
|
||||
using Eigen::MatrixXd;
|
||||
using Eigen::VectorXd;
|
||||
|
||||
int main()
|
||||
{
|
||||
MatrixXd m = MatrixXd::Random(3,3);
|
||||
m = (m + MatrixXd::Constant(3,3,1.2)) * 50;
|
||||
int main() {
|
||||
MatrixXd m = MatrixXd::Random(3, 3);
|
||||
m = (m + MatrixXd::Constant(3, 3, 1.2)) * 50;
|
||||
std::cout << "m =" << std::endl << m << std::endl;
|
||||
VectorXd v(3);
|
||||
v << 1, 2, 3;
|
||||
|
||||
@@ -4,12 +4,11 @@
|
||||
using Eigen::Matrix3d;
|
||||
using Eigen::Vector3d;
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Matrix3d m = Matrix3d::Random();
|
||||
m = (m + Matrix3d::Constant(1.2)) * 50;
|
||||
std::cout << "m =" << std::endl << m << std::endl;
|
||||
Vector3d v(1,2,3);
|
||||
|
||||
Vector3d v(1, 2, 3);
|
||||
|
||||
std::cout << "m * v =" << std::endl << m * v << std::endl;
|
||||
}
|
||||
|
||||
@@ -2,19 +2,17 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename Derived1, typename Derived2>
|
||||
void copyUpperTriangularPart(Eigen::MatrixBase<Derived1>& dst, const Eigen::MatrixBase<Derived2>& src)
|
||||
{
|
||||
void copyUpperTriangularPart(Eigen::MatrixBase<Derived1>& dst, const Eigen::MatrixBase<Derived2>& src) {
|
||||
/* Note the 'template' keywords in the following line! */
|
||||
dst.template triangularView<Eigen::Upper>() = src.template triangularView<Eigen::Upper>();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXi m1 = Eigen::MatrixXi::Ones(5,5);
|
||||
Eigen::MatrixXi m2 = Eigen::MatrixXi::Random(4,4);
|
||||
int main() {
|
||||
Eigen::MatrixXi m1 = Eigen::MatrixXi::Ones(5, 5);
|
||||
Eigen::MatrixXi m2 = Eigen::MatrixXi::Random(4, 4);
|
||||
std::cout << "m2 before copy:" << std::endl;
|
||||
std::cout << m2 << std::endl << std::endl;
|
||||
copyUpperTriangularPart(m2, m1.topLeftCorner(4,4));
|
||||
copyUpperTriangularPart(m2, m1.topLeftCorner(4, 4));
|
||||
std::cout << "m2 after copy:" << std::endl;
|
||||
std::cout << m2 << std::endl << std::endl;
|
||||
}
|
||||
|
||||
@@ -3,15 +3,13 @@
|
||||
|
||||
using Eigen::MatrixXf;
|
||||
|
||||
void copyUpperTriangularPart(MatrixXf& dst, const MatrixXf& src)
|
||||
{
|
||||
void copyUpperTriangularPart(MatrixXf& dst, const MatrixXf& src) {
|
||||
dst.triangularView<Eigen::Upper>() = src.triangularView<Eigen::Upper>();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
MatrixXf m1 = MatrixXf::Ones(4,4);
|
||||
MatrixXf m2 = MatrixXf::Random(4,4);
|
||||
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);
|
||||
|
||||
@@ -6,9 +6,8 @@ init init_obj;
|
||||
// [init]
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXd A(2,2);
|
||||
int main() {
|
||||
Eigen::MatrixXd A(2, 2);
|
||||
A << 2, -1, 1, 3;
|
||||
std::cout << "Here is the input matrix A before decomposition:\n" << A << "\n";
|
||||
std::cout << "[init]\n";
|
||||
@@ -23,8 +22,10 @@ int main()
|
||||
std::cout << "[matrixLU]\n";
|
||||
|
||||
std::cout << "[solve]\n";
|
||||
Eigen::MatrixXd A0(2,2); A0 << 2, -1, 1, 3;
|
||||
Eigen::VectorXd b(2); b << 1, 2;
|
||||
Eigen::MatrixXd A0(2, 2);
|
||||
A0 << 2, -1, 1, 3;
|
||||
Eigen::VectorXd b(2);
|
||||
b << 1, 2;
|
||||
Eigen::VectorXd x = lu.solve(b);
|
||||
std::cout << "Residual: " << (A0 * x - b).norm() << "\n";
|
||||
std::cout << "[solve]\n";
|
||||
@@ -36,15 +37,15 @@ int main()
|
||||
std::cout << "[modifyA]\n";
|
||||
|
||||
std::cout << "[recompute]\n";
|
||||
A0 = A; // save A
|
||||
A0 = A; // save A
|
||||
lu.compute(A);
|
||||
x = lu.solve(b);
|
||||
std::cout << "Residual: " << (A0 * x - b).norm() << "\n";
|
||||
std::cout << "[recompute]\n";
|
||||
|
||||
std::cout << "[recompute_bis0]\n";
|
||||
Eigen::MatrixXd A1(2,2);
|
||||
A1 << 5,-2,3,4;
|
||||
Eigen::MatrixXd A1(2, 2);
|
||||
A1 << 5, -2, 3, 4;
|
||||
lu.compute(A1);
|
||||
std::cout << "Here is the input matrix A1 after decomposition:\n" << A1 << "\n";
|
||||
std::cout << "[recompute_bis0]\n";
|
||||
@@ -53,5 +54,4 @@ int main()
|
||||
x = lu.solve(b);
|
||||
std::cout << "Residual: " << (A1 * x - b).norm() << "\n";
|
||||
std::cout << "[recompute_bis1]\n";
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix2f A, b;
|
||||
Eigen::LLT<Eigen::Matrix2f> llt;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "Here is the right hand side b:\n" << b << std::endl;
|
||||
std::cout << "Computing LLT decomposition..." << std::endl;
|
||||
llt.compute(A);
|
||||
std::cout << "The solution is:\n" << llt.solve(b) << std::endl;
|
||||
A(1,1)++;
|
||||
std::cout << "The matrix A is now:\n" << A << std::endl;
|
||||
std::cout << "Computing LLT decomposition..." << std::endl;
|
||||
llt.compute(A);
|
||||
std::cout << "The solution is now:\n" << llt.solve(b) << std::endl;
|
||||
int main() {
|
||||
Eigen::Matrix2f A, b;
|
||||
Eigen::LLT<Eigen::Matrix2f> llt;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "Here is the right hand side b:\n" << b << std::endl;
|
||||
std::cout << "Computing LLT decomposition..." << std::endl;
|
||||
llt.compute(A);
|
||||
std::cout << "The solution is:\n" << llt.solve(b) << std::endl;
|
||||
A(1, 1)++;
|
||||
std::cout << "The matrix A is now:\n" << A << std::endl;
|
||||
std::cout << "Computing LLT decomposition..." << std::endl;
|
||||
llt.compute(A);
|
||||
std::cout << "The solution is now:\n" << llt.solve(b) << std::endl;
|
||||
}
|
||||
|
||||
@@ -3,11 +3,10 @@
|
||||
|
||||
using Eigen::MatrixXd;
|
||||
|
||||
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
|
||||
std::cout << "The relative error is:\n" << relative_error << std::endl;
|
||||
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
|
||||
std::cout << "The relative error is:\n" << relative_error << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix3f A;
|
||||
Eigen::Vector3f b;
|
||||
A << 1,2,3, 4,5,6, 7,8,10;
|
||||
b << 3, 3, 4;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "Here is the vector b:\n" << b << std::endl;
|
||||
Eigen::Vector3f x = A.colPivHouseholderQr().solve(b);
|
||||
std::cout << "The solution is:\n" << x << std::endl;
|
||||
int main() {
|
||||
Eigen::Matrix3f A;
|
||||
Eigen::Vector3f b;
|
||||
A << 1, 2, 3, 4, 5, 6, 7, 8, 10;
|
||||
b << 3, 3, 4;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "Here is the vector b:\n" << b << std::endl;
|
||||
Eigen::Vector3f x = A.colPivHouseholderQr().solve(b);
|
||||
std::cout << "The solution is:\n" << x << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix2f A, b;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "Here is the right hand side b:\n" << b << std::endl;
|
||||
Eigen::Matrix2f x = A.ldlt().solve(b);
|
||||
std::cout << "The solution is:\n" << x << std::endl;
|
||||
int main() {
|
||||
Eigen::Matrix2f A, b;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "Here is the right hand side b:\n" << b << std::endl;
|
||||
Eigen::Matrix2f x = A.ldlt().solve(b);
|
||||
std::cout << "The solution is:\n" << x << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix3f A;
|
||||
A << 1, 2, 1,
|
||||
2, 1, 0,
|
||||
-1, 1, 2;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "The determinant of A is " << A.determinant() << std::endl;
|
||||
std::cout << "The inverse of A is:\n" << A.inverse() << std::endl;
|
||||
int main() {
|
||||
Eigen::Matrix3f A;
|
||||
A << 1, 2, 1, 2, 1, 0, -1, 1, 2;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
std::cout << "The determinant of A is " << A.determinant() << std::endl;
|
||||
std::cout << "The inverse of A is:\n" << A.inverse() << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix3f A;
|
||||
A << 1, 2, 5,
|
||||
2, 1, 4,
|
||||
3, 0, 3;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
Eigen::FullPivLU<Eigen::Matrix3f> lu_decomp(A);
|
||||
std::cout << "The rank of A is " << lu_decomp.rank() << std::endl;
|
||||
std::cout << "Here is a matrix whose columns form a basis of the null-space of A:\n"
|
||||
<< lu_decomp.kernel() << std::endl;
|
||||
std::cout << "Here is a matrix whose columns form a basis of the column-space of A:\n"
|
||||
<< lu_decomp.image(A) << std::endl; // yes, have to pass the original A
|
||||
int main() {
|
||||
Eigen::Matrix3f A;
|
||||
A << 1, 2, 5, 2, 1, 4, 3, 0, 3;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
Eigen::FullPivLU<Eigen::Matrix3f> lu_decomp(A);
|
||||
std::cout << "The rank of A is " << lu_decomp.rank() << std::endl;
|
||||
std::cout << "Here is a matrix whose columns form a basis of the null-space of A:\n"
|
||||
<< lu_decomp.kernel() << std::endl;
|
||||
std::cout << "Here is a matrix whose columns form a basis of the column-space of A:\n"
|
||||
<< lu_decomp.image(A) << std::endl; // yes, have to pass the original A
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf A = Eigen::MatrixXf::Random(3, 2);
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
Eigen::VectorXf b = Eigen::VectorXf::Random(3);
|
||||
std::cout << "Here is the right hand side b:\n" << b << std::endl;
|
||||
std::cout << "The least-squares solution is:\n"
|
||||
<< A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b) << std::endl;
|
||||
int main() {
|
||||
Eigen::MatrixXf A = Eigen::MatrixXf::Random(3, 2);
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
Eigen::VectorXf b = Eigen::VectorXf::Random(3);
|
||||
std::cout << "Here is the right hand side b:\n" << b << std::endl;
|
||||
std::cout << "The least-squares solution is:\n"
|
||||
<< A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b) << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix2f A;
|
||||
A << 1, 2, 2, 3;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
Eigen::SelfAdjointEigenSolver<Eigen::Matrix2f> eigensolver(A);
|
||||
if (eigensolver.info() != Eigen::Success) abort();
|
||||
std::cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << std::endl;
|
||||
std::cout << "Here's a matrix whose columns are eigenvectors of A \n"
|
||||
<< "corresponding to these eigenvalues:\n"
|
||||
<< eigensolver.eigenvectors() << std::endl;
|
||||
int main() {
|
||||
Eigen::Matrix2f A;
|
||||
A << 1, 2, 2, 3;
|
||||
std::cout << "Here is the matrix A:\n" << A << std::endl;
|
||||
Eigen::SelfAdjointEigenSolver<Eigen::Matrix2f> eigensolver(A);
|
||||
if (eigensolver.info() != Eigen::Success) abort();
|
||||
std::cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << std::endl;
|
||||
std::cout << "Here's a matrix whose columns are eigenvectors of A \n"
|
||||
<< "corresponding to these eigenvalues:\n"
|
||||
<< eigensolver.eigenvectors() << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix2d A;
|
||||
A << 2, 1,
|
||||
2, 0.9999999999;
|
||||
Eigen::FullPivLU<Eigen::Matrix2d> lu(A);
|
||||
std::cout << "By default, the rank of A is found to be " << lu.rank() << std::endl;
|
||||
lu.setThreshold(1e-5);
|
||||
std::cout << "With threshold 1e-5, the rank of A is found to be " << lu.rank() << std::endl;
|
||||
int main() {
|
||||
Eigen::Matrix2d A;
|
||||
A << 2, 1, 2, 0.9999999999;
|
||||
Eigen::FullPivLU<Eigen::Matrix2d> lu(A);
|
||||
std::cout << "By default, the rank of A is found to be " << lu.rank() << std::endl;
|
||||
lu.setThreshold(1e-5);
|
||||
std::cout << "With threshold 1e-5, the rank of A is found to be " << lu.rank() << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::ArrayXXf m(2,2);
|
||||
|
||||
int main() {
|
||||
Eigen::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);
|
||||
|
||||
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
|
||||
std::cout << m << std::endl << std::endl;
|
||||
|
||||
|
||||
// using the comma-initializer is also allowed
|
||||
m << 1.0,2.0,
|
||||
3.0,4.0;
|
||||
|
||||
m << 1.0, 2.0, 3.0, 4.0;
|
||||
|
||||
// print values to standard output
|
||||
std::cout << m << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::ArrayXXf a(3,3);
|
||||
Eigen::ArrayXXf b(3,3);
|
||||
a << 1,2,3,
|
||||
4,5,6,
|
||||
7,8,9;
|
||||
b << 1,2,3,
|
||||
1,2,3,
|
||||
1,2,3;
|
||||
|
||||
int main() {
|
||||
Eigen::ArrayXXf a(3, 3);
|
||||
Eigen::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
|
||||
std::cout << "a + b = " << std::endl << a + b << std::endl << std::endl;
|
||||
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::ArrayXf a = Eigen::ArrayXf::Random(5);
|
||||
a *= 2;
|
||||
std::cout << "a =" << std::endl
|
||||
<< a << std::endl;
|
||||
std::cout << "a.abs() =" << std::endl
|
||||
<< a.abs() << std::endl;
|
||||
std::cout << "a.abs().sqrt() =" << std::endl
|
||||
<< a.abs().sqrt() << std::endl;
|
||||
std::cout << "a.min(a.abs().sqrt()) =" << std::endl
|
||||
<< a.min(a.abs().sqrt()) << std::endl;
|
||||
std::cout << "a =" << std::endl << a << std::endl;
|
||||
std::cout << "a.abs() =" << std::endl << a.abs() << std::endl;
|
||||
std::cout << "a.abs().sqrt() =" << std::endl << a.abs().sqrt() << std::endl;
|
||||
std::cout << "a.min(a.abs().sqrt()) =" << std::endl << a.min(a.abs().sqrt()) << std::endl;
|
||||
}
|
||||
|
||||
@@ -3,17 +3,14 @@
|
||||
|
||||
using Eigen::MatrixXf;
|
||||
|
||||
int main()
|
||||
{
|
||||
MatrixXf m(2,2);
|
||||
MatrixXf n(2,2);
|
||||
MatrixXf result(2,2);
|
||||
int main() {
|
||||
MatrixXf m(2, 2);
|
||||
MatrixXf n(2, 2);
|
||||
MatrixXf result(2, 2);
|
||||
|
||||
m << 1, 2, 3, 4;
|
||||
n << 5, 6, 7, 8;
|
||||
|
||||
m << 1,2,
|
||||
3,4;
|
||||
n << 5,6,
|
||||
7,8;
|
||||
|
||||
result = (m.array() + 4).matrix() * m;
|
||||
std::cout << "-- Combination 1: --\n" << result << "\n\n";
|
||||
result = (m.array() * n.array()).matrix() * m;
|
||||
|
||||
@@ -3,16 +3,13 @@
|
||||
|
||||
using Eigen::MatrixXf;
|
||||
|
||||
int main()
|
||||
{
|
||||
MatrixXf m(2,2);
|
||||
MatrixXf n(2,2);
|
||||
MatrixXf result(2,2);
|
||||
int main() {
|
||||
MatrixXf m(2, 2);
|
||||
MatrixXf n(2, 2);
|
||||
MatrixXf result(2, 2);
|
||||
|
||||
m << 1,2,
|
||||
3,4;
|
||||
n << 5,6,
|
||||
7,8;
|
||||
m << 1, 2, 3, 4;
|
||||
n << 5, 6, 7, 8;
|
||||
|
||||
result = m * n;
|
||||
std::cout << "-- Matrix m*n: --\n" << result << "\n\n";
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::ArrayXXf a(2,2);
|
||||
Eigen::ArrayXXf b(2,2);
|
||||
a << 1,2,
|
||||
3,4;
|
||||
b << 5,6,
|
||||
7,8;
|
||||
int main() {
|
||||
Eigen::ArrayXXf a(2, 2);
|
||||
Eigen::ArrayXXf b(2, 2);
|
||||
a << 1, 2, 3, 4;
|
||||
b << 5, 6, 7, 8;
|
||||
std::cout << "a * b = " << std::endl << a * b << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::Array22f m;
|
||||
m << 1,2,
|
||||
3,4;
|
||||
m << 1, 2, 3, 4;
|
||||
Eigen::Array44f a = Eigen::Array44f::Constant(0.6);
|
||||
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;
|
||||
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);
|
||||
std::cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:\n" << a << "\n\n";
|
||||
}
|
||||
|
||||
@@ -3,12 +3,9 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf m(3,3);
|
||||
m << 1,2,3,
|
||||
4,5,6,
|
||||
7,8,9;
|
||||
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);
|
||||
|
||||
@@ -3,15 +3,11 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::Matrix4f m;
|
||||
m << 1, 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10,11,12,
|
||||
13,14,15,16;
|
||||
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();
|
||||
m.topLeftCorner(1, 3) = m.bottomRightCorner(3, 1).transpose();
|
||||
cout << "After assignment, m = " << endl << m << endl;
|
||||
}
|
||||
|
||||
@@ -3,18 +3,13 @@
|
||||
|
||||
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;
|
||||
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 << 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;
|
||||
cout << m.block(0, 0, i, i) << endl << endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,11 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
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;
|
||||
v.segment(1, 4) *= 2;
|
||||
cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl;
|
||||
}
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
#include <Eigen/LU>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Matrix3f A;
|
||||
Eigen::Vector3f b;
|
||||
A << 1,2,3, 4,5,6, 7,8,10;
|
||||
b << 3, 3, 4;
|
||||
std::cout << "Here is the matrix A:" << std::endl << A << std::endl;
|
||||
std::cout << "Here is the vector b:" << std::endl << b << std::endl;
|
||||
Eigen::Vector3f x = A.lu().solve(b);
|
||||
std::cout << "The solution is:" << std::endl << x << std::endl;
|
||||
int main() {
|
||||
Eigen::Matrix3f A;
|
||||
Eigen::Vector3f b;
|
||||
A << 1, 2, 3, 4, 5, 6, 7, 8, 10;
|
||||
b << 3, 3, 4;
|
||||
std::cout << "Here is the matrix A:" << std::endl << A << std::endl;
|
||||
std::cout << "Here is the vector b:" << std::endl << b << std::endl;
|
||||
Eigen::Vector3f x = A.lu().solve(b);
|
||||
std::cout << "The solution is:" << std::endl << x << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf m(2,4);
|
||||
int main() {
|
||||
Eigen::MatrixXf m(2, 4);
|
||||
Eigen::VectorXf v(2);
|
||||
|
||||
m << 1, 23, 6, 9,
|
||||
3, 11, 7, 2;
|
||||
|
||||
v << 2,
|
||||
3;
|
||||
|
||||
m << 1, 23, 6, 9, 3, 11, 7, 2;
|
||||
|
||||
v << 2, 3;
|
||||
|
||||
Eigen::Index index;
|
||||
// find nearest neighbour
|
||||
|
||||
@@ -2,20 +2,17 @@
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf mat(2,4);
|
||||
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 << 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;
|
||||
}
|
||||
|
||||
@@ -2,19 +2,17 @@
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf mat(2,4);
|
||||
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 << 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;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,9 @@
|
||||
#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;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf mat(2,4);
|
||||
mat << 1, 2, 6, 9,
|
||||
3, 1, 7, 2;
|
||||
|
||||
Eigen::Index maxIndex;
|
||||
int main() {
|
||||
Eigen::MatrixXf mat(2, 4);
|
||||
mat << 1, 2, 6, 9, 3, 1, 7, 2;
|
||||
|
||||
Eigen::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 << mat.col(maxIndex) << std::endl;
|
||||
std::cout << "And its sum is is: " << maxNorm << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::ArrayXXf a(2,2);
|
||||
|
||||
a << 1,2,
|
||||
3,4;
|
||||
int main() {
|
||||
Eigen::ArrayXXf a(2, 2);
|
||||
|
||||
a << 1, 2, 3, 4;
|
||||
|
||||
std::cout << "(a > 0).all() = " << (a > 0).all() << std::endl;
|
||||
std::cout << "(a > 0).any() = " << (a > 0).any() << std::endl;
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::VectorXf v(2);
|
||||
Eigen::MatrixXf m(2,2), n(2,2);
|
||||
|
||||
v << -1,
|
||||
2;
|
||||
|
||||
m << 1,-2,
|
||||
-3,4;
|
||||
Eigen::MatrixXf m(2, 2), n(2, 2);
|
||||
|
||||
v << -1, 2;
|
||||
|
||||
m << 1, -2, -3, 4;
|
||||
|
||||
std::cout << "v.squaredNorm() = " << v.squaredNorm() << std::endl;
|
||||
std::cout << "v.norm() = " << v.norm() << std::endl;
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf m(2,2);
|
||||
m << 1,-2,
|
||||
-3,4;
|
||||
int main() {
|
||||
Eigen::MatrixXf m(2, 2);
|
||||
m << 1, -2, -3, 4;
|
||||
|
||||
std::cout << "1-norm(m) = " << m.cwiseAbs().colwise().sum().maxCoeff()
|
||||
<< " == " << m.colwise().lpNorm<1>().maxCoeff() << std::endl;
|
||||
<< " == " << m.colwise().lpNorm<1>().maxCoeff() << std::endl;
|
||||
|
||||
std::cout << "infty-norm(m) = " << m.cwiseAbs().rowwise().sum().maxCoeff()
|
||||
<< " == " << m.rowwise().lpNorm<1>().maxCoeff() << std::endl;
|
||||
<< " == " << m.rowwise().lpNorm<1>().maxCoeff() << std::endl;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,9 @@
|
||||
#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;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXf m(2,2);
|
||||
|
||||
m << 1, 2,
|
||||
3, 4;
|
||||
int main() {
|
||||
Eigen::MatrixXf m(2, 2);
|
||||
|
||||
//get location of maximum
|
||||
m << 1, 2, 3, 4;
|
||||
|
||||
// get location of maximum
|
||||
Eigen::Index maxRow, maxCol;
|
||||
float max = m.maxCoeff(&maxRow, &maxCol);
|
||||
|
||||
//get location of minimum
|
||||
// get location of minimum
|
||||
Eigen::Index minRow, minCol;
|
||||
float min = m.minCoeff(&minRow, &minCol);
|
||||
|
||||
std::cout << "Max: " << max << ", at: " <<
|
||||
maxRow << "," << maxCol << std::endl;
|
||||
std:: cout << "Min: " << min << ", at: " <<
|
||||
minRow << "," << minCol << std::endl;
|
||||
std::cout << "Max: " << max << ", at: " << maxRow << "," << maxCol << std::endl;
|
||||
std::cout << "Min: " << min << ", at: " << minRow << "," << minCol << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
for (int size=1; size<=4; ++size)
|
||||
{
|
||||
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 i=0; i<m.rows(); ++i) // loop over rows
|
||||
m(i,j) = i+j*size; // to access matrix coefficients,
|
||||
// use operator()(int,int)
|
||||
int main() {
|
||||
for (int size = 1; size <= 4; ++size) {
|
||||
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 i = 0; i < m.rows(); ++i) // loop over rows
|
||||
m(i, j) = i + j * size; // to access matrix coefficients,
|
||||
// use operator()(int,int)
|
||||
std::cout << m << "\n\n";
|
||||
}
|
||||
|
||||
Eigen::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 []
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::Matrix3f m3;
|
||||
m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
|
||||
Eigen::Matrix4f m4 = Eigen::Matrix4f::Identity();
|
||||
Eigen::Vector4i v4(1, 2, 3, 4);
|
||||
|
||||
std::cout << "m3\n" << m3 << "\nm4:\n"
|
||||
<< m4 << "\nv4:\n" << v4 << std::endl;
|
||||
std::cout << "m3\n" << m3 << "\nm4:\n" << m4 << "\nv4:\n" << v4 << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,20 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Derived>
|
||||
Eigen::Block<Derived>
|
||||
topLeftCorner(Eigen::MatrixBase<Derived>& m, int rows, int cols)
|
||||
{
|
||||
template <typename Derived>
|
||||
Eigen::Block<Derived> topLeftCorner(Eigen::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 Eigen::MatrixBase<Derived>& m, int rows, int cols)
|
||||
{
|
||||
template <typename Derived>
|
||||
const Eigen::Block<const Derived> topLeftCorner(const Eigen::MatrixBase<Derived>& m, int rows, int cols) {
|
||||
return Eigen::Block<const Derived>(m.derived(), 0, 0, rows, cols);
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
int main(int, char**) {
|
||||
Eigen::Matrix4d m = Eigen::Matrix4d::Identity();
|
||||
std::cout << topLeftCorner(4*m, 2, 3) << std::endl; // calls the const version
|
||||
topLeftCorner(m, 2, 3) *= 5; // calls the non-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
|
||||
std::cout << "Now the matrix m is:" << std::endl << m << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
using Eigen::Matrix4d;
|
||||
|
||||
// define a custom template binary functor
|
||||
template<typename Scalar> struct MakeComplexOp {
|
||||
template <typename Scalar>
|
||||
struct MakeComplexOp {
|
||||
typedef std::complex<Scalar> result_type;
|
||||
result_type operator()(const Scalar& a, const Scalar& b) const { return result_type(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();
|
||||
std::cout << m1.binaryExpr(m2, MakeComplexOp<double>()) << std::endl;
|
||||
return 0;
|
||||
|
||||
@@ -2,16 +2,17 @@
|
||||
#include <iostream>
|
||||
|
||||
// define a custom template unary functor
|
||||
template<typename Scalar>
|
||||
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); }
|
||||
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**)
|
||||
{
|
||||
int main(int, char**) {
|
||||
Eigen::Matrix4d m1 = Eigen::Matrix4d::Random();
|
||||
std::cout << m1 << std::endl << "becomes: " << std::endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << std::endl;
|
||||
std::cout << m1 << std::endl
|
||||
<< "becomes: " << std::endl
|
||||
<< m1.unaryExpr(CwiseClampOp<double>(-0.5, 0.5)) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,16 +2,14 @@
|
||||
#include <iostream>
|
||||
|
||||
// define function to be applied coefficient-wise
|
||||
double ramp(double x)
|
||||
{
|
||||
double ramp(double x) {
|
||||
if (x > 0)
|
||||
return x;
|
||||
else
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
int main(int, char**) {
|
||||
Eigen::Matrix4d m1 = Eigen::Matrix4d::Random();
|
||||
std::cout << m1 << std::endl << "becomes: " << std::endl << m1.unaryExpr(std::ptr_fun(ramp)) << std::endl;
|
||||
return 0;
|
||||
|
||||
@@ -1,25 +1,20 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Derived>
|
||||
Eigen::Block<Derived, 2, 2>
|
||||
topLeft2x2Corner(Eigen::MatrixBase<Derived>& m)
|
||||
{
|
||||
template <typename Derived>
|
||||
Eigen::Block<Derived, 2, 2> topLeft2x2Corner(Eigen::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 Eigen::MatrixBase<Derived>& m)
|
||||
{
|
||||
template <typename Derived>
|
||||
const Eigen::Block<const Derived, 2, 2> topLeft2x2Corner(const Eigen::MatrixBase<Derived>& m) {
|
||||
return Eigen::Block<const Derived, 2, 2>(m.derived(), 0, 0);
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
int main(int, char**) {
|
||||
Eigen::Matrix3d m = Eigen::Matrix3d::Identity();
|
||||
std::cout << topLeft2x2Corner(4*m) << std::endl; // calls the const version
|
||||
topLeft2x2Corner(m) *= 2; // calls the non-const version
|
||||
std::cout << topLeft2x2Corner(4 * m) << std::endl; // calls the const version
|
||||
topLeft2x2Corner(m) *= 2; // calls the non-const version
|
||||
std::cout << "Now the matrix m is:" << std::endl << m << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Derived>
|
||||
Eigen::Reshaped<Derived, 4, 2>
|
||||
reshape_helper(Eigen::MatrixBase<Derived>& m)
|
||||
{
|
||||
template <typename Derived>
|
||||
Eigen::Reshaped<Derived, 4, 2> reshape_helper(Eigen::MatrixBase<Derived>& m) {
|
||||
return Eigen::Reshaped<Derived, 4, 2>(m.derived());
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
int main(int, char**) {
|
||||
Eigen::MatrixXd m(2, 4);
|
||||
m << 1, 2, 3, 4,
|
||||
5, 6, 7, 8;
|
||||
m << 1, 2, 3, 4, 5, 6, 7, 8;
|
||||
Eigen::MatrixXd n = reshape_helper(m);
|
||||
std::cout << "matrix m is:" << std::endl << m << std::endl;
|
||||
std::cout << "matrix n is:" << std::endl << n << std::endl;
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Derived>
|
||||
Eigen::VectorBlock<Derived, 2>
|
||||
firstTwo(Eigen::MatrixBase<Derived>& v)
|
||||
{
|
||||
template <typename Derived>
|
||||
Eigen::VectorBlock<Derived, 2> firstTwo(Eigen::MatrixBase<Derived>& v) {
|
||||
return Eigen::VectorBlock<Derived, 2>(v.derived(), 0);
|
||||
}
|
||||
|
||||
template<typename Derived>
|
||||
const Eigen::VectorBlock<const Derived, 2>
|
||||
firstTwo(const Eigen::MatrixBase<Derived>& v)
|
||||
{
|
||||
template <typename Derived>
|
||||
const Eigen::VectorBlock<const Derived, 2> firstTwo(const Eigen::MatrixBase<Derived>& v) {
|
||||
return Eigen::VectorBlock<const Derived, 2>(v.derived(), 0);
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
Eigen::Matrix<int,1,6> v; v << 1,2,3,4,5,6;
|
||||
std::cout << firstTwo(4*v) << std::endl; // calls the const version
|
||||
firstTwo(v) *= 2; // calls the non-const version
|
||||
int main(int, char**) {
|
||||
Eigen::Matrix<int, 1, 6> v;
|
||||
v << 1, 2, 3, 4, 5, 6;
|
||||
std::cout << firstTwo(4 * v) << std::endl; // calls the const version
|
||||
firstTwo(v) *= 2; // calls the non-const version
|
||||
std::cout << "Now the vector v is:" << std::endl << v << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Derived>
|
||||
const Eigen::Reshaped<const Derived>
|
||||
reshape_helper(const Eigen::MatrixBase<Derived>& m, int rows, int cols)
|
||||
{
|
||||
template <typename Derived>
|
||||
const Eigen::Reshaped<const Derived> reshape_helper(const Eigen::MatrixBase<Derived>& m, int rows, int cols) {
|
||||
return Eigen::Reshaped<const Derived>(m.derived(), rows, cols);
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
int main(int, char**) {
|
||||
Eigen::MatrixXd m(3, 4);
|
||||
m << 1, 4, 7, 10,
|
||||
2, 5, 8, 11,
|
||||
3, 6, 9, 12;
|
||||
m << 1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12;
|
||||
std::cout << m << std::endl;
|
||||
Eigen::Ref<const Eigen::MatrixXd> n = reshape_helper(m, 2, 6);
|
||||
std::cout << "Matrix m is:" << std::endl << m << std::endl;
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Derived>
|
||||
Eigen::VectorBlock<Derived>
|
||||
segmentFromRange(Eigen::MatrixBase<Derived>& v, int start, int end)
|
||||
{
|
||||
return Eigen::VectorBlock<Derived>(v.derived(), start, end-start);
|
||||
template <typename Derived>
|
||||
Eigen::VectorBlock<Derived> segmentFromRange(Eigen::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 Eigen::MatrixBase<Derived>& v, int start, int end)
|
||||
{
|
||||
return Eigen::VectorBlock<const Derived>(v.derived(), start, end-start);
|
||||
template <typename Derived>
|
||||
const Eigen::VectorBlock<const Derived> segmentFromRange(const Eigen::MatrixBase<Derived>& v, int start, int end) {
|
||||
return Eigen::VectorBlock<const Derived>(v.derived(), start, end - start);
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
Eigen::Matrix<int,1,6> v; v << 1,2,3,4,5,6;
|
||||
std::cout << segmentFromRange(2*v, 2, 4) << std::endl; // calls the const version
|
||||
segmentFromRange(v, 1, 3) *= 5; // calls the non-const version
|
||||
int main(int, char**) {
|
||||
Eigen::Matrix<int, 1, 6> v;
|
||||
v << 1, 2, 3, 4, 5, 6;
|
||||
std::cout << segmentFromRange(2 * v, 2, 4) << std::endl; // calls the const version
|
||||
segmentFromRange(v, 1, 3) *= 5; // calls the non-const version
|
||||
std::cout << "Now the vector v is:" << std::endl << v << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,16 +2,13 @@
|
||||
#include <Eigen/Core>
|
||||
|
||||
template <typename Derived>
|
||||
void print_size(const Eigen::EigenBase<Derived>& b)
|
||||
{
|
||||
std::cout << "size (rows, cols): " << b.size() << " (" << b.rows()
|
||||
<< ", " << b.cols() << ")" << std::endl;
|
||||
void print_size(const Eigen::EigenBase<Derived>& b) {
|
||||
std::cout << "size (rows, cols): " << b.size() << " (" << b.rows() << ", " << b.cols() << ")" << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Vector3f v;
|
||||
print_size(v);
|
||||
// v.asDiagonal() returns a 3x3 diagonal matrix pseudo-expression
|
||||
print_size(v.asDiagonal());
|
||||
int main() {
|
||||
Eigen::Vector3f v;
|
||||
print_size(v);
|
||||
// v.asDiagonal() returns a 3x3 diagonal matrix pseudo-expression
|
||||
print_size(v.asDiagonal());
|
||||
}
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/SVD>
|
||||
|
||||
float inv_cond(const Eigen::Ref<const Eigen::MatrixXf>& a)
|
||||
{
|
||||
float inv_cond(const Eigen::Ref<const Eigen::MatrixXf>& a) {
|
||||
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() {
|
||||
Eigen::MatrixXf m = Eigen::MatrixXf::Random(4, 4);
|
||||
std::cout << "matrix m:\n" << m << "\n\n";
|
||||
std::cout << "inv_cond(m): " << inv_cond(m) << "\n";
|
||||
std::cout << "inv_cond(m(1:3,1:3)): " << inv_cond(m.topLeftCorner(3,3)) << "\n";
|
||||
std::cout << "inv_cond(m+I): " << inv_cond(m+Eigen::MatrixXf::Identity(4, 4)) << "\n";
|
||||
std::cout << "inv_cond(m): " << inv_cond(m) << "\n";
|
||||
std::cout << "inv_cond(m(1:3,1:3)): " << inv_cond(m.topLeftCorner(3, 3)) << "\n";
|
||||
std::cout << "inv_cond(m+I): " << inv_cond(m + Eigen::MatrixXf::Identity(4, 4)) << "\n";
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
template <class ArgType>
|
||||
Circulant<ArgType> makeCirculant(const Eigen::MatrixBase<ArgType>& arg)
|
||||
{
|
||||
Circulant<ArgType> makeCirculant(const Eigen::MatrixBase<ArgType>& arg) {
|
||||
return Circulant<ArgType>(arg.derived());
|
||||
}
|
||||
|
||||
@@ -1,32 +1,24 @@
|
||||
namespace Eigen {
|
||||
namespace internal {
|
||||
template<typename ArgType>
|
||||
struct evaluator<Circulant<ArgType> >
|
||||
: evaluator_base<Circulant<ArgType> >
|
||||
{
|
||||
typedef Circulant<ArgType> XprType;
|
||||
typedef typename nested_eval<ArgType, XprType::ColsAtCompileTime>::type ArgTypeNested;
|
||||
typedef remove_all_t<ArgTypeNested> ArgTypeNestedCleaned;
|
||||
typedef typename XprType::CoeffReturnType CoeffReturnType;
|
||||
namespace internal {
|
||||
template <typename ArgType>
|
||||
struct evaluator<Circulant<ArgType> > : evaluator_base<Circulant<ArgType> > {
|
||||
typedef Circulant<ArgType> XprType;
|
||||
typedef typename nested_eval<ArgType, XprType::ColsAtCompileTime>::type ArgTypeNested;
|
||||
typedef remove_all_t<ArgTypeNested> ArgTypeNestedCleaned;
|
||||
typedef typename XprType::CoeffReturnType CoeffReturnType;
|
||||
|
||||
enum {
|
||||
CoeffReadCost = evaluator<ArgTypeNestedCleaned>::CoeffReadCost,
|
||||
Flags = Eigen::ColMajor
|
||||
};
|
||||
|
||||
evaluator(const XprType& xpr)
|
||||
: m_argImpl(xpr.m_arg), m_rows(xpr.rows())
|
||||
{ }
|
||||
enum { CoeffReadCost = evaluator<ArgTypeNestedCleaned>::CoeffReadCost, Flags = Eigen::ColMajor };
|
||||
|
||||
CoeffReturnType coeff(Index row, Index col) const
|
||||
{
|
||||
Index index = row - col;
|
||||
if (index < 0) index += m_rows;
|
||||
return m_argImpl.coeff(index);
|
||||
}
|
||||
evaluator(const XprType& xpr) : m_argImpl(xpr.m_arg), m_rows(xpr.rows()) {}
|
||||
|
||||
evaluator<ArgTypeNestedCleaned> m_argImpl;
|
||||
const Index m_rows;
|
||||
};
|
||||
CoeffReturnType coeff(Index row, Index col) const {
|
||||
Index index = row - col;
|
||||
if (index < 0) index += m_rows;
|
||||
return m_argImpl.coeff(index);
|
||||
}
|
||||
}
|
||||
|
||||
evaluator<ArgTypeNestedCleaned> m_argImpl;
|
||||
const Index m_rows;
|
||||
};
|
||||
} // namespace internal
|
||||
} // namespace Eigen
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
template <class ArgType>
|
||||
class Circulant : public Eigen::MatrixBase<Circulant<ArgType> >
|
||||
{
|
||||
public:
|
||||
Circulant(const ArgType& arg)
|
||||
: m_arg(arg)
|
||||
{
|
||||
EIGEN_STATIC_ASSERT(ArgType::ColsAtCompileTime == 1,
|
||||
YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
|
||||
class Circulant : public Eigen::MatrixBase<Circulant<ArgType> > {
|
||||
public:
|
||||
Circulant(const ArgType& arg) : m_arg(arg) {
|
||||
EIGEN_STATIC_ASSERT(ArgType::ColsAtCompileTime == 1, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
|
||||
}
|
||||
|
||||
typedef typename Eigen::internal::ref_selector<Circulant>::type Nested;
|
||||
typedef typename Eigen::internal::ref_selector<Circulant>::type Nested;
|
||||
|
||||
typedef Eigen::Index Index;
|
||||
Index rows() const { return m_arg.rows(); }
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::VectorXd vec(4);
|
||||
vec << 1, 2, 4, 8;
|
||||
Eigen::MatrixXd mat;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <Eigen/Core>
|
||||
#include <iostream>
|
||||
|
||||
template <class ArgType> class Circulant;
|
||||
template <class ArgType>
|
||||
class Circulant;
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
namespace Eigen {
|
||||
namespace internal {
|
||||
template <class ArgType>
|
||||
struct traits<Circulant<ArgType> >
|
||||
{
|
||||
typedef Eigen::Dense StorageKind;
|
||||
typedef Eigen::MatrixXpr XprKind;
|
||||
typedef typename ArgType::StorageIndex StorageIndex;
|
||||
typedef typename ArgType::Scalar Scalar;
|
||||
enum {
|
||||
Flags = Eigen::ColMajor,
|
||||
RowsAtCompileTime = ArgType::RowsAtCompileTime,
|
||||
ColsAtCompileTime = ArgType::RowsAtCompileTime,
|
||||
MaxRowsAtCompileTime = ArgType::MaxRowsAtCompileTime,
|
||||
MaxColsAtCompileTime = ArgType::MaxRowsAtCompileTime
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
namespace internal {
|
||||
template <class ArgType>
|
||||
struct traits<Circulant<ArgType> > {
|
||||
typedef Eigen::Dense StorageKind;
|
||||
typedef Eigen::MatrixXpr XprKind;
|
||||
typedef typename ArgType::StorageIndex StorageIndex;
|
||||
typedef typename ArgType::Scalar Scalar;
|
||||
enum {
|
||||
Flags = Eigen::ColMajor,
|
||||
RowsAtCompileTime = ArgType::RowsAtCompileTime,
|
||||
ColsAtCompileTime = ArgType::RowsAtCompileTime,
|
||||
MaxRowsAtCompileTime = ArgType::MaxRowsAtCompileTime,
|
||||
MaxColsAtCompileTime = ArgType::MaxRowsAtCompileTime
|
||||
};
|
||||
};
|
||||
} // namespace internal
|
||||
} // namespace Eigen
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
#include <iostream>
|
||||
|
||||
// [circulant_func]
|
||||
template<class ArgType>
|
||||
template <class ArgType>
|
||||
class circulant_functor {
|
||||
const ArgType &m_vec;
|
||||
public:
|
||||
const ArgType& m_vec;
|
||||
|
||||
public:
|
||||
circulant_functor(const ArgType& arg) : m_vec(arg) {}
|
||||
|
||||
const typename ArgType::Scalar& operator() (Eigen::Index row, Eigen::Index col) const {
|
||||
const typename ArgType::Scalar& operator()(Eigen::Index row, Eigen::Index col) const {
|
||||
Eigen::Index index = row - col;
|
||||
if (index < 0) index += m_vec.size();
|
||||
return m_vec(index);
|
||||
@@ -17,30 +18,25 @@ public:
|
||||
// [circulant_func]
|
||||
|
||||
// [square]
|
||||
template<class ArgType>
|
||||
template <class ArgType>
|
||||
struct circulant_helper {
|
||||
typedef Eigen::Matrix<typename ArgType::Scalar,
|
||||
ArgType::SizeAtCompileTime,
|
||||
ArgType::SizeAtCompileTime,
|
||||
Eigen::ColMajor,
|
||||
ArgType::MaxSizeAtCompileTime,
|
||||
ArgType::MaxSizeAtCompileTime> MatrixType;
|
||||
typedef Eigen::Matrix<typename ArgType::Scalar, ArgType::SizeAtCompileTime, ArgType::SizeAtCompileTime,
|
||||
Eigen::ColMajor, ArgType::MaxSizeAtCompileTime, ArgType::MaxSizeAtCompileTime>
|
||||
MatrixType;
|
||||
};
|
||||
// [square]
|
||||
|
||||
// [makeCirculant]
|
||||
template <class ArgType>
|
||||
Eigen::CwiseNullaryOp<circulant_functor<ArgType>, typename circulant_helper<ArgType>::MatrixType>
|
||||
makeCirculant(const Eigen::MatrixBase<ArgType>& arg)
|
||||
{
|
||||
Eigen::CwiseNullaryOp<circulant_functor<ArgType>, typename circulant_helper<ArgType>::MatrixType> makeCirculant(
|
||||
const Eigen::MatrixBase<ArgType>& arg) {
|
||||
typedef typename circulant_helper<ArgType>::MatrixType MatrixType;
|
||||
return MatrixType::NullaryExpr(arg.size(), arg.size(), circulant_functor<ArgType>(arg.derived()));
|
||||
}
|
||||
// [makeCirculant]
|
||||
|
||||
// [main]
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::VectorXd vec(4);
|
||||
vec << 1, 2, 4, 8;
|
||||
Eigen::MatrixXd mat;
|
||||
|
||||
@@ -9,81 +9,70 @@ using Eigen::SparseMatrix;
|
||||
|
||||
namespace Eigen {
|
||||
namespace internal {
|
||||
// MatrixReplacement looks-like a SparseMatrix, so let's inherit its traits:
|
||||
template<>
|
||||
struct traits<MatrixReplacement> : public Eigen::internal::traits<Eigen::SparseMatrix<double> >
|
||||
{};
|
||||
}
|
||||
}
|
||||
// MatrixReplacement looks-like a SparseMatrix, so let's inherit its traits:
|
||||
template <>
|
||||
struct traits<MatrixReplacement> : public Eigen::internal::traits<Eigen::SparseMatrix<double> > {};
|
||||
} // namespace internal
|
||||
} // namespace Eigen
|
||||
|
||||
// Example of a matrix-free wrapper from a user type to Eigen's compatible type
|
||||
// For the sake of simplicity, this example simply wrap a Eigen::SparseMatrix.
|
||||
class MatrixReplacement : public Eigen::EigenBase<MatrixReplacement> {
|
||||
public:
|
||||
public:
|
||||
// Required typedefs, constants, and method:
|
||||
typedef double Scalar;
|
||||
typedef double RealScalar;
|
||||
typedef int StorageIndex;
|
||||
enum {
|
||||
ColsAtCompileTime = Eigen::Dynamic,
|
||||
MaxColsAtCompileTime = Eigen::Dynamic,
|
||||
IsRowMajor = false
|
||||
};
|
||||
enum { ColsAtCompileTime = Eigen::Dynamic, MaxColsAtCompileTime = Eigen::Dynamic, IsRowMajor = false };
|
||||
|
||||
Index rows() const { return mp_mat->rows(); }
|
||||
Index cols() const { return mp_mat->cols(); }
|
||||
|
||||
template<typename Rhs>
|
||||
Eigen::Product<MatrixReplacement,Rhs,Eigen::AliasFreeProduct> operator*(const Eigen::MatrixBase<Rhs>& x) const {
|
||||
return Eigen::Product<MatrixReplacement,Rhs,Eigen::AliasFreeProduct>(*this, x.derived());
|
||||
template <typename Rhs>
|
||||
Eigen::Product<MatrixReplacement, Rhs, Eigen::AliasFreeProduct> operator*(const Eigen::MatrixBase<Rhs>& x) const {
|
||||
return Eigen::Product<MatrixReplacement, Rhs, Eigen::AliasFreeProduct>(*this, x.derived());
|
||||
}
|
||||
|
||||
// Custom API:
|
||||
MatrixReplacement() : mp_mat(0) {}
|
||||
|
||||
void attachMyMatrix(const SparseMatrix<double> &mat) {
|
||||
mp_mat = &mat;
|
||||
}
|
||||
void attachMyMatrix(const SparseMatrix<double>& mat) { mp_mat = &mat; }
|
||||
const SparseMatrix<double> my_matrix() const { return *mp_mat; }
|
||||
|
||||
private:
|
||||
const SparseMatrix<double> *mp_mat;
|
||||
private:
|
||||
const SparseMatrix<double>* mp_mat;
|
||||
};
|
||||
|
||||
|
||||
// Implementation of MatrixReplacement * Eigen::DenseVector though a specialization of internal::generic_product_impl:
|
||||
namespace Eigen {
|
||||
namespace internal {
|
||||
|
||||
template<typename Rhs>
|
||||
struct generic_product_impl<MatrixReplacement, Rhs, SparseShape, DenseShape, GemvProduct> // GEMV stands for matrix-vector
|
||||
: generic_product_impl_base<MatrixReplacement,Rhs,generic_product_impl<MatrixReplacement,Rhs> >
|
||||
{
|
||||
typedef typename Product<MatrixReplacement,Rhs>::Scalar Scalar;
|
||||
template <typename Rhs>
|
||||
struct generic_product_impl<MatrixReplacement, Rhs, SparseShape, DenseShape,
|
||||
GemvProduct> // GEMV stands for matrix-vector
|
||||
: generic_product_impl_base<MatrixReplacement, Rhs, generic_product_impl<MatrixReplacement, Rhs> > {
|
||||
typedef typename Product<MatrixReplacement, Rhs>::Scalar Scalar;
|
||||
|
||||
template<typename Dest>
|
||||
static void scaleAndAddTo(Dest& dst, const MatrixReplacement& lhs, const Rhs& rhs, const Scalar& alpha)
|
||||
{
|
||||
// This method should implement "dst += alpha * lhs * rhs" inplace,
|
||||
// however, for iterative solvers, alpha is always equal to 1, so let's not bother about it.
|
||||
eigen_assert(alpha==Scalar(1) && "scaling is not implemented");
|
||||
EIGEN_ONLY_USED_FOR_DEBUG(alpha);
|
||||
template <typename Dest>
|
||||
static void scaleAndAddTo(Dest& dst, const MatrixReplacement& lhs, const Rhs& rhs, const Scalar& alpha) {
|
||||
// This method should implement "dst += alpha * lhs * rhs" inplace,
|
||||
// however, for iterative solvers, alpha is always equal to 1, so let's not bother about it.
|
||||
eigen_assert(alpha == Scalar(1) && "scaling is not implemented");
|
||||
EIGEN_ONLY_USED_FOR_DEBUG(alpha);
|
||||
|
||||
// Here we could simply call dst.noalias() += lhs.my_matrix() * rhs,
|
||||
// but let's do something fancier (and less efficient):
|
||||
for(Index i=0; i<lhs.cols(); ++i)
|
||||
dst += rhs(i) * lhs.my_matrix().col(i);
|
||||
}
|
||||
};
|
||||
// Here we could simply call dst.noalias() += lhs.my_matrix() * rhs,
|
||||
// but let's do something fancier (and less efficient):
|
||||
for (Index i = 0; i < lhs.cols(); ++i) dst += rhs(i) * lhs.my_matrix().col(i);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
} // namespace internal
|
||||
} // namespace Eigen
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n = 10;
|
||||
Eigen::SparseMatrix<double> S = Eigen::MatrixXd::Random(n,n).sparseView(0.5,1);
|
||||
S = S.transpose()*S;
|
||||
Eigen::SparseMatrix<double> S = Eigen::MatrixXd::Random(n, n).sparseView(0.5, 1);
|
||||
S = S.transpose() * S;
|
||||
|
||||
MatrixReplacement A;
|
||||
A.attachMyMatrix(S);
|
||||
@@ -93,7 +82,7 @@ int main()
|
||||
|
||||
// Solve Ax = b using various iterative solver with matrix-free version:
|
||||
{
|
||||
Eigen::ConjugateGradient<MatrixReplacement, Eigen::Lower|Eigen::Upper, Eigen::IdentityPreconditioner> cg;
|
||||
Eigen::ConjugateGradient<MatrixReplacement, Eigen::Lower | Eigen::Upper, Eigen::IdentityPreconditioner> cg;
|
||||
cg.compute(A);
|
||||
x = cg.solve(b);
|
||||
std::cout << "CG: #iterations: " << cg.iterations() << ", estimated error: " << cg.error() << std::endl;
|
||||
@@ -121,9 +110,10 @@ int main()
|
||||
}
|
||||
|
||||
{
|
||||
Eigen::MINRES<MatrixReplacement, Eigen::Lower|Eigen::Upper, Eigen::IdentityPreconditioner> minres;
|
||||
Eigen::MINRES<MatrixReplacement, Eigen::Lower | Eigen::Upper, Eigen::IdentityPreconditioner> minres;
|
||||
minres.compute(A);
|
||||
x = minres.solve(b);
|
||||
std::cout << "MINRES: #iterations: " << minres.iterations() << ", estimated error: " << minres.error() << std::endl;
|
||||
std::cout << "MINRES: #iterations: " << minres.iterations() << ", estimated error: " << minres.error()
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,24 +2,22 @@
|
||||
#include <iostream>
|
||||
|
||||
// [functor]
|
||||
template<class ArgType, class RowIndexType, class ColIndexType>
|
||||
template <class ArgType, class RowIndexType, class ColIndexType>
|
||||
class indexing_functor {
|
||||
const ArgType &m_arg;
|
||||
const RowIndexType &m_rowIndices;
|
||||
const ColIndexType &m_colIndices;
|
||||
public:
|
||||
typedef Eigen::Matrix<typename ArgType::Scalar,
|
||||
RowIndexType::SizeAtCompileTime,
|
||||
ColIndexType::SizeAtCompileTime,
|
||||
ArgType::Flags&Eigen::RowMajorBit?Eigen::RowMajor:Eigen::ColMajor,
|
||||
RowIndexType::MaxSizeAtCompileTime,
|
||||
ColIndexType::MaxSizeAtCompileTime> MatrixType;
|
||||
const ArgType& m_arg;
|
||||
const RowIndexType& m_rowIndices;
|
||||
const ColIndexType& m_colIndices;
|
||||
|
||||
public:
|
||||
typedef Eigen::Matrix<typename ArgType::Scalar, RowIndexType::SizeAtCompileTime, ColIndexType::SizeAtCompileTime,
|
||||
ArgType::Flags & Eigen::RowMajorBit ? Eigen::RowMajor : Eigen::ColMajor,
|
||||
RowIndexType::MaxSizeAtCompileTime, ColIndexType::MaxSizeAtCompileTime>
|
||||
MatrixType;
|
||||
|
||||
indexing_functor(const ArgType& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
|
||||
: 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() (Eigen::Index row, Eigen::Index col) const {
|
||||
const typename ArgType::Scalar& operator()(Eigen::Index row, Eigen::Index col) const {
|
||||
return m_arg(m_rowIndices[row], m_colIndices[col]);
|
||||
}
|
||||
};
|
||||
@@ -27,22 +25,21 @@ public:
|
||||
|
||||
// [function]
|
||||
template <class ArgType, class RowIndexType, class ColIndexType>
|
||||
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)
|
||||
{
|
||||
typedef indexing_functor<ArgType,RowIndexType,ColIndexType> Func;
|
||||
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) {
|
||||
typedef indexing_functor<ArgType, RowIndexType, ColIndexType> Func;
|
||||
typedef typename Func::MatrixType MatrixType;
|
||||
return MatrixType::NullaryExpr(row_indices.size(), col_indices.size(), Func(arg.derived(), row_indices, col_indices));
|
||||
}
|
||||
// [function]
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::cout << "[main1]\n";
|
||||
Eigen::MatrixXi A = Eigen::MatrixXi::Random(4,4);
|
||||
Eigen::Array3i ri(1,2,1);
|
||||
Eigen::ArrayXi ci(6); ci << 3,2,1,0,0,2;
|
||||
Eigen::MatrixXi A = Eigen::MatrixXi::Random(4, 4);
|
||||
Eigen::Array3i ri(1, 2, 1);
|
||||
Eigen::ArrayXi ci(6);
|
||||
ci << 3, 2, 1, 0, 0, 2;
|
||||
Eigen::MatrixXi B = mat_indexing(A, ri, ci);
|
||||
std::cout << "A =" << std::endl;
|
||||
std::cout << A << std::endl << std::endl;
|
||||
@@ -51,12 +48,13 @@ int main()
|
||||
std::cout << "[main1]\n";
|
||||
|
||||
std::cout << "[main2]\n";
|
||||
B = mat_indexing(A, ri+1, ci);
|
||||
B = mat_indexing(A, ri + 1, ci);
|
||||
std::cout << "A(ri+1,ci) =" << std::endl;
|
||||
std::cout << B << std::endl << std::endl;
|
||||
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;
|
||||
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 << B << std::endl << std::endl;
|
||||
std::cout << "[main2]\n";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::Matrix2d a;
|
||||
a << 1, 2,
|
||||
3, 4;
|
||||
Eigen::MatrixXd b(2,2);
|
||||
b << 2, 3,
|
||||
1, 4;
|
||||
a << 1, 2, 3, 4;
|
||||
Eigen::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;
|
||||
Eigen::Vector3d v(1,2,3);
|
||||
Eigen::Vector3d w(1,0,0);
|
||||
Eigen::Vector3d v(1, 2, 3);
|
||||
Eigen::Vector3d w(1, 0, 0);
|
||||
std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::Vector3d v(1,2,3);
|
||||
Eigen::Vector3d w(0,1,2);
|
||||
int main() {
|
||||
Eigen::Vector3d v(1, 2, 3);
|
||||
Eigen::Vector3d w(0, 1, 2);
|
||||
|
||||
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
|
||||
std::cout << "Dot product via a matrix product: " << dp << std::endl;
|
||||
|
||||
std::cout << "Cross product:\n" << v.cross(w) << std::endl;
|
||||
Eigen::Vector2d v2(1,2);
|
||||
Eigen::Vector2d w2(0,1);
|
||||
double cp = v2.cross(w2); // returning a scalar between size-2 vectors
|
||||
Eigen::Vector2d v2(1, 2);
|
||||
Eigen::Vector2d w2(0, 1);
|
||||
double cp = v2.cross(w2); // returning a scalar between size-2 vectors
|
||||
std::cout << "Cross product for 2D vectors: " << cp << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::Matrix2d mat;
|
||||
mat << 1, 2,
|
||||
3, 4;
|
||||
Eigen::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;
|
||||
mat << 1, 2, 3, 4;
|
||||
Eigen::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;
|
||||
mat = mat * mat;
|
||||
std::cout << "Now mat is mat:\n" << mat << std::endl;
|
||||
}
|
||||
|
||||
@@ -2,15 +2,13 @@
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::Matrix2d a;
|
||||
a << 1, 2,
|
||||
3, 4;
|
||||
Eigen::Vector3d v(1,2,3);
|
||||
a << 1, 2, 3, 4;
|
||||
Eigen::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;
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::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);
|
||||
int main() {
|
||||
Eigen::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;
|
||||
Eigen::VectorXd v(2);
|
||||
v(0) = 4;
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
Eigen::MatrixXd m(2,5);
|
||||
m.resize(4,3);
|
||||
std::cout << "The matrix m is of size "
|
||||
<< m.rows() << "x" << m.cols() << std::endl;
|
||||
int main() {
|
||||
Eigen::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;
|
||||
Eigen::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;
|
||||
std::cout << "As a matrix, v is of size " << v.rows() << "x" << v.cols() << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Eigen::Matrix4d m;
|
||||
m.resize(4,4); // no operation
|
||||
std::cout << "The matrix m is of size "
|
||||
<< m.rows() << "x" << m.cols() << std::endl;
|
||||
m.resize(4, 4); // no operation
|
||||
std::cout << "The matrix m is of size " << m.rows() << "x" << m.cols() << std::endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user