Matrix Library
A simple Matrix operation library to perform Matrix Multiplication and find the transpose of a Matrix.
MATOPS::Matrix< T, m, n > Class Template Reference

This class is for matrices of smaller size. The user can define a Matrix A of size m x n of Datatype T as follows: MATOPS::Matrix<T,m,n> A. More...

#include <matrix.h>

Public Member Functions

virtual ~Matrix ()
 
size_t cols ()
 
size_t rows ()
 
 Matrix ()
 Default constructor to initialize the matrix with zeros. More...
 
 Matrix (std::initializer_list< std::initializer_list< T >> my_list)
 Overloaded constructor the initialize the Matrix from a 2D Initializer list. More...
 
 Matrix (std::vector< std::vector< T >> &my_vec)
 Overloaded constructor to initialize the Matrix from a 2D Vector. More...
 
Matrix< T, n, m > transpose ()
 Take an object of type Matrix and returns its Transpose. More...
 
Matrixoperator= (const Matrix &rhs)
 Equal to operator overloaded to copy one matrix into another. More...
 
const T & ElementAt (size_t i, size_t j) const
 Function to read value from the (i,j)th position of the Matrix. More...
 
T & ElementAt (size_t i, size_t j)
 Function to write value to the (i,j)th position of the Matrix. More...
 

Private Attributes

array [m][n]
 

Friends

template<size_t k, size_t p>
Matrix< T, m, k > operator* (const Matrix< T, m, n > &A, const Matrix< T, p, k > &B)
 Matrix multiplication using overloaded * operator. Takes 2 Matrices of size m x n and p x k. Checks if their inner dimensions match (i.e. n=p). If n=p then A and B is multiplied and a resultant Matrix of size m x k is returned. More...
 
std::ostream & operator<< (std::ostream &os, const Matrix &rhs)
 Operator overloaded to output/print an object of type Matrix. More...
 

Detailed Description

template<typename T, size_t m, size_t n>
class MATOPS::Matrix< T, m, n >

This class is for matrices of smaller size. The user can define a Matrix A of size m x n of Datatype T as follows: MATOPS::Matrix<T,m,n> A.

Template Parameters
T= DataType of the Matrix. Eg. int, float, double etc.
m= No. of Rows of the Matrix
n= No. of Cols of the Matrix

Warning: This class can't handle really large matrices. On initilizing large Matrices the program stack gets filled with data and there is no space left to do other operations. Care should be taken that the matrix sizes are less than 200.

Constructor & Destructor Documentation

◆ ~Matrix()

template<typename T, size_t m, size_t n>
virtual MATOPS::Matrix< T, m, n >::~Matrix ( )
inlinevirtual

Destructor

50 {}

◆ Matrix() [1/3]

template<typename T, size_t m, size_t n>
MATOPS::Matrix< T, m, n >::Matrix ( )
inline

Default constructor to initialize the matrix with zeros.

75  {
76  for(size_t i=0;i<m;i++)
77  {
78  for(size_t j=0;j<n;j++)
79  {
80  array[i][j]=0;
81  }
82  }
83  }
T array[m][n]
Definition: matrix.h:44

◆ Matrix() [2/3]

template<typename T, size_t m, size_t n>
MATOPS::Matrix< T, m, n >::Matrix ( std::initializer_list< std::initializer_list< T >>  my_list)
inline

Overloaded constructor the initialize the Matrix from a 2D Initializer list.

Parameters
my_list= A 2D initializer_list of the the m x n Matrix. Eg: {{1,2},{3,4}}
90  :Matrix()
91  {
92  //std::cout<<my_list(0).<<'\n';
93  try{
94  if((m!=my_list.size()))
95  {
96  throw "Wrong Matrix dimension initialized\n";
97  }
98  }catch (const char* msg)
99  {
100  std::cerr<< msg<<'\n';
101  exit(0);
102  }
103 
104 
105  int row_n=0;
106  for(std::initializer_list<T> i:my_list)
107  { int col_n=0;
108 
109  try{
110  if((n!=i.size()))
111  {
112  throw "Wrong Matrix dimension initialized\n";
113  }
114  }catch (const char* msg)
115  {
116  std::cerr<< msg<<'\n';
117  exit(0);
118  }
119 
120  for(T j : i)
121  {
122  ElementAt(row_n,col_n)=j;
123  col_n++;
124  // cout<<j<<" ";
125  }
126  row_n++;
127  //cout<<'\n';
128  }
129  }
Matrix()
Default constructor to initialize the matrix with zeros.
Definition: matrix.h:74
const T & ElementAt(size_t i, size_t j) const
Function to read value from the (i,j)th position of the Matrix.
Definition: matrix.h:234

◆ Matrix() [3/3]

template<typename T, size_t m, size_t n>
MATOPS::Matrix< T, m, n >::Matrix ( std::vector< std::vector< T >> &  my_vec)
inline

Overloaded constructor to initialize the Matrix from a 2D Vector.

Parameters
my_vec= A 2D initializer_list of the the m x n Matrix. Eg: {{1,2},{3,4}}
137  :Matrix()
138  {
139  try{
140  if((m!=my_vec.size())||(n!=my_vec[0].size()))
141  {
142  throw "Wrong Matrix dimension initialized \n";
143  }
144  }catch (const char* msg)
145  {
146  std::cerr<< msg<<'\n';
147  exit(0);
148  }
149 
150  for(size_t i=0; i<my_vec.size(); ++i)
151  for(size_t j=0; j<my_vec[0].size(); ++j)
152  ElementAt(i,j) = my_vec[i][j];
153  }
Matrix()
Default constructor to initialize the matrix with zeros.
Definition: matrix.h:74
const T & ElementAt(size_t i, size_t j) const
Function to read value from the (i,j)th position of the Matrix.
Definition: matrix.h:234

Member Function Documentation

◆ cols()

template<typename T, size_t m, size_t n>
size_t MATOPS::Matrix< T, m, n >::cols ( )
inline
Returns
Returns the number of columns in the Matrix
56  {
57  return n;
58  }

◆ ElementAt() [1/2]

template<typename T, size_t m, size_t n>
const T& MATOPS::Matrix< T, m, n >::ElementAt ( size_t  i,
size_t  j 
) const
inline

Function to read value from the (i,j)th position of the Matrix.

Parameters
i= row index
j= column index
Returns
235  { return array[i][j]; }
T array[m][n]
Definition: matrix.h:44

◆ ElementAt() [2/2]

template<typename T, size_t m, size_t n>
T& MATOPS::Matrix< T, m, n >::ElementAt ( size_t  i,
size_t  j 
)
inline

Function to write value to the (i,j)th position of the Matrix.

Parameters
i
j
Returns
244  { return array[i][j]; }
T array[m][n]
Definition: matrix.h:44

◆ operator=()

template<typename T, size_t m, size_t n>
Matrix& MATOPS::Matrix< T, m, n >::operator= ( const Matrix< T, m, n > &  rhs)
inline

Equal to operator overloaded to copy one matrix into another.

219  {
220  if( this != &rhs )
221  for(size_t i=0; i<m; ++i)
222  for(size_t j=0; j<n; ++j)
223  ElementAt(i,j) = rhs.ElementAt(i,j);
224  return *this;
225  }
const T & ElementAt(size_t i, size_t j) const
Function to read value from the (i,j)th position of the Matrix.
Definition: matrix.h:234

◆ rows()

template<typename T, size_t m, size_t n>
size_t MATOPS::Matrix< T, m, n >::rows ( )
inline
Returns
Returns the number of Rows in the Matrix.
65  {
66  return m;
67  }

◆ transpose()

template<typename T, size_t m, size_t n>
Matrix<T,n,m> MATOPS::Matrix< T, m, n >::transpose ( )
inline

Take an object of type Matrix and returns its Transpose.

Returns
Given input Matrix A, the Transpose of A is returned.
162  {
163  Matrix<T,n,m> result;
164  for(size_t i=0;i<m;++i)
165  {
166  for(size_t j=0;j<n;++j)
167  {
168  result.ElementAt(j,i)=ElementAt(i,j);
169  }
170 
171  }
172  return result;
173 
174  }
const T & ElementAt(size_t i, size_t j) const
Function to read value from the (i,j)th position of the Matrix.
Definition: matrix.h:234

Friends And Related Function Documentation

◆ operator*

template<typename T, size_t m, size_t n>
template<size_t k, size_t p>
Matrix<T,m,k> operator* ( const Matrix< T, m, n > &  A,
const Matrix< T, p, k > &  B 
)
friend

Matrix multiplication using overloaded * operator. Takes 2 Matrices of size m x n and p x k. Checks if their inner dimensions match (i.e. n=p). If n=p then A and B is multiplied and a resultant Matrix of size m x k is returned.

Template Parameters
k= No of cols of Matrix B
p= No of rows of Matrix B
Parameters
A= Matrix A
B= Matrix B
Returns
Resultant Matrix Product of A and B i.e. A*B
189  {
190  try{ // Throw exception if Matrix inner dimensions don't match.
191  if(n!=p)
192  {
193  throw "Matrix Inner Dimensions don't match can't multiply!!! \n";
194  }
195  }
196  catch (const char* msg)
197  {
198  std::cerr<< msg<<'\n';
199  exit(0);
200  }
201 
202  Matrix<T,m,k> result;
203 
204  for (size_t i_m = 0; i_m < m; ++i_m) {
205  for (size_t i_k = 0; i_k < k; ++i_k) {
206 
207  for (size_t i_n = 0; i_n < n; ++i_n) {
208  result.ElementAt(i_m,i_k) += A.ElementAt(i_m,i_n) * B.ElementAt(i_n,i_k);
209  }
210  }
211  }
212  return result;
213  }

◆ operator<<

template<typename T, size_t m, size_t n>
std::ostream& operator<< ( std::ostream &  os,
const Matrix< T, m, n > &  rhs 
)
friend

Operator overloaded to output/print an object of type Matrix.

252  {
253  for(size_t i=0; i<m; ++i)
254  {
255  for(size_t j=0; j<n; ++j)
256  {os<<rhs.ElementAt(i,j)<<" ";}
257  os<<'\n';
258  }
259  return os;
260  }

Member Data Documentation

◆ array

template<typename T, size_t m, size_t n>
T MATOPS::Matrix< T, m, n >::array[m][n]
private

The documentation for this class was generated from the following file: