首页 > 其他分享 >Eigen

Eigen

时间:2024-02-26 09:02:38浏览次数:18  
标签:Eigen namespace 矩阵 using include MatrixXf


c++# #eigen# #线性代数# #矩阵# #lib#


Eigen是一个轻量级的矩阵库,除了稀疏矩阵不成熟以外,其他的矩阵和向量操作都比较完善,而且速度不错.

#include <Eigen/Dense>
using namespace Eigen;
int main()
{
	MatrixXf m1(3,4);   //动态矩阵,建立3行4列。
	MatrixXf m2(4,3);	//4行3列,依此类推。
	MatrixXf m3(3,3);
 
	Vector3f v1;		//若是静态数组,则不用指定行或者列
	/* 初始化 */
	Matrix3d m = Matrix3d::Random();
	m1 = MatrixXf::Zero(3,4);		//用0矩阵初始化,要指定行列数
	m2 = MatrixXf::Zero(4,3);
	m3 = MatrixXf::Identity(3,3);	//用单位矩阵初始化
	v1 = Vector3f::Zero();			//同理,若是静态的,不用指定行列数
 
	m1 << 1,0,0,1,		//也可以以这种方式初始化
		1,5,0,1,
		0,0,9,1;
	m2 << 1,0,0,
		0,4,0,
		0,0,7,
		1,1,1;
}

使用Map函数,可以实现Eigen的矩阵和c++中的数组直接转换,语法如下:

//@param MatrixType 矩阵类型
//@param MapOptions 指的是指针是否对齐,Aligned, or Unaligned. The default is Unaligned.
//@param StrideType 
/*
	Map<typename MatrixType,
		int MapOptions,
		typename StrideType>
*/
//pf是float数组 mf是flost矩阵
Map<MatrixXf> mf(pf,rows,columns);
//mi是const Vector4i,pi是int*
Map<const Vector4i> mi(pi);

eigen重载了基础的+ - * / += -= *= /=
*可以表示标量和矩阵或者矩阵和矩阵

#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
Matrix2d a;
 a << 1, 2,
 3, 4;
 MatrixXd b(2,2);
 b << 2, 3,
 1, 4;
 std::cout << "a + b =\n" << a + b << std::endl;
 std::cout << "a - b =\n" << a - b << std::endl;
 std::cout << "Doing a += b;" << std::endl;
 a += b;
 std::cout << "Now a =\n" << a << std::endl;
 Vector3d v(1,2,3);
 Vector3d w(1,0,0);
 std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
 a << 1, 2,
 3, 4;
 Vector3d v(1,2,3);
 std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;
 std::cout << "0.1 * v =\n" << 0.1 * v << std::endl;
 std::cout << "Doing v *= 2;" << std::endl;
 v *= 2;
 std::cout << "Now v =\n" << v << std::endl;
}
/*
	a + b =
	3 5
	4 8
	a - b =
	-1 -1
	 2  0
	Doing a += b;
	Now a =
	3 5
	4 8
	-v + w - v =
	-1
	-4
	-6
	a * 2.5 =
	2.5  5
	7.5 10
	0.1 * v =
	0.1
	0.2
	0.3
	Doing v *= 2;
	Now v =
	2
	4
	6
*/
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main()
{
	Vector3d v(1,2,3);
	Vector3d w(0,1,2);
	 cout << "Dot product: " << v.dot(w) << endl;
	double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar
	 cout << "Dot product via a matrix product: " << dp << endl;
	 cout << "Cross product:\n" << v.cross(w) << endl;
}
/*
Dot product: 8
Dot product via a matrix product: 8
Cross product:
1
-2
1
*/

解 ax = b

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

除了colPivHouseholderQr,还有以下的函数:

