Skip to content

Instantly share code, notes, and snippets.

@AtsushiSakai
Created March 23, 2013 11:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtsushiSakai/5227370 to your computer and use it in GitHub Desktop.
Save AtsushiSakai/5227370 to your computer and use it in GitHub Desktop.
行列演算ライブラリEigenの換算(Reduction)関連(平均,最大値,最小値,各行・列への演算)の関数サンプルプログラム
/*
FileName: ReductionSample.cpp
Discription:行列演算ライブラリEigenの換算関数のサンプル
Author: Atsushi Sakai
Update: 2013/03/23
*/
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Geometry> //EigenのGeometry関連の関数を使う場合,これが必要
//データ表示用マクロ
#define PRINT_MAT(X) cout << #X << ":\n" << X << endl << endl
using namespace std;
using namespace Eigen;
void Sample1(){
cout<<"====1. 合計====="<<endl;
MatrixXf a(2,2);
a<<1,2,3,4;
PRINT_MAT(a);
//行列の各要素の合計
PRINT_MAT(a.sum());
VectorXf b(2);
b<<1.1,2.2;
PRINT_MAT(b);
//ベクトルの合計
PRINT_MAT(b.sum());
}
void Sample2(){
cout<<"====2. 積算====="<<endl;
MatrixXf a(2,2);
a<<1,2,3,4;
PRINT_MAT(a);
//行列の各要素の積算
PRINT_MAT(a.prod());
VectorXf b(2);
b<<1.1,2.2;
PRINT_MAT(b);
//ベクトルの合計
PRINT_MAT(b.prod());
}
void Sample3(){
cout<<"====3. 平均====="<<endl;
MatrixXf a(2,2);
a<<1,2,3,4;
PRINT_MAT(a);
//行列の各要素の平均
PRINT_MAT(a.mean());
VectorXf b(2);
b<<1.1,2.2;
PRINT_MAT(b);
//ベクトルの各要素の平均
PRINT_MAT(b.mean());
}
void Sample4(){
cout<<"====4. 最大最小要素====="<<endl;
MatrixXf a(2,2);
a<<1,2,3,4;
PRINT_MAT(a);
//行列の要素の最大値と最小値
PRINT_MAT(a.maxCoeff());
PRINT_MAT(a.minCoeff());
//最大値最小値の要素のインデックスを知りたい場合
MatrixXf::Index row,col;
PRINT_MAT(a.maxCoeff(&row,&col));
PRINT_MAT(row);
PRINT_MAT(col);
PRINT_MAT(a.minCoeff(&row,&col));
PRINT_MAT(row);
PRINT_MAT(col);
VectorXf b(2);
b<<1.1,2.2;
PRINT_MAT(b);
//ベクトルの合計
PRINT_MAT(b.maxCoeff());
PRINT_MAT(b.minCoeff());
}
void Sample5(){
cout<<"====5. 行列の対角和(トレース)====="<<endl;
MatrixXf a(2,2);
a<<1,2,3,4;
PRINT_MAT(a);
//行列のトレース
PRINT_MAT(a.trace());
//対角和なので,この演算と一緒
PRINT_MAT(a.diagonal().sum());
}
void Sample6(){
cout<<"====6. ノルム計算===="<<endl;
MatrixXf a(2,2);
a<<1,2,3,4;
PRINT_MAT(a);
//それぞれの要素の二乗の和
PRINT_MAT(a.squaredNorm());
//ユークリッドノルム(先ほどの二乗和のルート)
PRINT_MAT(a.norm());
//一次ノルム(数値を変えることで任意の次数のノルムが計算できる)
PRINT_MAT(a.lpNorm<1>());
//無限大ノルム
PRINT_MAT(a.lpNorm<Infinity>());
}
void Sample7(){
cout<<"====7. 判定===="<<endl;
//判定関数はArrayのみで使える(MatrixやVectorでは使えない)
ArrayXXf a(2,2);
a<<1,2,3,4;
PRINT_MAT(a);
//各要素が2以上であるか(1:True,0:False)
PRINT_MAT((a>2));
//すべての要素が2以上であるか
PRINT_MAT((a>2).all());
//一つでも2以上の要素があるか?
PRINT_MAT((a>2).any());
//2以上の要素がいくつあるか?
PRINT_MAT((a>2).count());
}
void Sample8(){
cout<<"====8. 各行・列に対する換算===="<<endl;
MatrixXf a(2,4);
a<<1,2,3,4,
5,6,7,8;
PRINT_MAT(a);
//各行・列の最大値
PRINT_MAT(a.rowwise().maxCoeff());
PRINT_MAT(a.colwise().maxCoeff());
//各列の最大値の和
PRINT_MAT(a.colwise().maxCoeff().sum());
//各列に対する足し算引き算
VectorXf v(2);
v<<1,2;
PRINT_MAT(v);
//それぞれの列に1と2を足す
PRINT_MAT((a.colwise()+v));
}
int main()
{
cout<<"Eigen Reduction Sample Code"<<endl;
Sample1();
Sample2();
Sample3();
Sample4();
Sample5();
Sample6();
Sample7();
Sample8();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment