Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Gauss-Elim/GEtest
Binary file not shown.
41 changes: 41 additions & 0 deletions Gauss-Elim/GEtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// GEtest.cpp
// Author: Isaac Stone
// Language: C++
//
// Synopsis: This program simply tests the return
// value of the gaussElim() function described in
// "gaussElim.h". The mat_gen() function generates
// an augmented matrix that is stored into A. Both
// the matrix and the solution to Ax=b are printed
// to the screen

#include <stdlib.h>
#include <stdio.h>
#include "matrix.h"
#include "vectorCode.h"
#include "mat_gen.h"
#include "gaussElim.h"

using namespace std;

int main( int argc, char **argv ) {

Matrix A;
vector<double> a_sols;

A = mat_gen();
a_sols.resize( A.size(), 0 );

// Perform Gussian Elimination with
// back substitution
a_sols = gaussElim( A );

// Print augmented matrix A
printMatrix(A);
printf( "\n" );

// Printf solutions to Ax=b
printVector( a_sols );

return 0;
}
26 changes: 26 additions & 0 deletions Gauss-Elim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
### Gaussian Elimination and Back-Substution Algorithm - gaussElim.h

#### Author: Isaac Stone, University of Tennessee Undergrad of Computer Science

### gaussElim.h
gaussElim.h - Included is gaussElim.h, which implements the Gaussian Elimination and back-substituion method for an augmented matrix. The functionality is fairly simple. The function gaussElim() can be called by any c++ program that includes "gaussElim.h". It takes as its only argument a Matrix ( described in matrix.cpp ) and returns a Vector ( described in vectorCode.cpp ) that is the solution to Ax=b. If no solution exists, or only the trivial solution exists, the program prints an error and exits.

#### Other files worth noting:

mat_gen.h - Generates an augmented matrix using pseudorandom values. It returns a Matrix and can be included via "mat_gen.h". For more, see commments in file.

GEtest.cpp - Used to test gaussElim.h with various inputs from mat_gen.h.

makefile - If working on a linux system, make sure all files are in working directory and type "make" to compile.

iterativeSolvers.h, iterativeSolver.cpp, matrix.h, matrix.cpp, vectorCode.h, VectorCode.cpp -
All these files are dependencies for Matrix and Vector data structures needed for gaussElim.h and mat_gen.h to function as-is. They are written and developed by Christopher Johnson and obtained via GitHub at:

### https://github.com/Christopher42/computational-mathematics

where extensive documentation can be found, along with examples and testing information.

Project was compiled on a linux machine using g++ -std=11 and was produced under the GNU GeneralPublic License, version 3. Feel free to modify, include, and improve gaussElim.h and mat_gen.h under the GGPL. Thank you.


Acknowledgements to Christopher Johnson, the author of the computational-mathematics repository on GitHhub.
89 changes: 89 additions & 0 deletions Gauss-Elim/gaussElim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// GEtest.cpp
// Author: Isaac Stone
// Language: C++
//
// Synopsis: This .cpp file implements Guassin Elimination with
// back-substitution for an augmented matrix. The function
// gaussElim() takes a Matrix ( matrix.h ) as an argument and returns a
// solution to Ax=b in the form of a Vector ( vectorCode.h ). If no solution or
// only the trivial solution exists, a message is printed
// to the screen and the program exits.

#include <vector>
#include <cstdio>
#include <cstdlib>
#include "matrix.h"
#include "vectorCode.h"

using namespace std;

// Elimination step
void Elim( double x, Vector &a, Vector &b ) {

for ( unsigned i = 0; i < a.size(); i++ )
a[i] = a[i] - ( x * b[i] );
}

Vector gaussElim( Matrix A ) {

int i, j, p, n;
double sum, xn, x;
Vector tmp, sols;

x = 0.0;
p = 0;
n = A.size();

// Guassian Elimination
for ( i = 0; i < (n - 1) ; i++ )
{
p = i;
while( A[p][i] == 0 )
{
p++;
if ( p > n )
{
printf( "No solution or trivial solution\n" );
exit(1);
}
}

// Swap rows
if ( p != i ) {
tmp = A[p];
A[p] = A[i];
A[i] = tmp;
}

for ( j = i+1; j < n; j++ )
{
x = A[j][i] / A[i][i];
Elim( x, A[j], A[i] );
}
}

// Test for no solution
if ( A[ n - 1 ][ n - 1 ] == 0 )
{
printf( "No solution or trivial solution\n" );
exit(1);
}

// Perform back-substitution
sols.resize( n, 0 );

xn = A[n-1][n] / A[n-1][n-1];
sols[n-1] = xn;

for ( int i = n-1; i >= 0; i-- )
{
sum = 0;
for ( j = i+1; j < n+1; j++ ) {
sum += ( A[i][j] * sols[j] );
}
xn = ( A[i][n] - sum ) / A[i][i];
sols[i] = xn;
}

return sols;
}
19 changes: 19 additions & 0 deletions Gauss-Elim/gaussElim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// GEtest.cpp
// Author: Isaac Stone
// Language: C++
//
// Synopsis: This file includes headers gaussElim.cpp to implement
// Guassin Elimination with back-substitution for an augmented matrix. The function
// gaussElim() takes a Matrix ( matrix.h ) as an argument and returns a
// solution to Ax=b in the form of a Vector ( vectorCode.h ). If no solution or
// only the trivial solution exists, a message is printed
// to the screen and the program exits.
//
// Check "gaassElim.cpp" for more information.

#include <vector>
#include <cstdlib>
#include "matrix.h"
#include "vectorCode.h"

Vector gaussElim( Matrix A );
Loading