matrix Class Reference

Representation of a two-dimensional matrix. More...

#include "matrix.h"

List of all members.

Public Member Functions

 matrix ()
 Constructs an empty matrix.
 matrix (int numberRows, int numberColumns, double value=0)
 Constructs a matrix with given number of rows and columns.
int numRows () const
 Returns the current number of rows for the matrix.
int numColumns () const
 Returns the current number of columns for the matrix.
matrix size () const
 Returns a row-vector whose entries designate the current dimensions of the matrix.
bool operator== (const matrix &other) const
 Tests if two matrices have identical dimension and content.
bool operator!= (const matrix &other) const
 The converse of the equivalence test.
double operator() (int r, int c) const
 Provides read-only access to a specific matrix element.
double & operator() (int r, int c)
 Provides a live reference to a specific matrix element.
double operator() (int k) const
 Provides read-only access to a matrix element specified with a linear index.
double & operator() (int k)
 Provides a live reference to a matrix element specified with a linear index.
matrix operator- () const
 Generates a matrix that is the negated form of the current one.
matrix operator~ () const
 Generates a matrix that is the transpose form of the current one.
matrix operator+ (const matrix &other) const
 Generates a matrix that is the element-by-element sum of two matrices.
matrixoperator+= (const matrix &other)
 Mutates a matrix by doing an element-by-element addition with another matrix.
matrix operator+ (double scalar) const
 Generates a matrix by adding a scalar constant to each element of another matrix.
matrixoperator+= (double scalar)
 Mutates a matrix by adding a scalar constant to each element.
matrix operator- (const matrix &other) const
 Generates a matrix that is the element-by-element difference of two matrices.
matrixoperator-= (const matrix &other)
 Mutates a matrix by doing an element-by-element addition with another matrix.
matrix operator- (double scalar) const
 Generates a matrix by subtracting a scalar constant from each element of another matrix.
matrixoperator-= (double scalar)
 Mutates a matrix by subtracting a scalar constant from each element.
matrix operator* (const matrix &other) const
 Generates a matrix that is the product of two matrices.
matrixoperator*= (const matrix &other)
 Mutates a matrix by multiplying it with another.
matrix operator* (double scalar) const
 Generates a matrix by multiplying each element of another matrix by a scalar constant.
matrixoperator*= (double scalar)
 Mutates a matrix by multiplying each element by a scalar constant.


Detailed Description

Representation of a two-dimensional matrix.

This represents a two-dimensional matrix of double precision floating-point numbers. It may be that the span of one or both of the dimensions is one, in which case this represents a row vector or column vector. One or both of the dimensions might be zero, in which case it represents an empty matrix.

When linearized, the matrix is stored in column-major order.


Constructor & Destructor Documentation

matrix (  ) 

Constructs an empty matrix.

Technically, this matrix is 0x0 dimensional.

matrix ( int  numberRows,
int  numberColumns,
double  value = 0 
)

Constructs a matrix with given number of rows and columns.

By default, all matrix entries are zero, but they can be set to any indicated scalar value.

Parameters:
numberRows the initial number of rows
numberColumns the initial number of columns
value the initial value for all entries (0 by default)


Member Function Documentation

int numRows (  )  const

Returns the current number of rows for the matrix.

int numColumns (  )  const

Returns the current number of columns for the matrix.

matrix size (  )  const

Returns a row-vector whose entries designate the current dimensions of the matrix.

As an example, if a matrix currently had contents:

 5 15  3  8
20  2 14  3
a call to size would return the result
2 4
as there are two rows and four columns.

bool operator== ( const matrix other  )  const

Tests if two matrices have identical dimension and content.

Parameters:
other the second matrix in an expression (m == other)

bool operator!= ( const matrix other  )  const

The converse of the equivalence test.

Parameters:
other the second matrix in an expression (m != other)

double operator() ( int  r,
int  c 
) const

Provides read-only access to a specific matrix element.

As a simple example, assume that matrix m has contents:

 5 15  3  8
20  2 14  3
This method is invoked when using the syntax
double v = m(1,2);
in which case the value 14 results (as that is row 1, column 2).

Parameters:
r the indicated row (with 0 designating the top row)
c the indicated column (with 0 designating the leftmost column)

double& operator() ( int  r,
int  c 
)

Provides a live reference to a specific matrix element.

The caller may use the reference to directly mutate the content of the cell. As a simple example, assume that matrix m has contents:

 5 15  3  8
20  2 14  3
This method is invoked when using the syntax
m(1,2) = 25;
in which case the matrix now has contents
 5 15  3  8
20  2 25  3

Note that if a row or column is indicated beyond the current extent of the matrix, the matrix is automatically reized to make room for the new entry, with other new cells getting value 0. For example the syntax This method is invoked when using the syntax

m(3,2) = 50;
causes the above matrix to be reconfigured as
 5 15  3  8
20  2 25  3
 0  0  0  0
 0  0 50  0

Parameters:
r the indicated row (with 0 designating the top row)
c the indicated column (with 0 designating the leftmost column)

double operator() ( int  k  )  const

Provides read-only access to a matrix element specified with a linear index.

Matrices are linearized using the column-major convention. As a simple example, assume that matrix m has contents:

 5 15  3  8
20  2 14  3
This method is invoked when using the syntax
double v = m(5);
in which case the value 14 results (as that has index 5 in column-major order).

Parameters:
k the zero-indexed location in column-major order

double& operator() ( int  k  ) 

Provides a live reference to a matrix element specified with a linear index.

