ublasのouter_prodの計算式
boostのublasにはouter_prodというベクトルとベクトルの転置の積を計算する関数が存在する。 どちらのベクトルが転置されているかちゃんと書いたものがなかったのでメモ。 直積の演算としては直感通りの式。 $$ x = \left( \begin{array}{c} x_{1} \\ x_{2} \\ \end{array} \right), y = \left( \begin{array}{c} y_{1} \\ y_{2} \\ \end{array} \right) $$ $$ \mathrm{outer\_prod}(x, y) = xy^{T} = \left( \begin{array}{cc} x_{1}y_{1} & x_{1}y_{2} \\ x_{2}y_{1} & x_{2}y_{2} \\ \end{array} \right) $$
#include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/matrix.hpp> namespace ublas = boost::numeric::ublas; int main(int argc, char const* argv[]) { ublas::vector<double> x(2); x(0) = 1.0; x(1) = 2.0; ublas::vector<double> y(2); y(0) = 3.0; y(1) = 4.0; ublas::matrix<double> A = ublas::outer_prod(x, y); std::cout << A(0, 0) << ", " << A(0, 1) << std::endl;//3, 4 std::cout << A(1, 0) << ", " << A(1, 1) << std::endl;//6, 8 return 0; }