Eigen學習筆記(7)-歸約、迭代器和廣播

原文:Eigen官網-Reductions, visitors and broadcasting

1. Reductions

在Eigen中,Reduction是用來處理matrix或者array的某類特徵,然後返回一個標量。

1.1 求和

sum()返回matrix或array中所有元素的和。

1.2 範數計算

L2範數 squareNorm(),等價於計算vector的自身點積,也等價於vector中所有元素的平方和。

norm()返回squareNorm()的開方根。
這些操作應用於matrix,norm() 會返回Frobenius或Hilbert-Schmidt範數。

如果你想使用其他Lp範數,可以使用lpNorm< p >()方法。p可以取Infinity,表示L∞範數。
示例如下:

#include <Eigen/Dense>
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
  VectorXf v(2);
  MatrixXf m(2,2), n(2,2);
  
  v << -1,
       2;
  
  m << 1,-2,
       -3,4;
  cout << "v.squaredNorm() = " << v.squaredNorm() << endl;
  cout << "v.norm() = " << v.norm() << endl;
  cout << "v.lpNorm<1>() = " << v.lpNorm<1>() << endl;
  cout << "v.lpNorm<Infinity>() = " << v.lpNorm<Infinity>() << endl;
  cout << endl;
  cout << "m.squaredNorm() = " << m.squaredNorm() << endl;
  cout << "m.norm() = " << m.norm() << endl;
  cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << endl;
  cout << "m.lpNorm<Infinity>() = " << m.lpNorm<Infinity>() << endl;
}

結果如下:

v.squaredNorm() = 5
v.norm() = 2.23607
v.lpNorm<1>() = 3
v.lpNorm<Infinity>() = 2

m.squaredNorm() = 30
m.norm() = 5.47723
m.lpNorm<1>() = 10
m.lpNorm<Infinity>() = 4

Operator norm: 1-norm和∞-norm可以通過其他方式得到。

示例如下:

#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
  MatrixXf m(2,2);
  m << 1,-2,
       -3,4;
  cout << "1-norm(m)     = " << m.cwiseAbs().colwise().sum().maxCoeff()
       << " == "             << m.colwise().lpNorm<1>().maxCoeff() << endl;
  cout << "infty-norm(m) = " << m.cwiseAbs().rowwise().sum().maxCoeff()
       << " == "             << m.rowwise().lpNorm<1>().maxCoeff() << endl;
}

結果如下:

1-norm(m)     = 6 == 6
infty-norm(m) = 7 == 7

1.2 布爾歸約

這些功能通常和Array中提供的元素級的比較操作一起使用。

操作 功能
all() 如果Matrix或Array中所有元素的對應操作返回Ture則返回True
any() 如果Matrix或Array中所有元素中至少一個元素的對應操作返回Ture則返回True
count() 返回Matrix或Array中所有元素對應操作返回True的數量

示例如下:

#include <Eigen/Dense>
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
  ArrayXXf a(2,2);
  
  a << 1,2,
       3,4;
  cout << "(a > 0).all()   = " << (a > 0).all() << endl;
  cout << "(a > 0).any()   = " << (a > 0).any() << endl;
  cout << "(a > 0).count() = " << (a > 0).count() << endl;
  cout << endl;
  cout << "(a > 2).all()   = " << (a > 2).all() << endl;
  cout << "(a > 2).any()   = " << (a > 2).any() << endl;
  cout << "(a > 2).count() = " << (a > 2).count() << endl;
}

結果如下:

(a > 0).all()   = 1
(a > 0).any()   = 1
(a > 0).count() = 4

(a > 2).all()   = 0
(a > 2).any()   = 1
(a > 2).count() = 2

2. Visitors

當我們想獲取某元素在Matrix或Array中的位置的時候,迭代器是必須的。常用的有:minCoeff(&x, &y)maxCoeff(&x, &y)

傳入visitor中的參數是某個變量的指針,其中存儲了該變量在矩陣中的行、列位置。

示例如下:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
  Eigen::MatrixXf m(2,2);
  
  m << 1, 2,
       3, 4;
  //get location of maximum
  MatrixXf::Index maxRow, maxCol;
  float max = m.maxCoeff(&maxRow, &maxCol);
  //get location of minimum
  MatrixXf::Index minRow, minCol;
  float min = m.minCoeff(&minRow, &minCol);
  cout << "Max: " << max <<  ", at: " <<
     maxRow << "," << maxCol << endl;
  cout << "Min: " << min << ", at: " <<
     minRow << "," << minCol << endl;
}

結果如下:

Max: 4, at: 1,1
Min: 1, at: 0,0

3. Partial reductions-部分歸約

Eigen中支持對Matrx或Array的行或列進行歸約操作,返回該行或該列中的相應值。部分歸約可以使用colwise()rowwise()函數。其中,colwise()返回一個行向量,rowwise()返回一個列向量。
示例如下:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main()
{
  Eigen::MatrixXf mat(2,4);
  mat << 1, 2, 6, 9,
         3, 1, 7, 2;
  
  std::cout << "Column's maximum: " << std::endl
   << mat.colwise().maxCoeff() << std::endl;
}

結果如下:

Column's maximum: 
3 2 7 9

3.1 結合部分歸約和其他操作

可以利用部分歸約操作的結果進行進一步處理,比如尋找一個矩陣中最大的列向量和。
示例如下:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
  MatrixXf mat(2,4);
  mat << 1, 2, 6, 9,
         3, 1, 7, 2;
  
  MatrixXf::Index   maxIndex;
  float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex);
  
  std::cout << "Maximum sum at position " << maxIndex << std::endl;
  std::cout << "The corresponding vector is: " << std::endl;
  std::cout << mat.col( maxIndex ) << std::endl;
  std::cout << "And its sum is is: " << maxNorm << std::endl;
}

結果如下:

Maximum sum at position 2
The corresponding vector is: 
6
7
And its sum is is: 13

4. Broadcasting

廣播背後的概念類似於部分歸約,其區別在於廣播構造一個表達式,其中向量(列或行)通過在一個方向上覆制而被解釋爲矩陣。

比如將一個列向量加到一個矩陣的每列上。

注意:對Array類型,*=,/=和/這些操作可以進行行/列級的操作,但不使用與Matrix,因爲會與矩陣乘混淆。當用Broadcasting對Matrix類進行操作只能應用於Vector類型,而不能用於Matrix類型。同樣,對Array類進行操作時,和VectorXf對應的ArrayXf類型。

示例如下:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main()
{
  Eigen::MatrixXf mat(2,4);
  Eigen::VectorXf v(2);
  
  mat << 1, 2, 6, 9,
         3, 1, 7, 2;
         
  v << 0,
       1;
       
  //add v to each column of m
  mat.colwise() += v;
  
  std::cout << "Broadcasting result: " << std::endl;
  std::cout << mat << std::endl;
}

結果如下:

Broadcasting result: 
1 2 6 9
4 2 8 3

4.1 結合廣播和其他操作

計算矩陣中哪列與目標向量距離(Euclidean distance)最近。

示例如下:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
  Eigen::MatrixXf m(2,4);
  Eigen::VectorXf v(2);
  
  m << 1, 23, 6, 9,
       3, 11, 7, 2;
       
  v << 2,
       3;
  MatrixXf::Index index;
  // find nearest neighbour
  (m.colwise() - v).colwise().squaredNorm().minCoeff(&index);
  cout << "Nearest neighbour is column " << index << ":" << endl;
  cout << m.col(index) << endl;
}

結果如下:

Nearest neighbour is column 0:
1
3

參考:

“Eigen教程(7)”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章