Click here to Skip to main content
Licence CPOL
First Posted 11 Oct 2004
Views 45,563
Bookmarked 23 times

RtwMatrix Class

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

About the Author

Rajitha Wimalasooriya

Technical Lead

Sri Lanka Sri Lanka

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generallicense issue Pinmemberhellowh20:08 9 Jul '08  
AnswerRe: license issue PinmemberRajitha Wimalasooriya20:16 28 Mar '10  
GeneralSlow determinant Pinmemberszopen11110:01 10 Aug '06  
GeneralRe: Slow determinant Pinmembermartinpacker5:12 4 Oct '06  
Generalpublic void Reset(), Clear() , the indices are not flexible [modified] PinmemberKaijoe23:53 10 Jul '06  
GeneralInverting doesn't work Pinmembermozzy5556:08 8 Dec '05  
AnswerRe: Inverting doesn't work PinmemberRajitha Wimalasooriya20:12 28 Mar '10  
GeneralAbout Inverse Pinmemberjrf477216:18 30 Nov '04  
GeneralRe: About Inverse PinmemberRajitha Wimalasooriya18:20 30 Nov '04  
QuestionWho needs inverse ? PinmemberJonathan de Halleux20:59 19 Oct '04  
AnswerRe: Who needs inverse ? PinmemberWillemM20:13 20 Oct '04  
GeneralRe: Who needs inverse ? PinmemberJonathan de Halleux21:53 20 Oct '04  
GeneralRe: Who needs inverse ? PinmemberWillemM22:15 20 Oct '04  
GeneralRe: Who needs inverse ? PinmemberLibor Tinka3:42 4 Nov '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 11 Oct 2004
Article Copyright 2004 by Rajitha Wimalasooriya
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid