您的位置:首页 > 编程语言 > MATLAB

C++矩阵库Eigen的仿Matlab矩阵运算

2015-01-18 13:13 239 查看
与其他矩阵库相比,Eigen(Visit)相比,Eigen只需要拷贝所有include文件到指定位置,无需编译即可使用;此外,用法上模仿Matlab矩阵操作;

上述特点,使其具有很好的实用性。

附上测试代码,以便学习和使用。

//http://eigen.tuxfamily.org/dox/group__QuickRefPage.html
#include <iostream>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

typedef Matrix<float, 1, 3> RVector;

int main()
{
int cnt = 0;
//定义一个矩阵并赋值
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);
cout << '[' << cnt++ << "] " << ": " << "m=" << endl;
cout << m << endl;
cout << "m.cols()=" << m.cols() << ", m.rows()=" << m.rows() << ", size()=" << m.size() << endl << endl;
m << 1, 2, 3, 4;   //先行后列
cout << '[' << cnt++ << "] " << ": " << "comma赋值,m=" << endl;
cout << m << endl;
cout << "m的第一行:" << m(1) << endl;

//产生一个随机矩阵
MatrixXd m1 = MatrixXd::Random(3,3);   //3 X 3 随机矩阵,值介于-1到1之间
cout << '[' << cnt++ << "] " << ": " << "m1 =" << endl << m1 << endl << endl;

MatrixXd m2 = MatrixXd::Constant(3,3,1.2);  //3 x 3 值为1.2的矩阵
cout << '[' << cnt++ << "] " << ": " <<  "m2 = " << endl;
cout << m2 << endl << endl;

m1 = (m1 + m2) * 5;
cout << '[' << cnt++ << "] " << ": " <<  "m1 = (m1 + m2) * 5 =" << endl << m1 << endl << endl;

//向量赋值与矩阵相乘---Comma-initialization
VectorXd v(3);
v << 1, 2, 3;
cout << '[' << cnt++ << "] " << ": v" << endl << v << endl << endl;
cout << '[' << cnt++ << "] " << "m1 * v =" << endl << m1 * v << endl << endl;

//通过循环为向量赋值
VectorXd v1(3);
for(int i = 0; i < 3; i++)
v1(i) = i;
cout << '[' << cnt++ << "] " << "列向量v1=" << endl << v1 << endl << endl;

//行向量
RVector rv;
for(int i = 0; i < 3; i++)
rv(i) = i;
cout << '[' << cnt++ << "] " << "行向量:" << rv << endl << endl;

//矩阵重新调整大小
MatrixXd m3 = MatrixXd::Random(3, 4);
cout << '[' << cnt++ << "] " << ": " <<  "m3 = " << endl;
cout << m3 << endl << endl;
cout << "m3.resize(5, 5)=" << endl;
m3.resize(5, 5);   //不保持原值
cout << m3 << endl << endl;

MatrixXd m4= MatrixXd::Random(3, 3);
cout << '[' << cnt++ << "] " << ": " <<  "m4 = " << endl;
cout << m4 << endl;
//矩阵的transpose转置
cout << "M4转置=" << endl;
cout << m4.transpose() << endl;
//矩阵的conjugate转置
cout << "M4共轭=" << endl;
cout << m4.conjugate() << endl;
//矩阵的adjoint转置
cout << "M4.adjoint=" << endl;
cout << m4.adjoint() << endl << endl;

//矩阵与标量的+,-,×,/运算略
//矩阵与矢量的运算
MatrixXd ma = MatrixXd::Random(2, 3);
MatrixXd vb = MatrixXd::Random(3, 1);
cout << '[' << cnt++ << "] " << ": " <<  "ma = " << endl;
cout << ma << endl;
cout << "vb = " << endl;
cout << vb << endl;
cout << "ma * vb = " << endl;
cout << ma * vb << endl;

//dot点乘和cross叉积略,见参考
return 0;
}


运行结果:

[0] : m=

  3  -1

2.5 1.5

m.cols()=2, m.rows()=2, size()=4

[1] : comma赋值,m=

1 2

3 4

m的第一行:3

[2] : m1 =

 0.680375   0.59688 -0.329554

-0.211234  0.823295  0.536459

 0.566198 -0.604897 -0.444451

[3] : m2 =

1.2 1.2 1.2

1.2 1.2 1.2

1.2 1.2 1.2

[4] : m1 = (m1 + m2) * 5 =

9.40188  8.9844 4.35223

4.94383 10.1165  8.6823

8.83099 2.97551 3.77775

[5] : v

1

2

3

[6] m1 * v =

40.4274

51.2237

26.1153

[7] 列向量v1=

0

1

2

[8] 行向量:0 1 2

[9] : m3 =

   0.10794  -0.270431    0.83239  -0.716795

-0.0452059  0.0268018   0.271423   0.213938

  0.257742   0.904459   0.434594  -0.967399

m3.resize(5, 5)=

6.91676e-310            0            0            0            0

6.91676e-310            0            0            0            0

  2.122e-314            0            0            0            0

  3.7008e-33            0            0            0            0

7.23757e-320            0            0            0            0

[10] : m4 =

-0.514226 -0.686642 -0.782382

-0.725537 -0.198111  0.997849

 0.608354 -0.740419 -0.563486

M4转置=

-0.514226 -0.725537  0.608354

-0.686642 -0.198111 -0.740419

-0.782382  0.997849 -0.563486

M4共轭=

-0.514226 -0.686642 -0.782382

-0.725537 -0.198111  0.997849

 0.608354 -0.740419 -0.563486

M4.adjoint=

-0.514226 -0.725537  0.608354

-0.686642 -0.198111 -0.740419

-0.782382  0.997849 -0.563486

[11] : ma =

0.0258648   0.22528  0.275105

 0.678224 -0.407937 0.0485744

vb =

-0.012834

  0.94555

-0.414966

ma * vb =

0.0985221

-0.414586
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: