Eigen笔记——基本数据类型及初始化(2)

在Eigen中,除了常见的Vector和Matrix,还有Array,比较Matrix而言,Array类提供了更为一般的数组功能。Array类可以对两个矩阵相应元素的进行运算

下面是常见的例子

1、array动态矩阵的加减运算,注意:矩阵的加减要是相同尺寸

    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

是对应元素的加减

2、相乘

    ArrayXXf a1(2,2);
    ArrayXXf b1(2,2);
    a1 << 1,2,
            3,4;
    b1 << 5,6,
            7,8;
    cout << "a1 * b1 = " << endl << a1 * b1 << endl;

结果如下:

a1 * b1 = 
 5 12
21 32

此时的结果是对应元素的相乘,而不是矩阵运算中的点乘

 

3、Matrix转换成Array的运算

    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() + 1;
    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: --
2 3
4 5

Matrix也可以像Array一样,进行元素级别的运算,有对应的方法:cwiseProduct、array()

 

4、Array转换成Matrix运算

    ArrayXXf a2(2,2);
    ArrayXXf b2(2,2);
    a2 << 1,2,
            3,4;
    b2 << 5,6,
            7,8;
    cout << "a2.matrix() * b2.matrix() = " << endl << a2.matrix() * b2.matrix() << endl;

结果如下:

a2.matrix() * b2.matrix() = 
19 22
43 50

5、Array和Matrix的混用

    MatrixXf m1(2,2);
    MatrixXf n1(2,2);
    MatrixXf result1(2,2);

    m1 << 1,2,
            3,4;
    n1 << 5,6,
            7,8;

    result1 = (m1.array() + 4).matrix() * m1;
    cout << "-- Combination 1: --" << endl << result1 << endl << endl;
    result1 = (m1.array() * n1.array()).matrix() * m1;
    cout << "-- Combination 2: --" << endl << result1 << endl << endl;

结果如下:

-- Combination 1: --
23 34
31 46

-- Combination 2: --
 41  58
117 170

 

6、Array的其他数值运算

ArrayXf a3 = ArrayXf::Random(5);
    a3*= 2;
    cout << "a3 =" << endl
         << a3 << endl;
    cout << "a3.abs() =" << endl
         << a3.abs() << endl;
    cout << "a3.abs().sqrt() =" << endl
         << a3.abs().sqrt() << endl;
    cout << "a3.min(a3.abs().sqrt()) =" << endl
         << a3.min(a3.abs().sqrt()) << endl;

 

Array的初始化数据的方法和Matrix一样。具体参考前面的例子。

 

——————————————————————————————————————————————————————————————————————————————————————————

这里添加一个在cartographer里运用到的例子

  bool Contains(const Eigen::Array2i& cell_index) const {
    return (Eigen::Array2i(0, 0) <= cell_index).all() &&
           (cell_index <
            Eigen::Array2i(cell_limits_.num_x_cells, cell_limits_.num_y_cells))
               .all();
  }
Eigen::Array2i(0, 0) <= cell_index,对cell_index的每个元素都进行判断,看是否都大于零。.all()函数,是判断对象的每个元素是否都为真。
cell_index < Eigen::Array2i(cell_limits_.num_x_cells, cell_limits_.num_y_cells),判断cell_index的每个元素是否都小于最大数。
这个是判断cell_index是否在index的范围内。
posted @ 2023-05-04 14:27  水水滴答  阅读(362)  评论(0编辑  收藏  举报