uBLAS——Boost 线性代数基础程序库
uBLAS——Boost 线性代数基础程序库 (二)
namespace ublas = boost::numeric::ublas;
row() vs matrix_row<> ,column() vs matrix_column<>
(0)功能
- row(A, m)、column(A, n) 用于索引矩阵的某一行,某一列
- matrix_row<>、matrix_column<> 根据矩阵的某一行,某一列创建行向量、列向量
(1)头文件
#include <boost\numeric\ublas\matrix_proxy.hpp>
(2)意义和用法
- (1)row()、column()是函数,接受矩阵,一个行索引/列索引;
- (2)matrix_row<>,matrix_column<>,是模板类,用于列定义
- (3)不需要给 row()、column() 指定参数类型,因为函数具有自动类型推导的功能;
(3)row()、column() 的实现用到了 matrix_row<>、matrix_column<>
matrix_column<M> column (M &data, typename M::size_type j) { return matrix_column<M> (data, j); }
因为函数的自动类型推导,为了避免类型不匹配,推荐使用 row()、column。
矩阵乘法与矩阵向量乘法
使用同一个全局函数:ublas::axpy_prod(),其所在的头文件为
#include <boost\numeric\ublas\operation.hpp>
(1)矩阵向量乘法、
- v=Ax (默认或 init=true)
-
v+=Ax
(init=false)
BOOST_UBLAS_INLINE V& axpy_prod (const matrix_expression< E1 > & A, const vector_expression< E2 > & x, V & v, bool init = true )
(2)向量矩阵乘法
- v=ATx (默认或init=true)
-
v+=ATx
(init=false)
BOOST_UBLAS_INLINE V& axpy_prod( const vector_expression< E1 > & e1, const matrix_expression< E2 > & e2, V & v, bool init = true );
(3)矩阵矩阵乘法
- M=AX (默认或init=true)
-
M+=AX
(init=false)
BOOST_UBLAS_INLINE M& axpy_prod (const matrix_expression< E1 > & e1, const matrix_expression< E2 > & e2, M & m, bool init = true )
将 init = true 或者取默认值,其实是执行 M.clear();
References
[1] Special Products