Skip to content

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.

Notifications You must be signed in to change notification settings

max-baydoun/small-matrix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Small Matrix - By Mohamad Baydoun ✖

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.

Specifications

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.
SmallMatrix m;
None
SmallMatrix(int, int) A constructor which initialises a zero matrix with the dimensions given by mNumRows and mNumCols.
SmallMatrix m(7, 4);
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.
SmallMatrix m(7, 4, 42.2);
None
SmallMatrix(std::initializer_list<std::initializer_list<double>> const&) A constructor which initialises a matrix with a given initialiser list of initialiser list of doubles i.e. a 2D initialiser list of doubles. Each inner initialiser list represents a single row where each element in the inner initialiser list represents a column. GIVEN
SmallMatrix m({
    {1.0, 2.0, 3.0, 4.0},
    {5.0, 6.0, 7.0, 8.0},
});
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.
SmallMatrix m1;
SmallMatrix m2(m1);
None
SmallMatrix(SmallMatrix&&) Move constructor. Specified object should be invalidated after move.
SmallMatrix m1;
SmallMatrix m2(std::move(m1));
None
SmallMatrix& operator=(SmallMatrix const&) Copy assignment.
SmallMatrix m1;
SmallMatrix m2;
m2 = m1;
None
SmallMatrix& operator=(SmallMatrix&&) Move assignment. Specified object should be invalidated after move.
SmallMatrix m1;
SmallMatrix m2;
m2 = std::move(m1);
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)
SmallMatrix m(1, 1);
m(0, 0) = 24.4;
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.
SmallMatrix m(1, 1);
m(0, 0);
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.
SmallMatrix m(1, 1);
auto r = m.row(0);
r[0] = 2.2;
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.
SmallMatrix m(1, 1);
m.row(0);
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.
SmallMatrix m(1, 1);
auto c = m.col(0);
r[0] = 2.2;
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.
SmallMatrix m(1, 1);
m.col(0);
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.
SmallMatrix m(1, 1);
auto s = m.size();
s.first;
s.second;
None
bool isSmall() const Returns true if the matrix is using a small-storage-optimised data structure.
SmallMatrix m(1, 1);
s.isSmall();
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.
SmallMatrix m(1, 1);
s.resize(100, 100);
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.
SmallMatrix m(2, 4);
s.insertRow(0, {1, 2, 3, 4});
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.
SmallMatrix m(3, 2);
s.insertCol(2, {1, 2, 3});
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.
SmallMatrix m(3, 2);
s.eraseRow(2);
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.
SmallMatrix m(3, 2);
s.eraseCol(1);
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.
SmallMatrix m1({{1, 2, 3}, {4, 5, 6}});
SmallMatrix m2({{1, 2, 3}, {4, 5, 6}});
m1 == m2;
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.
SmallMatrix m1({{1, 2, 3}, {4, 5, 7}});
SmallMatrix m2({{1, 2, 3}, {4, 5, 6}});
m1 != m2;
None.
friend SmallMatrix operator+(SmallMatrix const&, SmallMatrix const&) Returns the matrix result of the element-wise addition of the two specified matrices.
SmallMatrix m1({{1, 2}, {3, 4}, {5, 6}});
SmallMatrix m2({{1, 2}, {3, 4}, {5, 6}});
auto r = m1 + m2;
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.
SmallMatrix m1({{1, 2}, {3, 4}, {5, 6}});
SmallMatrix m2({{1, 2}, {3, 4}, {5, 6}});
auto r = m1 - m2;
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.
SmallMatrix m1({{1, 2}, {3, 4}, {5, 6}});
SmallMatrix m2({{1, 2}, {3, 4}});
auto r = m1 * m2;
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.
SmallMatrix m({{1, 2}, {3, 4}, {5, 6}});
auto r = 42.2 * m;
None.
friend SmallMatrix operator*(SmallMatrix const&, double) Returns the matrix result of the scalar multiplication of the the specified scalar value and specified matrix.
SmallMatrix m({{1, 2}, {3, 4}, {5, 6}});
auto r = m * 42.2;
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.
SmallMatrix m1({{1, 2}, {3, 4}, {5, 6}});
SmallMatrix m2({{1, 1}, {1, 1}, {, 1}});
auto m1 += m2;
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.
SmallMatrix m1({{1, 2}, {3, 4}, {5, 6}});
SmallMatrix m2({{1, 1}, {1, 1}, {, 1}});
auto m1 -= m2;
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.
SmallMatrix m1({{1, 2}, {3, 4}, {5, 6}});
SmallMatrix m2({{1, 2}, {3, 4}});
auto m1 *= m2;
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.
SmallMatrix m({{1, 2}, {3, 4}, {5, 6}});
auto m1 *= 42.2;
None.
friend SmallMatrix transpose(SmallMatrix const&) Returns the result of the tranpose on the specified matrix.
SmallMatrix m({{1, 2, 3}, {4, 5, 6}});
auto r = transpose(m);
None.
friend std::ostream& operator<<(std::ostream&, SmallMatrix const&) Writes the contents of the matrix to the output stream.
SmallMatrix m({{1, 2, 3}, {4, 5, 6}});
std::cout << m;
None.

Compiling

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 '''

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages