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 numColumns () const
 Returns the current number of columns for the matrix.
int numRows () const
 Returns the current number of rows for the matrix.
matrix size () const
 Returns a row-vector whose entries designate the current dimensions of the matrix.
void reshape (int r, int c)
 Reshapes the matrix to the new number of rows and columns.
matrix diag () const
 Converts between two-dimensional matrixes and one-dimensional diagonals.
matrix transpose () const
 Generates a matrix that is the transpose form of the current one.
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 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 numColumns (  )  const

Returns the current number of columns for the matrix.

int numRows (  )  const

Returns the current number of rows 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.

void reshape ( int  r,
int  c 
)

Reshapes the matrix to the new number of rows and columns.

Parameters:
r desired number of rows
c desired number of columns
Exceptions:
invalid_argument if total number of elements does not remain the same

matrix diag (  )  const

Converts between two-dimensional matrixes and one-dimensional diagonals.

Specifically, if the given matrix is one-dimensional, then this constructs a new square matrix that has the given entries along the diagonal, and zeros elsewhere.

Alternatively, if the given matrix is two-dimensional, this returns a new matrix that represents a row-vector describing the diagonal of the given matrix.

As an example, given a matrix m with contents:

 5 15  3
a call to m.diag() would return the result
 5  0  0
 0 15  0
 0  0  3

Conversely, given a matrix m with contents

 5 15  3  8
20  2 25  3
 0  0 23  9
 0  0 50  0
a call to m.diag() would return the result
 5  2 23  9

Note that if the original matrix is not square, the diagonal has length that is the shorter of the number of rows or number of columns. For example, givne a matrix m with contents

 5 15  3  8
20  2 25  3
a call to m.diag() would return the result
 5  2

matrix transpose (  )  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.transpose()
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.

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)
Exceptions:
out_of_range if either index is invalid for the current dimensions

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)
Exceptions:
out_of_range if either index is negative

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
Exceptions:
out_of_range if index is invalid

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 unless it is either a row vector or column vector.

Parameters:
k the zero-indexed location in column-major order
Exceptions:
out_of_range if index is negative
out_of_range if index is beyond the extent of a two-dimensional matrix

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 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.

Exceptions:
invalid_argument if matrix dimensions do not agree

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.

Exceptions:
invalid_argument if matrix dimensions do not agree

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.

Exceptions:
invalid_argument if matrix dimensions do not agree

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.

Exceptions:
invalid_argument if matrix dimensions do not agree

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.

Exceptions:
invalid_argument if the inner matrix dimensions do not agree. That is, when computing A*B, the number of columns in A must match the number of rows in 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 Thu Jan 27 12:58:28 2011 by  doxygen 1.5.6