/*
LinearSolverExample.cpp
By Matthew Fisher
Example of LinearSolver and sparse matrix classes
*/
#include <iostream>
#include <math.h>
using namespace std;
typedef unsigned int UINT;
#include "ArrayVector.h"
#include "SparseMatrix.h"
#include "LinearSolver.h"
#include "SparseMatrix.cpp"
#include "LinearSolver.cpp"
/*
This will demonstrate the use of the LinearSolver class to find the solution to Ax = b,
A =
( 1 2 6 0 )
( 0 6 0 5 )
( 9 3 2 1 )
( 6 0 7 0 )
b =
( 1 )
( -5 )
( 0 )
( 2 )
For which the solution, by Mathematica, is
x =
( 0.0972763 )
( -0.155642 )
( 0.202335 )
( -0.81323 )
*/
void main()
{
LinearSolver S;
S.Init(4); //initalize the solver as a 4x4 sparse matrix
//fill in the matrix, row by row
S.PushElement(0, 0, 1.0);
S.PushElement(0, 1, 2.0);
S.PushElement(0, 2, 6.0);
S.PushElement(1, 1, 6.0);
S.PushElement(1, 3, 5.0);
S.PushElement(2, 0, 9.0);
S.PushElement(2, 1, 3.0);
S.PushElement(2, 2, 2.0);
S.PushElement(2, 3, 1.0);
S.PushElement(3, 0, 6.0);
S.PushElement(3, 2, 7.0);
//fill in the b-vector
S.b[0] = 1.0;
S.b[1] = -5.0;
S.b[2] = 0.0;
S.b[3] = 2.0;
//solve the system Ax = b. The result will be in S.x
S.BiCGradSolve();
cout << "x = " << endl;
cout << S.x[0] << endl;
cout << S.x[1] << endl;
cout << S.x[2] << endl;
cout << S.x[3] << endl;
cin.get();
}
Top