Ray Tracer  2020
Public Member Functions | Static Public Member Functions | Protected Attributes | Related Functions | List of all members
Matrix Class Reference

Basic class for matrices. More...

#include <Matrix.h>

Inheritance diagram for Matrix:
Vector Direction Normal Point

Public Member Functions

 Matrix (size_t size=1)
 Square Matrix constructor. More...
 
 Matrix (size_t rows, size_t cols)
 General Matrix constructor. More...
 
 Matrix (const Matrix &mat)
 Matrix copy constructor. More...
 
virtual ~Matrix ()
 Matrix destructor. More...
 
Matrixoperator= (const Matrix &mat)
 Matrix assignment operator. More...
 
double & operator() (size_t row, size_t col)
 Matrix element access. More...
 
const double & operator() (size_t row, size_t col) const
 Matrix element access (const version). More...
 
size_t numRows () const
 Number of rows in a Matrix. More...
 
size_t numCols () const
 Number of columns in a Matrix. More...
 
size_t numElements () const
 Number of elements in a Matrix. More...
 
Matrix operator- () const
 Unary minus. More...
 
Matrixoperator+= (const Matrix &mat)
 Matrix addition-assignment operator. More...
 
Matrixoperator-= (const Matrix &mat)
 Matrix subtraction-assignment operator. More...
 
Matrixoperator*= (double s)
 Matrix-scalar multiplication-assignment operator. More...
 
Matrixoperator/= (double s)
 Matrix-scalar multiplication-assignment operator. More...
 
Matrix transpose () const
 Matrix transpose. More...
 

Static Public Member Functions

static Matrix identity (size_t rows, size_t cols)
 Factory method for Identity Matrices. More...
 
static Matrix zero (size_t rows, size_t cols)
 Factory method for Zero Matrices. More...
 

Protected Attributes

size_t rows_
 Number of rows in the Matrix. More...
 
size_t cols_
 Number of columns in the Matrix. More...
 
std::vector< double > data_
 Storage for Matrix data elements. More...
 

Related Functions

(Note that these are not member functions.)

Matrix operator* (const Matrix &lhs, const Matrix &rhs)
 Matrix-Matrix multiplication operator. More...
 
Matrix operator+ (const Matrix &lhs, const Matrix &rhs)
 Matrix addition operator. More...
 
Matrix operator- (const Matrix &lhs, const Matrix &rhs)
 Matrix subtraction operator. More...
 
Matrix operator* (double s, const Matrix &mat)
 scalar-Matrix multiplication operator. More...
 
Matrix operator* (const Matrix &mat, double s)
 Matrix-scalar multiplication operator. More...
 
Matrix operator/ (const Matrix &mat, double s)
 Matrix-scalar division operator. More...
 
std::ostream & operator<< (std::ostream &outputStream, const Matrix &mat)
 Stream insertion operator. More...
 
std::istream & operator>> (std::istream &inputStream, Matrix &mat)
 Stream extraction operator. More...
 

Detailed Description

Basic class for matrices.

This class provides basic matrix operatations (addition, subtraction, multiplication, etc.) as overloaded operators. This is sufficient for COSC432 and the Ray Tracer assignment, but does not include more advanced operations like inverse, or matrix decompositions.

The class is designed to be simple and easy to understand rather than optimal or hyper-efficient. For production code a more professional library (such as Eigen) is recommended.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

Matrix::Matrix ( size_t  size = 1)

Square Matrix constructor.

This constructor creates a square (size x size) matrix. If no size is provided, it creates a 1 x 1 matrix, and so also provides a default constructor.

Parameters
sizeThe size of the resulting Matrix.

◆ Matrix() [2/3]

Matrix::Matrix ( size_t  rows,
size_t  cols 
)

General Matrix constructor.

This constructor creates a matrix of the specified size. The number of rows and columns is specified and must be postivie integers.

Parameters
rowsThe number of rows in the resulting Matrix.
colsThe number of columns in the resulting Matrix.

◆ Matrix() [3/3]

Matrix::Matrix ( const Matrix mat)

Matrix copy constructor.

