跳过正文
  1. Posts/

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

·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的使用办法:
#

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

以看出默认是按列优先的… 如果需要按行优先,可以修改矩阵的定义方式: typedef Matrix<double, Dynamic, Dynamic,RowMajor>rMatrixXd;//定义矩阵行优先 double arr[9]={1,2,3,4,5,6,7,8,9}; Map 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) 

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

qt 5.x初探 (3)

·2 分钟
update3: 终于知道了正确的学习姿势… 用百度把要用的东西大概描述出来,然后总能找到一个是你要的。。。

qt 5.x 学习笔记 (2)

·7 分钟
先来放一波过程中用到的资料和官方文档好了。 basic layout_qt5.8 QBoxLayout Class_qt5.8 QString Class 5.8 QChar Class qt 5.8

qt 5.x 初探(1)

·1 分钟
嘛。。为了系统安全课来学一波qt… 现在算是写出了一个可以打开文件,保存文件的记事本。。。