forum.alglib.net
http://forum.alglib.net/

Error in rmatrixinverse
http://forum.alglib.net/viewtopic.php?f=2&t=324
Page 1 of 1

Author:  Lee [ Sun Mar 20, 2011 11:28 pm ]
Post subject:  Error in rmatrixinverse

Hi alglib folks. First of all, thank you for making such a nice library. It is very useful!

I have run into an error in rmatrixinverse. Instead of returning the inverse of the input, it returns the negative of the inverse. When the returned inverse is multiplied with the original matrix, I get the negative of the identity matrix, not the identity matrix.

Please let me know if I am doing something incorrect. I appreciate your help.

I will provide C# test code to demonstrate.

Code:
// INITIALIZE VARIABLES
double[,] identity= new double[3, 3];
double[,] inverse = new double[3, 3];
double[,] original = new double[3, 3];
original[0, 0] = 1970.8937129336348;
original[0, 1] = 950.2643718955394;
original[0, 2] = 68.593866605156848;
original[1, 0] = -717.567202536722;
original[1, 1] = 1679.1060115738908;
original[1, 2] = 1237.0289481009058;
original[2, 0] = 0.31232455601872822;
original[2, 1] = -0.21461017714273767;
original[2, 2] = 0.92541657839832714;
for(int row_index = 0; row_index < 3; row_index++)
{
  for(int column_index = 0; column_index < 3; column_index++)
  {
     inverse[row_index, column_index] = original[row_index, column_index];
  }
}

// COMPUTE INVERSE
int success = 0;
alglib.matinv.matinvreport report = new alglib.matinv.matinvreport();
alglib.matinv.rmatrixinverse(ref inverse, 3, ref success, ref report);
if (success != 1)
{
  throw new Exception("Failure to solve for inverse.");
}

// CHECK RESULTS
ablas.rmatrixgemm(3,
        3,
        3,
        1.0,
        ref inverse,   // 3 x 3
        0,
        0,
        0,
        ref original,   // 3 x 3
        0,
        0,
        0,
        0,
        ref identity,  // 3 x 3
        0,
        0);

for(int row_index = 0; row_index < 3; row_index++)
{
  for(int column_index = 0; column_index < 3; column_index++)
  {
     if(row_index == column_index)
     {
       if(Math.Abs(identity[row_index, column_index] - 1) > Math.Pow(10,-6))
       {
            throw new Exception("Inverse multiplied by original does not give identity.");
       }
     }
     else
     {
       if(Math.Abs(identity[row_index, column_index]) > Math.Pow(10,-6))
       {
            throw new Exception("Inverse multiplied by original does not give identity.");
       }
     }
  }
}

Author:  Sergey.Bochkanov [ Wed Mar 23, 2011 5:45 pm ]
Post subject:  Re: Error in rmatrixinverse

I've tried to reproduce this error. First, I was unable to compile your code with recent version of ALGLIB. Some arguments were passed with unnecessary ref's (don't know how you've managed to compile it). Second, after fixes, it calculated inverse successfully, with desired precision.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/