git clone https://github.com/eigenteam/eigen-git-mirror
这是我编译的程序
g++ -O3 -isystem eigen-git-mirror -o eigen_indexing eigen_indexing.cpp
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::ArrayX3d array = Eigen::ArrayX3d::Random(8, 3);
Eigen::ArrayXi indices(5);
indices << 4, 7, 0, 2, 1;
Eigen::Array3i cols(0,1,2);
std::cout << "array = " << std::endl << array << std::endl << std::endl;
std::cout << "indices = " << std::endl << indices << std::endl << std::endl;
std::cout << "cols = " << std::endl << cols << std::endl << std::endl;
std::cout << "array(indices,cols) = " << std::endl << array(indices,cols) << std::endl << std::endl;
std::cout << "Equivalent but Eigen::placeholders::all syntax may evolve " << std::endl << std::endl;
std::cout << "array(indices, Eigen::placeholders::all) = " << std::endl << array(indices,Eigen::placeholders::all) << std::endl << std::endl;
std::cout << "Assignment also works array(indices,cols) = 0.0;" << std::endl<< std::endl;
array(indices,cols) = 0.0;
std::cout << "array = " << std::endl << array << std::endl << std::endl;
std::cout << "Assignment also works array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);" << std::endl<< std::endl;
array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);
std::cout << "array = " << std::endl << array << std::endl << std::endl;
return 0;
}
如果程序不能编译,这是我用过的版本
cd eigen-git-mirror
# git checkout master
git checkout a31719c75f244673c962ee65f279606bee6fc7ef
cd ..
这是程序的输出
array =
-0.999984 0.358593 0.342299
-0.736924 0.869386 -0.984604
0.511211 -0.232996 -0.233169
-0.0826997 0.0388327 -0.866316
0.0655345 0.661931 -0.165028
-0.562082 -0.930856 0.373545
-0.905911 -0.893077 0.177953
0.357729 0.0594004 0.860873
indices =
4
7
0
2
1
cols =
0
1
2
array(indices,cols) =
0.0655345 0.661931 -0.165028
0.357729 0.0594004 0.860873
-0.999984 0.358593 0.342299
0.511211 -0.232996 -0.233169
-0.736924 0.869386 -0.984604
Equivalent but Eigen::placeholders::all syntax may evolve
array(indices, Eigen::placeholders::all) =
0.0655345 0.661931 -0.165028
0.357729 0.0594004 0.860873
-0.999984 0.358593 0.342299
0.511211 -0.232996 -0.233169
-0.736924 0.869386 -0.984604
Assignment also works array(indices,cols) = 0.0;
array =
0 0 0
0 0 0
0 0 0
-0.0826997 0.0388327 -0.866316
0 0 0
-0.562082 -0.930856 0.373545
-0.905911 -0.893077 0.177953
0 0 0
Assignment also works array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);
array =
-8.1607 5.24396 2.65277
-1.68001 -9.05071 9.82075
3.07838 -4.75094 5.12821
-0.0826997 0.0388327 -0.866316
6.92334 4.02381 4.72164
-0.562082 -0.930856 0.373545
-0.905911 -0.893077 0.177953
0.538576 8.20642 -3.43532