RE: Matrix determinant

"König, Oliver" <[email protected]>
Newsgroups gmane.comp.lib.mtl.devel
Message-ID <[email protected]>
Hi, 
We didn't find a determinant implemented in MTL as well, therefore we implemented our own. It works fine for our needs and I include the code in the following:

#include <mtl/matrix.h>
#include <mtl/mtl.h>
#include <mtl/dense1D.h>
#include <mtl/utils.h>
#include <mtl/lu.h>

/*!This function calculates the determinant of a matrix */
template<class MatrixType>
double lu_det(const MatrixType& _Matrix){

	int size = _Matrix.nrows();
	MatrixType LU(size,size);
	dense1D<int> pvector(size);

	copy(_Matrix, LU);
	lu_factor(LU, pvector);

	//For every permutation of rows the product of 
	//diagonal elements has to be multyplied by -1
	int sign = 1;
	for (int i = 0 ; i < size ; ++i){
   		if ( pvector[i] != i+1 ) 
       		sign *= -1; 
	}

	typename MatrixType::iterator iter;
	iter = LU.begin();

	double det = 1.0;
	unsigned ind = 0;
	for ( iter = LU.begin(); iter != LU.end(); ++iter ){
		ind = iter.index(); 
   		det *= LU(ind, ind);
	}

	det *= sign;
	return det;
}

Kind regards
ok


---------------------------------------------------- 
Oliver Koenig 
Center of Structure Technologies 
Institute of Mechanical Systems 
LEO C12 
ETH Zurich 
CH-8092 Zurich 
Phone : +41 1 632 23 28 
Fax :   +41 1 632 17 02 
Mail :  [email protected] 
WWW :  http://www.imes.ethz.ch/st/ 
---------------------------------------------------- 

-----Original Message----- 
From: Enes Makalic [mailto:[email protected]] 
Sent: Freitag, 30. August 2002 08:49 
To: [email protected] 
Subject: MTL: Matrix determinant 
Hi, 
I am trying to calculate the determinant of a matrix in MTL 
and cant seem to find any functions to do this. 
Is this possible in MTL, if so, how ? 
Thank you, 
- Enes. 

_______________________________________________ 
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/ 
_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.