Skip to content

var::matrix

Class for 2d matrix of objects. More...

#include "matrix.hpp"

Public Classes

Name
struct LU
Struct for PLU decomposition.

Protected Classes

Name
class Col
Col class for another operator[].

Protected Types

Name
template <typename T >
using std::vector< std::vector< T > >
table
2D vector definition

Public Functions

Name
matrix(int r, int c)
Construct a new matrix object.
matrix()
Default construct a new matrix object.
matrix(std::initializer_list< std::initializer_list< D >> a)
Construct a new matrix object.
int row()
Returns the number of rows.
int col()
Returns the number of columns.
int size()
Returns total number of elements.
auto begin()
Mainly invoked for the ranged for loop.
auto end()
Returns end of data.
auto begin() const
Returns const begin.
auto end() const
Returns const end.
D get_element(int i, int j)
Get the element object.
std::vector< D > get_row(int i)
Returns the row at an index.
std::vector< D > get_col(int j)
Returns the column at an index.
void resize(int r, int c)
Resizes the matrix.
void push_row(std::vector< D > a)
Inserts row at the end or the matrix.
void push_col(std::vector< D > a)
Inserts column at the end of the matrix.
void insert_row(int i, std::vector< D > a)
Inserts row at specefic index.
void insert_col(int j, std::vector< D > a)
Inserts column at specefic index.
void pop_row()
Removes last row.
void pop_col()
removes last column
void erase_row(int i)
Erase row at index.
void erase_col(int j)
Erases column at index.
void row_swap(int i1, int i2)
Swaps row of matrix.
void col_swap(int j1, int j2)
Colum swap.
void replace_row(int i, std::vector< D > a)
Replaces certain row.
void replace_col(int j, std::vector< D > a)
Replaces certain colum.
void sort_rows(int d =1)
Sorts all rows.
void sort_cols(int d =1)
Sorts all columns.
void sort_row(int i, int d =1)
Sorts rows at specefic index.
void sort_col(int j, int d =1)
Sorts column at specefic index.
template <typename LAMBDA >
void
row_op(int i, LAMBDA f)
Does operations on specefic rows.
template <typename LAMBDA >
void
col_op(int j, LAMBDA f)
Does operations on a specefic column.
template <typename LAMBDA >
void
mat_op(LAMBDA f)
Function operation on all of the matrix.
void join_row(matrix other)
Combines the rows of another matrix.
void join_col(matrix other)
Combines the columns of another matrix.
void turn_to(D n)
Converts all elements to n.
D sum()
Sum of all elements.
D tr()
Trace of a matrix.
D det()
Returns the determinant.
matrix T()
Transpose of a matrix.
matrix cofactor()
Returns cofactor matrix.
matrix M(int i, int j)
Minor of a matrix.
matrix adj()
Adjugate of a matrix.
matrix inv()
Returns inverse of a matrix.
matrix rref()
Reduced row echolon form.
matrix ref()
Row echolon form.
LU plu()
PLU decomposition.
bool is_square()
Checks if matrix is square.
bool is_empty()
Returns if the matrix is empty or not.
bool is_identity()
Checks if matrix is an identity matrix.
Col operator[](int i)
[][] operator for matrix
matrix operator+(const matrix & other)
Matrix addition.
matrix operator+(D n)
matrix+var as var as added to all elements
void operator+=(matrix a)
Increments current matrix by matrix a
void operator+=(D a)
Specific variable increment.
void operator++(int a)
Increment operator.
matrix operator-()
Negative of a matrix.
matrix operator-(const matrix & other)
Matrix subtraction.
matrix operator-(D n)
matrix-var
void operator-=(matrix a)
Decrement current matrix by matrix a
void operator-=(D a)
Decrement current matrix by value a
void operator--(int a)
Decrement operator.
matrix operator*(const matrix & other)
Matrix multiplication.
matrix operator*(D n)
matrix*var
matrix mathmul(const matrix & other)
Normal multiplicatoin.
matrix operator/(matrix & other)
Matrix division using inverse.
matrix operator/(D n)
matrix/var
matrix mathdiv(const matrix & other)
Normal math division.
matrix operator()(int i1, int i2)
Returns a sliced matrix.
std::vector< D > operator()(int i, int j1, int j2)
Column per row slicing.
matrix operator()(int i1, int i2, int j1, int j2)
Row and column slice.

Protected Functions

Name
void check_col(int j)
checks index for columns
void check_row(int i)
checks index for rows
table< D > TT()
Returns transpose for data.
void check_size(int r, int c)
Checks other matrix size for operators.
matrix MM(int i, int j, matrix & other)
Protected minor (used in DET)
D DET(matrix a)
Recursive determinant.
void square()
throws exception if matrix is not a square

Friends

