## Matrix Package |

- Title:
- Matrix Package
- Language:
- C++
- Author:
- Philip J. Erdelsky
- Date:
- December 2, 1998
- Usage:
- Public domain; no restrictions on use
- Portability:
- Any C++ compiler that implements templates, exceptions and properties
- Keywords:
- Matrix
- Abstract:
- A C++ package to perform basic matrix operations
- Source:
- matrix.txt

This package performs all of the most common matrix operations for rather arbitrary matrices. The package is implemented as a template class, in which the type of the matrix elements is a parameter.

The file MATRIX.H includes the entire package. It must be included in, or copied into, any source file that uses the package.

To define a matrix, first choose a type for the matrix elements. Usually, this is float or double. It must be a type which implements the usual aritmetic operations: addition, subtraction, multiplication, division and comparison.

Then define a matrix type as follows:

general form: typedef matrix<T> mymatrix; example: typedef matrix<float> mymatrix;

There are two constructors:

mymatrix A(height, width); int height; number of rows in matrix int width ; number of columns in matrix mymatrix A(B); mymatrix B; matrix to be copied into A

Notice that the dimensions of a matrix are determined when it is constructed and cannot be changed dynamically.

The usual matrix operations are implemented as overloaded operators:

A + B A - B - A A * B x * B x is a scalar of the same type as matrix elements A = B assignment of matrices A == B comparison of matrices

Other operations are as follows:

A.inverse A.transpose A.height A.width A(i,j) element in i-th row and j-th column of A, where the upper left element is A(1,1) out << A edit and send a matrix to an output stream in >> A read a matrix from an input stream

A static function returns an identity matrix of the specified size:

mymatrix::I(N) returns N X N identity matrix

Both the input and output operations process the elements in lexicographical order, i.e., the order in which English text is usually read when the matrix elements are in their usual positions.

If there is an error, the package will throw an exception called mymatrix::exception, which has a member that explains the error. You can catch the exception as follows:

try { ... matrix operations } catch (mymatrix::exception &E) { cout << E.message; }

Expressions such as A.height, A.width, A.transpose and A.inverse are implemented as properties. If the C++ compiler does not support properties, slight changes will convert them to member functions.