C++ Bc. 2 cpp

Z GeoWikiCZ
(přesměrováno z C plus plus Bc. 2 cpp)
#include <iostream>
#include <cmath>
#include <matvec/matvec.h>


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


double dominantni_vlastni_cislo_matice(const GNU_gama::Mat<>& A, 
                                       double rel_chyba = 1e-5)
{
  using namespace GNU_gama;

  if ( !A.cols() || (A.cols() != A.rows()) )
    throw Chyba("Chybna dimenze matice A");

  Vec<> v( A.cols() );
  v.set_all(1);
  
  double vlc0, vlc1 = 0;

  do {

    vlc0 = vlc1;
    
    v    = A*v;
    vlc1 = v.norm_L2();
    v   /= vlc1;

  } while( std::abs( (vlc0 - vlc1)/vlc1 ) > rel_chyba);

  return vlc1;
}


int main()
{
  GNU_gama::Mat<> A1 (3, 3), A2 (3, 3);

  A1 =
    -261, 209, -49,
    -530, 422, -98,
    -800, 631, -144;
  
  A2 =
    -261,   0, -49,
    -530, 422, -98,
    -800, 631, -144;
  
  try 
    {
      std::cout << dominantni_vlastni_cislo_matice(A1) << std::endl;
      std::cout << dominantni_vlastni_cislo_matice(A2) << std::endl;
    }
  catch(Chyba err)
    {
      std::cout << err.text << std::endl;
    }
}
10
339.375

[ Zpět ]