Name
matrix operator+(D n, matrix & other)
var+matrix
matrix operator-(D n, matrix & other)
var-matrix
matrix operator*(D n, matrix & other)
var*matrix
matrix operator/(D n, matrix & other)
var/matrix

Detailed Description

template <typename D >
class var::matrix;

Class for 2d matrix of objects.

Template Parameters:

  • D can be of any type

Protected Types Documentation

using table

template <typename T >
using var::matrix< D >::table =  std::vector<std::vector<T> >;

2D vector definition

Template Parameters:

  • T

Public Functions Documentation

function matrix

inline matrix(
    int r,
    int c
)

Construct a new matrix object.

Parameters:

  • r number of rows
  • c number of colums

Usage:

var::matrix<int> m(2, 3); // 2 rows, 3 colums

function matrix

inline matrix()

Default construct a new matrix object.

Usage:

var::matrix<int> m;

function matrix

inline matrix(
    std::initializer_list< std::initializer_list< D >> a
)

Construct a new matrix object.

Usage:

var::matrix<int> m = {
   {1, 2, 3},
   {4, 5, 6},
   {7, 8, 9}
};

Getters

ainitializer list

function row

inline int row()

Returns the number of rows.

Return: int

Usage:

var::matrix<int> m(2, 3);
int r = m.row();

function col

inline int col()

Returns the number of columns.

Return: int

function size

inline int size()

Returns total number of elements.

Return: int

function begin

inline auto begin()

Mainly invoked for the ranged for loop.

Return: std::vector<D>

Usage:

for(auto i: m){
   for(j: i){
     // do smth
   }
}

function end

inline auto end()

Returns end of data.

Return: std::vector<D>

function begin

inline auto begin() const

Returns const begin.

Return: std::vector<D>

function end

inline auto end() const

Returns const end.

Return: std::vector<D>

function get_element

inline D get_element(
    int i,
    int j
)

Get the element object.

Parameters:

  • i row index
  • j column index

Return: D

  • Unlike the [] operator this does index checking

function get_row

inline std::vector< D > get_row(
    int i
)

Returns the row at an index.

Parameters:

  • i row index

Return: std::vector<D>

Usage:

var::matrix<int> m(3, 3);
auto col = m.get_col(1);

function get_col

inline std::vector< D > get_col(
    int j
)

Returns the column at an index.

Parameters:

  • j column index

Return: std::vector<D>

Mutable methods

function resize

inline void resize(
    int r,
    int c
)

Resizes the matrix.

Parameters:

  • r number of rows
  • c number of colums

Usage:

m.resize(3, 3);

Warning

This resets all elements in the matrix

function push_row

inline void push_row(
    std::vector< D > a
)

Inserts row at the end or the matrix.

Parameters:

  • a row vector

Usage:

m.push_row({1, 2, 3});

function push_col

inline void push_col(
    std::vector< D > a
)

Inserts column at the end of the matrix.

Parameters:

  • a column vector

function insert_row

inline void insert_row(
    int i,
    std::vector< D > a
)

Inserts row at specefic index.

Parameters:

  • i row index
  • a row vector

Usage:

m.insert_row(1, {1, 2, 3});

function insert_col

inline void insert_col(
    int j,
    std::vector< D > a
)

Inserts column at specefic index.

Parameters:

  • j column index
  • a column vector

function pop_row

inline void pop_row()

Removes last row.

Usage:

m.pop_row(1);

function pop_col

inline void pop_col()

removes last column

function erase_row

inline void erase_row(
    int i
)

Erase row at index.

Parameters:

  • i row index

Usage:

m.erase_row(1);

function erase_col

inline void erase_col(
    int j
)

Erases column at index.

Parameters:

  • j column index

function row_swap

inline void row_swap(
    int i1,
    int i2
)

Swaps row of matrix.

Parameters:

  • i1 first row
  • i2 second row

Usage:

m.row_swap(0, 1); // swaps row 0 with row 1

function col_swap

inline void col_swap(
    int j1,
    int j2
)

Colum swap.

Parameters:

  • j1 first column
  • j2 second column

function replace_row

inline void replace_row(
    int i,
    std::vector< D > a
)

Replaces certain row.

Parameters:

  • i row index
  • a std::vector

function replace_col

inline void replace_col(
    int j,
    std::vector< D > a
)

Replaces certain colum.

Parameters:

  • j column index
  • a std::vector

function sort_rows

inline void sort_rows(
    int d =1
)

Sorts all rows.

Parameters:

  • d details are shown below
  • d = 1 is accending order \(\rightarrow\) sort_rows();
  • d = 0 is decending order \(\rightarrow\) sort_rows(0);

Usage:

m.sort_rows();

function sort_cols

inline void sort_cols(
    int d =1
)

Sorts all columns.

