跳过正文
  1. Posts/

Eigen: C++开源矩阵学习笔记

·736 字·2 分钟

接触 Eigen 的原因是最近在看 caffe/caffe2 源码,caffe2 中使用了 Eigen 库。Eigen 是一个基于 C++ 模板的线性代数库,直接将库下载后放在项目目录下,然后包含头文件就能使用,非常方便。对于 Linux 用户,只需要把头文件放到 /usr/include 下即可。此外,Eigen 的接口清晰,稳定高效。

之后会更新一些 Eigen 中我用过的函数。

ubuntu14.04LTS 下使用方式:
#

1sudo apt-get install libeigen3-dev
2cd /usr/include/eigen3
3sudo cp -R Eigen  /usr/include

然后尝试运行如下代码,直接编译即可。如果可以正常运行,表明安装完毕。

 1#include <iostream>
 2#include <Eigen/Dense>
 3
 4
 5//using Eigen::MatrixXd;
 6using namespace Eigen;
 7using namespace Eigen::internal;
 8using namespace Eigen::Architecture;
 9
10
11using namespace std;
12
13
14int main()
15{
16
17        cout<<"*******************1D-object****************"<<endl;
18
19
20        Vector4d v1;
21        v1<< 1,2,3,4;
22        cout<<"v1=\n"<<v1<<endl;
23
24
25        VectorXd v2(3);
26        v2<<1,2,3;
27        cout<<"v2=\n"<<v2<<endl;
28
29
30        Array4i v3;
31        v3<<1,2,3,4;
32        cout<<"v3=\n"<<v3<<endl;
33
34
35        ArrayXf v4(3);
36        v4<<1,2,3;
37        cout<<"v4=\n"<<v4<<endl;
38
39}

map的使用办法:
#

1double arr[9]={1,2,3,4,5,6,7,8,9};
2Map<MatrixXd> A(arr,3,3);

得到

11 4 7
22 5 8
33 6 9

可以看出默认是按列优先的……

如果需要按行优先,可以修改矩阵的定义方式:

1typedef Matrix<double, Dynamic, Dynamic,RowMajor> rMatrixXd; // 定义矩阵行优先
2double arr[9]={1,2,3,4,5,6,7,8,9};
3Map<rMatrixXd> A(arr,3,3);

map 使用的时候,只需要指定 Map<> 中缺少的(dynamic)维度。

比如

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2018年04月05日 星期四 18时21分59秒
 4File Name :b.cpp
 5************************************************ */
 6
 7#include <bits/stdc++.h>
 8#include <Eigen/Dense>
 9using namespace std;
10using namespace Eigen;
11int main()
12{
13    double arr[100]={1,2,3,4,5,6,7,8,9};
14    Map<Array<double,Dynamic,1> >A(arr,4);
15    cout<<A;
16
17
18    return 0;
19}

得到结果

1 2 3 4

平均值
#

对于矩阵:

1 4 7 2 5 8 3 6 9

按行求平均值 A.rowwise().mean()

得到:

4 5 6

按列求平均值 A.colwise().mean() 得到 2 5 8

unaryExpr()
#

参数为一元函数算子,表示对每一项应用该一元算子。具体看例子

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2018年04月05日 星期四 18时21分59秒
 4File Name :b.cpp
 5************************************************ */
 6
 7#include <bits/stdc++.h>
 8#include <Eigen/Dense>
 9using namespace std;
10using namespace Eigen;
11int main()
12{
13    double arr[9]={1,2,3,4,5,6,7,8,9};
14    Map<MatrixXd> A(arr,3,3);
15    cout<<A<<endl;
16    auto sqr = [](double f) { return f * f; };
17    cout<<A.unaryExpr(sqr);
18
19
20    return 0;
21}

返回的结果为: 1 16 49 4 25 64 9 36 81

replicate
#

将一个对象重复多次。

语法为 A.replicate(x,y),表示将 A 横向扩展 x 次(包含本身),纵向扩展 y 次(包含本身),共得到 x*y 个

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2018年04月05日 星期四 18时21分59秒
 4File Name :b.cpp
 5************************************************ */
 6
 7#include <bits/stdc++.h>
 8#include <Eigen/Dense>
 9using namespace std;
10using namespace Eigen;
11int main()
12{
13    double arr[9]={1,2,3,4,5,6,7,8,9};
14    Map<MatrixXd> A(arr,3,3);
15    cout<<A<<endl;
16    cout<<A.replicate(3,3);
17
18
19
20    return 0;
21}

相关文章

qt 5.x 初探 (5) 

·166 字·1 分钟
qt_5.9_ui_doc 还是比直接写代码方便点。。。所以不妨学习一个! 以及。。。qt在2017年6月1号发布了5.9。。。所以之前是5.8。。。现在变成5.9了。。。