Ray Tracer
2020
|
Basic class for matrices. More...
#include <Matrix.h>
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... | |
Matrix & | operator= (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... | |
Matrix & | operator+= (const Matrix &mat) |
Matrix addition-assignment operator. More... | |
Matrix & | operator-= (const Matrix &mat) |
Matrix subtraction-assignment operator. More... | |
Matrix & | operator*= (double s) |
Matrix-scalar multiplication-assignment operator. More... | |
Matrix & | operator/= (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... | |
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.
Matrix::Matrix | ( | size_t | size = 1 | ) |
Matrix::Matrix | ( | size_t | rows, |
size_t | cols | ||
) |
Matrix::Matrix | ( | const Matrix & | mat | ) |
|
virtual |
|
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.
rows | The number of rows in the resulting Matrix. |
cols | The number of columns in the resulting Matrix. |
size_t Matrix::numCols | ( | ) | const |
size_t Matrix::numElements | ( | ) | const |
size_t Matrix::numRows | ( | ) | const |
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.
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.
const
reference to the requested element of the Matrix. 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.
s | The scalar multiplier to apply to this . |
this
, to allow chaining of assignment. Matrix Matrix::operator- | ( | ) | const |
Matrix & Matrix::operator/= | ( | double | s | ) |
Matrix-scalar multiplication-assignment operator.
This divides a Matrix in place.
s | The scalar multiplier to divide this by. |
this
, to allow chaining of assignment. Matrix Matrix::transpose | ( | ) | const |
|
static |
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.
lhs | The Matrix on the left hand side of the *. |
rhs | The Matrix on the right hand side of the *. |
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.
|
related |
Stream insertion operator.
Provides the ability to output Matrices to C++ streams, including std::cout
and filestreams
.
outputStream | the stream to send the Matrix to. |
mat | the matrix to send to the stream. |
|
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.
inputStream | the stream to read the Matrix from. |
mat | the matrix to read from the stream. |
|
protected |
Number of columns in the Matrix.
|
protected |
Storage for Matrix data elements.
|
protected |
Number of rows in the Matrix.