Parameters:

  • d details are shown below
  • d = 1 is accending order \(\rightarrow\) sort_rows();
  • d = 0 is decending order \(\rightarrow\) sort_rows(0);

function sort_row

inline void sort_row(
    int i,
    int d =1
)

Sorts rows at specefic index.

Parameters:

  • i row index
  • d details are shown below
  • d = 1 is accending order \(\rightarrow\) sort_rows();
  • d = 0 is decending order \(\rightarrow\) sort_rows(0);

Usage:

m.sort_row(0, 0);

function sort_col

inline void sort_col(
    int j,
    int d =1
)

Sorts column at specefic index.

Parameters:

  • j column index
  • d details are shown below
  • d = 1 is accending order \(\rightarrow\) sort_rows();
  • d = 0 is decending order \(\rightarrow\) sort_rows(0);

function row_op

template <typename LAMBDA >
inline void row_op(
    int i,
    LAMBDA f
)

Does operations on specefic rows.

Parameters:

  • i column index
  • f function to change column elements

Template Parameters:

  • LAMBDA std::function

Usage:

auto f = [](int A){return A*2+3;};
m.row_op(1, f);

function col_op

template <typename LAMBDA >
inline void col_op(
    int j,
    LAMBDA f
)

Does operations on a specefic column.

Parameters:

  • j column index
  • f function to change column elements

Template Parameters:

  • LAMBDA std::function

Exception

All row and column operations (inserting, removing .. etc) throw an std::invalid_argument if: 1. There is a size mismatch 2. Invalid index

function mat_op

template <typename LAMBDA >
inline void mat_op(
    LAMBDA f
)

Function operation on all of the matrix.

Parameters:

  • f Function

Template Parameters:

  • LAMBDA std::function

Usage:

auto f = [](int A){return A*2+3;};
m.mat_op(f);

function join_row

inline void join_row(
    matrix other
)

Combines the rows of another matrix.

Parameters:

  • other matrix

Usage:

// m2 must have same number of columns as m1
m1.join_row(m2);

function join_col

inline void join_col(
    matrix other
)

Combines the columns of another matrix.

Parameters:

  • other matrix

  • other must have same number of rows

function turn_to

inline void turn_to(
    D n
)

Converts all elements to n.

Parameters:

  • n the specifeid variable

Usage:

m.turn_to(1);

Immutable methods

function sum

inline D sum()

Sum of all elements.

Return: D

Usage:

auto sum = m.sum();

function tr

inline D tr()

Trace of a matrix.

Return: D

function det

inline D det()

Returns the determinant.

Return: D

function T

inline matrix T()

Transpose of a matrix.

Return: matrix

Usage:

var::matrix<int> m(3, 3);
auto T = m.T();

function cofactor

inline matrix cofactor()

Returns cofactor matrix.

Return: matrix

function M

inline matrix M(
    int i,
    int j
)

Minor of a matrix.

Parameters:

  • i row index
  • j column index

Return: matrix

function adj

inline matrix adj()

Adjugate of a matrix.

Return: matrix

function inv

inline matrix inv()

Returns inverse of a matrix.

Return: matrix

function rref

inline matrix rref()

Reduced row echolon form.

Return: matrix

function ref

inline matrix ref()

Row echolon form.

Return: matrix

function plu

inline LU plu()

PLU decomposition.

Return: LU

Usage:

// the output is LU struct
auto LU = m.plu();
auto L = LU.L;
auto U = LU.U;
auto P = LU.p;

function is_square

inline bool is_square()

Checks if matrix is square.

Return:

  • true
  • false

Usage:

bool square = m.is_square();

function is_empty

inline bool is_empty()

Returns if the matrix is empty or not.

Return:

  • true
  • false

function is_identity

inline bool is_identity()

Checks if matrix is an identity matrix.

Return:

  • true
  • false

Operator methods

function operator[]

inline Col operator[](
    int i
)

[][] operator for matrix

Parameters:

  • i row index

Return: Col which then returns &D

Usage:

// m[row][col]
m[0][0] = 5;
  • Does not check the correct row and colum index;

function operator+

inline matrix operator+(
    const matrix & other
)

Matrix addition.

Parameters:

  • other

Return: matrix

Usage:

// m1 and m2 are matrix class
auto summed = m1+m2;

function operator+

inline matrix operator+(
    D n
)

matrix+var as var as added to all elements

Parameters:

  • n variable to add

Return: matrix

Usage:

auto m_add_2 = m+2; // or 2+m

function operator+=

inline void operator+=(
    matrix a
)

Increments current matrix by matrix a

Parameters:

  • a matrix

Usage:

m1+=m2; // m1,m2 -> matrix

function operator+=

inline void operator+=(
    D a
)

Specific variable increment.

Parameters:

  • a increments each value by a

Usage:

m+=2; // increments every element by 2

function operator++

inline void operator++(
    int a
)

Increment operator.

