简介
本文介绍一下Dense Matrix如何与c/C++的数组进行交互操作,这在引入其他的库中的vector向量和矩阵到Eigen中时要使用到的技术。
有时,你有一些定义好的数据,可能是数组,你需要在Eigen内使用它。一个可选的方法是你拷贝一份数据,在添加到Eigen中,这样会有些工程的问题,数据的一致性等问题。幸运的是,Eigen内为此提供了Map类,提供了便利的使用方法。
Map在Eigen的四元数quarternion、排列permutation等都有应用。
Map类型及变量
在Eigen内,Map对象的模板类型定义如下:
Map<Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> >
构造器
首先,需要了解的一点是: Map没有缺省的构造器。这里使用示例来介绍Map的变量定义/构造方法。
要构造一个Map变量,需要提供另外的2个其他的信息给Eigen: 想要得到的具体的矩阵或者向量、一个指向存储了系数的数组的地址指针。比如,要定义一个编译时即知道尺寸大小的浮点矩阵,其中pf是一个指向浮点类型数组的指针。可以这样做:
Map<MatrixXf> mf(pf,rows,columns);
一个固定尺寸大小的只读整形向量的Map类型,可以通过一个int * p
指针,通过构造器,这样定义:
Map<const Vector4i> mi(pi);
在这里,看起来没有传递给构造器尺寸大小,这是因为已经在模板参数Vector4i
中隐含指定了。
类Map提供了足够的灵活性,以满足多种数据形式。这里提供了有2个模板参数的定义:
Map<typename MatrixType,
int MapOptions,
typename StrideType>
这里简单介绍一下:
- MapOptions: 指定指针是否对齐(Aligned),或者不对齐(Unaligned);缺省值为不对齐(Unaligned)。
- StrideType: 让使用者指定内存中数组的布局,类型为class Stride。下面的例子指定其数据数组使用行优先(row-major)格式。
示例:
//matrix_map1.cpp
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
int array[9];
for(int i = 0; i < 9; ++i) array[i] = i;
Map<VectorXi> vi(array,9);
Map<const Vector4i> fixed_v(array); // unknown type name 'Vector5i' -- 这种模式size不能大于4
cout<< "vector vi: "<< endl << vi << endl<< endl;
cout<< "fixed-vector : "<< endl << fixed_v << endl<< endl;
cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array) << endl;
cout << "Row-major:\n" << Map<Matrix<int,2,4,RowMajor> >(array) << endl;
cout << "Row-major using stride:\n" <<
Map<Matrix<int,2,4>, Unaligned, Stride<1,4> >(array) << endl;
}
执行结果:
$ g++ -I /usr/local/include/eigen3 matrix_map1.cpp -o matrix_map1
$ ./matrix_map1
vector vi:
0
1
2
3
4
5
6
7
8
fixed-vector :
0
1
2
3
Column-major:
0 2 4 6
1 3 5 7
Row-major:
0 1 2 3
4 5 6 7
Row-major using stride:
0 1 2 3
4 5 6 7
从结果可以看到,Map()进行映射时,缺省时列有先column-major,或者安装Eigen中的设定执行。
其实, Stride提供了更大的灵活性,具体信息可以查询Map和Stride的说明文档。
使用Map变量
对Map变量的使用,就和其映射的类型一致。比如Map到一个矩阵,则和使用矩阵对象一样。如果是向量,则按照向量的类型进行访问。
直接看Eigen中的示例。
//matrix_map2.cpp
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
typedef Matrix<float,1,Dynamic> MatrixType;
typedef Map<MatrixType> MapType;
typedef Map<const MatrixType> MapTypeConst; // a read-only map
const int n_dims = 5;
int main()
{
MatrixType m1(n_dims), m2(n_dims);
m1.setRandom();
m2.setRandom();
float *p = &m2(0); // get the address storing the data for m2
MapType m2map(p,m2.size()); // m2map shares data with m2
MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
cout << "m1: " << m1 << endl;
cout << "m2: " << m2 << endl;
cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
cout << "Squared euclidean distance, using map: " << (m1-m2map).squaredNorm() << endl;
m2map(3) = 7; // this will change m2, since they share the same array
cout << "Updated m2: " << m2 << endl;
cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl;
//m2mapconst(2) = 5; // compile-time error: expression is not assignable
cout << endl;
}
执行结果:
$ g++ -I /usr/local/include/eigen3 matrix_map2.cpp -o matrix_map2
$
$ ./matrix_map2
m1: -0.999984 -0.736924 0.511211 -0.0826997 0.0655345
m2: -0.562082 -0.905911 0.357729 0.358593 0.869386
Squared euclidean distance: 1.08479
Squared euclidean distance, using map: 1.08479
Updated m2: -0.562082 -0.905911 0.357729 7 0.869386
m2 coefficient 2, constant accessor: 0.357729
可以看到,对系数的访问,使用括号运算符(int index)
,矩阵的一些计算保持不变。
但这里有一个限制:你自己定义的函数,使用Eigen的类型时,如果使用了Map,这和其对应的密集矩阵类Matrix可不一致了,因为它们是不同的类型Class。
使用placement new
修改映射的数组
Map映射后得到的Matrix或者vector对象,也可以被修改。但这需要使用C++的placement new
语法。
这里先对C++的Placemet new
做一下说明,这涉及到3种不同的new
: new、operator new、placement new
。
placement new
是在用户指定的内存位置上构建新的对象,那么这样构建时,就不需要再额外分配内存空间,只需要调用对象的构造函数。就是这样!因为内存和数据都已经在那里了。其语法如此这样:new(&p) constructor(......)
。
下面看Eigen内的一下Map相关的示例:
//matrix_map3.cpp
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
int data[] = {1,2,3,4,5,6,7,8,9};
Map<RowVectorXi> ov(data,9);
cout << "Origin data: " << ov << endl;
Map<RowVectorXi> v(data,4);
cout << "The mapped vector v is: " << v << "\n";
// placement new
new (&v) Map<RowVectorXi>(data+4,5);
cout << "Changed, Now v is: " << v << "\n";
cout << "Again origin data: " << ov << endl;
}
执行结果:
$ g++ -I /usr/local/include/eigen3 matrix_map3.cpp -o matrix_map3
promote:eigen david$ ./matrix_map3
Origin data: 1 2 3 4 5 6 7 8 9
The mapped vector v is: 1 2 3 4
Changed, Now v is: 5 6 7 8 9
Again origin data: 1 2 3 4 5 6 7 8 9
使用这种placement new
语法,可以声明一个Map对象,而不用先分配内存,也无需知道具体真实的数据。
比如:
//matrix_map4.cpp
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int n_matrices = 3;
int data[] = {1,2,3,4,5,6,7,8,9};
int* get_matrix_pointer(int i)
{
return data+3*i;
}
int main()
{
Map<Matrix3i> A(NULL); // // 此时对数据一无所知
VectorXi b(n_matrices);
for (int i = 0; i < n_matrices; i++)
{
int * pp = get_matrix_pointer(i);
cout << "point : " << pp << " : data " << *pp << endl;
//
new (&A) Map<Matrix3i>( pp );
cout<< "Matrix A: "<< endl<<A<<endl;
b(i) = A.trace();
}
cout<< "vector b : "<< endl<<b<<endl;
}
执行结果:
$ g++ -I /usr/local/include/eigen3 matrix_map4.cpp -o matrix_map4
$ ./matrix_map4
point : 0x106cb7880 : data 1
Matrix A:
1 4 7
2 5 8
3 6 9
point : 0x106cb788c : data 4
Matrix A:
4 7 0
5 8 0
6 9 0
point : 0x106cb7898 : data 7
Matrix A:
7 0 0
8 0 0
9 0 0
vector b :
15
12
7