Lesson 7 Matrix-matrix and matrix-vector multiplication
2015-11-02 21:08
399 查看
Matrix-matrix multiplication is again done with
outer product. Thus, all these cases are handled by just two operators:
binary operator * as in
compound operator *= as in
Note: if you read the above paragraph on expression templates and are worried that doing
matrix multiplication as a special case and takes care of introducing a temporary here, so it will compile
tmp = m*m;
m = tmp;
If you know your matrix product can be safely evaluated into the destination matrix without aliasing issue, then you can use the noalias() function
to avoid the temporary, e.g.:
c.noalias() += a * b;
For more details on this topic, see the page on aliasing.
Note: for BLAS users worried about performance, expressions such as
operator*. Since vectors are a special case of matrices, they are implicitly handled there too, so matrix-vector product is really just a special case of matrix-matrix product, and so is vector-vector
outer product. Thus, all these cases are handled by just two operators:
binary operator * as in
a*b
compound operator *= as in
a*=b(this multiplies on the right:
a*=bis equivalent to
a = a*b)
#include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { Matrix2d mat; mat << 1, 2, 3, 4; Vector2d u(-1,1), v(2,0); std::cout << "Here is mat*mat:\n" << mat*mat << std::endl; std::cout << "Here is mat*u:\n" << mat*u << std::endl; std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl; std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl; std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl; std::cout << "Let's multiply mat by itself" << std::endl; mat = mat*mat; std::cout << "Now mat is mat:\n" << mat << std::endl; }
Note: if you read the above paragraph on expression templates and are worried that doing
m=m*mmight cause aliasing issues, be reassured for now: Eigen treats
matrix multiplication as a special case and takes care of introducing a temporary here, so it will compile
m=m*mas:
tmp = m*m;
m = tmp;
If you know your matrix product can be safely evaluated into the destination matrix without aliasing issue, then you can use the noalias() function
to avoid the temporary, e.g.:
c.noalias() += a * b;
For more details on this topic, see the page on aliasing.
Note: for BLAS users worried about performance, expressions such as
c.noalias() -= 2 * a.adjoint() * b;are fully optimized and trigger a single gemm-like function call.
相关文章推荐
- 子进程和父进程的关系和示例
- iOS 心得九:应用缓存的查看和清除
- bit、byte、位、字节、汉字的关系
- OpenGL一些函数详解(二)
- WPF学习(一)--布局控件简介
- 玩转树莓派--图形界面登陆
- 个人之对于Block的初探究
- 使用ubuntu15.04手动搭建LNMP服务器框架
- poj 1006 暴搜
- Win7系统的最佳浏览效果的调节方法
- NeoTalen的由来
- HDU 5517 (ACM 2015 沈阳) Triple [树状数组]
- 选择器
- 产生随机数
- Linux命令大观
- [Android Lint] xxx is not translated in xxx 的解决方法
- #pragma分析
- 一行排奇数列的HTML排版
- c++中的引用和指针
- LeetCode:Rectangle Area