Matrix

Matrix Object

class JSL::Matrix

As with JSL::Vector, a Matrix is a member of R^(m,n), and hence is a wrapper for std::vector<std::vector<double>> with various other overloaded operators.

Public Functions

inline Matrix(const int n, const int m)

Initialises a zero matrix of the specified size.

Parameters:
  • n – The number of rows

  • m – The number of columns

inline Matrix(std::vector<std::vector<double>> input)

Initialises a matrix from a vector-of-vectors, inferring the size as appropriate. Throws an error if the columns are not uniform size.

Parameters:

input – The data to be copied into the matrix as Matrix(i,j) = input[i][j]

inline Matrix(const Matrix &input)

Copy constructor.

inline int Rows() const

Private access.

Returns:

The number of rows in the matrix

inline int Columns() const

Private access.

Returns:

The number of columns in the matrix

inline Matrix Transpose()

Returns the matrix P such that P(i,j) = Q(j,i)

Returns:

The mathematical transpose of the current matrix

inline JSL::Vector GetRow(int rowID) const

Allows vectorised access to an entire row (very quick to to contiguus storage)

Parameters:

rowID – The row index to output

Returns:

The row, packaged as a JSL::Vector

inline JSL::Vector GetColumn(int colID) const

Allows vectorised access to an entire column (slow due to non-contiguous storage)

Parameters:

columnID – The column index to output

Returns:

The column, packaged as a JSL::Vector

inline double &operator()(int rowID, int columnID)

Allows access similar to [i][j], but without exposing the internal structure of the matrix to machinations. Allows modification, i.e. A(i,j) = 2 sets the value.

Parameters:
  • rowID – row coordinate of target

  • columnID – column coordinate of target

Returns:

A reference to the data point

inline const double &operator()(int rowID, int columnID) const

Annoying const override of access syntax (see non-const version)

inline Matrix &operator+=(const Matrix &rhs)

In-place addition of two matrices.

Parameters:

rhs – The matrix to be accumulated into the current object. Must be the same dimensions as the calling object.

Returns:

A reference to the now-modified calling object

inline Matrix &operator-=(const Matrix &rhs)

In-place subtraction of two matrices.

Parameters:

rhs – The matrix to be subtracted from the current object. Must be the same dimensions as the calling object.

Returns:

A reference to the now-modified calling object

inline Matrix &operator+=(const double &scalar)

In-place addition of a scalar onto the calling object.

Parameters:

scalar – The double to be accumulated into the current object.

Returns:

A reference to the now-modified calling object

inline Matrix &operator-=(const double &scalar)

In-place subtraction of a scalar onto the calling object.

Parameters:

scalar – The double to be subtracted from the current object.

Returns:

A reference to the now-modified calling object

inline Matrix &operator*=(const double &scalar)

In-place multiplication of a scalar onto the calling object.

Parameters:

scalar – The double to be accumulated into the current object.

Returns:

A reference to the now-modified calling object

inline Matrix &operator/=(const double &scalar)

In-place division of a scalar onto the calling object.

Parameters:

scalar – The double to divide the current object by.

Returns:

A reference to the now-modified calling object

inline std::string to_string() const

Converts the matrix to a human-readable string.

Returns:

A string representing the object

Public Static Functions

static inline Matrix Identity(int n)

specialised psuedo-constructor for the identity matrix

Returns:

The matrix A such that A[i][i] = 1, otherwise A[i][j] = 0

Operator Overloads

Equality

inline bool JSL::operator==(const Matrix &lhs, const Matrix &rhs)

An overloaded equality checker. Checks size, then checks each entry - quick for finding mismatches, but requires full sweep to confirm total equality.

Parameters:
  • lhs – The first matrix

  • rhs – the value to be compared to lhs for equality

Returns:

True, if equal, false if not

inline bool JSL::operator!=(const Matrix &lhs, const Matrix &rhs)

Simply returns the inverse of the equality operator.

Parameters:
  • lhs – The first matrix

  • rhs – the value to be compared to lhs for equality

Returns:

False if equal, true if not

inline bool JSL::MatrixSizesEqual(const Matrix &m1, const Matrix &m2)

Confirms the sizes (i.e. row count and column count) of the matrices are equal.

Parameters:
  • lhs – The first matrix

  • rhs – the value to be compared to lhs for size equality

Returns:

True, if equal in size, false if not

Addition/Subtraction

inline Matrix JSL::operator+(const Matrix &lhs, const Matrix &rhs)

Performs obvious matrix addition (a+b)_ij = a_ij + b_ij. Throws an error if the matrices are not the same size.

Parameters:
  • lhs – The first vector to be summed

  • rhs – The second vector to be summed (order is irrelevant)

Returns:

The vector lhs + rhs

inline Matrix JSL::operator+(const Matrix &lhs, const double &scalar)

Adds the value of scalar to every element in the matrix.

Parameters:
  • lhs – The matrix to be summed

  • scalar – The scalar to be added element-wise

Returns:

The matrix lhs + scalar

inline Matrix JSL::operator+(const double &scalar, const Matrix &rhs)

Exactly equivalent to JSL::operator+(const Matrix &lhs, const double &scalar), just swapped around.

Parameters:
  • scalar – The scalar to be added element-wise

  • rhs – The matrix to be summed

Returns:

The scalar + rhs

inline Matrix JSL::operator-(const Matrix &lhs, const Matrix &rhs)

Performs obvious matrix subtraction (a-b)_ij = a_ij - b_ij. Throws an error if the matrices are not the same size.

Parameters:
  • lhs – The first vector

  • rhs – The vector to be subtracted from the first

Returns:

The matrix lhs - rhs

inline Matrix JSL::operator-(const Matrix &lhs, const double &scalar)

Subtracts the value of scalar to every element in the Matrix.

Parameters:
  • lhs – The base matrix

  • scalar – The scalar to be subtracted from the base matrix element wise

Returns:

The vector lhs - scalar.

inline Matrix JSL::operator-(const double &scalar, const Matrix &rhs)

A slightly odd operation (included for completeness) - adds the value of scalar to the negative of the elements of the vector.

Parameters:
  • scalar – The value which acts as a base

  • rhs – The vector which will be subtracted elementwise from the base scalar

Returns:

The vector scalar - rhs

Scalar Multiplication/Division

inline Matrix JSL::operator*(const Matrix &lhs, const double &scalar)

Alias of JSL::operator+(const double &scalar,const Matrix &rhs) with the operation order swapped around.

Parameters:
  • lhs – The matrix to multiply

  • scalar – The value to multiply elements by

Returns:

The pointwise product of the elements of lhs and the scalar

inline Matrix JSL::operator*(const double &scalar, const Matrix &rhs)

Naive element-wise scalar multiplication.

Parameters:
  • scalar – The value to multiply elements by

  • rhs – The matrix to multiply

Returns:

The pointwise product of the elements of rhs and the scalar

inline Matrix JSL::operator/(const Matrix &lhs, const double &scalar)

Essentially an alias for JSL::operator+(const double &scalar,const Matrix &rhs) with the scalar set to one-over itself, i.e. pointwise division of the provided matrix.

Parameters:
  • lhs – The matrix to divide

  • scalar – The value to divide elements by

Returns:

The pointwise divisor of the elements of lhs and the scalar

Matrix and Vector Products

inline Matrix JSL::operator*(const Matrix &lhs, const Matrix &rhs)

Performs standard matix multiplication, (AB)_ij = A_ik B_kj. Only works on matrices of compatible sizes, and left-right ordering matters.

Parameters:
  • lhs – The left hand size of the multiplication

  • The – right hand side of the multiplication

Returns:

The product lhs * rhs accordingto standard matrix product rules. Size of output is lhs.Rows by rhs.Columns

inline Vector JSL::operator*(const Matrix &lhs, const Vector &rhs)

Multiplies a vector by the matrix, following the rule (Av)_i = A_ij v_j.

Parameters:
  • lhs – The matrix operating on the matrix

  • rhs – The vector to be operated upon

Returns:

The product (lhs * rhs), which is a Vector the same size as rhs

Streaming

inline std::ostream &JSL::operator<<(std::ostream &os, const Matrix &obj)

Calls JSL::Matrix::to_string() and then passes it to the provided stream, enabling sweet, smooth output such as std::cout << M << std::endl.

Parameters:
  • os – An output stream capable of parsing strings

  • obj – A Matrix object to be inserted into the stream for output

Returns:

A reference to the modified stream