2010年11月3日水曜日

行列と転置行列の積


  1. #include <iostream>  
  2. #include <boost/numeric/ublas/matrix.hpp>  
  3. #include <boost/numeric/ublas/io.hpp>  
  4. using namespace std;  
  5. using namespace boost::numeric::ublas;  
  6.   
  7. int main()  
  8. {  
  9.     matrix<int> A(2, 2), B(2, 2);  
  10.   
  11.     for (int i = 0; i < 2; i++)  
  12.     {  
  13.         for (int j = 0; j < 2; j++)  
  14.         {  
  15.             A(i, j) = i * 2 + j + 1;  
  16.             B(i, j) = i * 2 + j + 2;  
  17.         }  
  18.     }  
  19.   
  20.     cout << A << endl;  
  21.     cout << B << endl;  
  22.     cout << prod(A, trans(B)) << endl;  
  23.   
  24.     return 0;  
  25. }  
  26.   
  27. /* execution result 
  28. [2,2]((1,2),(3,4)) 
  29. [2,2]((2,3),(4,5)) 
  30. [2,2]((8,14),(18,32)) 
  31.  
  32. */   

上のプログラムは下記の計算を行っています.


スッキリ書けて素晴らしい.d(゚∀゚)b

0 件のコメント:

コメントを投稿