Eigen库(2014-01-18 14:30:39)


首先到Eigen官网上下载Eigen源码包,下载后解压完直接放到自己平时软件所在的目录下(例如E:\EigenRoot,并在VS2010的附加包含目录下指定该路径即可),不需要安装。Eigen下载地址为
  http://eigen.tuxfamily.org/index.php?title=Main_Page
http://eigen.tuxfamily.org/dox/unsupported/group__MatrixFunctions__Module.html#title9
使用Eigen的第一个例子:
#include <iostream>
#include <vector>
#include <Eigen/Eigen>
using namespace Eigen;
using namespace std;
int main()
{
 /*
 v1 =
  5
  6
    result:
    20 25
    24 30
 */
 Eigen::Vector2d v1, v2;     //Eigen中的变量
 v1 << 5, 6;   //默认的向量为列向量
 cout  << "v1 = " << endl << v1 << endl;
 v2 << 4, 5 ;
 Matrix2d result = v1*v2.transpose();
 cout << "result: " << endl << result << endl;
 system("pause");
 return 0;
}
第二个例子,计算矩阵的基本初等函数:
#include <iostream>
#if 0
#include <vector>
#include <Eigen/Eigen>
#endif
#include <unsupported/Eigen/MatrixFunctions>
using namespace Eigen;
using namespace std;
int main()
{
 /*
 v1 =
  5
  6
    result:
    20 25
    24 30
 */
#if 0
 Eigen::Vector2d v1, v2;     //Eigen中的变量
 v1 << 5, 6;   //默认的向量为列向量
 cout  << "v1 = " << endl << v1 << endl;
 v2 << 4, 5 ;
 Matrix2d result = v1*v2.transpose();
 cout << "result: " << endl << result << endl;
#endif
 /*
  http://eigen.tuxfamily.org/dox/unsupported/group__MatrixFunctions__Module.html
 The matrix A is:
 0 -0.785398         0
 0.785398         0         0
 0         0         0
 The matrix exponential of A is:
 0.707107 -0.707107         0
 0.707107  0.707107         0
 0         0         1
 对上面的矩阵A求正弦的话会出现如下断言失败
 Assertion failed: "Taylor series does not converge" && 0, file e:\eigenroot\unsupported\eigen\src\matrixfunctions\matrixfunctionatomic.h, line 89
也就是泰勒级数sinA=A - A^3/3! + A^5/5! -A^7/7!.....对某些矩阵A不收敛,这一点跟实数域和复数域的情况不一样。
 cos(A) =
 1.32461       0       0
 0 1.32461       0
 0       0       1
 */
#if 1
 { 
  const double pi = std::acos(-1.0); 
  MatrixXd A(3,3); 
  A << 0,    -pi/4, 0,      
   pi/4, 0,     0,      
   0,    0,     0; 
  std::cout << "The matrix A is:\n" << A << "\n\n"; 
  std::cout << "The matrix exponential of A is:\n" << A.exp() << "\n\n";
  //A = MatrixXd::Random(3,3);
  //MatrixXd sinA = A.sin();
  //std::cout << "sin(A) = \n" << sinA << "\n\n";
  MatrixXd cosA = A.cos();
  std::cout << "cos(A) = \n" << cosA << "\n\n";
  // The matrix functions satisfy sin^2(A) + cos^2(A) = I,like the scalar functions.
  //std::cout << "sin^2(A) + cos^2(A) = \n" << sinA*sinA + cosA*cosA << "\n\n";
 }
#endif
 system("pause");
 return 0;
}
第三个例子:
/*
验证:
{{-3,1,-1},{-7,5,-1},{-6,6,-2}}^2={{8,-4,4},{-8,12,4},{-12,12,4}}
{{-3,1,-1},{-7,5,-1},{-6,6,-2}}^3={{-20,12,-12},{-84,76,-12},{-72,72,-8}}
矩阵代数是不可交换的,然而是结合的,至今研究过的大部分系统都是结合的。约定+只用于可交换的系统,而×不必受此限制。
m1:
-3  1 -1
-7  5 -1
-6  6 -2
m2:
 8 -4  4
-8 12  4
-12 12  4
m3:
-20  12 -12
-84  76 -12
-72  72  -8
*/
#if 1
 MatrixXf m1(3,3); 
 m1<<-3,1,-1,-7,5,-1,-6,6,-2;
 cout << "m1: " << endl << m1 << endl;
 MatrixXf m2 = m1*m1;
 cout << "m2: " << endl << m2 << endl;
 MatrixXf m3 = m1*m1*m1;
 cout << "m3: " << endl << m3 << endl;
#endif
第四个例子:
由矩阵的幂和无穷级数导出的等式:
sin{{4,6,0},{-3,-5,0},{-3,-6,1}}={{-2,0,-1},{1,0,1},{0,1,1}}{{sin1,0,0},{0,sin1,0},{0,0,-sin2}}{{-1,-1,0},{-1,-2,1},{1,2,0}}
cos{{2,0,0},{1,2,-1},{1,0,1}}发散,真的吗?
20131220注意:
{{-2,0,-1},{1,0,1},{0,1,1}}的逆矩阵是{{-1,-1,0},{-1,-2,1},{1,2,0}},而不是{{-1,-1,1},{-1,-2,2},{0,1,0}}。
A:
 4  6  0
-3 -5  0
-3 -6  1
B:
-2  0 -1
 1  0  1
 0  1  1
C:
 0.841471         0         0
        0  0.841471         0
        0         0 -0.909297
D:
-1 -1  0
-1 -2  1
 1  2  0
sin(A) =
      2.59224       3.50154 -1.35828e-008
     -1.75077      -2.66007  2.92391e-007
     -1.75077      -3.50154      0.841471
BCD:
 2.59224  3.50154        0
-1.75077 -2.66007        0
-1.75077 -3.50154 0.841471
#if 1
 MatrixXf A(3,3); 
 A<<4,6,0,-3,-5,0,-3,-6,1;
 cout << "A: " << endl << A << endl;
 MatrixXf B(3,3);
 B<<-2,0,-1,1,0,1,0,1,1;
 cout << "B: " << endl << B << endl;
 MatrixXf C(3,3);
 C<<sinf(1),0,0,0,sinf(1),0,0,0,-sinf(2);
 cout << "C: " << endl << C << endl;
#if 0
 MatrixXf D = B.inverse();
#else
 MatrixXf D(3,3);
 D<<-1,-1,0,-1,-2,1,1,2,0;
#endif
 cout << "D: " << endl << D << endl;
 MatrixXf sinA = A.sin();
 std::cout << "sin(A) = \n" << sinA << "\n\n";
 MatrixXf BCD = B*C*D;
 cout << "BCD: " << endl << BCD << endl;
#endif 
A:
 2  0  0
 1  2 -1
 1  0  1
cosA:
    -0.416147 -9.20451e-008  7.45058e-008
    -0.956449     -0.416147      0.956449
    -0.956449 -1.97194e-007      0.540302
#if 1
 MatrixXf A(3,3); 
 A<<2,0,0,1,2,-1,1,0,1;
 cout << "A: " << endl << A << endl;
 MatrixXf cosA = A.cos();
 cout << "cosA: " << endl << cosA << endl;
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值