CS488 PA4
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
Matrix Class Reference

#include <Matrix.h>

Inherited by Vector.

Public Member Functions

 Matrix ()
 
 Matrix (int rows, int columns)
 
 Matrix (const Matrix &original)
 
 ~Matrix ()
 
double get (int r, int c) const
 
double get (int i) const
 
Matrix getColumn (int c) const
 
int getColumns () const
 
int getRows () const
 
double & operator() (int r, int c)
 
double & operator() (int i)
 
Matrixoperator= (std::initializer_list< double > values)
 
Matrixoperator= (const Matrix &other)
 

Protected Member Functions

void allocateMemory (int rows, int columns)
 
void deallocateMemory ()
 
void setValues (double value)
 
void setValues (const Matrix &other)
 
void setValues (const double *values)
 
void setValues (double **values)
 

Protected Attributes

double ** values
 
int columns
 
int rows
 

Friends

double cof (const Matrix &A, int i, int j)
 
double det (double a)
 
double det (const Matrix &A)
 
double dot (const Matrix &A, const Matrix &B)
 
Matrix identity (int size)
 
double mminor (const Matrix &A, int i, int j)
 
Matrix operator| (const Matrix &A, const Matrix &B)
 
Matrix operator+ (const Matrix &A, const Matrix &B)
 
Matrix operator- (const Matrix &A, const Matrix &B)
 
Matrix operator* (const Matrix &A, const Matrix &B)
 
Matrix operator* (double k, const Matrix &A)
 
Matrix operator* (const Matrix &A, double k)
 
bool operator== (const Matrix &A, const Matrix &B)
 
bool operator!= (const Matrix &A, const Matrix &B)
 
Matrix submatrix (const Matrix &A, int i, int j)
 
Matrix trans (const Matrix &A)
 

Detailed Description

An encapsulation of a Matrix.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

Matrix::Matrix ( )

Default Constructor (constructs a 2x2 Matrix).

◆ Matrix() [2/3]

Matrix::Matrix ( int  rows,
int  columns 
)

Explicit Value Constructor.

Parameters
rowsThe number of rows
columnsThe number of columns

◆ Matrix() [3/3]

Matrix::Matrix ( const Matrix original)

A copy constructor.

Note: A copy constructor is critical because without on we can't pass a Matrix by value (which requires that it be possible to make a copy).

Parameters
originalThe Matrix to copy

◆ ~Matrix()

Matrix::~Matrix ( )

Destructor

Member Function Documentation

◆ allocateMemory()

void Matrix::allocateMemory ( int  rows,
int  columns 
)
protected

Allocate memory for this Matrix.

Parameters
rowsThe number of rows
columnsThe number of columns

◆ deallocateMemory()

void Matrix::deallocateMemory ( )
protected

Deallocate the memory used by this Matrix

◆ get() [1/2]

double Matrix::get ( int  r,
int  c 
) const

Get a particular element of this Matrix.

Parameters
rThe row index
cThe column index
Exceptions
out_of_rangeif r or c are out of bounds
Returns
The value of the element

◆ get() [2/2]

double Matrix::get ( int  i) const

Get a particular element of this Matrix if it contains a single row or single column.

Parameters
iThe index
Exceptions
out_of_rangeif i is out of bounds
length_errorif this is neither a single row nor single column
Returns
The value of the element

◆ getColumn()

Matrix Matrix::getColumn ( int  c) const

Get a column of this Matrix.

Note: This is not an efficient method, and it would be much better to use rows rather than columns (since we are using row-major ordering). However, this is more consistent with the mathematical treatment in most books.

Parameters
cThe index of the column (0-based)
Exceptions
out_of_rangeif c is out of bounds
Returns
The column at the given index

◆ getColumns()

int Matrix::getColumns ( ) const

Get the number of columns in this Matrix.

Returns
The number of columns

◆ getRows()

int Matrix::getRows ( ) const

Get the number of rows in this Matrix.

Returns
The number of rows

◆ operator()() [1/2]

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

Access an element of this Matrix using the function-call operator.

Note: This method returns by reference so that this operator can be used on the left side of the assignment operator. Though this can be dangerous in general (since the value being referred to may not always exist), in this case it shouldn't cause any problems.

