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.
-- 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 m
and 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
.
-- Combination 1: --23 3431 46-- Combination 2: -- 41 58117 170
- Lesson 11 Converting between array and matrix expressions
- Converting Between XML and JSON
- Converting between XML and JSON
- Converting Between ASCII and EBCDIC
- Converting Between YUV and RGB
- Converting Between YUV and RGB
- [Python]Converting Between Characters and Numeric Codes
- Converting text files between Unix and Windows
- Converting between ROS images and OpenCV images
- Difference between Business and Matrix
- Recipe 1.2. Converting Between Characters and Numeric Codes
- Difference between pointer and array
- Lesson 7 Matrix-matrix and matrix-vector multiplication
- Matrix to convert between HSL and RGB
- Matrix to convert between HSV and RGB
- A matrix cheatsheet between matlab and numpy
- LeetCode Summary Array and Matrix
- compare array between shell and tcl
- Lesson 10 Other array coefficient-wise operations
- 计算机网络运输层
- SVN在Android Studio中的配置
- cscope配置
- Axure中的原型如何导出成为HTML5源码
- Lesson 11 Converting between array and matrix expressions
- 好东西 一起分享 Android 入门视频
- CSS定位属性之间的相互作用
- 重谈最大公约数和最小公倍数
- python编码规范
- 【软考】下午题答题经验总结
- GCD 和延时调用
- 第五届全国职工职业技能大赛决赛焊工决赛闭幕
- Android 拍照并显示在ImageView中(解决图片压缩失真的问题)