C++ Bc. 2 cpp: Porovnání verzí

Z GeoWikiCZ
Bez shrnutí editace
 
m (zjednodušená verze)
 
(Není zobrazeno 7 mezilehlých verzí od 3 dalších uživatelů.)
Řádek 1: Řádek 1:
[ [[C plus plus Bc. 2|Zpět]] ]
#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
 
[ [[C++ Bc. 2|Zpět]] ]
 
[[Kategorie:Programování]]
[[Kategorie:C++]]

Aktuální verze z 27. 11. 2006, 20:56

#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 ]