This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Wrong determinant with dynamic matrix

Tags: None
(comma "," separated)
ilyav
Registered Member
Posts
2
Karma
0
Hello. I don't understand why this code calculates different determinants for the same matrix. According to the package manager Eigen version 3.1.2 and GCC version 4.7.3 are installed on my Ubuntu 13.04 64-bit. I've also tried Eigen 3.2.0 and development branch. Compiling with Clang gives the same results. Did I miss some obvious thing?

P. S. Sorry for my English
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
please put the code here under [CODE][/CODE] tags.
ilyav
Registered Member
Posts
2
Karma
0
Sorry for this
Code: Select all
//#define EIGEN_DONT_ALIGN true // no changes

#include <Eigen/Dense>
#include <Eigen/LU>
#include <iostream>

using namespace Eigen;
using namespace std;

int main()
{
   const int n=2;
   
   Matrix<int,Dynamic,Dynamic> a(n,n);
   //MatrixXi a(n,n); // doesn't work too
   
   a<<2,-3,1,-1;
   cout<<a<<endl;
   cout<<a.determinant()<<endl;
   /*
    * output:
    *  2 -3
    *  1 -1
    * -2
    */

   cout<<endl;
   
   Matrix<int,n,n> b;
   b<<2,-3,1,-1;
   cout<<b<<endl;
   cout<<b.determinant()<<endl;
   /*
    * output:
    *  2 -3
    *  1 -1
    * 1
    */
}
jitseniesen
Registered Member
Posts
204
Karma
2
The function .determinant() computes the determinant of a general matrix using an LU decomposition with partial pivoting. This is not defined for integer matrices, and this is why the first half of your program fails. We should change Eigen so that it fails at compile-time.

If the size of the matrix is known at compile-time and small enough, then .determinant() computes the determinant directly. This works also for integer matrices and explains why the second half of the program succeeds.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]