#include "matrix.h"
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. | |
matrix & | operator+= (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. | |
matrix & | operator+= (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. | |
matrix & | operator-= (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. | |
matrix & | operator-= (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. | |
matrix & | operator*= (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. | |
matrix & | operator*= (double scalar) |
Mutates a matrix by multiplying each element by a scalar constant. |
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.
matrix | ( | int | numberRows, | |
int | numberColumns, | |||
double | value = 0 | |||
) |
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 |
void reshape | ( | int | r, | |
int | c | |||
) |
Reshapes the matrix to the new number of rows and columns.
r | desired number of rows | |
c | desired number of columns |
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
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
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
5 2
matrix transpose | ( | ) | const |
bool operator== | ( | const matrix & | other | ) | const |
Tests if two matrices have identical dimension and content.
other | the second matrix in an expression (m == other) |
bool operator!= | ( | const matrix & | other | ) | const |
The converse of the equivalence test.
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
double v = m(1,2);
r | the indicated row (with 0 designating the top row) | |
c | the indicated column (with 0 designating the leftmost column) |
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
m(1,2) = 25;
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;
5 15 3 8 20 2 25 3 0 0 0 0 0 0 50 0
r | the indicated row (with 0 designating the top row) | |
c | the indicated column (with 0 designating the leftmost column) |
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
double v = m(5);
k | the zero-indexed location in column-major order |
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
m(5) = 25
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.
k | the zero-indexed location in column-major order |
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 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
2 14 23 10 11 12 5 9
C = A + B
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;
invalid_argument | if matrix dimensions do not agree |
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
2 14 23 10 11 12 5 9
A += B
7 29 26 18 31 14 19 12
invalid_argument | if matrix dimensions do not agree |
matrix operator+ | ( | double | scalar | ) | const |
matrix& operator+= | ( | double | scalar | ) |
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
2 14 23 10 11 12 5 9
C = A - B
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;
invalid_argument | if matrix dimensions do not agree |
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
2 14 23 10 11 12 5 9
A -= B
3 1 -20 -2 9 -10 9 -6
invalid_argument | if matrix dimensions do not agree |
matrix operator- | ( | double | scalar | ) | const |
matrix& operator-= | ( | double | scalar | ) |
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
3 10 2 14
C = A * B
283 150
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. |
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
3 10 2 14
A *= B
283 150
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 |
matrix& operator*= | ( | double | scalar | ) |