TMatrixSolver: A Simple Program and Some Hints

The program input and output are also avaliable.



//
//	Test file - TMatrixSolver class
//	We'll use several classes for a better demonstration 
//

#include <iostream.h>
#include <fstream.h>
#include "tfullmat.h"
#include "tbndmat.h"
#include "tskylmat.h"
#include "tsolve.h"

int main() 
{
	ofstream out;
	ifstream in;
	TFMatrix  A(10,10),	// A: 10x10 zero matrix
	 	  b(10,1),	// b: 10x1 zero vector 
	 	  x(b);		// x: defined as a copy of b

	TFBMatrix Z(10,4);	// Z: 10x10 zero banded matrix
				// with band 4
	TSkylMatrix M(10);	// M: 10x10 zero skyline matrix

	int  iter = 50;		// parameters for iterative methods
	REAL tol = 1e-10, 	
	     w = 1.2;	

//	
// 	Reading matrices from file "solv.in"
//	
	out.open("solv.dat");
	in.open("solv.in");

	b.Input(in);
	A.Input(in);	
	Z.Input(in);
	M.Input(in);

	Z += .01;	
	Z.Print("Z = Z + 0.01");	
	Z.Print("Z = Z + 0.01",out);	// Saves Z on "solv.dat"		
//
//	Solving Linear Systems with TMatrixSolver class
//

	TMatrixSolver solver1(&A),solver2(&Z);
	
	solver1.SetSOR(iter,w,tol,0);
	solver1.Solve(b,x,0);
	x.Print("x: SOR solution u of Ax = b");				
	x.Print("x: SOR solution u of Ax = b",out);
	
	solver1.ResetSolver();
	solver1.Solve(b,x,0);
	x.Print("x: Gauss-Jacobi solution u of Ax = b");		
	x.Print("x: Gauss-Jacobi solution u of Ax = b",out);
	
	solver2.SetDirect(ELU);
	solver2.Solve(b,x,0);
	x.Print("x: LU solution u of Zx = b");				
	x.Print("x: LU solution u of Zx = b",out);
	
	solver2.SetMatrix(&M);
	solver2.SetDirect(ECholesky);
	solver2.Solve(b,x,0);
	x.Print("x: Cholesky solution u of Mx = b");				
	x.Print("x: Cholesky solution u of Mx = b",out);

	out.close();
	in.close();	

	return(0);	
}
			


Input:


	10 1
	1  
	0.5  
	1  
	0  
	-12  
	1.4  
	0 
	0  
	0.6
	1  

	10 10 	
	4.5  0.1  0.5  0.3    0    0  0.5  0.1    0  0.5
	 -1  6.9    0    0    0    0    0    1    0  0.2
	0.5    0  8.5    0    1  0.5    0    0    0    1
	  0    0    0  3.5    0    0  0.3    0 -0.1  0.3  
	  0    0  0.2    0 -4.5    0    0    0    0  0.3
	  1  0.4    0    0    0   15    0  0.8    0  1.1
	  0    0  1.1 -0.3    0    0  7.5    0    0  2.2
	  1    0    0    0    0    0    0  9.5    0  1.3
	2.1    0    0    0 -2.3    0    0    0  -11  0.9
	3.4    1    0  2.2    0    5  0.4   -1    2   50

	10 10	
	30  8  9  2  1	0  0  0  0  0   
	 1 56  3  6  1  1  0  0  0  0   
	 2  2 39  1  1  2  1  0  0  0    
	 1  1  3 86  0  1  3  2  0  0   
	 1  1  1  2 33  1  0  1  1  0   
	 0  2  2  1  1 45  1  3  2  2   
	 0  0  1  4  8  3 89  4  2  5     
	 0  0  0  1  2  0  0 15  3  3   
	 0  0  0  0  7  3  3  1 30  2   
	 0  0  0  0  0  8  1  0  0 19						
	 
	 10				
	 40  8  9  2  1  0  2  1  0  0   
	 56  3  0  1  1  0  0  0  0   
	 91  1  1  2  1  4  2  0    
	 50  0  1  2  2  0  0   
	 33  1  0  1  1  5   
	 55  1  3  2  2   
	 28  4  2  5     
	 85  3  3   
	 30  2    
	 97	
			


Output:


Writing matrix 'Z = Z + 0.01 (banded matrix)' (10 x 10):
	30.01  8.01  9.01  2.01  1.01  0  0  0  0  0  
	1.01  56.01  3.01  6.01  1.01  1.01  0  0  0  0  
	2.01  2.01  39.01  1.01  1.01  2.01  1.01  0  0  0  
	1.01  1.01  3.01  86.01  0.01  1.01  3.01  2.01  0  0  
	1.01  1.01  1.01  2.01  33.01  1.01  0.01  1.01  1.01  0  
	0  2.01  2.01  1.01  1.01  45.01  1.01  3.01  2.01  2.01  
	0  0  1.01  4.01  8.01  3.01  89.01  4.01  2.01  5.01  
	0  0  0  1.01  2.01  0.01  0.01  15.01  3.01  3.01  
	0  0  0  0  7.01  3.01  3.01  1.01  30.01  2.01  
	0  0  0  0  0  8.01  1.01  0.01  0.01  19.01  

Writing matrix 'x: SOR solution u of Ax = b' (10 x 1):
	0.240915  
	0.110903  
	-0.215589  
	-0.019732  
	2.65819  
	0.0745732  
	0.0259698  
	-0.027627  
	-0.563  
	0.0165703  

Writing matrix 'x: Gauss-Jacobi solution u of Ax = b' (10 x 1):
	0.240915  
	0.110903  
	-0.215589  
	-0.019732  
	2.65819  
	0.0745732  
	0.0259698  
	-0.027627  
	-0.563  
	0.0165703  

Writing matrix 'x: LU solution u of Zx = b' (10 x 1):
	0.0332698  
	0.0131871  
	0.0307105  
	-0.00338202  
	-0.370257  
	0.0293421  
	0.0267365  
	0.0224466  
	0.0975053  
	0.0387568  

Writing matrix 'x: Cholesky solution u of Mx = b' (10 x 1):
	0.0304882  
	0.0101088  
	0.0105366  
	-0.00163231  
	-0.371338  
	0.0297321  
	-0.0108086  
	0.000993919  
	0.028396  
	0.0287783  


Hints:

Working with input/output files
To use input and output files, you first need to follow the usual steps: to include an adequate library, to assign a variable to each file, and to open the files. Then you'll can use Input and Print commands. All test programs on this page made use of output files, as solv.dat.
Top of this page
Return to Main Menu