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

#include <Matrix.h>

Public Member Functions

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

Protected Member Functions

void allocateMemory ()
 
void deallocateMemory ()
 
void setSize (int rows, int columns)
 
void setValues (double value)
 
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 (const Matrix &A)
 
Matrix identity (int size)
 
double mminor (const Matrix &A, int i, int j)
 
Matrix multiply (const Vector &a, const Vector &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)
 
Vector operator* (const Vector &v, const Matrix &M)
 
Vector operator* (const Matrix &M, const Vector &v)
 
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 ( int  rows,
int  columns 
)

A constructor that does not allow for initial values (i.e., that initializes all elements to 0.0). The Matrix must be initialized with the assignment operator.

Example of use:

Matrix y(3,3);
Parameters
rowsThe number of rows
columnsThe number of columns

◆ Matrix() [2/3]

Matrix::Matrix ( const Matrix original)

A copy constructor.

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

Parameters
originalThe Matrix to copy

◆ Matrix() [3/3]

Matrix::Matrix ( const Vector &  v)
explicit

Construct a Matrix from a Vector.

The resulting Matrix will be eithe nx1 or 1xn depending on the orientation of the Vector

Parameters
vThe Vector to "copy"

◆ ~Matrix()

Matrix::~Matrix ( )

Destructor.

Member Function Documentation

◆ allocateMemory()

void Matrix::allocateMemory ( )
protected

Allocate memory for this Matrix.

◆ deallocateMemory()

void Matrix::deallocateMemory ( )
protected

Deallocate the memory used by this Matrix.

◆ get()

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

◆ 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()()

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=() [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 values is of 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 do not conform
Returns
The Matrix referred to by this

◆ setSize()

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

Set the size of this Matrix. Note: This method should only be called by constructors.

Parameters
rowsThe number of rows
columnsThe number of columns

◆ setValues() [1/3]

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/3]

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() [3/3]

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

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
length_errorif the Matrix isn't square
Returns
\(|A|\)

◆ 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
\( I \)

◆ mminor

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

Calculate a particular minor for a square matrix (i.e., the determinant 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
out_of_rangeif i or j are out of bounds
Returns
The minor

◆ multiply

Matrix multiply ( const Vector &  a,
const Vector &  b 
)
friend

Multiply a column Vector and a row Vector.

Note: We can't overload the * operator for this purpose because it is already overloaded in the Vector class for the dot/inner product

Parameters
aThe column Vector
bThe row Vector
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 different elements (beyond a given TOLERANCE).

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

◆ operator* [1/5]

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} \) (m x s)

◆ operator* [2/5]

Vector operator* ( const Vector &  v,
const Matrix M 
)
friend

Multiply a row Vector and a Matrix.

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

It is worth noting that, if this method returned a Matrix rather than a Vector, it could be implemented as return (Matrix(v) * M);

Parameters
vThe row Vector
MThe Matrix
Exceptions
length_errorif the shapes of v and M do not conform
Returns
The row Vector \( \mathbf{v} \mathbf{M} \)

◆ operator* [3/5]

Vector operator* ( const Matrix M,
const Vector &  v 
)
friend

Multiply a Matrix and a column Vector.

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

It is worth noting that, if this method returned a Matrix rather than a Vector, it could be implemented as return return (M * Matrix(v));

Parameters
MThe Matrix
vThe column Vector
Returns
column Vector \( \mathbf{M} \mathbf{v} \)

◆ operator* [4/5]

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* [5/5]

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 the Matrix B from the Matrix A (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 (within a given TOLERANCE for each element).

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

◆ submatrix

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

Remove a row and column from a matrix

Parameters
AThe matrix (which must be square)
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 columns in this Matrix.

◆ rows

int Matrix::rows
protected

The number of rows in this Matrix.

◆ values

double** Matrix::values
protected

The elements of this Matrix.


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