This constructor creates a new Matrix which is a copy of an existing one. The new Matrix has independent storage etc.

Parameters
matThe Matrix to copy.

◆ ~Matrix()

Matrix::~Matrix ( )
virtual

Matrix destructor.

The Matrices are based on std::vector, so there's not much to do here, but it is provided for completeness. Note that this is virtual, since other classes (like Vector) may inherit from Matrix.

Member Function Documentation

◆ identity()

Matrix Matrix::identity ( size_t  rows,
size_t  cols 
)
static

Factory method for Identity Matrices.

This returns an Identity Matrix of a given size. Identity Matrices have ones on the main diagonal, and zeros off the diagonal. For non-square Matrices, any 'extra' rows or columns are all zero.

Parameters
rowsThe number of rows in the resulting Matrix.
colsThe number of columns in the resulting Matrix.
Returns
A rows x cols Identity matrix

◆ numCols()

size_t Matrix::numCols ( ) const

Number of columns in a Matrix.

Returns
The number of columns in the given Matrix.

◆ numElements()

size_t Matrix::numElements ( ) const

Number of elements in a Matrix.

Returns
The number of elements in the given Matrix.

◆ numRows()

size_t Matrix::numRows ( ) const

Number of rows in a Matrix.

Returns
The number of rows in the given Matrix.

◆ operator()() [1/2]

double & Matrix::operator() ( size_t  row,
size_t  col 
)

Matrix element access.

This method provides access to the elements of a Matrix using the syntax A(i,j). This notation is equivalent to A.operator()(i,j). Since a reference is returned, this can be used for assignment, eg: A(1,2)=3.1415; The values of row and column must be less than the size of the Matrix.

Parameters
rowThe row of the Matrix to access.
colThe column of the Matrix to access.
Returns
A reference to the requested element of the Matrix.

◆ operator()() [2/2]

const double & Matrix::operator() ( size_t  row,
size_t  col 
) const

Matrix element access (const version).

This method provides access to the elements of a const Matrix using the syntax A(i,j). Since a const reference is returned, this cannot be used for assignment, but it can be applied to const Matrices. The values of row and column must be less than the size of the Matrix.

Parameters
rowThe row of the Matrix to access.
colThe column of the Matrix to access.
Returns
A const reference to the requested element of the Matrix.

◆ operator*=()

Matrix & Matrix::operator*= ( double  s)

Matrix-scalar multiplication-assignment operator.

This scales a Matrix in place. Note that there is no *= operator defined between two Matrices. This is because Matrix multiplication is not commutative, so A*B != B*A in general. That makes A*=B a little confusing. Following C++ convention, this should mean A=A*B, but we'll often be multiplying vectors by matrices, and a*=B would intuitively mean a=B*a. This is easily avoided by not having *= for two Matrices.

Parameters
sThe scalar multiplier to apply to this.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator+=()

Matrix & Matrix::operator+= ( const Matrix mat)

Matrix addition-assignment operator.

This adds a Matrix to an existing one in place. The two Matrices must have the same size.

Parameters
matThe Matrix to add to this.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator-()

Matrix Matrix::operator- ( ) const

Unary minus.

This provides the ability to negate a Matrix. -A is equivalent to A.operator-()

Returns
A negated version of the Matrix.

◆ operator-=()

Matrix & Matrix::operator-= ( const Matrix mat)

Matrix subtraction-assignment operator.

This subtracts a Matrix from an existing one in place. The two Matrices must have the same size.

Parameters
matThe Matrix to subtract from this.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator/=()

Matrix & Matrix::operator/= ( double  s)

Matrix-scalar multiplication-assignment operator.

This divides a Matrix in place.

Parameters
sThe scalar multiplier to divide this by.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator=()

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

Matrix assignment operator.

This allows expressions like A = B; where A and B are Matrices. It also allows chained assignment, like A = B = C;

Parameters
matThe Matrix on the right hand side of the assignment.
Returns
a reference to this, to allow chaining of assignment.

◆ transpose()

Matrix Matrix::transpose ( ) const

Matrix transpose.

This creates the transpose of a Matrix, which is a new Matrix with the original rows and columns interchanged.