Parameters:

  • a

Usage:

m++; all values will be increased by 1

function operator-

inline matrix operator-()

Negative of a matrix.

Return: matrix

Usage:

auto negated = -m;

function operator-

inline matrix operator-(
    const matrix & other
)

Matrix subtraction.

Parameters:

  • other matrix

Return: matrix

Usage:

auto subtracted = m1-m2;

function operator-

inline matrix operator-(
    D n
)

matrix-var

Parameters:

  • n variable to add

Return: matrix

Usage:

auto m_take_2 = m-2; // or 2-m

function operator-=

inline void operator-=(
    matrix a
)

Decrement current matrix by matrix a

Parameters:

  • a matrix

Usage:

m1 -= m2;

function operator-=

inline void operator-=(
    D a
)

Decrement current matrix by value a

Parameters:

  • a Decrements each value by a

Usage:

m -= 2; // subtracts each element by 2

function operator--

inline void operator--(
    int a
)

Decrement operator.

Parameters:

  • a

Usage:

m--; // deacreses every value of m by 1

function operator*

inline matrix operator*(
    const matrix & other
)

Matrix multiplication.

Parameters:

  • other matrix

Return: matrix

Usage:

auto matrix_mul = m*m;

function operator*

inline matrix operator*(
    D n
)

matrix*var

Parameters:

  • n variable multiplying with

Return: matrix

Usage:

auto m_by_2 = m*2; // or 2*m

function mathmul

inline matrix mathmul(
    const matrix & other
)

Normal multiplicatoin.

Parameters:

  • other matrix

Return: matrix

Usage:

auto mul_m = m1.mathmul(m2);

function operator/

inline matrix operator/(
    matrix & other
)

Matrix division using inverse.

Parameters:

  • other matrix

Return: matrix

function operator/

inline matrix operator/(
    D n
)

matrix/var

Parameters:

  • n our variable

Return: matrix

Usage:

auto m_over_2 = m/2; // 2/m can work

function mathdiv

inline matrix mathdiv(
    const matrix & other
)

Normal math division.

Parameters:

  • other matrix

Return: matrix

Usage:

auto divided = m1.mathdiv(m2);

function operator()

inline matrix operator()(
    int i1,
    int i2
)

Returns a sliced matrix.

Parameters:

  • i1 index row 1 (inclusive)
  • i2 index row 2 (exclusive)

Return: matrix

Usage:

// m(inclusive, exclusive)
auto sliced = m(1, 3); // row-1 to row-2

function operator()

inline std::vector< D > operator()(
    int i,
    int j1,
    int j2
)

Column per row slicing.

Parameters:

  • i row index
  • j1 column index 1 (inclusive)
  • j2 column index 2 (exclusive)

Return: std::vector<D>

  • Condition j2 >= j1 must be true.

function operator()

inline matrix operator()(
    int i1,
    int i2,
    int j1,
    int j2
)

Row and column slice.

Parameters:

  • i1 row index 1 (inclusive)
  • i2 row index 2 (exclusive)
  • j1 column index 1 (inclusive)
  • j2 column index 2 (exclusive)

Return: matrix

Exception

All () operators will throw an std::invalid_argument unless:

  1. Index is correct and valid

  2. \(i_2 \geq i_1\) and \(j_2 \geq j_1\)

Protected Functions Documentation

function check_col

inline void check_col(
    int j
)

checks index for columns

Parameters:

  • j column index

function check_row

inline void check_row(
    int i
)

checks index for rows

Parameters:

  • i row index

function TT

inline table< D > TT()

Returns transpose for data.

Return: table<D>

function check_size

inline void check_size(
    int r,
    int c
)

Checks other matrix size for operators.

Parameters:

  • r row
  • c column

function MM

inline matrix MM(
    int i,
    int j,
    matrix & other
)

Protected minor (used in DET)

Parameters:

  • i row index
  • j column index
  • other matrix

Return: matrix

function DET

inline D DET(
    matrix a
)

Recursive determinant.

Parameters:

  • a matrix type

Return: D

function square

inline void square()

throws exception if matrix is not a square

Friends

friend operator+

friend matrix operator+(
    D n,

    matrix & other
);

var+matrix

Parameters:

  • n variable to add
  • other matrix

Return: matrix

friend operator-

friend matrix operator-(
    D n,

    matrix & other
);

var-matrix

Parameters:

  • n variable to add
  • other matrix

Return: matrix

friend operator*

friend matrix operator*(
    D n,

    matrix & other
);

var*matrix

Parameters:

  • n variable multiplying with
  • other matrix

Return: matrix

friend operator/

friend matrix operator/(
    D n,

    matrix & other
);

var/matrix

Parameters:

  • n our variable
  • other matrix

Return: matrix


Updated on 4 September 2022 at 18:30:41 EEST