MatrixPseudoInverse Method (Double) |
Namespace: Accord.Math
// Declare a non-invertible matrix as double[,] matrix = { { 6.0, 1.0, 2.0 }, { 0.0, 8.0, 1.0 }, }; // Let's check that this matrix really cannot be // inverted using standard matrix inversion: bool isSingular = matrix.IsSingular(); // should be true // Compute the pseudo-inverse using double[,] pinv = matrix.PseudoInverse(); // We can write the result with string strInv = pinv.ToCSharp(); // The result should be: // new double[,] // { // { 0.193548387096774, 0.0161290322580645, -0.0806451612903226 }, // { 0.010752688172043, 0.139784946236559, -0.032258064516129 }, // { -0.0860215053763441, -0.118279569892473, 0.258064516129032 } // }; // We can confirm this is indeed the pseudo-inverse // by checking whether "matrix * pinv(matrix) * matrix" // equals the original matrix: double[,] r = matrix.Dot(pinv.Dot(matrix)); // Again we write the result: string strA = r.ToCSharp(); // The result should be: // new double[,] // { // { 6.0, 1.0, 2.0 }, // { 0.0, 8.0, 1.0 }, // };