19 #include<initializer_list>    41                 template<
typename T, 
size_t m, 
size_t n>
    76                                 for(
size_t i=0;i<m;i++)
    78                                         for(
size_t j=0;j<n;j++)
    90                         Matrix(std::initializer_list<std::initializer_list<T>> my_list):
Matrix()
    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';
   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)
   164                                 for(
size_t i=0;i<m;++i)
   166                                         for(
size_t j=0;j<n;++j)
   187                         template<
size_t k, 
size_t p>
   193                                                 throw "Matrix Inner Dimensions don't match can't multiply!!! \n";
   196                                 catch (
const char* msg)
   198                                         std::cerr<< msg<<
'\n';
   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) {
   221                                         for(
size_t i=0; i<m; ++i)
   222                                         for(
size_t j=0; j<n; ++j)
   235                                         { 
return array[i][j]; }
   244                                 { 
return array[i][j]; }
   253                                 for(
size_t i=0; i<m; ++i)
   255                                                 for(
size_t j=0; j<n; ++j)
   276                 template<
typename Data2>
   277                                 void store_csv(Data2 ** C, 
int m_1, 
int n_2, std::string path)
   282                                                 for(
int i=0;i<m_1;i++)
   284                                                         for(
int j=0;j<n_2;j++)
   308                 template<
typename My_data> My_data 
convert_to(
const std::string &str)
   310                                 std::istringstream ss(str); 
   329                 template<
typename Data1>
   349                                 M=(Data1 **)calloc(n,
sizeof(Data1*)); 
   352                                         M[i]=(Data1*)calloc(n,
sizeof(Data1));
   365                         Data1** 
add(Data1** M1, Data1** M2, 
int n)
   367                                 Data1** temp = Init_matrix(n);
   368                             for(
int i=0; i<n; i++)
   369                                 for(
int j=0; j<n; j++)
   370                                     temp[i][j] = M1[i][j] + M2[i][j];
   382                         Data1** 
sub(Data1** M1, Data1** M2, 
int n)
   384                                 Data1** temp = Init_matrix(n);
   385                                 for(
int i=0; i<n; i++)
   386                                         for(
int j=0; j<n; j++)
   387                                                 temp[i][j] = M1[i][j] - M2[i][j];
   420                                         Data1** C=Init_matrix(n);
   427                                                                 C[i][j]+=A[i][k]*B[k][j];
   434                                 Data1** C = Init_matrix(n); 
   438                                 Data1** A11 = Init_matrix(k);
   439                                 Data1** A12 = Init_matrix(k);
   440                                 Data1** A21 = Init_matrix(k);
   441                                 Data1** A22 = Init_matrix(k);
   442                                 Data1** B11 = Init_matrix(k);
   443                                 Data1** B12 = Init_matrix(k);
   444                                 Data1** B21 = Init_matrix(k);
   445                                 Data1** B22 = Init_matrix(k);
   453                                                 A12[i][j] = A[i][k+j];
   454                                                 A21[i][j] = A[k+i][j];
   455                                                 A22[i][j] = A[k+i][k+j];
   457                                                 B12[i][j] = B[i][k+j];
   458                                                 B21[i][j] = B[k+i][j];
   459                                                 B22[i][j] = B[k+i][k+j];
   463                                 Data1** TEMP_B12_B22 = sub(B12, B22, k);
   464                                 Data1** TEMP_A11_A12 = add(A11, A12, k);
   465                                 Data1** TEMP_A21_A22 = add(A21, A22, k);
   466                                 Data1** TEMP_B21_B11 = sub(B21, B11, k);
   469                                 Data1** P1 = StrassenMultiply(A11, TEMP_B12_B22, k);
   470                                 Data1** P2 = StrassenMultiply(TEMP_A11_A12, B22, k);
   471                                 Data1** P3 = StrassenMultiply(TEMP_A21_A22, B11, k);
   472                                 Data1** P4 = StrassenMultiply(A22, TEMP_B21_B11, k);
   474                                 Data1** TEMP_A11_A22 = add(A11, A22, k);
   475                                 Data1** TEMP_B11_B22 = add(B11, B22, k);
   476                                 Data1** TEMP_A12_A22 = sub(A12, A22, k);
   477                                 Data1** TEMP_B21_B22 = add(B21, B22, k);
   478                                 Data1** TEMP_A11_A21 = sub(A11, A21, k);
   479                                 Data1** TEMP_B11_B12 = add(B11, B12, k);
   481                                 Data1** P5 = StrassenMultiply(TEMP_A11_A22, TEMP_B11_B22, k);
   482                                 Data1** P6 = StrassenMultiply(TEMP_A12_A22, TEMP_B21_B22, k);
   483                                 Data1** P7 = StrassenMultiply(TEMP_A11_A21, TEMP_B11_B12, k);
   485                                 Data1** TEMP_P5_P4 = add(P5, P4, k);
   486                                 Data1** TEMP_P5_P4_P6 = add(TEMP_P5_P4, P6, k);
   487                                 Data1** TEMP_P5_P1 = add(P5, P1, k);
   488                                 Data1** TEMP_P5_P1_P3 = sub(TEMP_P5_P1, P3, k);
   491                                 Data1** C11 = sub(TEMP_P5_P4_P6, P2, k);
   492                                 Data1** C12 = add(P1, P2, k);
   493                                 Data1** C21 = add(P3, P4, k);
   494                                 Data1** C22 = sub(TEMP_P5_P1_P3, P7, k);
   498                                 for(
int i=0; i<k; i++)
   500                                         for(
int j=0; j<k; j++)
   503                                                 C[i][j+k] = C12[i][j];
   504                                                 C[k+i][j] = C21[i][j];
   505                                                 C[k+i][k+j] = C22[i][j];
   510                                 for(
int i=0; i<k; i++) {
   511                                                 free(TEMP_B12_B22[i]);
   512                                                 free(TEMP_A11_A12[i]);
   513                                                 free(TEMP_A21_A22[i]);
   514                                                 free(TEMP_B21_B11[i]);
   516                                                 free(TEMP_A11_A22[i]);
   517                                                 free(TEMP_B11_B22[i]);
   518                                                 free(TEMP_A12_A22[i]);
   519                                                 free(TEMP_B21_B22[i]);
   520                                                 free(TEMP_A11_A21[i]);
   521                                                 free(TEMP_B11_B12[i]);
   524                                                 free(TEMP_P5_P4_P6[i]);
   526                                                 free(TEMP_P5_P1_P3[i]);
   602                                                 std::cout<<C[i][j]<<
" ";
   615                 std::vector<std::vector<Data1>> 
load_CSV(
const std::string &path)
   617                     std::ifstream indata;
   622                         std::cerr<<
"File path: '"<<path<<
"' doesn't exist\n";
   626                     std::vector<std::vector<Data1>> dataList;
   627                     std::string line = 
"";
   629                     while(getline(indata,line))
   631                         std::stringstream lineStream(line);
   633                         std::vector<Data1> temp;
   634                         while (std::getline(lineStream,cell,
','))
   636                             temp.push_back(convert_to<Data1>(cell)); 
   638                         dataList.push_back(temp);
   652                         std::vector<std::vector<Data1>> MAT= load_CSV(path);
   654                         for(std::vector<Data1> a: MAT)
   698                         std::ifstream indata;
   699                         indata.open(
"configure.txt");
   703                         LEAF_SIZE=std::stoi(str);
   731                 void matmul(std::string file_1, std::string file_2, std::string path, 
bool print=
false)
   735                                         #ifndef SET_LEAF_SIZE    736                                                 set_configerd_Leaf_size(); 
   742                                         std::vector<std::vector<Data1>> MAT_1= load_CSV(file_1);
   743                                         std::vector<std::vector<Data1>> MAT_2= load_CSV(file_2);
   746                                         int m_1=MAT_1.size(), n_1=MAT_1[0].size();  
   747                                         int m_2=MAT_2.size(), n_2=MAT_2[0].size(); 
   752                                                         throw "Matrix Inner Dimensions don't match !!! \n";
   754                                           }
catch (
const char* msg)
   756                                                         std::cerr<<msg<<
'\n';
   760                                                 int max_n= std::max(std::max(m_1,n_1),n_2); 
   775                                                 A=Init_matrix(dim_n); 
   776                                                 B=Init_matrix(dim_n); 
   779                                                         for(
int k=0;k<m_1;k++)
   781                                                                 for(
int l=0;l<n_1;l++)
   783                                                                         A[k][l]= MAT_1[k][l];
   787                                                         for(
int k=0;k<m_2;k++)
   789                                                                 for(
int l=0;l<n_2;l++)
   791                                                                         B[k][l]= MAT_2[k][l];
   798                                                         C=StrassenMultiply(A, B, dim_n);
   803                                                                         print_Mat(A, m_1, n_1); 
   805                                                                         std::cout<<
"\nB: \n";
   806                                                                         print_Mat(B, m_2, n_2); 
   808                                                                         std::cout<<
"\nANSWER: \n"; 
   809                                                                         print_Mat(C, m_1, n_2);
   814                                                 store_csv<Data1>(C, m_1,n_2,path);
   817                                                 for(
int i=0;i<dim_n;i++)
   842                             std::vector<std::vector<Data1>> MAT= load_CSV(path); 
   844                             Data1** A= (Data1 **)calloc(MAT[0].size(),
sizeof(Data1*));
   846                             for(
int i=0;i<MAT[0].size();i++)
   848                                         A[i]=(Data1*)calloc(MAT.size(),
sizeof(Data1));
   851                                 for(
int i=0;i<MAT.size();i++)
   853                                         for(
int j=0;j<MAT[0].size();j++)
   859                                 store_csv<Data1>(A,MAT[0].size(),MAT.size(),str_path);
   860                                 for(
int i=0;i<MAT[0].size();i++)
   873                                             std::vector<std::vector<Data1>> MAT= load_CSV(path); 
   875                                             Data1** A= (Data1 **)calloc(MAT[0].size(),
sizeof(Data1*));
   877                                             for(
int i=0;i<MAT[0].size();i++)
   879                                                         A[i]=(Data1*)calloc(MAT.size(),
sizeof(Data1));
   882                                                 for(
int i=0;i<MAT.size();i++)
   884                                                         for(
int j=0;j<MAT[0].size();j++)
   890                                                 store_csv<Data1>(A,MAT[0].size(),MAT.size(),path);
   891                                                 store_csv<Data1>(A,MAT[0].size(),MAT.size(),path);
   893                                                 for(
int i=0;i<MAT[0].size();i++)
 Matrix()
Default constructor to initialize the matrix with zeros. 
Definition: matrix.h:74
 
Matrix & operator=(const Matrix &rhs)
Equal to operator overloaded to copy one matrix into another. 
Definition: matrix.h:219
 
std::vector< std::vector< Data1 > > load_CSV(const std::string &path)
Function to load CSV file. This function is internally called by MATOPS::BigMatrix<Data1>::matmul and...
Definition: matrix.h:615
 
int LEAF_SIZE
Definition: matrix.h:332
 
This is the Class for handling Large Matrices. It takes in large Matrices stored as comma-separated v...
Definition: matrix.h:330
 
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory. 
Definition: matrix.h:345
 
void set_LEAF_SIZE(int leaf_size)
This Function sets the LEAF_SIZE i.e. the array size at which we shift from Strassan's Algo to normal...
Definition: matrix.h:685
 
friend std::ostream & operator<<(std::ostream &os, const Matrix &rhs)
Operator overloaded to output/print an object of type Matrix. 
Definition: matrix.h:251
 
void Transpose(std::string path)
This is a function to find the Transpose of a BigMatrix and stores it in the same csv file (In-palce ...
Definition: matrix.h:871
 
My_data convert_to(const std::string &str)
Function template to convert variable type from string to int,float, double or any other Datatype spc...
Definition: matrix.h:308
 
void print_Mat(Data1 **C, int m, int n)
This function is called from within the MATOPS::BigMatrix<Data1>::matmul function when print == True...
Definition: matrix.h:596
 
void store_csv(Data2 **C, int m_1, int n_2, std::string path)
Function template to Store csv file at a given destination file. This function is internally called b...
Definition: matrix.h:277
 
void set_configerd_Leaf_size()
When matrix.h is normally called i.e. without any SET_LEAF_SIZE flag, set_configerd_Leaf_size() funct...
Definition: matrix.h:696
 
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
 
void matmul(std::string file_1, std::string file_2, std::string path, bool print=false)
This is the BigMatrix multiplication Function that multiplies two matrices A and B stored in A...
Definition: matrix.h:731
 
Matrix(std::vector< std::vector< T >> &my_vec)
Overloaded constructor to initialize the Matrix from a 2D Vector. 
Definition: matrix.h:137
 
void Mat_print(std::string path)
Function to print a Matrix from a .csv file. 
Definition: matrix.h:650
 
friend 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...
Definition: matrix.h:188
 
T array[m][n]
Definition: matrix.h:44
 
Matrix< T, n, m > transpose()
Take an object of type Matrix and returns its Transpose. 
Definition: matrix.h:161
 
size_t cols()
Definition: matrix.h:55
 
Data1 ** sub(Data1 **M1, Data1 **M2, int n)
Function to Subtract 2 square Matrices of size n. 
Definition: matrix.h:382
 
Data1 ** add(Data1 **M1, Data1 **M2, int n)
Function to Add 2 square Matrices of size n. 
Definition: matrix.h:365
 
T & ElementAt(size_t i, size_t j)
Function to write value to the (i,j)th position of the Matrix. 
Definition: matrix.h:243
 
size_t rows()
Definition: matrix.h:64
 
This class is for matrices of smaller size. The user can define a Matrix A of size m x n of Datatype ...
Definition: matrix.h:42
 
virtual ~Matrix()
Definition: matrix.h:50
 
Matrix(std::initializer_list< std::initializer_list< T >> my_list)
Overloaded constructor the initialize the Matrix from a 2D Initializer list. 
Definition: matrix.h:90
 
void Transpose(std::string path, std::string str_path)
This is a function to find the Transpose of a BigMatrix and stores it in a csv file. 
Definition: matrix.h:840
 
Data1 ** StrassenMultiply(Data1 **A, Data1 **B, int n)
The main Strassen's Algorithm function implemented using recursion. Takes in square Matrices A and B...
Definition: matrix.h:405
 
The main matrix.h namespace named MATOPS- MATrix OPerations. It contains 2 Matrix classes...
Definition: matrix.h:29