Eigen教程(4)


整理下Eigen庫的教程,參考:http://eigen.tuxfamily.org/dox/index.html

Array類和元素級操作

為什么使用Array

相對於Matrix提供的線性代數運算,Array類提供了更為一般的數組功能。Array類為元素級的操作提供了有效途徑,比如點加(每個元素加值)或兩個數據相應元素的點乘。

Array

Array是個類模板(類似於Matrx),前三個參數是必須指定的,后三個是可選的,這點和Matrix是相同的。

Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>

Eigen也提供的一些常用類定義,Array是同時支持一維和二維的(Matrix二維,Vector一維)。

Type Tyoedef
Array<float,Dynamic,1> ArrayXf
Array<float,3,1> Array3f
Array<double,Dynamic,Dynamic> ArrayXXd
Array<double,3,3> Array33d

獲取元素

讀寫操作重載於matrix, << 可以用於初始化array或打印。

#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
  ArrayXXf  m(2,2);
  
  // assign some values coefficient by coefficient
  m(0,0) = 1.0; m(0,1) = 2.0;
  m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);
  
  // print values to standard output
  cout << m << endl << endl;
 
  // using the comma-initializer is also allowed
  m << 1.0,2.0,
       3.0,4.0;
     
  // print values to standard output
  cout << m << endl;
}

加法和減法

和matrix類似,要求array的尺寸一致。同時支持array+/-scalar的操作!

#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
  ArrayXXf a(3,3);
  ArrayXXf b(3,3);
  a << 1,2,3,
       4,5,6,
       7,8,9;
  b << 1,2,3,
       1,2,3,
       1,2,3;
       
  // Adding two arrays
  cout << "a + b = " << endl << a + b << endl << endl;
  // Subtracting a scalar from an array
  cout << "a - 2 = " << endl << a - 2 << endl;
}

輸出

a + b = 
 2  4  6
 5  7  9
 8 10 12

a - 2 = 
-1  0  1
 2  3  4
 5  6  7

乘法

支持array*scalar(類似於matrix),但是當執行array*array時,執行的是相應元素的乘積,因此兩個array必須具有相同的尺寸。

int main()
{
  ArrayXXf a(2,2);
  ArrayXXf b(2,2);
  a << 1,2,
       3,4;
  b << 5,6,
       7,8;
  cout << "a * b = " << endl << a * b << endl;
}

a * b = 
 5 12
21 32

其他元素級操作

Function function
abs 絕對值
sqrt 平方根
min(.) 兩個array相應元素的最小值
int main()
{
  ArrayXf a = ArrayXf::Random(5);
  a *= 2;
  cout << "a =" << endl 
       << a << endl;
  cout << "a.abs() =" << endl 
       << a.abs() << endl;
  cout << "a.abs().sqrt() =" << endl 
       << a.abs().sqrt() << endl;
  cout << "a.min(a.abs().sqrt()) =" << endl 
       << a.min(a.abs().sqrt()) << endl;
}

array和matrix之間的轉換

當需要線性代數類操作時,請使用Matrix;但需要元素級操作時,需要使用Array。這樣就需要提供兩者的轉化方法。

Matrix提供了.array()函數將它們轉化為Array對象。

Array提供了.matrix()函數將它們轉化為Matrix對象。

在Eigen,在表達式中混合Matrix和Array操作是被禁止的,但是可以將array表達式結果賦值為matrix。

另外,Matrix提供了cwiseProduct函數也實現了點乘。

#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
  MatrixXf m(2,2);
  MatrixXf n(2,2);
  MatrixXf result(2,2);
  m << 1,2,
       3,4;
  n << 5,6,
       7,8;
  result = m * n;
  cout << "-- Matrix m*n: --" << endl << result << endl << endl;
  result = m.array() * n.array();
  cout << "-- Array m*n: --" << endl << result << endl << endl;
  result = m.cwiseProduct(n);
  cout << "-- With cwiseProduct: --" << endl << result << endl << endl;
  result = m.array() + 4;
  cout << "-- Array m + 4: --" << endl << result << endl << endl;
}

輸出

-- Matrix m*n: --
19 22
43 50

-- Array m*n: --
 5 12
21 32

-- With cwiseProduct: --
 5 12
21 32

-- Array m + 4: --
5 6
7 8

類似, array1.matrix() * array2.matrix() 將執行矩陣乘法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM