We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,307 Members — Technology Publication meets Social Media

# determinant of n by n matrix!

By CppBuilder2006 on Apr 15th, 2010 9:53 am

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();
}``````

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.

firstPerson
Industrious Poster
4,046 posts since Dec 2008
Reputation Points: 851
Skill Endorsements: 15

I'm a math student! :)

CppBuilder2006
Junior Poster
110 posts since Feb 2009
Reputation Points: 5
Skill Endorsements: 0

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.

Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Skill Endorsements: 5

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

CppBuilder2006
Junior Poster
110 posts since Feb 2009
Reputation Points: 5
Skill Endorsements: 0

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

slazzer10
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Skill Endorsements: 0

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

aruldave
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10