Matrices are linearized using the column-major convention. As a simple example, assume that matrix m has contents:

 5 15  3  8
20  2 14  3
This method is invoked when using the syntax
m(5) = 25
in which case the matrix now has contents
 5 15  3  8
20  2 25  3

Note: unlike the (row,column) form of this assignment, a matrix cannot be resized using the linearized index. Use of an index that is beyond the extent of the matrix will result in a runtime error.

Parameters:
k the zero-indexed location in column-major order

matrix operator- (  )  const

Generates a matrix that is the negated form of the current one.

As a simple example, assume that matrix m has contents:

 5 15  3  8
20  2 14  3
Then the expression
-m
will result in a new matrix
 -5 -15  -3  -8
-20  -2 -14  -3

Note that the contents of the original matrix m remain unchanged.

matrix operator~ (  )  const

Generates a matrix that is the transpose form of the current one.

As a simple example, assume that matrix m has contents:

 5 15  3  8
20  2 14  3
Then the expression
~m
will result in a new matrix
 5 20
15  2
 3 14
 8  3

Note that the contents of the original matrix m remain unchanged.

matrix operator+ ( const matrix other  )  const

Generates a matrix that is the element-by-element sum of two matrices.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
and matrix B has contents:
 2 14 23 10
11 12  5  9
Then the syntax
C = A + B
will result in a new matrix C with contents
 7 29 26 18
31 14 19 12

Note that the contents of both A and B remain unchanged, although one could be reassigned using a syntax such as

A = A + B;
See also operator+= below.

Note: a runtime error occurs if the two matrices do not have equivalent dimensions.

matrix& operator+= ( const matrix other  ) 

Mutates a matrix by doing an element-by-element addition with another matrix.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
and matrix B has contents:
 2 14 23 10
11 12  5  9
Then the command
A += B
will cause the contents of A to be set to
 7 29 26 18
31 14 19 12
while leaving the contents of B unchanged.

Note: a runtime error occurs if the two matrices do not have equivalent dimensions.

matrix operator+ ( double  scalar  )  const

Generates a matrix by adding a scalar constant to each element of another matrix.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
Then the syntax
B = A + 5;
will result in a new matrix B with contents
10 20  8 13
25  7 19  8
while leaving the contents of A unchanged.

matrix& operator+= ( double  scalar  ) 

Mutates a matrix by adding a scalar constant to each element.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
Then the syntax
A += 5
causes the contents of A to be set to
10 20  8 13
25  7 19  8

matrix operator- ( const matrix other  )  const

Generates a matrix that is the element-by-element difference of two matrices.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
and matrix B has contents:
 2 14 23 10
11 12  5  9
Then the syntax
C = A - B
will result in a new matrix C with contents
  3   1 -20  -2
  9 -10   9  -6

Note that the contents of both A and B remain unchanged, although one could be reassigned using a syntax such as

A = A - B;
See also operator-= below.

Note: a runtime error occurs if the two matrices do not have equivalent dimensions.

matrix& operator-= ( const matrix other  ) 

Mutates a matrix by doing an element-by-element addition with another matrix.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
and matrix B has contents:
 2 14 23 10
11 12  5  9
Then the command
A -= B
will cause the contents of A to be set to
  3   1 -20  -2
  9 -10   9  -6
while leaving the contents of B unchanged.

Note: a runtime error occurs if the two matrices do not have equivalent dimensions.

matrix operator- ( double  scalar  )  const

Generates a matrix by subtracting a scalar constant from each element of another matrix.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
Then the syntax
B = A - 5;
will result in a new matrix B with contents
 0 10 -2  3
15 -3  9 -2
while leaving the contents of A unchanged.

matrix& operator-= ( double  scalar  ) 

Mutates a matrix by subtracting a scalar constant from each element.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
Then the syntax
A -= 5
causes the contents of A to be set to
 0 10 -2  3
15 -3  9 -2

matrix operator* ( const matrix other  )  const

Generates a matrix that is the product of two matrices.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
and matrix B has contents:
 3
10
 2
14
Then the syntax
C = A * B
will result in a new matrix C with contents
283
150
Note that the contents of both A and B remain unchanged.

Note: the inner dimensions of the product must agree or else a runtime error occurs. That is the number of columns in A must match the number of rows in B when computing A*B. The resulting product has the same number of rows as A and the same number of columns as B.

matrix& operator*= ( const matrix other  ) 

Mutates a matrix by multiplying it with another.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
and matrix B has contents:
 3
10
 2
14
Then the syntax
A *= B
will cause the contents of A to be reset to
283
150
while leaving the contents of B unchanged.

Note: the inner dimensions of the product must agree or else a runtime error occurs. That is the number of columns in A must match the number of rows in B when computing A*B. The resulting product has the same number of rows as A and the same number of columns as B.

matrix operator* ( double  scalar  )  const

Generates a matrix by multiplying each element of another matrix by a scalar constant.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
Then the syntax
B = A * 2;
will result in a new matrix B with contents
10 30  6 16
40  4 28  6
while leaving the contents of A unchanged.

matrix& operator*= ( double  scalar  ) 

Mutates a matrix by multiplying each element by a scalar constant.

As a simple example, assume that matrix A has contents:

 5 15  3  8
20  2 14  3
Then the syntax
A *= 2
causes the contents of A to be set to
10 30  6 16
40  4 28  6


The documentation for this class was generated from the following file:

Generated on Wed Sep 8 16:20:55 2010 by  doxygen 1.5.6