Returns
A transposed copy of this.

◆ zero()

Matrix Matrix::zero ( size_t  rows,
size_t  cols 
)
static

Factory method for Zero Matrices.

This returns a Zero Matrix of a given size. Zero matrices have all entries set to Zero (unsurprisingly).

Parameters
rowsThe number of rows in the resulting Matrix.
colsThe number of columns in the resulting Matrix.
Returns
A rows x cols Zero Matrix

Friends And Related Function Documentation

◆ operator* [1/3]

Matrix operator* ( const Matrix lhs,
const Matrix rhs 
)
friend

Matrix-Matrix multiplication operator.

This creates the product of two Matrices. The first Matrix must have the same number of columns as the second Matrix has rows.

Note that multiplication is declared as a friend. It could be declared as a normal method Matrix operator*(const Matrix& rhs);. The friend form provides a more symmetric view of the two Matrices being multiplied. It could also be defined in terms of a *= operator, but it is not clear whether A *= B; means A = A*B or A = B*A. So this is omitted to avoid confusion.

Parameters
lhsThe Matrix on the left hand side of the *.
rhsThe Matrix on the right hand side of the *.
Returns
The Matrix formed by lhs * rhs.

◆ operator*() [2/3]

Matrix operator* ( const Matrix mat,
double  s 
)
related

Matrix-scalar multiplication operator.

This creates the product of a Matrix and a scalar.

Parameters
matThe Matrix to be scaled.
sThe scalar value to multiply the Matrix by.
Returns
The Matrix formed by mat*s.

◆ operator*() [3/3]

Matrix operator* ( double  s,
const Matrix mat 
)
related

scalar-Matrix multiplication operator.

This creates the product of a scalar and a Matrix.

Parameters
sThe scalar value to multiply the Matrix by.
matThe Matrix to be scaled.
Returns
The Matrix formed by s*mat.

◆ operator+()

Matrix operator+ ( const Matrix lhs,
const Matrix rhs 
)
related

Matrix addition operator.

This performs addition of two Matrices. The two Matrices must have the same size

Parameters
lhsThe Matrix on the left hand side of the +.
rhsThe Matrix on the right hand side of the +.
Returns
The Matrix formed by lhs + rhs.

◆ operator-()

Matrix operator- ( const Matrix lhs,
const Matrix rhs 
)
related

Matrix subtraction operator.

This performs subtraction of two Matrices. The two Matrices must have the same size

Parameters
lhsThe Matrix on the left hand side of the -.
rhsThe Matrix on the right hand side of the -.
Returns
The Matrix formed by lhs - rhs.

◆ operator/()

Matrix operator/ ( const Matrix mat,
double  s 
)
related

Matrix-scalar division operator.

This creates the result of a Matrix divided by a scalar. Division by a scalar, s, is essentially the same as multiplcation (or scaling) by 1/s. Division by numbers close to 0 will result in NaN and Inf values and wildly unstable results as normal.

Parameters
matThe Matrix to be scaled.
sThe scalar value to divide the Matrix by.
Returns
The Matrix formed by mat/s.

◆ operator<<()

std::ostream & operator<< ( std::ostream &  outputStream,
const Matrix mat 
)
related

Stream insertion operator.

Provides the ability to output Matrices to C++ streams, including std::cout and filestreams.

Parameters
outputStreamthe stream to send the Matrix to.
matthe matrix to send to the stream.
Returns
The updated output stream.

◆ operator>>()

std::istream & operator>> ( std::istream &  inputStream,
Matrix mat 
)
related

Stream extraction operator.

Provides the ability to read Matrices from C++ streams, including std::cin and filestreams. The size of the Matrix determines how much data is read from the stream.

Parameters
inputStreamthe stream to read the Matrix from.
matthe matrix to read from the stream.
Returns
The updated input stream.

Member Data Documentation

◆ cols_

size_t Matrix::cols_
protected

Number of columns in the Matrix.

◆ data_

std::vector<double> Matrix::data_
protected

Storage for Matrix data elements.

◆ rows_

size_t Matrix::rows_
protected

Number of rows in the Matrix.


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