Lesson 11 Converting between array and matrix expressions

来源:互联网 发布:淘宝抓取 编辑:程序博客网 时间:2024/05/22 17:21

When should you use objects of the Matrix class and when should you use objects of the Array class? You cannot applyMatrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both Matrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives access to all operations regardless of the choice of declaring objects as arrays or as matrices.

Matrix expressions have an .array() method that 'converts' them into array expressions, so that coefficient-wise operations can be applied easily. Conversely, array expressions have a .matrix() method. As with all Eigen expression abstractions, this doesn't have any runtime cost (provided that you let your compiler optimize). Both .array() and.matrix() can be used as rvalues and as lvalues.

Mixing matrices and arrays in an expression is forbidden with Eigen. For instance, you cannot add a matrix and array directly; the operands of a + operator should either both be matrices or both be arrays. However, it is easy to convert from one to the other with .array() and .matrix(). The exception to this rule is the assignment operator: it is allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix variable.

The following example shows how to use array operations on a Matrix object by employing the .array() method. For example, the statement result = m.array() * n.array() takes two matrices m and n, converts them both to an array, uses to multiply them coefficient-wise and assigns the result to the matrix variable result (this is legal because Eigenallows assigning array expressions to matrix variables).

As a matter of fact, this usage case is so common that Eigen provides a const .cwiseProduct(.) method for matrices to compute the coefficient-wise product. This is also shown in the example program.

Example:Output:
#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 2243 50-- Array m*n: -- 5 1221 32-- With cwiseProduct: -- 5 1221 32-- Array m + 4: --5 67 8

Similarly, if array1 and array2 are arrays, then the expression array1.matrix() * array2.matrix() computes their matrix product.

Here is a more advanced example. The expression (m.array() + 4).matrix() * m adds 4 to every coefficient in the matrix mand then computes the matrix product of the result with m. Similarly, the expression (m.array() * n.array()).matrix() * m computes the coefficient-wise product of the matrices m and n and then the matrix product of the result with m.

Example:Output:
#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.array() + 4).matrix() * m;
cout << "-- Combination 1: --" << endl << result << endl << endl;
result = (m.array() * n.array()).matrix() * m;
cout << "-- Combination 2: --" << endl << result << endl << endl;
}
-- Combination 1: --23 3431 46-- Combination 2: -- 41  58117 170
0 0