patterncppMinor
Calculating the determinant of a matrix
Viewed 0 times
determinantcalculatingthematrix
Problem
I wanted to do some exercise and came up with the idea of a good challenge (for my level of course). I tried to implement Laplace's algorithm for computing the determinant, recursively.
```
#include
#include
#include
#include
using namespace std;
int getMinimoCount = 0; //ignore these, just to keep track of the recursion.
int calcDetCount = 0;
void printMatrix ( vector > M) {
//just does what it means
int size = M.size();
for( int i = 0; i > getMinimo( vector > src, int I, int J, int ordSrc ) {
// Compute and return the minimum of the element I J
// If the element is not in the Ith row or Jth column it will get copied to the minimum matrix
getMinimoCount++;
vector > minimo( ordSrc-1, vector (ordSrc-1,0));
int rowCont = 0;
for( int i=0; i > src, int ord) {
// Here be recursion.
calcDetCount++;
if ( ord == 2 ) {
double mainDiag = src[0][0] * src[1][1];
double negDiag = src[1][0] * src[0][1];
return mainDiag - negDiag; }
else {
double det = 0;
for( int J = 0; J > min = getMinimo( src, 0, J, ord);
if ( (J % 2) == 0 ) { det += src[0][J] * calcDet( min, ord-1); }
else { det -= src[0][J] * calcDet( min, ord-1); }
};
return det;
}
}
int main() {
// Just some UI to gather the matrix. not really convinced of this.
int ord;
cout > ord; cout > mainMatrix( ord, vector (ord, 0));
cout > mainMatrix[countY][countX];};
};
system("CLS");
cout << "############## MATRIX DET ##############" << endl << endl;
cout << endl << endl << " This is the input matrix:" << endl << endl << endl;
printMatrix( mainMatrix );
system("PAUSE");
system("CLS");
cout << "############## MATRIX DET ##############" << endl << endl;
cout << " Working...!" << endl;
double det = calcDet( mainMatrix, ord );
system("CLS");
cout << endl << endl << "############## MATRIX DET ##############" << endl << endl;
cout << " Det =\t" << det << endl << endl;
cout << " getMinimo() chiamata: " << g
```
#include
#include
#include
#include
using namespace std;
int getMinimoCount = 0; //ignore these, just to keep track of the recursion.
int calcDetCount = 0;
void printMatrix ( vector > M) {
//just does what it means
int size = M.size();
for( int i = 0; i > getMinimo( vector > src, int I, int J, int ordSrc ) {
// Compute and return the minimum of the element I J
// If the element is not in the Ith row or Jth column it will get copied to the minimum matrix
getMinimoCount++;
vector > minimo( ordSrc-1, vector (ordSrc-1,0));
int rowCont = 0;
for( int i=0; i > src, int ord) {
// Here be recursion.
calcDetCount++;
if ( ord == 2 ) {
double mainDiag = src[0][0] * src[1][1];
double negDiag = src[1][0] * src[0][1];
return mainDiag - negDiag; }
else {
double det = 0;
for( int J = 0; J > min = getMinimo( src, 0, J, ord);
if ( (J % 2) == 0 ) { det += src[0][J] * calcDet( min, ord-1); }
else { det -= src[0][J] * calcDet( min, ord-1); }
};
return det;
}
}
int main() {
// Just some UI to gather the matrix. not really convinced of this.
int ord;
cout > ord; cout > mainMatrix( ord, vector (ord, 0));
cout > mainMatrix[countY][countX];};
};
system("CLS");
cout << "############## MATRIX DET ##############" << endl << endl;
cout << endl << endl << " This is the input matrix:" << endl << endl << endl;
printMatrix( mainMatrix );
system("PAUSE");
system("CLS");
cout << "############## MATRIX DET ##############" << endl << endl;
cout << " Working...!" << endl;
double det = calcDet( mainMatrix, ord );
system("CLS");
cout << endl << endl << "############## MATRIX DET ##############" << endl << endl;
cout << " Det =\t" << det << endl << endl;
cout << " getMinimo() chiamata: " << g
Solution
Includes
You're using C++. You really ought to
Namespace
This is a very bad idea. It pollutes the global namespace with everything from
System calls
Please don't do this. It makes your code completely non-portable. If you really must use an OS-dependent feature like this, at the very least isolate it in its own function, so you only have to change it one place instead of all through your code.
Algorithm
The way that's taught in high school for calculating the determinant of the matrix is rather inefficient (though simple to apply). The time is proportional to \$n!\$ -- that's right, factorial.
Especially on a computer, it's much better to get the determinant by one of the decomposition methods (basically, extended Gaussian elimination). This is an \$O(n^3)\$ process instead. Check out Wikipedia for some ideas.
#include
#include You're using C++. You really ought to
#include the C++ versions of the headers instead of the C ones:#include
#include Namespace
using namespace std;This is a very bad idea. It pollutes the global namespace with everything from
std. If std gets updated and includes a symbol that conflicts with something in your project, you're in big trouble. It's not much work to prepend std:: to your objects, but if you're really that lazy, just import what you need:using std::vector;
using std::cin;
using std::cout;
using std::endl;System calls
system("CLS");Please don't do this. It makes your code completely non-portable. If you really must use an OS-dependent feature like this, at the very least isolate it in its own function, so you only have to change it one place instead of all through your code.
Algorithm
The way that's taught in high school for calculating the determinant of the matrix is rather inefficient (though simple to apply). The time is proportional to \$n!\$ -- that's right, factorial.
Especially on a computer, it's much better to get the determinant by one of the decomposition methods (basically, extended Gaussian elimination). This is an \$O(n^3)\$ process instead. Check out Wikipedia for some ideas.
Code Snippets
#include <math.h>
#include <stdlib.h>#include <cmath>
#include <cstdlib>using namespace std;using std::vector;
using std::cin;
using std::cout;
using std::endl;system("CLS");Context
StackExchange Code Review Q#88378, answer score: 7
Revisions (0)
No revisions yet.