1
0

28_matmult.cpp 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <boost/numeric/ublas/matrix.hpp>
  2. #include <iomanip>
  3. #include <iostream>
  4. using namespace boost::numeric::ublas;
  5. using namespace std;
  6. // A custom, more pretty way to print matrices
  7. std::ostream &operator<<(std::ostream &Str, matrix<double> const &v)
  8. {
  9. for (int i = 0; i < v.size1(); i++)
  10. {
  11. Str << ((i == 0) ? "[[" : " [");
  12. for (int j = 0; j < v.size2(); j++)
  13. {
  14. Str << setw(4);
  15. Str << v(i, j);
  16. Str << setw(0);
  17. }
  18. Str << ((i == v.size1() - 1) ? "]]" : "]");
  19. if (i != v.size1() - 1)
  20. Str << endl;
  21. }
  22. return Str;
  23. }
  24. int main()
  25. {
  26. matrix<double> A(2, 2);
  27. A(0, 0) = 1;
  28. A(0, 1) = 2;
  29. A(1, 0) = 3;
  30. A(1, 1) = 4;
  31. matrix<double> B(2, 2);
  32. B(0, 0) = -1;
  33. B(0, 1) = 0;
  34. B(1, 0) = 0;
  35. B(1, 1) = -1;
  36. matrix<double> C = prod(A, B);
  37. cout << "Multiplication of " << endl
  38. << A << endl;
  39. cout << "and " << endl
  40. << B << endl;
  41. cout << "is" << endl;
  42. cout << C << endl;
  43. return 0;
  44. }