Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Math - GaussJordan function (A function for quick Gauss Jordan Matrix Elminiation - solving equations)
Math - GaussJordan function [message #59085] Sat, 29 October 2022 20:55
frederik.dumarey is currently offline  frederik.dumarey
Messages: 24
Registered: December 2021
Location: Belgium
Promising Member
Hey all,

I recently needed a Matrix elimination technique in order to solve some linear equations. Gauss-Jordan is a really quick algorithm for this purpose, and the fact that I wanted a function that accepts 2D arrays by reference needed some special handling using a template Smile

For those of you that are interested in the algorithm, or want to see how I solved the multi dimensional by ref function header:


template <typename TwoDArray>
void GaussJordan(const int& n, TwoDArray& a)
{
	for (int i=0; i<n; i++)
	{
		if(a[i][i] == 0.0)
		{
			cout << "Error, zero pivot values found!";
			exit(1);
		}
		
		for (int j=0; j<n; j++)
		{
			if (i!=j)
			{
				double ratio = a[j][i]/a[i][i];
				for (int k=0; k<n+1; k++)
				{
					a[j][k] = a[j][k] - ratio * a[i][k];
				}
			}
		}
	}
	
	for (int i=0; i<n; i++)
	{
		a[i][n] /= a[i][i];
		a[i][i] = 1.0;
	}
}



Have coding fun!
Frederik.


Regards,

Frederik Dumarey
Belgium
Previous Topic: compiling issue with Clang
Next Topic: Make THISFN simpler and more powerful
Goto Forum:
  


Current Time: Fri Apr 19 17:48:57 CEST 2024

Total time taken to generate the page: 0.03615 seconds