C++ Bc. 32 cpp
#include <iostream>
#include <matvec/matvec.h>
struct Chyba
{
Chyba(std::string t) : text(t) {}
std::string text;
};
void reseni(const GNU_gama::Mat<>& U, GNU_gama::Vec<>& x)
{
if (U.rows() != U.cols() || U.rows() != x.dim())
throw Chyba("chybne zadane dimenze");
const int N = x.dim();
for (int i=1; i<=N; i++)
{
double p = U(i,i);
if (p == 0) throw Chyba("nulovy prvek na diagonale");
p = x(i)/p;
x(i) = p;
for (int j=i+1; j<=N; j++)
{
x(j) -= U(j,i)*p;
}
}
}
int main()
{
GNU_gama::Mat<> L(5,5);
L =
3, 0, 0, 0, 0,
0, 1, 0, 0, 0,
3, 4, 1, 0, 0,
4, 2, 0, 2, 0,
1, 1, 2, 6, 3;
GNU_gama::Vec<> b(5); b = 3, 2, 14, 16, 48;
try
{
reseni(L, b);
std::cout << trans(b);
}
catch(Chyba& ch)
{
std::cout << ch.text;
}
}
[ Zpět ]