gamesmathematical programsotherabout author

Matrix library

ru

download source code

This program is a sorce code of matrix library. It includes two classes Matrix and MatrixException. The program demonstrates object-orienting programming, exception handling, operators overloading, working with dinamical memory. The code can be used for mathematical problems where you need use matrices and/or vectors.

Creation of the matrices (constructors)

Matrix M(3,3); - creates matrix 3x3.
Matrix M(2,2 ,1,0,0,1);- creates matrix 2x2 and fill it's elements. So M is matrix which has ones on diagonal and zeros on the other positions.
Note: All init elements of the constructor should have type double or type int
Let's see on example Matrix M(2,2, 1.0,1,0,1). Such creation and initialization will produces difficult to find error because first initialization element has type double but all the others have type int. You should use ALL initialization elements as well. If you creates matrix 3x3 use 9 initializaton numbers or don't use them at all.
Matrix M(3) such initialization produces matrix 1x1 with initialization element equals 3

Access to matrix elements and getting dimensions of the matrix

For getting number of rows of the matrix use GetRows() member, analogous use GetCols() member for getting number of columns.

Example

Matrix M(3,2);
int i=M.GetRows(); // i = 3
int j=M.GetCols(); // j = 2

To get access to matrix elements use overload () operator. Matrix elements zero index based.

Example

Matrix M(2,2, 1,2,3,4);
int i=M(1,0); // i = 3 equals to the element of the second row and first column

Operators

This library uncludes only simple operations such as addition, substruction, multiplication, division, matrix trasposition, inversion, division/mutiplication by number, addition/substracton with a number (if matrix has dimensions 1x1), unary +, unary -. All of the operations have natural notation. For transposition you can use T function or unary *

Example

Matrix A(3,2);
Matrix B=T(A);
Matrix C=*A;//matrices B and C are equal to transposition of matrix A.

Type transformation

It's possible to transformate the matrix to double type if matrix has dimensions 1x1

Example

Matrix A(1,1, 3.4);
Matrix B(1.8);
double a=double(A); // a=3.4
double b=double(B); // b=1.8

Exceptions

The code includes MatrixException class which is use when some operator impossible to execute.

Example

try{
  Matrix A(2,2, 1,2,3,4);
  Matrix B(1,2, 1,2);
  Matrix C;
  C=A+B;
}
catch(CMatrixException ex){
  ex.PrintMessage(); //print: Addition isn't possible
}

Example

#include "matrix.cpp"
void main(){
  try{
    Matrix A(3,1, 1,2,3),B(2,1, 1,2); // creates two vectors
    double d=2;
    Matrix C=T(B),D=*B; //  C = D = Transposition(B).
    A.Print(); // print content of the matrix
    Print(B); // print content of the matrix
    B/=d; // B=B/d;
    A=A+B; // addition
    int i=B.GetRows(); // i=2
    i=B.GetCols(); // i=1
    d=B(0,0);// d=1
  }
  catch(CMatrixException Me){
    Me.PrintMessage();// Addition isn't possible
  }
}