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

MatrixXd.deteminant() unexplained behaviour - Eigen 3.13

Tags: None
(comma "," separated)
User avatar
prasnuts
Registered Member
Posts
5
Karma
0
OS
Hi,
The following is a matrix that I get after accumulating some data points.

12259.3 127.299 12233.8 124.392
41.709 12146.1 25.6935 12129.9
12193.4 25.6935 12168.8 0.852933
1.38459 12129.9 0.852933 12129.3


When I calculate the deteminant of the matrix I get 0.0331911 as the answer, which is incorrect.
In order to be sure, I defined a fresh matrix and gave it the above values where I get 9.62156e+07 as the answer which is correct.
This is the code snippet where I obtain the first answer.
Code: Select all
   std::cout << "Covar[0] is" << std::endl << covarVec[index] << std::endl << std::endl;
   std::cout << "Deteminant is = " << covarVec[index].determinant() << std::endl << std::endl;

This is the code snippet where the values are hardcoded.
Code: Select all
   Eigen::MatrixXd p = Eigen::MatrixXd(4,4);
   p(0,0) = 12259.300;   p(0,1) = 127.299;   p(0,2) = 12233.800;   p(0,3) = 124.392;
   p(1,0) = 127.299;   p(1,1) = 12254.000;   p(1,2) = 102.288;   p(1,3) = 12251.100;
   p(2,0) = 12233.800;   p(2,1) = 102.288;   p(2,2) = 12213.200;   p(2,3) = 99.9517;
   p(3,0) = 124.392;   p(3,1) = 12251.100;   p(3,2) = 99.9517;   p(3,3) = 12248.400;

   cout << "Expected covar[0]" << endl;
   cout << p << endl << endl;
   cout << "Expected Determinant = " << p.determinant() << endl;
   cout << "Max double = " << std::numeric_limits<double>::max() << endl;

And the results obtained are:
Image
Although I am using a vector to store number of MatrixXd instances, I don't think that it should matter as the values of the matrixs are correct, hence the deteminant should also be correct. Am I wrong to have thought like that?
I am a bit perplexed how this could happen. Any input would be much appreciated.

Thank you,
Prasad
User avatar
prasnuts
Registered Member
Posts
5
Karma
0
OS
Small precision that I forgot to add. I am also using the Eigen/StdVector in my implementation. Sadly, having it or not didn't change the results I got.

Code: Select all
   std::vector<Eigen::MatrixXd, Eigen::aligned_allocator<Eigen::MatrixXd> > covarVec;
User avatar
prasnuts
Registered Member
Posts
5
Karma
0
OS
If it helps; their norms are the same but Eigen values differ. Here is the output.

Image
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Sorry but I don't understand your issue. The first matrix you shown is not the same than the one of the code. Please, output with full precision the matrix for which you think the determinant is not correct. Also, an alternative to compute the determinant of a matrix is:
Code: Select all
p.jacobiSvd().singularValues().prod()
User avatar
prasnuts
Registered Member
Posts
5
Karma
0
OS
Hi Gaël,
Thank you very much for your quick response. I am sorry that my explanation of the problem wasn’t clear. And I am extremely sorry to have incuded incorrect values for the matrix in the first comment, which I had got from another test case. I will try to do better this time.
covarVec is a vector of MatrixXd. The number of elements in the vector and the size of the matrix depend on the user input. For one of the test cases, I print the value of the first element in the vector (covarVec[0]) which is 4x4 matrix. Then I calculate its determinant.
Here is the output.
Image
And here is the code snippet.
Code: Select all
std::cout << "Covar[0] is" << std::endl << covarVec[index] << std::endl << std::endl;
std::cout << "Deteminant is = " << covarVec[index].determinant() << std::endl << std::endl;

The value 0.0331911 is incorrect. As you can see, I am not performing any other operation between the two lines and the application is single threaded as well. Hence, I think it is fair to assume that as soon as the value is displayed, the deteminant is calculated.

In order to do a simple test, I created another 4x4 matrix giving the values obtained from the covarVec[0] and calculated its determinant which gave me the correct answer of 9.62156e+07.
This is the code snippet.
Code: Select all
   Eigen::MatrixXd p = Eigen::MatrixXd(4,4);
   p(0,0) = 12259.300;   p(0,1) = 127.299;   p(0,2) = 12233.800;   p(0,3) = 124.392;
   p(1,0) = 127.299;   p(1,1) = 12254.000;   p(1,2) = 102.288;   p(1,3) = 12251.100;
   p(2,0) = 12233.800;   p(2,1) = 102.288;   p(2,2) = 12213.200;   p(2,3) = 99.9517;
   p(3,0) = 124.392;   p(3,1) = 12251.100;   p(3,2) = 99.9517;   p(3,3) = 12248.400;

   cout << "Expected covar[0]" << endl;
   cout << p << endl << endl;
   cout << "Expected Determinant = " << p.determinant() << endl;
   cout << "Max double = " << std::numeric_limits<double>::max() << endl;

And here is the output.
Image
Since the values of both the matrices are equivalent, they should give the same determinant. Yet, it doesn’t happen.
Using the alternative code snippet you have given, I obtain the same results as the previous case.
This is not a test case specific behavior as I get erratic behavior irrespective of the test inputs.

Finally, here are the outputs with higher precision.
For the covarVec
Image
For the hardcoded matrix.
Image

Thank you,
Prasad
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
The last digits might be of high importance, I recommend you to try the "hardcoded" version with full precision. If you still get the "correct" result, then you probably have some memory problems with your code and I recommend you to try it with walgrind.
User avatar
prasnuts
Registered Member
Posts
5
Karma
0
OS
Hi Gaël,

Thank you very much for the insight. You are spot on with the precision playing a part in the output. It is exactly what that had happened. Once I make the hardcoded values to be as same as that of the highest possible precision values, the deteminant appears to be the same. I wouldn't have imagined that it would play such a part, now I know better, thanks to your guidance.

For the completion of the thread, I am attaching the obtained outputs using the highest available precision.
Obtained output
Image
Expected output
Image

I really appreciate your advices, thank you very much again,
Prasad.


Bookmarks



Who is online

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