title: (Pseudo)-Inverse of N by N matrix with zero determinant
body: <p>I would like to take the inverse of a nxn matrix to use in my GraphSlam.</p>
  
  <p>The issues that I encountered:</p>
  
  <ul>
  <li><code>.inverse()</code> Eigen-library (3.1.2) doesn't allow zero values, returns <code>NaN</code></li>
  <li>The LAPACK (3.4.2) library doesn't allow to use a zero determinant, but allows zero values (used example code from <a href="https://stackoverflow.com/questions/3519959/computing-the-inverse-of-a-matrix-using-lapack-in-c">Computing the inverse of a matrix using lapack in C</a>)</li>
  <li>Seldon library (5.1.2) wouldn't compile for some reason </li>
  </ul>
  
  <p>Did anyone successfully implemented an <em>n</em> x <em>n</em> matrix inversion code that allows negative, zero-values and a determinant of zero? Any good library (C++) recommendations?</p>
  
  <p>I try to calculate the omega in the following for GraphSlam:
  <a href="http://www.acastano.com/others/udacity/cs_373_autonomous_car.html" rel="nofollow noreferrer">http://www.acastano.com/others/udacity/cs_373_autonomous_car.html</a></p>
  
  <hr>
  
  <p>Simple example:</p>
  
  <pre><code>[ 1 -1  0 0 ]
  [ -1 2 -1 0 ]
  [ 0 -1  1 0 ]
  [ 0  0  0 0 ]
  </code></pre>
  
  <p>Real example would be 170x170 and contain 0's, negative values, bigger positive values.
  Given simple example is used to debug the code. </p>
  
  <hr>
  
  <p>I can calculate this in matlab (Moore-Penrose pseudoinverse) but for some reason I'm not able to program this in C++.</p>
  
  <pre><code>A = [1 -1 0 0; -1 2 -1 0; 0 -1 1 0; 0 0 0 0]
  B = pinv(A)
  B=
  [0.56   -0.12  -0.44  0]
  [-0.12  0.22   -0.11  0]
  [-0.44  -0.11   0.56  0]
  [0  0  0   0]
  </code></pre>
  
  <hr>
  
  <p>For my application I can (temporarily) remove the dimension with zero's.<br>
  So I am going to remove the 4th column and the 4th row. <br>
  I can also do that for my 170x170 matrix, the 4x4 was just an example. <br></p>
  
  <p>A:</p>
  
  <pre><code>[ 1 -1  0 ]
  [ -1 2 -1 ]
  [ 0 -1  1 ]
  </code></pre>
  
  <p>So removing the 4th column and the 4th row wouldn’t bring a zero determinant.
  But I can still have a zero determinant if my matrix is as above.
  This when the sum of each row or each column is zero. (Which I will have all the time in GraphSlam)<br></p>
  
  <p>The LAPACK-solution (Moore-Penrose Inverse based) worked if the determinant was not zero (used example code from <a href="https://stackoverflow.com/questions/3519959/computing-the-inverse-of-a-matrix-using-lapack-in-c">Computing the inverse of a matrix using lapack in C</a>).<br> But failed as a "pseudoinverse" with a determinant of zero.<br></p>
  
  <hr>
  
  <p><strong>SOLUTION:</strong> (all credits to Frank Reininghaus), using SVD(singular value decomposition)<br>
  <a href="http://sourceware.org/ml/gsl-discuss/2008-q2/msg00013.html" rel="nofollow noreferrer">http://sourceware.org/ml/gsl-discuss/2008-q2/msg00013.html</a> <br><br>
  Works with:</p>
  
  <ul>
  <li>Zero values (even full 0 rows and full 0 columns)</li>
  <li>Negative values </li>
  <li>Determinant of zero</li>
  </ul>
  
  <p>A^-1:</p>
  
  <pre><code>[0.56   -0.12  -0.44]
  [-0.12  0.22   -0.11]
  [-0.44  -0.11   0.56]
  </code></pre>