2
$\begingroup$

I have done a program to calculate a determinant of a matrix. My program works, but the problem is that it takes long time to calculate, especially for big matrix. Could you tell me how can a perform my program in order to calculate the determinant in the shortest possible time?

double Matrice::Determinant(int n) {    cout<<"n = "<get_el(0,0);    } else if (n == 2) {       det = this->get_el(0,0) * this->get_el(1,1) - this->get_el(1,0) * this->get_el(0,1);    } else {       det = 0;       for (j1=0;j1

Thank you.

  • 1
    Your algorithm runs in time $O(n!)$ and is probably numerically unstable. Always try to use a library routine when doing calculations with matrices. In this case, there is no LAPACK routine for determinant, but you can use some of the decomposition routines to get access to the eigenvalue. See for example the link in my comment to Berci's answer.2012-10-01

2 Answers 2

2

One simple way is to do an LU decomposition and then multiply the determinants of the resulting matrices, because

$\det M=\det LU =\det L \det U.$

This is $O(n^3)$.

There are faster but more complicated ways.

I should mention that answer is just if you are writing this for your own edification. If you actually need to use such a routine in some application, you should follow Yuval Filmus's advice and find a library routine.

5

Use the Gaussian algorithm to get an upper triangle matrix, and multiply its elements in the diagonal.

  • 0
    Here is a link to some code using LAPACK: http://www.r-bloggers.com/matrix-determinant-with-the-lapack-routine-dspsv/2012-10-01