Decomposition Method 矩阵特殊要求 速度
PartialPivLU partialPivLu() 可逆 ++
FullPivLU fullPivLu() None -
HouseholderQR householderQr() None ++
ColPivHouseholderQR colPivHouseholderQr() None +
FullPivHouseholderQR fullPivHouseholderQr() None -
LLT llt() 正定 +++
LDLT ldlt() 正或负半定 Positive or negative semidefinite +++
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
Matrix2f A;
 A << 1, 2, 2, 3;
 cout << "Here is the matrix A:\n" << A << endl;
SelfAdjointEigenSolver<Matrix2f> eigensolver(A);
if (eigensolver.info() != Success) abort();
 cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl;
 cout << "Here's a matrix whose columns are eigenvectors of A \n"
 << "corresponding to these eigenvalues:\n"
 << eigensolver.eigenvectors() << endl;
}
/*
	Here is the matrix A:
	1 2
	2 3
	The eigenvalues of A are:
	-0.236
	4.24
	Here's a matrix whose columns are eigenvectors of A 
	corresponding to these eigenvalues:
	-0.851 -0.526
	 0.526 -0.851
*/

小矩阵(4 * 4及以下)eigen会自动优化,默认采用LU分解,效率不高

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
Matrix3f A;
 A << 1, 2, 1,
 2, 1, 0,
 -1, 1, 2;
 cout << "Here is the matrix A:\n" << A << endl;
 cout << "The determinant of A is " << A.determinant() << endl;
 cout << "The inverse of A is:\n" << A.inverse() << endl;
}
/*
	Here is the matrix A:
	1 2 1
	2 1 0
	-1 1 2
	The determinant of A is -3
	The inverse of A is:
	-0.667      1  0.333
	  1.33     -1 -0.667
		-1      1      1
*/
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
MatrixXf A = MatrixXf::Random(3, 2);
 cout << "Here is the matrix A:\n" << A << endl;
VectorXf b = VectorXf::Random(3);
 cout << "Here is the right hand side b:\n" << b << endl;
 cout << "The least-squares solution is:\n"
 << A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << endl;
}
/*
	Here is the matrix A:
	  0.68  0.597
	-0.211  0.823
	 0.566 -0.605
	Here is the right hand side b:
	-0.33
	0.536
	-0.444
	The least-squares solution is:
	-0.67
	0.314
*/

稀疏矩阵的头文件包括:

  #include <Eigen/SparseCore>
  #include <Eigen/SparseCholesky>
  #include <Eigen/IterativeLinearSolvers>
  #include <Eigen/Sparse>

初始化有两种方式:
1.使用三元组插入

typedef Eigen::Triplet<double> T;
std::vector<T> tripletList;
triplets.reserve(estimation_of_entries); //estimation_of_entries是预估的条目
for(...)
{
	tripletList.push_back(T(i,j,v_ij));//第 i,j个有值的位置的值
}
SparseMatrixType mat(rows,cols);
mat.setFromTriplets(tripletList.begin(), tripletList.end());
// mat is ready to go!

2.直接将已知的非0值插入

SparseMatrix<double> mat(rows,cols);
mat.reserve(VectorXi::Constant(cols,6));
for(...)
{
	// i,j 个非零值 v_ij != 0
	mat.insert(i,j) = v_ij;
}
mat.makeCompressed(); // optional

稀疏矩阵支持大部分一元和二元运算:

sm1.real() sm1.imag() -sm1 0.5*sm1
sm1+sm2 sm1-sm2 sm1.cwiseProduct(sm2)

二元运算中,稀疏矩阵和普通矩阵可以混合使用

//dm表示普通矩阵
dm2 = sm1 + dm1;

也支持计算转置矩阵和伴随矩阵

标签:Eigen,namespace,矩阵,using,include,MatrixXf
From: https://www.cnblogs.com/diphda/p/18033577/own-1yf4dc

