Accord.Math.Decompositions.SingularValueDecomposition.Inverse C# (CSharp) Method

Inverse() public method

Computes the (pseudo-)inverse of the matrix given to the Singular value decomposition.
public Inverse ( ) : ].double[
return ].double[
        public double[,] Inverse()
        {
            double e = Threshold;

            // X = V*S^-1
            int vrows = v.GetLength(0);
            int vcols = v.GetLength(1);
            var X = new double[vrows,s.Length];
            for (int i = 0; i < vrows; i++)
            {
                for (int j = 0; j < vcols; j++)
                {
                    if (System.Math.Abs(s[j]) > e)
                        X[i, j] = v[i, j]/s[j];
                }
            }

            // Y = X*U'
            int urows = u.GetLength(0);
            int ucols = u.GetLength(1);
            var Y = new double[vrows,urows];
            for (int i = 0; i < vrows; i++)
            {
                for (int j = 0; j < urows; j++)
                {
                    double sum = 0;
                    for (int k = 0; k < ucols; k++)
                        sum += X[i, k]*u[j, k];
                    Y[i, j] = sum;
                }
            }

            return Y;
        }
    }

Usage Example

        public void InverseTest()
        {
            double[,] value = new double[,]
            { 
                  { 1.0, 1.0 },
                  { 2.0, 2.0 }
            };

            SingularValueDecomposition target = new SingularValueDecomposition(value);

            double[,] expected = new double[,]
            {
               { 0.1, 0.2 },
               { 0.1, 0.2 }
            };

            double[,] actual = target.Solve(Matrix.Identity(2));
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.001));

            actual = target.Inverse();
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.001));
        }
All Usage Examples Of Accord.Math.Decompositions.SingularValueDecomposition::Inverse