Click here to Skip to main content
15,891,372 members
Articles / Programming Languages / C#
Article

RtwMatrix Class

Rate me:
Please Sign up or sign in to vote.
3.32/5 (8 votes)
11 Oct 2004CPOL2 min read 62.4K   1.3K   24   14
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.

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generallicense issue Pin
hellowh9-Jul-08 20:08
hellowh9-Jul-08 20:08 
AnswerRe: license issue Pin
Rajitha Wimalasooriya28-Mar-10 20:16
Rajitha Wimalasooriya28-Mar-10 20:16 
GeneralSlow determinant Pin
szopen11110-Aug-06 10:01
szopen11110-Aug-06 10:01 
GeneralRe: Slow determinant Pin
martinpacker4-Oct-06 5:12
martinpacker4-Oct-06 5:12 
Generalpublic void Reset(), Clear() , the indices are not flexible [modified] Pin
Kaijoe10-Jul-06 23:53
Kaijoe10-Jul-06 23:53 
GeneralInverting doesn't work Pin
mozzy5558-Dec-05 6:08
mozzy5558-Dec-05 6:08 
AnswerRe: Inverting doesn't work Pin
Rajitha Wimalasooriya28-Mar-10 20:12
Rajitha Wimalasooriya28-Mar-10 20:12 
GeneralAbout Inverse Pin
jrf477230-Nov-04 16:18
jrf477230-Nov-04 16:18 
GeneralRe: About Inverse Pin
Rajitha Wimalasooriya30-Nov-04 18:20
Rajitha Wimalasooriya30-Nov-04 18:20 
It seems you have a valid point there. I think it's better to return false if the matrix is not a square matrix. I have included the coding below how IsInvertible() method should be changed to accomplish this. Thanks for pointing it out.

public bool IsInvertible()
{
    try
    {
        if(this.Determinant() == 0)
        {
	    return false;
        }
    }
    catch(RtwMatrixException ex)
    {
        return false;
    }

    return true;
} 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.