相关文章

  • ../inst/include/Eigen/src/Core/MathFunctions.h:487:16: error: no member named 'R
    Asmentionedin conda-forge/r-base-feedstock#163(comment),IsuccessfullyinstalledsctransforminMacsiliconM1Maxbyfirstrun exportPKG_CPPFLAGS="-DHAVE_WORKING_LOG1P intheterminalandtheninstallthepackageinR.......
  • Eigen中变换矩阵Eigen::Isometry3d T的使用方法(左乘和右乘)
    https://zhuanlan.zhihu.com/p/610439768?utm_id=0 一、基本定义Eigen::Isometry3dT_imu_to_lidar=Eigen::Isometry3d::Identity()转换矩阵本质是一个4*4的矩阵二、操作方法.translation():无参数,返回当前变换平移部分的向量表示(可修改),可以索引[]获取各分量.rotation(......
  • Eigen库
    前言  最近在学习计算机图形学的一些知识,里面要大量运用像矩阵运算这样的线性代数知识,而Eigen库就是一个支持我们用c++语言进行线性代数、矩阵和矢量运算的第三方库。想着后续真正开始写代码后这个库用的应该挺多的,于是就先来简单学习一下这个库。另外学习这个之前最好还是先了......
  • Eigen
     http://eigen.tuxfamily.org/index.php?title=Main_Page EigenisaC++templatelibraryforlinearalgebra:matrices,vectors,numericalsolvers,andrelatedalgorithms. wgethttp://bitbucket.org/eigen/eigen/get/3.3.7.tar.bz2tar-jxvf3.3.7.tar.bz2Howto&q......
  • [转]By not providing "FindEigen3.cmake" in CMAKE_MODULE_PATH this project has
    在编译安装的时候出现如下问题,是Eigen3的Cmake依赖问题,已经安装eigen3,但在项目的find_package(Eigen3QUERIED)中,无法找到FindEigen3.Cmake. CMakeErroratloam_velodyne/CMakeLists.txt:13(find_package):Bynotproviding"FindEigen3.cmake"inCMAKE_MODULE_......
  • Ubuntu18.04 安装Opencv3.4.15、PCL1.8.1、VTK7.1.0、Eigen3.4、Pangolin0.6、Sophus
    Eigen3.4安装方法mkdirbuild&&cdbuildcmake..sudomakeinstall安装后头文件安装在/usr/local/include/eigen3/,可以打开看一看安装的库Pangolin0.6安装方法+安装依赖项目sudoapt-getinstalllibglew-devsudoapt-getinstalllibboost-devli......
  • Eigen库操作
    #include<iostream>#include<eigen3/Eigen/Dense>usingnamespacestd;usingnamespaceEigen;intmain(){Matrix2fss;ss<<2.3f,3.2f,3.4f,3.1f;cout<<ss<<endl;cout<<"======="<<endl;......
  • Eigen::Tensor实现permute方法
    需求使用C++处理Eigen::Tensor希望交换指定维度的位置注意是交换(改变内存顺序)而不是reshape实现torch.tensor中内置了permute方法实现快速交换Eigen::Tensor中实现相同操作需要一点技巧例如,将一个1x2x3的tensor排列为3x1x2那么对应t1[0,1,1]==t2[1,0,1]则排列生效代码如......
  • eigen库稀疏矩阵
    使用Eigen的稀疏矩阵库可以有效地处理具有大量零元素的矩阵,以节省内存和计算资源。eigen库稀疏矩阵:SparseMatrix<double>Hsta=SparseMatrix<double>(4,4);//定义一个大小为4*4名字为Hsta的稀疏矩阵。插入数据 Hsta.insert(0,0)=1;//向Hsta的1行1列插入数字1。......
  • 安装eigen
    1安装eigensudoapt-getinstallgdb查看版本pkg-config--modversioneigen33.4.0查看安装的路径dpkg-Llibeigen3-dev/./usr/usr/include/usr/include/eigen3/usr/include/eigen3/Eigen/usr/include/eigen3/Eigen/Cholesky/usr/include/eigen3/Eigen/Cholmod......