Matrices are a very useful thing in computer programming, especially in 3D graphics programming. Out of all the mathematics I learned at University, matrices are the one thing that keep rearing their head over and over.
I am not going to go into major detail over what a matrix is - wikipedia can tell you that much better than I can - so will focus mainly on how to implement one in C#.
Essentially, a matrix is a 2-dimensional array of numbers, often representing a set of equations. The number of columns and rows is flexible.
Using the Matrix class below, the constructor can either take an x and y value or an array to determine how big to make the internal array for the matrix.
The Zero() function sets all entries in the matrix to zero by looping through the entire array.
The SetIdentity() function makes the matrix an identity matrix, which means that all the values are 0, except for the values along the diagonal from top left to bottom right being set to 1.
The Transpose function transforms the columns of the matrix into the rows, and vice versa.
The Scale function multiplies each value in the matrix by a scaling factor.
The determinant of an array is found by adding all the products of the entries of each diagonal sloping in a right direction, and subtracting the products of the entries of each diagonal sloping in a opposite direction. This is done with the Determinant function
The Trace function returns the sum of the main diagonal of the matrix.
The matrix class has also got a few overloaded operators, to make handling matrices easier.
The + operator is overloaded so that when passed two matrices, each value in the one matrix is added to the equivalent value in the second matrix. Both matrices need to be the same size for this to work.
A second overload for the + operator takes a scalar value and adds it to the matrix, which just adds that value to each value in the matrix.
The - operator performs identically to the + operator, except that it subtracts instead of adds the matrices.
The** * **operator multiplies the two matrices together, which is a little more involved than addition.