Skip to content

Instantly share code, notes, and snippets.

@harshraj22
Created June 13, 2019 10:59
Show Gist options
  • Save harshraj22/144252bafd0ee3bcb67b1770a956f5af to your computer and use it in GitHub Desktop.
Save harshraj22/144252bafd0ee3bcb67b1770a956f5af to your computer and use it in GitHub Desktop.
A template for matrix expressions
#include<iostream>
#include<vector>
#include<utility> // for std:: pair
#include<stdbool.h> // for bool datatype
#include<iomanip> // for formatted printing ( padding ) in show()
using namespace std;
template<typename T>
class matrix{
private:
vector< vector<T> > v; //vector of vectors, just like a 2D array
int ROW,COL;
T det(); // for calculating determinant
public:
matrix(int row=0,int col=0,T val=(T)0); // constructor, can be used at default constructor due to default parameters
pair<int,int> row_col(); // returns a pair containg number of rows and columns
matrix<T>& show(); // prints whole matrix
T get_val(int row,int col);
matrix<T>& set_all(T val,bool diag=false); // depending on flag value, assigns all(or diagonal elements) the provided value
matrix<T>& set_val(int row,int col,T val);
matrix<T>& operator+(matrix<T>& mat);
matrix<T>& operator-(matrix<T>& mat);
};
template<typename T>
matrix<T>::matrix(int row,int col,T val)
:ROW(row), // member list initialisation........
COL(col){
v.resize(row,vector<T> (col));
set_all(val); // can be skipped and directly initialised in v.resize
}
template<typename T>
pair<int,int> matrix<T>::row_col(){
return {ROW,COL};
}
template<typename T>
matrix<T>& matrix<T>::show(){
for(auto i:v){
for(auto j:i)
cout<<setw(4)<<j<<" "; // padding
cout<<endl;
}
return *this; // function chaining
}
template<typename T>
T matrix<T>::get_val(int row,int col){
if(row<ROW && col<COL)
return v[row][col];
else cout<<"That doesn't exist. Check row and col !. Giving a random value.\n";
return v[0][0];
}
template<typename T>
matrix<T>& matrix<T>::set_all(T val,bool diag){ // if diag is true, it assigns diagonal elements only.
int ROW=row_col().first ,COL=row_col().second ;
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
if(i!=j && diag==true) // add && ROW==COL if you want strictly diagonal elements to be assigned (in case of square matrix)
continue;
set_val(i,j,val); //v[i][j]=val;
}
}
return *this;
}
template<typename T>
matrix<T>& matrix<T>::set_val(int row,int col,T val){
if(row<ROW && col<COL)
v[row][col]=val;
else cout<<"That doesn't exist. Check size !\n";
return *this;
}
template<typename T>
matrix<T>& matrix<T>::operator+(matrix<T>& mat){
if(make_pair(ROW,COL)!=mat.row_col()){
cout<<"Can't add"<<endl; // you may change this to throw some error
return *this;
}
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++)
v[i][j]+=mat.get_val(i,j);
}
return *this;
}
template<typename T>
matrix<T>& matrix<T>::operator-(matrix<T>& mat){
if(make_pair(ROW,COL)!=mat.row_col()){
cout<<"Can't subtract"<<endl; // you may change this to throw some error
return *this;
}
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++)
v[i][j]-=mat.get_val(i,j);
}
return *this;
}
int main(){
matrix<int> mymatrix(2,3),my(2,3),var(2,3);
// my.set_val(1,1,5);
(mymatrix-my.set_all(1,true)+var.set_all(5)).show();
//mymatrix.set_val(1,0,45).show();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment