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>
|
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...
|
|
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.
◆ ~Matrix()
template<typename T, size_t m, size_t n>
◆ Matrix() [1/3]
template<typename T, size_t m, size_t n>
Default constructor to initialize the matrix with zeros.
76 for(
size_t i=0;i<m;i++)
78 for(
size_t j=0;j<n;j++)
T array[m][n]
Definition: matrix.h:44
◆ Matrix() [2/3]
template<typename T, size_t m, size_t n>
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}} |
94 if((m!=my_list.size()))
96 throw "Wrong Matrix dimension initialized\n";
98 }
catch (
const char* msg)
100 std::cerr<< msg<<
'\n';
106 for(std::initializer_list<T> i:my_list)
112 throw "Wrong Matrix dimension initialized\n";
114 }
catch (
const char* msg)
116 std::cerr<< msg<<
'\n';
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>
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}} |
140 if((m!=my_vec.size())||(n!=my_vec[0].size()))
142 throw "Wrong Matrix dimension initialized \n";
144 }
catch (
const char* msg)
146 std::cerr<< msg<<
'\n';
150 for(
size_t i=0; i<my_vec.size(); ++i)
151 for(
size_t j=0; j<my_vec[0].size(); ++j)
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
◆ cols()
template<typename T, size_t m, size_t n>
- Returns
- Returns the number of columns in the Matrix
◆ 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>
Function to write value to the (i,j)th position of the Matrix.
- Parameters
-
- Returns
244 {
return array[i][j]; }
T array[m][n]
Definition: matrix.h:44
◆ operator=()
template<typename T, size_t m, size_t n>
Equal to operator overloaded to copy one matrix into another.
221 for(
size_t i=0; i<m; ++i)
222 for(
size_t j=0; j<n; ++j)
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>
- Returns
- Returns the number of Rows in the Matrix.
◆ transpose()
template<typename T, size_t m, size_t n>
Take an object of type Matrix and returns its Transpose.
- Returns
- Given input Matrix A, the Transpose of A is returned.
163 Matrix<T,n,m> result;
164 for(
size_t i=0;i<m;++i)
166 for(
size_t j=0;j<n;++j)
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
◆ operator*
template<typename T, size_t m, size_t n>
template<size_t k, size_t p>
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
-
- Parameters
-
- Returns
- Resultant Matrix Product of A and B i.e. A*B
193 throw "Matrix Inner Dimensions don't match can't multiply!!! \n";
196 catch (
const char* msg)
198 std::cerr<< msg<<
'\n';
202 Matrix<T,m,k> result;
204 for (
size_t i_m = 0; i_m < m; ++i_m) {
205 for (
size_t i_k = 0; i_k < k; ++i_k) {
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);
◆ 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.
253 for(
size_t i=0; i<m; ++i)
255 for(
size_t j=0; j<n; ++j)
256 {os<<rhs.ElementAt(i,j)<<
" ";}
◆ array
template<typename T, size_t m, size_t n>
The documentation for this class was generated from the following file: