C++ Bc. 1 cpp

Z GeoWikiCZ
#include <iostream>
#include <matvec/matvec.h>

struct Chyba
{
  template <typename T> Chyba(T t) : text(t) {}
  
  std::string text;
};


GNU_gama::Mat<> soucin(const GNU_gama::Mat<>&, const GNU_gama::Mat<>&);
void            soucin(const GNU_gama::Mat<>&, const GNU_gama::Mat<>&, GNU_gama::Mat<>&);


int main()
{
  GNU_gama::Mat<> A (2, 3), B (3, 3), B1 (2, 3), C;

  A =
    1, 5, 2,
    1, 2, 6;
  
  B =
    2, 2, 4,
    1, 2, 3,
    4, 2, 2;

  B1 =
    2, 3, 1,
    1, 3, 4;
  
  try 
    {
      std::cout << A * B << std::endl;
      std::cout << soucin(A, B);
      std::cout << "-------------------------------------\n\n";

      soucin(A, B1, C);
      std::cout << C << std::endl;
    }
  catch(Chyba err)
    {
      std::cout << err.text << std::endl;
    }

  return 0;
}


GNU_gama::Mat<> soucin(const GNU_gama::Mat<>& A, const GNU_gama::Mat<>& B)
{
  GNU_gama::Mat<> C;
  soucin(A, B, C);

  return C;
}


void soucin(const GNU_gama::Mat<>& A, const GNU_gama::Mat<>& B, GNU_gama::Mat<>& C)
{

  if (A.cols() != B.rows() || A.rows() == 0 || A.cols() == 0 || B.cols() == 0)
    {
      throw Chyba("Matice nelze nasobit");
    }

  C.reset (A.rows(), B.cols());
  
  double c_ij;
  
  for (int i = 1; i <= C.rows(); i++)
    for (int j = 1; j <= C.cols(); j++)
      {
        c_ij = 0.0;
        for (int k = 1; k <= A.cols(); k++)
          {
            c_ij += A(i, k) * B(k, j);
          }
        C(i, j) = c_ij;
      }

  return;
}

[ Zpět ]