Lesson 11 Converting between array and matrix expressions

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 22
43 50

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

-- With cwiseProduct: --
 5 12
21 32

-- Array m + 4: --
5 6
7 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 34
31 46

-- Combination 2: --
 41  58
117 170

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值