Examples of use:

d = y(1,1);
y(2,3) = 5.0;
Parameters
rThe row index
cThe column index
Exceptions
out_of_rangeif r or c are out of bounds
Returns
The value of the element

◆ operator()() [2/2]

double & Matrix::operator() ( int  i)

Access a particular element of this Matrix using the function call operator if it contains a single row or single column.

Parameters
iThe index
Exceptions
out_of_rangeif i is out of bounds
length_errorif this is neither a single row nor single column
Returns
The the element

◆ operator=() [1/2]

Matrix & Matrix::operator= ( std::initializer_list< double >  values)

Assign an initializer_list to this Matrix.

Example of use:

y = {1, 2, 3,
4, 5, 6,
7, 8, 9};

Note: This method is not void so that one can write x = y = {1, 2} (which first assigns {1, 2} to y and then assigns the result of that assignment to x). It returns the result by reference because there is no concern that this will not refer to something.

Parameters
valuesThe initializer_list containing the values
Exceptions
length_errorif the list is the wrong size
Returns
The Matrix referred to by this

◆ operator=() [2/2]

Matrix & Matrix::operator= ( const Matrix other)

Assign another Matrix to this Matrix.

Note: This method is not void so that one can write x = y = z (which first assigns z to y and then assigns the result of that assignment to x). It returns the result by reference because there is no concern that this will not refer to something.

Parameters
otherThe Matrix to copy
Exceptions
length_errorif the shapes don't conform
Returns
The Matrix referred to by this

◆ setValues() [1/4]

void Matrix::setValues ( double  value)
protected

Set the value of all elements in this Matrix to the given value.

Parameters
valueThe value to assign to every element

◆ setValues() [2/4]

void Matrix::setValues ( const Matrix other)
protected

Set the value of all elements in this Matrix to the value of the corresponding elements in another Matrix.

Parameters
otherThe other Matrix

◆ setValues() [3/4]

void Matrix::setValues ( const double *  values)
protected

Set the value of each element in this Matrix to the value of the corresponding element in a row-major array.

Parameters
valuesA pointer to the row-major array

◆ setValues() [4/4]

void Matrix::setValues ( double **  values)
protected

Set the value of each element in this Matrix to the value of the corresponding element in a "two-dimensional" array.

Parameters
valuesA pointer to the "two-dimensional" array

Friends And Related Function Documentation

◆ cof

double cof ( const Matrix A,
int  i,
int  j 
)
friend

Calculate the cofactor of a square matrix (i.e., the signed determinate of the matrix with row i and column j removed).

Parameters
AThe matrix (which must be square)
iThe index of the row to exclude
jThe index of the column to exclude
Exceptions
length_errorif the Matrix isn't square
out_of_rangeif i or j are out of bounds
Returns
The cofactor

◆ det [1/2]

double det ( double  a)
friend

Calculate the determinant of a scalar.

Parameters
aThe value of the scalar
Returns
The determinant of a (which is just a)

◆ det [2/2]

double det ( const Matrix A)
friend

Calculate the determinant of a square matrix.

Note: This implementation is not efficient since it explicitly creates each of the minors. However, it is easy to understand.

Parameters
AThe matrix (which must be square)
Exceptions
length_errorif the Matrix is smaller than 2x2 or isn't square
Returns
The value of the determinant

◆ dot

double dot ( const Matrix A,
const Matrix B 
)
friend

Calculate the dot product (more commonly known as the scalar product) of two Matrixes.

Note: While the steps in this calculation are the same as in matrix multiplication (of a 1xn and nx1 Matrix), the result is a scalar, not a Matrix. Hence, the need for this method.

We could return (trans(A)*B).get(0,0) but this would create two matrices needlessly.

This function is in the Matrix class, rather than the Vector class, because it is sometimes used with Matrixes

Parameters
AThe left-side Matrix
BThe right-side Matrix
Exceptions
length_errorif the sizes don't conform
Returns
The dot product

◆ identity

Matrix identity ( int  size)
friend

Create and return an identity matrix.

