A SmallMatrix
is a small-storage-optimised matrix whose elements are allocated on the stack if the number of elements is less than 144 allowing fast read/write speeds. If the number of elements is 144 or greater, then its contents are allocated on the heap.
The specification for SmallMatrix
is summarised below:
Method | Description | Usage | Exceptions |
---|---|---|---|
SmallMatrix() |
A constructor which initialises an empty matrix with no rows and no columns. |
|
None |
SmallMatrix(int, int) |
A constructor which initialises a zero matrix with the dimensions given by mNumRows and mNumCols . |
|
None |
SmallMatrix(int, int, double) |
A constructor which intialises a matrix whose elements are all initialised with the given value, and has the dimensions given by mNumRows and mNumCols . |
|
None |
SmallMatrix(std::initializer_list<std::initializer_list<double>> const&) |
|
Throws invalid_argument if the initialiser list is not rectangular i.e. each row does not have the same number of columns. |
|
SmallMatrix(SmallMatrix const&) |
Copy constructor. |
|
None |
SmallMatrix(SmallMatrix&&) |
Move constructor. Specified object should be invalidated after move. |
|
None |
SmallMatrix& operator=(SmallMatrix const&) |
Copy assignment. |
|
None |
SmallMatrix& operator=(SmallMatrix&&) |
Move assignment. Specified object should be invalidated after move. |
|
None |
~SmallMatrix() |
Destructor. | None | |
double& operator()(int, int) |
Returns the reference of the matrix element at the specified row and column index. The order of access is: (row, col) |
|
Throws out_of_range if the specified row and column is outside the range [0, max_row) and [0, max_col) respectively.Throws out_of_range if the matrix has no rows and no columns. |
const double& operator()(int, int) const |
Returns the constant reference of the matrix element at the specified row and column index. It is guaranteed that the returned element is not modified. |
|
Throws out_of_range if the specified row and column is outside the range [0, max_row) and [0, max_col) respectively.Throws out_of_range if the matrix has no rows and no columns. |
std::vector<double*> row(int) |
Returns a vector of pointers to each of the elements of the row of the matrix at the specified row index. |
|
Throws out_of_range if the specified row index is outside the range [0, max_row) . |
std::vector<double const*> row(int) const |
Returns a vector of pointers to each of the elements of constant type of the row of the matrix at the specified row index. |
|
Throws out_of_range if the specified row index is outside the range [0, max_row) . |
std::vector<double*> col(int) |
Returns a vector of pointers to each of the elements of the column of the matrix at the specified column index. |
|
Throws out_of_range if the specified column index is outside the range [0, max_col) . |
std::vector<double const*> col(int) const |
Returns a vector of pointers to each of the elements of constant type of the column of the matrix at the specified column index. |
|
Throws out_of_range if the specified column index is outside the range [0, max_col) . |
std::pair<int, int> size() const |
Returns the size of the matrix where the first of the pair is the number of rows and the second of the pair is the number of columns. |
|
None |
bool isSmall() const |
Returns true if the matrix is using a small-storage-optimised data structure. |
|
None |
void resize(int, int) |
Resizes the matrix to the new number of rows and new number of columns. If any matrix dimension is increased, then the newly created dimension is zero-initialised. If any matrix dimension is decreased, then its previously-allocated elements are truncated. |
|
Throws out_of_range if the specified row or column index is negative. |
void insertRow(int, std::vector const&) |
Inserts a row at the specified row index. If the number of columns in the matrix is zero, then the matrix is resized to match the size of the specified row vector. |
|
Throws out_of_range if the specified row index is outside the range [0, max_row] .Throws invalid_argument if the size of the specified vector is not equal to the number of columns in the matrix. |
void insertCol(int, std::vector const&) |
Inserts a column at the specified column index. If the number of rows in the matrix is zero, then the matrix is resized to match the size of the specified column vector. |
|
Throws out_of_range if the specified column index is outside the range [0, max_col] .Throws invalid_argument if the size of the specified vector is not equal to the number of rows in the matrix. |
void eraseRow(int) |
Erases the row at the specified row index. |
|
Throws out_of_range if the specified row index is outside the range [0, max_row) . |
void eraseCol(int) |
Erases the column at the specified column index. |
|
Throws out_of_range if the specified column index is outside the range [0, max_col) . |
friend bool operator==(SmallMatrix const&, SmallMatrix const&) |
Returns true if all of the elements in the left-hand side matrix are equal to its positionally-corresponding element in the right-hand side matrix. Otherwise, false. |
|
None. |
friend bool operator!=(SmallMatrix const&, SmallMatrix const&) |
Returns true if any of the elements in the left-hand side matrix are not equal to its positionally-corresponding element in the right-hand side matrix. Otherwise, false. |
|
None. |
friend SmallMatrix operator+(SmallMatrix const&, SmallMatrix const&) |
Returns the matrix result of the element-wise addition of the two specified matrices. |
|
Throws invalid_argument if the number of rows and columns on the left-hand side is not equal to the number of rows and columns on the right-hand side respectively. |
friend SmallMatrix operator-(SmallMatrix const&, SmallMatrix const&) |
Returns the matrix result of the element-wise subtraction of the two specified matrices. |
|
Throws invalid_argument if the number of rows and columns on the left-hand side is not equal to the number of rows and columns on the right-hand side respectively. |
friend SmallMatrix operator*(SmallMatrix const&, SmallMatrix const&) |
Returns the matrix result of the matrix multiplication of the two specified matrices. |
|
Throws invalid_argument if the number of columns on the left-hand side is not equal to the number of rows on the right-hand side. |
friend SmallMatrix operator*(double, SmallMatrix const&) |
Returns the matrix result of the scalar multiplication of the the specified scalar value and specified matrix. |
|
None. |
friend SmallMatrix operator*(SmallMatrix const&, double) |
Returns the matrix result of the scalar multiplication of the the specified scalar value and specified matrix. |
|
None. |
SmallMatrix& operator+=(SmallMatrix const&) |
Returns *this after the element-wise addition of *this and the specified matrix. This operation is equivalent to *this = *this + m . |
|
Throws invalid_argument if the number of rows and columns of *this is not equal to the number of rows and columns of the specified matrix respectively. |
SmallMatrix& operator-=(SmallMatrix const&) |
Returns *this after the element-wise subtraction of *this and the specified matrix. This operation is equivalent to *this = *this - m . |
|
Throws invalid_argument if the number of rows and columns of *this is not equal to the number of rows and columns of the specified matrix respectively. |
SmallMatrix& operator*=(SmallMatrix const&) |
Returns *this after the matrix multiplication of *this and the specified matrix. This operation is equivalent to *this = *this * m . |
|
Throws invalid_argument if the number of columns of *this is not equal to the number of rows of the specified matrix. |
SmallMatrix& operator*=(double) |
Returns *this after the scalar multiplication of *this and the specified scalar value. This operation is equivalent to *this = *this * s . |
|
None. |
friend SmallMatrix transpose(SmallMatrix const&) |
Returns the result of the tranpose on the specified matrix. |
|
None. |
friend std::ostream& operator<<(std::ostream&, SmallMatrix const&) |
Writes the contents of the matrix to the output stream. |
|
None. |
It is compiled with C++14.
To compile with the given main file, use the following command, ''' g++ -std=c++14 main.cpp SmallMatrix.cpp -o small_matrix '''