determinant of n by n matrix!

CppBuilder2006 2 Tallied Votes 438 Views Share

this program contains a template function that can calculate determinant of any n by n matrix using permutations.
it can be run in Visual C++ 2010 Express.

#include <conio.h>
#include <iostream>
#include <vector>
using namespace std;

class Permute
{
	vector<int> set;
	vector<vector<int> > all;

public:
	void Print()
	{
		unsigned size = all.size();
		for(unsigned i = 0; i < size; i++)
		{
			unsigned size_i = all[i].size();
			for(unsigned j = 0; j < size_i; j++)
				cout<< all[i][j] << "  ";
			cout<< "\n";
		}
	}
	//-------------------------------------------
	void Run()
	{
		unsigned size = set.size();
		if(size == 0)
			return;
		if(size == 1)
		{
			all.push_back(set);
			return;
		}

		unsigned i = 0; /* sweeper item can be any number between 0 & size - 1 */

		vector<int> subset;
		for(unsigned j = 0; j < size; j++)
		{
			if(i != j)
				subset.push_back(set[j]);
		}
		Permute P(subset);

		unsigned allsize = P.Size();

		for(unsigned k = 0; k < allsize; k++)
		{
			unsigned size_k = P[k].size();
			for(unsigned m = 0; m <= size_k; m++)
			{
				P[k].insert(P[k].begin() + m,set[i]);
				all.push_back(P[k]);
				P[k].erase(P[k].begin() + m);
			}
		}
	}
	//-------------------------------------------
	unsigned Size()
	{
		return all.size();
	}
	//-------------------------------------------
	vector<int>& operator [](unsigned i)
	{
		if(Size() <= i)
			i = Size() - 1;
		return all[i];
	}
public:
	Permute(vector<int> set)
		: set(set)
	{
		Run();
	}
};

int Sign(vector<int> v) // sign of the permutation
{
	unsigned returns = 0;
	unsigned size = v.size();
	for(unsigned i = 0; i < size; i++)
		for(unsigned j = i + 1; j < size; j++)
		{
			if(v[j] < v[i])
				returns++;
		}

	if(returns % 2 == 0)
		return 1;
	else
		return -1;
}

template<const int n> // n by n matrix
double Determinant(double a[n][n]) // determinant
{
	vector<int> v;
	for(unsigned i = 0; i < n; i++)
		v.push_back(i);
	Permute P(v);
	unsigned size = P.Size();

	double sum = 0;
	for(unsigned k = 0; k < size; k++)
	{
		double prod = Sign(P[k]);
		for(unsigned i = 0; i < n; i++)
			prod *= a[i][P[k][i]];
		sum += prod;
	}
	return sum;
}

int main()
{
	double a[3][3] =
	{
		{0, 0, 1},
		{5,-2, 6},
		{1, 1, 2}
	};	
	cout<< Determinant<3>(a);
	_getch();
}
mrnutty 761 Senior Poster

Not sure what technique you used, but in comp Sci its usually done
by making the matrix in echelon form and multiplying the diagonal numbers
Nice job though.

CppBuilder2006 -5 Junior Poster

I'm a math student! :)

Fbody 682 Posting Maven Featured Poster

I haven't bothered to test this because I don't remember the correct process so I wouldn't know valid results from invalid. This concerns me though:

Permute(vector<int> set)
		: set(set)
	{
		Run();
	}

Do initializers automatically resolve the "this" pointer? Otherwise, this looks like a scoping issue to me.

CppBuilder2006 -5 Junior Poster

Do initializers automatically resolve the "this" pointer?

class Permute : it fills all with all possible permutations of set !
classes can be used as functions! class Permute is a more complete function! And in this program it is marginal!

I wouldn't know valid results from invalid.

good concern! thiiiiiiiiiiiiiiiiis may solve the problem to some extent

slazzer10 0 Newbie Poster

cn i ask the code above? cn i use dev c++?

aruldave 0 Newbie Poster

The result can be verified by comparing the value of this determinant program result to the list of matrix calculators @ Online Matrix Calculators

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.