Note: This function must return by value because the result Matrix is a local variable.

Parameters
sizeThe number of rows == columns in the (square) matrix
Returns
An identity Matrix

◆ mminor

double mminor ( const Matrix A,
int  i,
int  j 
)
friend

Calculate the minor (i.e., the determinant of the submatrix formed by deleting a row and column).

Parameters
AThe matrix (which must be square)
iThe index of the row to exclude
jThe index of the column to exclude
Exceptions
out_of_rangeif i or j is out of bounds
length_errorif A isn't square
Returns
The minor

◆ operator!=

bool operator!= ( const Matrix A,
const Matrix B 
)
friend

Compare two matrices to see if they have different elements.

Parameters
AThe first matrix
BThe second matrix
Exceptions
length_errorif the shapes of A and B do not conform
Returns
true if they have any different elements; false otherwise

◆ operator* [1/3]

Matrix operator* ( const Matrix A,
const Matrix B 
)
friend

Multiply two matrices

Note: This method must return by value because the result Matrix is a local variable.

Parameters
AThe first matrix (m x n)
BThe second matrix (n x s)
Exceptions
length_errorif the shapes of A and B do not conform
Returns
\( \mathbf{A} \mathbf{B} \) [which is (m x s)]

◆ operator* [2/3]

Matrix operator* ( double  k,
const Matrix A 
)
friend

Multiply a scalar and a matrix.

Note: This method must return by value because the result Matrix is a local variable.

Parameters
kThe scalar
AThe matrix
Returns
\( k \mathbf{A} \)

◆ operator* [3/3]

Matrix operator* ( const Matrix A,
double  k 
)
friend

Multiply a matrix and a scalar.

Note: This method must return by value because the result Matrix is a local variable.

Parameters
AThe matrix
kThe scalar
Returns
\( \mathbf{A} k \)

◆ operator+

Matrix operator+ ( const Matrix A,
const Matrix B 
)
friend

Add the Matrix A and the Matrix B.

Note: This method must return by value because the result Matrix is a local variable.

Parameters
AOne Matrix
BThe other Matrix
Exceptions
length_errorif the shapes of A and B do not conform
Returns
\( \mathbf{A} + \mathbf{B} \)

◆ operator-

Matrix operator- ( const Matrix A,
const Matrix B 
)
friend

Subtract another Matrix from this matrix (component by component).

Note: This method must return by value because the result Matrix is a local variable.

Parameters
AOne Matrix
BThe other Matrix
Exceptions
length_errorif the shapes of A and B do not conform
Returns
\( \mathbf{A} - \mathbf{B} \)

◆ operator==

bool operator== ( const Matrix A,
const Matrix B 
)
friend

Compare two matrices to see if they have identical elements

Parameters
AThe first matrix
BThe second matrix
Exceptions
length_errorif the shapes of A and B do not conform
Returns
true or false

◆ operator|

Matrix operator| ( const Matrix A,
const Matrix B 
)
friend

Concatenate the columns of Matrix A and the columns Matrix B.

Note: This method must return by value because the result Matrix is a local variable.

Parameters
AThe left Matrix
BThe right Matrix
Exceptions
length_errorif the shapes of A and B do not conform
Returns
\( \mathbf{A} | \mathbf{B} \)

◆ submatrix

Matrix submatrix ( const Matrix A,
int  i,
int  j 
)
friend

Remove a row and column from a matrix.

Parameters
AThe matrix
iThe index of the row to exclude
jThe index of the col to exclude
Exceptions
length_errorthe Matrix is smaller than 2x2
out_of_rangeif i or j are out of bounds
Returns
The submatrix (i.e., a with row i and column j excluded)

◆ trans

Matrix trans ( const Matrix A)
friend

Create and return a transposed version of a given matrix.

Note: This function must return by value because the result Matrix is a local variable.

Parameters
AThe original Matrix
Returns
\( \mathbf{A}^T \)

Member Data Documentation

◆ columns

int Matrix::columns
protected

The number of rows in this Matrix.

◆ rows

int Matrix::rows
protected

The number of columnd in this Matrix.

◆ values

double** Matrix::values
protected

The elements of this Matrix.


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