Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

RtwMatrix Class

0.00/5 (No votes)
11 Oct 2004 1  
A general purpose matrix class.

Sample Image - rtwmatrix.jpg

Introduction

I thought of writing this matrix class because I needed a matrix class with more features and that can be used in a different syntax than the System.Drawing.Drawing2D.Matrix provided by the .NET class libraries.

Description

Unlike the System.Drawing.Drawing2D.Matrix which only has a limited usage, this class is a general purpose class that can be used in any matrix manipulation purposes. Most of the code in the class is self explanatory, so I'll jump straight to the properties and methods exposed by the class.

Properties

  • Rows (get) - Number of rows in the matrix.
  • Columns (get) - Number of columns in the matrix.

Methods

  • Determinant() - Returns the determinant of the matrix.
  • Adjoint() - Returns the adjoint matrix.
  • Minor(int row, int column) - Returns the minor with respect to the elements represented by the row and the column.
  • IsIdentity() - Returns true if the matrix is an identity matrix otherwise false.
  • IsInvertible() - Returns true if the matrix is invertible, otherwise false.
  • Reset() - Makes the matrix an identity matrix.
  • Clear() - Makes the matrix a zero matrix.

Operators

  • == Equals
  • != Not equal
  • * Multiply (3 overloads)
  • + Addition
  • - Subtract
  • / Division
  • ^ Power of
  • ~ Transpose
  • ! Inverse

An indexer is also implemented so the elements of a matrix instance can be accessed as a multidimensional array. This provides a more natural way of accessing elements than implementing separate GetElement() and SetElement() methods.

private float[,] m_matrix;

public float this[int row, int column]
{
    get
    {
        return m_matrix[row,column];
    }

    set
    {
        m_matrix[row,column] = value;
    }
}

Using the class

The way to create an instance of the RtwMatrix class is shown below:

int rows = 3;    //specify the number of rows

int cols = 3;    //specify the number of columns


RtwMatrix mtx = new RtwMatrix(rows, cols);

//the matrix is a zero matrix by default

//add code to initialize the elements

An example of using a RtwMatrix instance follows:

RtwMatrix mtx1, mtx2, mtxResult;

//code to initialize mtx1 and mtx2 goes here


try
{
    mtxResult = mtx1 * mtx2;        //multiply and store the result


    Console.WriteLine(!mtxResult);  //print the inverse to screen

}
catch(RtwMatrixException ex)
{
    Console.WriteLine(ex.Message);
}

More code can be found in the demo project on using the RtwMatrix class. I hope this class can be found useful. All comments and bug reports are welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here