// test_Gauss.cpp (c) adolfo@di-mare.com #include "Gaussian_Elimination.h" #include "rational.h" #include "BUnit.h" #undef Matriz_Repeat #undef Matrix_Sparse #undef Matrix_Dense #define USE_Matrix_List #if defined(USE_Matrix_Dense) #include "Matrix_Dense.h" typedef Mx::Matrix_Dense< rational > Rat_Matrix; #endif #if defined(USE_Matrix_List) #include "Matrix_List.h" typedef Mx::Matrix_List< rational > Rat_Matrix; #endif #if defined(USE_Matrix_Sparse) #include "Matrix_Sparse.h" typedef Mx::Matrix_Sparse< rational > Rat_Matrix; #endif #if defined(USE_Matriz_Repeat) #include "Matriz_Repeat.h" typedef Matriz_Repeat< rational > Rat_Matrix; #endif using namespace std; /// Clase simple para probar \c Gauss(). class test_Gauss : public TestCase { Rat_Matrix ID; ///< Matriz identidad Rat_Matrix M1, M2, M3, M4; Rat_Matrix B1, B2, B3, B4; public: void do_cout(); void setUp(); bool run(); private: void Ejecutor(const Rat_Matrix& M, const Rat_Matrix& B); void test_makeMatrix_long(); }; // test_Gauss /// Inicializa los valores para \c test_Gauss. void test_Gauss::setUp() { ID.reSize(3,3); // Matriz identidad B1.reSize( ID.rows(), 1 ); // B1 queda sincronizado con ID ID(0,0) = 1; ID(0,1) = 0; ID(0,2) = 0; B1(0,0) = 10; ID(1,0) = 0; ID(1,1) = 1; ID(1,2) = 0; B1(1,0) = 10; ID(2,0) = 0; ID(2,1) = 0; ID(2,2) = 1; B1(2,0) = 10; M2.reSize(3,3); B2.reSize( M2.rows(), 1 ); M2(0,0) = 14; M2(0,1) = 21; M2(0,2) = 7; B2(0,0) = 1; M2(1,0) = -7; M2(1,1) = 14; M2(1,2) = 7; B2(1,0) = 0; M2(2,0) = 7; M2(2,1) = 35; M2(2,2) = 14; B2(2,0) = 14; M3.reSize(3,3); B3.reSize( M3.rows(), 1 ); M3(0,0) = 0; M3(0,1) = 0; M3(0,2) = rational(1,3); B3(0,0) = 4; M3(1,0) = 0; M3(1,1) = 0; M3(1,2) = rational(2,3); B3(1,0) = 4; M3(2,0) = 0; M3(2,1) = 0; M3(2,2) = rational(1,3); B3(2,0) = 4; M4.reSize(3,3); B4.reSize( M4.rows(), 1 ); M4(0,0) = 13; M4(0,1) = 26; M4(0,1) = 39; B4(0,0) = 65; M4(1,0) = 52; M4(1,1) = 65; M4(1,1) = 78; B4(1,0) = -91; M4(2,0) = 91; M4(2,1) = 104; M4(2,2) = 130; B4(2,0) = -26; } /// cout << M; template void print( std::ostream &COUT , MAT & V ) { for (unsigned i=0; i < V.rows(); ++i) { for (unsigned j=0; j < V.cols(); ++j) { COUT << ' ' << V(i,j); } COUT << '\n'; } } #if 0 template std::ostream& operator<< (std::ostream &COUT, const MAT& M) { print( COUT , M ); return COUT; } /// Graba en \c cout el valor actual de las matrices de prueba. void test_Gauss::do_cout() { cout << endl << endl << "=============" << endl; cout << "ID " << ID << endl << "......" << endl; cout << "B1 " << B1 << endl << "======" << endl; cout << endl << endl << "======" << endl; cout << "M2 " << M2 << endl << "======" << endl; cout << "B2 " << B2 << endl << "======" << endl; cout << endl << endl << "======" << endl; cout << "M3 " << M3 << endl << "======" << endl; cout << "B3 " << B3 << endl << "======" << endl; cout << endl << endl << "======" << endl; cout << "M4 " << M4 << endl << "======" << endl; cout << "B4 " << B4 << endl << "======" << endl; } #endif /// Método auxiliar que permite hace pruebas con todas las matrices void test_Gauss::Ejecutor(const Rat_Matrix& M, const Rat_Matrix& B) { Rat_Matrix X; Rat_Matrix Bmod = B; if ( rational(0) == Gaussian_Elimination(M, X, B) ) { fail_Msg("Matriz singular"); return; } assertTrue(M*X == B); // La matriz sabe multiplicar assertTrue( check_ok( M ) ); for (unsigned i=1; i<12; ++i) { for (unsigned j=0; j( B(j,0).num() * j, B(j,0).den() + j); } Gaussian_Elimination(M, X, Bmod); assertTrue( check_ok( X ) ); assertTrue( check_ok( Bmod ) ); assertTrue(M*X == Bmod); } } /// Ejecuta las pruebas para \c test_Gauss. bool test_Gauss::run() { Rat_Matrix X; assertTrue(ID*B1 == B1); Ejecutor(ID, B1); assertTrue( rational(0) == Gaussian_Elimination(M2, X, B2) ); // M2 es singular assertTrue( rational(0) == Gaussian_Elimination(M3, X, B3) ); // M3 es singular Ejecutor(M4, B4); B4.swap( M4 ); assertTrue( check_ok( M2 ) ); test_makeMatrix_long(); return wasSuccessful(); } Rat_Matrix makeMatrix_long(const char **V , int rows , int cols); /// Ejecuta las pruebas para \c makeMatrix_long(). void test_Gauss::test_makeMatrix_long() { {{ // test::makeMatrix_long() const char *M_val[3] = { " 0 1 2 3 4 5 6" , "10 11 12 13 14 15 16" , "20 21 22 23 24 25 26" }; Rat_Matrix M = makeMatrix_long( M_val, 3, 7 ); for (unsigned i=0; i::test_makeMatrix_long() */ Rat_Matrix makeMatrix_long(const char **V , int rows , int cols) { Rat_Matrix M; if (V == 0 || rows<=0 || cols<=0) { return M; } if (*V == 0) { return M; } enum { SI_Digito, NO_Digito }; M.reSize(rows, cols); for ( int row=0; row