.NET Matrix Library
Inverse Method
See Also  Example Send comments on this topic


Computes the inverse of the matrix and returns a reference to it.

Syntax

Visual Basic (Declaration) 
Public Function Inverse() As Matrix
C# 
public Matrix Inverse()
C++/CLI 
public:
Matrix^ Inverse(); 

Return Value

A Matrix object representing the inverse of the current matrix.

Exceptions

ExceptionDescription
NotSquareMatrixExceptionMatrix is not square.
SingularMatrixExceptionMatrix is found to be singular.

Example

The following example demonstrates both the Inverse method and the use of the LU class to compute a matrix inverse.
Visual BasicCopy Code
Imports System
Imports Bluebit.MatrixLibrary

Class Test

    Public Shared Sub Main()

        Dim A, B, C As Matrix
        'Declaring a random 5x5 Matrix
        A = New Matrix(5, 5)
        A.FillRandom()
        Console.WriteLine("Matrix A")
        Console.WriteLine(A)

        Try
            'Store the inverse of A in matrix B
            B = A.Inverse
            Console.WriteLine("Matrix B = inverse of A")
            Console.WriteLine(B)
            'Test the result. A*C should be an identity matrix
            C = Matrix.Multiply(A, B)
            Console.WriteLine("C = A*B")
            Console.WriteLine(C)

        Catch e As SingularMatrixException
            Console.WriteLine(e.Message)
        End Try

        'Alternative method using the LU object

        Dim luDec As LU
        luDec = New LU(A)
        'Check for matrix singularity
        If luDec.IsSingular Then
            Console.WriteLine("Matrix is Singular")
        Else
            B = luDec.Inverse
            Console.WriteLine("Matrix B = inverse of A")
            Console.WriteLine(B)
            'Test the result. A*C should be an identity matrix
            C = Matrix.Multiply(A, B)
            Console.WriteLine("C = A*B")
            Console.WriteLine(C)
        End If

        Console.Read()
    End Sub

End Class
C#Copy Code
using System;
using Bluebit.MatrixLibrary;

class Test
{
    static void Main(string[] args)
    {
        Matrix A, B, C;
        //Declaring a random 5x5 Matrix
        A = new Matrix(5, 5);
        A.FillRandom();
        Console.WriteLine("Matrix A");
        Console.WriteLine(A);

        try
        {
            //Store the inverse of A in matrix B
            B = A.Inverse();
            Console.WriteLine("Matrix B = inverse of A");
            Console.WriteLine(B);
            //Test the result. A*C should be an identity matrix
            C = Matrix.Multiply(A, B);
            Console.WriteLine("C = A*B");
            Console.WriteLine(C);
        }
        catch (SingularMatrixException e)
        {
            Console.WriteLine(e.Message);
        }

        //Alternative method using the LU object

        LU luDec;
        luDec = new LU(A);
        //Check for matrix singularity
        if (luDec.IsSingular)
        {
            Console.WriteLine("Matrix is Singular");
        }
        else
        {
            B = luDec.Inverse();
            Console.WriteLine("Matrix B = inverse of A");
            Console.WriteLine(B);
            //Test the result. A*C should be an identity matrix
            C = Matrix.Multiply(A, B);
            Console.WriteLine("C = A*B");
            Console.WriteLine(C);
        }

        Console.Read();
    }
}
C++Copy Code
#include "stdafx.h"

using namespace System;
using namespace Bluebit::MatrixLibrary;

int main(array<System::String ^> ^args)
{
    Matrix ^A, ^B, ^C;
    //Declaring a random 5x5 Matrix
    A = gcnew Matrix(5, 5);
    A->FillRandom();
    Console::WriteLine("Matrix A");
    Console::WriteLine(A);

    try
    {
        //Store the inverse of A in matrix B
        B = A->Inverse();
        Console::WriteLine("Matrix B = inverse of A");
        Console::WriteLine(B);
        //Test the result. A*C should be an identity matrix
        C = Matrix::Multiply(A, B);
        Console::WriteLine("C = A*B");
        Console::WriteLine(C);
    }   
    catch(SingularMatrixException^ e)
    {
        Console::WriteLine(e->Message);
    }

    //Alternative method using the LU object

    LU^ luDec;
    luDec = gcnew LU(A);
    //Check for matrix singularity
    if(luDec->IsSingular)
    {
        Console::WriteLine("Matrix is Singular");
    }
    else
    {
        B = luDec->Inverse();
        Console::WriteLine("Matrix B = inverse of A");
        Console::WriteLine(B);
        //Test the result. A*C should be an identity matrix
        C = Matrix::Multiply(A, B);
        Console::WriteLine("C = A*B");
        Console::WriteLine(C);
    }
    
    Console::Read();
    
	return 0;
}

Remarks

Use Inverse method with error handling to trap errors arising from the matrix being a singular matrix.

An alternative method for computing the matrix inverse is to use the LU object. The LU object can then be reused for further computations as solving systems of linear equations.

Requirements

Platforms: Windows 7, Windows Vista, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows 2000

See Also

.NET Matrix Library Documentation