Template Class Matrix

Inheritance Relationships

Derived Type

Class Documentation

template<class T>
class Matrix

A class containing the Matrix object, set up as an array of Row objects. Includes basic operations, as well as the importCsv() function for more advanced reading from file.

Template Parameters
  • T: the type of the values in the matrix

Subclassed by necsim::Map< T >

Public Functions

Matrix(unsigned long rows = 0, unsigned long cols = 0)

The standard constructor.

Parameters
  • rows: optionally provide the number of rows.
  • cols: optionally provide the number of columns.

Matrix(const Matrix &m)

The copy constructor.

Parameters
  • m: a Matrix object to copy from.

virtual ~Matrix()

The destructor.

void setSize(unsigned long rows, unsigned long cols)

Sets the matrix size. Similar concept to that for Rows.

Parameters
  • rows: the number of rows.
  • cols: the number of columns.

unsigned long getCols() const

Getter for the number of columns.

Return
the number of columns.

unsigned long getRows() const

Getter for the number of rows.

Return
the number of rows.

void fill(T val)

Fills the matrix with the given value.

Parameters
  • val: the value to fill

unsigned long index(const unsigned long &row, const unsigned long &col) const

Gets the index of a particular row and column in the matrix.

Return
the index of row and column within the matrix
Parameters
  • row: the row number to index
  • col: the column number to index

T &get(const unsigned long &row, const unsigned long &col)

Gets the value at a particular index.

Return
the value at the specified row and column
Parameters
  • row: the row number to get the value at
  • col: the column number to get the value at

const T &get(const unsigned long &row, const unsigned long &col) const

Gets the value at a particular index.

Return
the value at the specified row and column
Parameters
  • row: the row number to get the value at
  • col: the column number to get the value at

T getCopy(const unsigned long &row, const unsigned long &col) const

Gets the value at a particular index.

Return
the value at the specified row and column
Parameters
  • row: the row number to get the value at
  • col: the column number to get the value at

vector<T>::iterator begin()

Returns iterators for range-based for loops.

Return
iterator to the start of the vector

vector<T>::iterator end()

Returns end iterators for range-based for loops.

Return
iterator to the end of the vector

vector<T>::const_iterator begin() const

Returns iterators for range-based for loops.

Return
iterator to the start of the vector

vector<T>::const_iterator end() const

Returns end iterators for range-based for loops.

Return
iterator to the end of the vector

double getMean() const

Gets the arithmetic mean of the Matrix.

Return
the mean value in the matrix

T sum() const
Matrix &operator=(const Matrix &m)

Overloading the = operator.

Parameters
  • m: the matrix to copy from.

Matrix operator+(const Matrix &m) const

Overloading the + operator.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Return
the matrix object which is the sum of the two matrices.
Parameters
  • m: the matrix to add to this matrix.

Matrix operator-(const Matrix &m) const

Overloading the - operator.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Return
the matrix object which is the subtraction of the two matrices.
Parameters
  • m: the matrix to subtract from this matrix.

Matrix &operator+=(const Matrix &m)

Overloading the += operator so that the new object is written to the current object.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Parameters
  • m: the Matrix object to add to this matrix.

Matrix &operator-=(const Matrix &m)

Overloading the -= operator so that the new object is written to the current object.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Parameters
  • m: the Matrix object to subtract from this matrix.

Matrix operator*(const double s) const

Overloading the * operator for scaling.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Return
the scaled matrix.
Parameters
  • s: the constant to scale the matrix by.

Matrix operator*(Matrix &m) const

Overloading the * operator for matrix multiplication.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension. Multiplies each value in the matrix with its corresponding value in the other matrix.
Return
the product of each ith,jth value of the matrix.
Parameters
  • m: the matrix to multiply with

Matrix &operator*=(const double s)

Overloading the *= operator so that the new object is written to the current object.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Parameters
  • m: the Matrix object to add to this matrix.

Matrix &operator*=(const Matrix &m)

Overloading the *= operator so that the new object is written to the current object.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Parameters
  • m: the Matrix object to add to this matrix.

Matrix operator/(const double s) const

Overloading the / operator for scaling.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Return
the scaled matrix.
Parameters
  • s: the constant to scale the matrix by.

Matrix &operator/=(const double s)

Overloading the /= operator so that the new object is written to the current object.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Parameters
  • m: the Matrix object to add to this matrix.

Matrix &operator/=(const Matrix &m)

Overloading the /= operator so that the new object is written to the current object.

Note
If matrices are of different sizes, the operation is performed on the 0 to minimum values of each dimension.
Parameters
  • m: the Matrix object to add to this matrix.

void setValue(const unsigned long &row, const unsigned long &col, const char *value)

Sets the value at the specified indices, including handling type conversion from char to the template class.

Parameters
  • row: the row index.
  • col: the column index.
  • value: the value to set

void setValue(const unsigned long &row, const unsigned long &col, const T &value)

Sets the value at the specified indices, including handling type conversion from char to the template class.

Parameters
  • row: the row index.
  • col: the column index.
  • value: the value to set

virtual void import(const string &filename)

Imports the matrix from a csv file.

Exceptions
  • runtime_error: if type detection for the filename fails.
Parameters
  • filename: the file to import.

bool importCsv(const string &filename)

Imports the matrix from a csv file using the fast-csv-parser method.

Return
true if the csv can be imported.
Parameters
  • filename: the path to the file to import.Imports the matrix from a csv file using the standard, slower method.
Parameters
  • filename: the path to the file to import.

Protected Attributes

unsigned long num_cols = {}
unsigned long num_rows = {}
vector<T> matrix

Friends

ostream &writeOut(ostream &os, const Matrix &m)

Writes the object to the output stream.

Note
This is done slightly inefficiently to preserve the output taking the correct form.
Return
the output stream
Parameters
  • os: the output stream to write to
  • m: the object to write out

istream &readIn(istream &is, Matrix &m)

Reads in from the input stream.

Return
Parameters
  • is: the input stream to read from
  • m: the object to read into

ostream &operator<<(ostream &os, const Matrix &m)

Overloading the << operator for outputting to an output stream. This can be used for writing to console or storing to file.

Return
the output stream.
Parameters
  • os: the output stream.
  • m: the matrix to output.

istream &operator>>(istream &is, Matrix &m)

Overloading the >> operator for inputting from an input stream. This can be used for writing to console or storing to file.

Return
the input stream.
Parameters
  • is: the input stream.
  • m: the matrix to input to.