Matrix Library
A simple Matrix operation library to perform Matrix Multiplication and find the transpose of a Matrix.
MATOPS::BigMatrix< Data1 > Class Template Reference

This is the Class for handling Large Matrices. It takes in large Matrices stored as comma-separated values (CSV) files and perform both Multiplication (Strassan's Algorithm) and transpose. It contains all the important functions namely ,matmul function, StrassanMultiply function and other helper functions that are needed for smooth functioning of the library. This class also contains blocks of code that are gated by the SET_LEAF_SIZE flag, for example the set_LEAF_SIZE() function and the set_configerd_Leaf_size() function. When matrix.h is compiled with the SET_LEAF_SIZE flag raised the set_LEAF_SIZE() function is active and set_configerd_Leaf_size() function is inactive. And the vice-versa happens when matrix.h is compiled without the SET_LEAF_SIZE flag. More...

#include <matrix.h>

Public Member Functions

void Mat_print (std::string path)
 Function to print a Matrix from a .csv file. More...
 
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 O(n^3) solution, this prevents the StrassenMultiply recursion function from down to a leaf size/Matrix size of 1. Once we reach a square matrix array of size LEAF_SIZE x LEAF_SIZE or lesser, we perform the multiplication using the naive O(n^3) time complexity solution. More...
 
void set_configerd_Leaf_size ()
 When matrix.h is normally called i.e. without any SET_LEAF_SIZE flag, set_configerd_Leaf_size() function gets activated (set_LEAFT_SIZE() function gets deactivated). The set_configerd_Leaf_size() function searches for the configure.txt file (generated earlier by configure_lib.cpp during the configuration process) and sets the optimal value of LEAF_SIZE. This function is called by the MATOPS::BigMatrix<Data1>::matmul before starting the multiplication process. More...
 
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.csv and B.csv respectively and store the result in C.csv file. Before beginning the multiplication process it calls the MATOPS::BigMatrix<Data1>::set_configerd_Leaf_size() function to parses the configure.txt (generated by configure_lib.cpp during the configuration process) and sets the optimal LEAF_SIZE value. More...
 
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. More...
 
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 transpose). More...
 

Private Member Functions

Data1 ** Init_matrix (int n)
 Function to dynamically allocate/initialize an n x n matrix in the memory. More...
 
Data1 ** add (Data1 **M1, Data1 **M2, int n)
 Function to Add 2 square Matrices of size n. More...
 
Data1 ** sub (Data1 **M1, Data1 **M2, int n)
 Function to Subtract 2 square Matrices of size n. More...
 
Data1 ** StrassenMultiply (Data1 **A, Data1 **B, int n)
 The main Strassen's Algorithm function implemented using recursion. Takes in square Matrices A and B. More...
 
void print_Mat (Data1 **C, int m, int n)
 This function is called from within the MATOPS::BigMatrix<Data1>::matmul function when print == True. More...
 
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 MATOPS::BigMatrix< Data1 >::Transpose to load the BigMatrix's to be multiplied or Transposed. It throws an error if path is invalid or CSV doesn't exist. More...
 

Private Attributes

int LEAF_SIZE
 

Detailed Description

template<typename Data1>
class MATOPS::BigMatrix< Data1 >

This is the Class for handling Large Matrices. It takes in large Matrices stored as comma-separated values (CSV) files and perform both Multiplication (Strassan's Algorithm) and transpose. It contains all the important functions namely ,matmul function, StrassanMultiply function and other helper functions that are needed for smooth functioning of the library. This class also contains blocks of code that are gated by the SET_LEAF_SIZE flag, for example the set_LEAF_SIZE() function and the set_configerd_Leaf_size() function. When matrix.h is compiled with the SET_LEAF_SIZE flag raised the set_LEAF_SIZE() function is active and set_configerd_Leaf_size() function is inactive. And the vice-versa happens when matrix.h is compiled without the SET_LEAF_SIZE flag.

Template Parameters
Data1= Datatype of the BigMatrix. Eg. int, float, double etc.

Member Function Documentation

◆ add()

template<typename Data1>
Data1** MATOPS::BigMatrix< Data1 >::add ( Data1 **  M1,
Data1 **  M2,
int  n 
)
inlineprivate

Function to Add 2 square Matrices of size n.

Parameters
M1= Pointer pointing to BigMatrix M1 loaded into memory
M2= Pointer pointing to BigMatrix M2 loaded into memory
n= Size of the Matrices
Returns
Returns a pointer pointing to a memory location containing the sum of M1 and M2
366  {
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];
371  return temp;
372  }
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345

◆ Init_matrix()

template<typename Data1>
Data1** MATOPS::BigMatrix< Data1 >::Init_matrix ( int  n)
inlineprivate

Function to dynamically allocate/initialize an n x n matrix in the memory.

Parameters
n= No. of rows and cols of the BigMatrix.
Returns
Returns a pointer pointing to a memory location containing 0 matrix of size n x n.
346  { // Calloc chunk of Memory
347 
348  Data1** M;
349  M=(Data1 **)calloc(n,sizeof(Data1*));
350  for(int i=0;i<n;i++)
351  {
352  M[i]=(Data1*)calloc(n,sizeof(Data1));
353  }
354  return M;
355  }

◆ load_CSV()

template<typename Data1>
std::vector<std::vector<Data1> > MATOPS::BigMatrix< Data1 >::load_CSV ( const std::string &  path)
inlineprivate

Function to load CSV file. This function is internally called by MATOPS::BigMatrix<Data1>::matmul and MATOPS::BigMatrix< Data1 >::Transpose to load the BigMatrix's to be multiplied or Transposed. It throws an error if path is invalid or CSV doesn't exist.

Parameters
path="path to CSV file i.e. to be loaded"
Returns
A pointer to the Matrix loaded in memory.
616  {
617  std::ifstream indata;
618  indata.open(path);
619 
620  if(indata.fail()) // Check if File exists
621  {
622  std::cerr<<"File path: '"<<path<<"' doesn't exist\n";
623  exit(0);
624  }
625 
626  std::vector<std::vector<Data1>> dataList;
627  std::string line = "";
628 
629  while(getline(indata,line))
630  {
631  std::stringstream lineStream(line);
632  std::string cell;
633  std::vector<Data1> temp;
634  while (std::getline(lineStream,cell,','))
635  {
636  temp.push_back(convert_to<Data1>(cell)); // convert from string to Data1 and store it in an 2D array.
637  }
638  dataList.push_back(temp);
639 
640  }
641  indata.close(); // Close file
642  return dataList; // Return the 2D vector
643  }

◆ Mat_print()

template<typename Data1>
void MATOPS::BigMatrix< Data1 >::Mat_print ( std::string  path)
inline

Function to print a Matrix from a .csv file.

Parameters
path= "path to .csv i.e. to be printed"
651  {
652  std::vector<std::vector<Data1>> MAT= load_CSV(path);
653 
654  for(std::vector<Data1> a: MAT)
655  {
656  for(Data1 i: a)
657  {
658  std::cout<< i <<" ";
659  }
660  std::cout<<'\n';
661  }
662  }
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

◆ matmul()

template<typename Data1>
void MATOPS::BigMatrix< Data1 >::matmul ( std::string  file_1,
std::string  file_2,
std::string  path,
bool  print = false 
)
inline

This is the BigMatrix multiplication Function that multiplies two matrices A and B stored in A.csv and B.csv respectively and store the result in C.csv file. Before beginning the multiplication process it calls the MATOPS::BigMatrix<Data1>::set_configerd_Leaf_size() function to parses the configure.txt (generated by configure_lib.cpp during the configuration process) and sets the optimal LEAF_SIZE value.

Parameters
file_1= "path to A.csv"
file_2= "path to B.csv"
path= path to a store file (no need to predefine C.csv file in the directory, it gets generated automatically.)
print= True, To see all the Matrices i.e. A,B and C in the output terminal/stdio.

Overall Working: When matmul is called, it first sets the the optimal LEAF_SIZE value. It then loads the two csv files from "path to A.csv" and "path to B.csv" directories into two 2D vectors namely MAT_1 and MAT_2 of type Data1 using MATOPS::BigMatrix<Data1>::load_CSV function. The dimensions of both the matrices are determined from MAT_1 and MAT_2 say m_1,n_1 ad m_2,n_2 respectively. If the inner dimensions of both MAT_1 and MAT_2 don't match an error is thrown and the program is exited. If the inner dimensions match then we proceed for Multiplication. Before calling the MATOPS::BigMatrix<Data1>::StrassenMultiply fuction, we pad the matrices with zeros to make both MAT_1 and MAT_2 square matrices of equal dimension, dim_n. Where dim_n is a power of 2 i.e. greater than or equal to max(m_1,n_1,n_2) or max(m_1,m_2,n_2) (since n_1=m_2). We callocate (by calling MATOPS::BigMatrix<Data1>::Init_matrix(dim_n) two chunk of memory of size dim_n x dim_n pointed by pointers A and B. Finally we build the Zero padded square Matrices by copying the contents from from MAT_1 and MAT_2 into the memory (pointed by A and B) respectively. These Memory addresses and dim_n are then passed on to the MATOPS::BigMatrix<Data1>::StrassenMultiply function to evaluate the product of A and B. The MATOPS::BigMatrix<Data1>::StrassenMultiply() func returns a pointer pointing to a location in the memory where the answer is stored. This memory address is then passed to the MATOPS::BigMatrix<Data1>::store_csv() along with the resultant matrix dimension (i.e. m_1 x n_2) and the storage destination path to store the final result in a csv file.

Finally all the callocated memories are freed up using the free command.

733  {
734 
735  #ifndef SET_LEAF_SIZE
736  set_configerd_Leaf_size(); // Set the optimal LEAF_SIZE by parsing the configuration file genrated by configure_lib.cpp.
737  #endif
738  // std::cout<<"Current LEAF_SIZE value: "<<LEAF_SIZE<<'\n'; // Just a check to see what LEAF_SIZE value is being used
739 
740  // Parse the CSV files and get the Matrices to be multiplied
741 
742  std::vector<std::vector<Data1>> MAT_1= load_CSV(file_1);
743  std::vector<std::vector<Data1>> MAT_2= load_CSV(file_2);
744 
745  // Get the dimensions of both the Matrices.
746  int m_1=MAT_1.size(), n_1=MAT_1[0].size(); // Matrix 1 M,N
747  int m_2=MAT_2.size(), n_2=MAT_2[0].size(); // Matrix 2 M,N
748 
749  try{
750  if(n_1 != m_2) // Check if inner dimensions of the Matrices Match. If not then Throw error.
751  {
752  throw "Matrix Inner Dimensions don't match !!! \n";
753  }
754  }catch (const char* msg)
755  {
756  std::cerr<<msg<<'\n';
757  exit(0);
758  }
759 
760  int max_n= std::max(std::max(m_1,n_1),n_2); // Find the max of all the Matrix dimensions, to find the next highest power of 2.
761  int dim_n=1;
762 
763  // Find the next highest power of 2 using left shift operator (dim_n = Next highest power of 2).
764  // This will be used to pad zeros to both matrices A and B, and ultimately given as input to StrassenMultiply() function.
765  while(dim_n<max_n)
766  {
767  dim_n=dim_n<<1;
768  }
769 
770  // Initialize 3 pointers to point at Matrices A and B, and to store the Result of AxB into C.
771  Data1 ** A;
772  Data1 ** B;
773  Data1 ** C;
774 
775  A=Init_matrix(dim_n); // Allocate the memory for Matrix A of size dim_n
776  B=Init_matrix(dim_n); // Allocate the memory for Matrix B of size dim_n
777 
778  // Build the Zero padded square Matrices A and B by copying contents from from MAT_1 and MAT_2 into A and B respectively.
779  for(int k=0;k<m_1;k++)
780  {
781  for(int l=0;l<n_1;l++)
782  {
783  A[k][l]= MAT_1[k][l];
784  }
785  }
786 
787  for(int k=0;k<m_2;k++)
788  {
789  for(int l=0;l<n_2;l++)
790  {
791  B[k][l]= MAT_2[k][l];
792 
793  }
794 
795  }
796 
797  //Call Strassen's Algorithm function to get A x B and store in C.
798  C=StrassenMultiply(A, B, dim_n);
799 
800  if(print==true) // if print is true then print A, B and the result A.
801  {
802  std::cout<<"A: \n";
803  print_Mat(A, m_1, n_1); //Call the print_Mat function to print the Matrix
804 
805  std::cout<<"\nB: \n";
806  print_Mat(B, m_2, n_2); //Call the print_Mat function to print the Matrix
807 
808  std::cout<<"\nANSWER: \n"; //Call the print_Mat function to print the Matrix
809  print_Mat(C, m_1, n_2);
810 
811  }
812 
813  // Store the Result C into a CSV file, whose location is given by "path"
814  store_csv<Data1>(C, m_1,n_2,path);
815 
816  // Free The memory before quitting
817  for(int i=0;i<dim_n;i++)
818  {
819  free(A[i]);
820  free(B[i]);
821  free(C[i]);
822  }
823 
824  free(A);
825  free(B);
826  free(C);
827 
828 
829  } // matmul function ends here
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
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345
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 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
Data1 ** StrassenMultiply(Data1 **A, Data1 **B, int n)
The main Strassen&#39;s Algorithm function implemented using recursion. Takes in square Matrices A and B...
Definition: matrix.h:405

◆ print_Mat()

template<typename Data1>
void MATOPS::BigMatrix< Data1 >::print_Mat ( Data1 **  C,
int  m,
int  n 
)
inlineprivate

This function is called from within the MATOPS::BigMatrix<Data1>::matmul function when print == True.

Parameters
CPointer to the memory where the array is stored.
mNo of rows of the Matrix
nNo of Columns of the Matrix
597  {
598  for(int i=0;i<m;i++)
599  {
600  for(int j=0;j<n;j++)
601  {
602  std::cout<<C[i][j]<<" ";
603  }
604  std::cout<<'\n';
605  }
606  }

◆ set_configerd_Leaf_size()

template<typename Data1>
void MATOPS::BigMatrix< Data1 >::set_configerd_Leaf_size ( )
inline

When matrix.h is normally called i.e. without any SET_LEAF_SIZE flag, set_configerd_Leaf_size() function gets activated (set_LEAFT_SIZE() function gets deactivated). The set_configerd_Leaf_size() function searches for the configure.txt file (generated earlier by configure_lib.cpp during the configuration process) and sets the optimal value of LEAF_SIZE. This function is called by the MATOPS::BigMatrix<Data1>::matmul before starting the multiplication process.

697  {
698  std::ifstream indata;
699  indata.open("configure.txt");
700  std::string str="";
701  getline(indata,str);
702  indata.close();
703  LEAF_SIZE=std::stoi(str);
704  }
int LEAF_SIZE
Definition: matrix.h:332

◆ set_LEAF_SIZE()

template<typename Data1>
void MATOPS::BigMatrix< Data1 >::set_LEAF_SIZE ( int  leaf_size)
inline

This Function sets the LEAF_SIZE i.e. the array size at which we shift from Strassan's Algo to normal O(n^3) solution, this prevents the StrassenMultiply recursion function from down to a leaf size/Matrix size of 1. Once we reach a square matrix array of size LEAF_SIZE x LEAF_SIZE or lesser, we perform the multiplication using the naive O(n^3) time complexity solution.

A very high value of LEAF_SIZE leads to lesser recursion calls but ends up giving more weightage to the O(n^3) solution, thus suffer high execution time.

On the other hand a very low LEAF_SIZE value leads to higher number of resursion calls and gives lesser weightage to the O(n^3) solution, which again leads to high execution time.

Hence an optimal LEAF_SIZE value has to be found that gives the minimum execution time by finding a trade off between the O(n^3) solution and recurssion calls. This indirectly implies that the value of LEAF_SIZE vary from one machine to another, thus we need to experimentally find out this value for each machine and configure the matrix.h library accordingly. This experimentation is done by the configure_lib.cpp, which raises the SET_LEAF_SIZE flag (thus activating the set_LEAF_SIZE() function) during execution, as a result of which the "matrix.h" library is called (in its configuration mode) by configure_lib.cpp. This allows configure_lib.cpp to manipulate the LEAF_SIZE (private variable) of the matrix.h file. Hence configure.cpp can test the hardware for different values of LEAF_SIZE and pick its optimal (i.e. the value that gives the lowest execution time) value and finally store the optimal value in the configure.txt file.

686  {
687  LEAF_SIZE=leaf_size;
688  }
int LEAF_SIZE
Definition: matrix.h:332

◆ StrassenMultiply()

template<typename Data1>
Data1** MATOPS::BigMatrix< Data1 >::StrassenMultiply ( Data1 **  A,
Data1 **  B,
int  n 
)
inlineprivate

The main Strassen's Algorithm function implemented using recursion. Takes in square Matrices A and B.

Parameters
A= BigMatrix A
B= BigMatrix B
n= Size of both A and B
Returns
Returns a square matrix i.e. the Multiplication result of A and B.

The LEAF_SIZE is set by the MATOPS::BigMatrix<Data1>::set_configerd_Leaf_size() function from witin the MATOPS::BigMatrix<Data1>::matmul() function. The input Matrices A and B are both broken down into 4 blocks, these matrices are used to calculate the 7 Strassen's coeffcient matrices. In order to calculate the Strassen's Coefficent the StrassenMultiply function is recurssively called. Once the matrix sizes becomes equal to or less than LEAF_SIZE, we hit the base condition and perform the Matrix multiplication using the O(n^3) solution. Finally all the callocated memory (allocated by Init_matrix()) is freed up.

406  {
407  /* Naive Strassan's Algorithm (more time consuming)
408  if(n==1)
409  {
410  Data1** C=Init_matrix(1);
411  C[0][0]=A[0][0]*B[0][0];
412  return C;
413  }*/
414 
415  // Once array size of LEAF_SIZE x LEAF_SIZE or lesser is reached we switch to the O(n^3) Matrix Multiplication solution, since after this
416  // stage the recursion calls become a burden to the whole algorithm and we end up getting high execution time. The LEAF_SIZE is set by the
417  // MATOPS::BigMatrix<Data1>::set_configerd_Leaf_size() function from witin the MATOPS::BigMatrix<Data1>::matmul() function.
418  if(n<=LEAF_SIZE)
419  {
420  Data1** C=Init_matrix(n);
421  for(int i=0;i<n;i++)
422  {
423  for(int j=0;j<n;j++)
424  {
425  for(int k=0;k<n;k++)
426  {
427  C[i][j]+=A[i][k]*B[k][j];
428  }
429  }
430  }
431  return C;
432  }
433 
434  Data1** C = Init_matrix(n); // Initialize an nxn matrix to store
435  int k = n/2;
436 
437  // Initialize the Block Matrices
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);
446 
447  // Build the block Matrices
448  for(int i=0;i<k;i++)
449  {
450  for(int j=0;j<k;j++)
451  {
452  A11[i][j] = A[i][j];
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];
456  B11[i][j] = B[i][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];
460  }
461  }
462  // Determine the Strassen's Coefficients
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);
467 
468 
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);
473 
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);
480 
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);
484 
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);
489 
490 
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);
495 
496  // Building the returning C Matrix
497 
498  for(int i=0; i<k; i++)
499  {
500  for(int j=0; j<k; j++)
501  {
502  C[i][j] = C11[i][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];
506  }
507  }
508 
509  // Free up all calloced memory.
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]);
515 
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]);
522 
523  free(TEMP_P5_P4[i]);
524  free(TEMP_P5_P4_P6[i]);
525  free(TEMP_P5_P1[i]);
526  free(TEMP_P5_P1_P3[i]);
527 
528  free(A11[i]);
529  free(A12[i]);
530  free(A21[i]);
531  free(A22[i]);
532  free(B11[i]);
533  free(B12[i]);
534  free(B21[i]);
535  free(B22[i]);
536  free(P1[i]);
537  free(P2[i]);
538  free(P3[i]);
539  free(P4[i]);
540  free(P5[i]);
541  free(P6[i]);
542  free(P7[i]);
543  free(C11[i]);
544  free(C12[i]);
545  free(C21[i]);
546  free(C22[i]);
547  }
548 
549  free(TEMP_B12_B22);
550  free(TEMP_A11_A12);
551  free(TEMP_A21_A22);
552  free(TEMP_B21_B11);
553 
554  free(TEMP_A11_A22);
555  free(TEMP_B11_B22);
556  free(TEMP_A12_A22);
557  free(TEMP_B21_B22);
558  free(TEMP_A11_A21);
559  free(TEMP_B11_B12);
560 
561  free(TEMP_P5_P4);
562  free(TEMP_P5_P4_P6);
563  free(TEMP_P5_P1);
564  free(TEMP_P5_P1_P3);
565 
566  free(A11);
567  free(A12);
568  free(A21);
569  free(A22);
570  free(B11);
571  free(B12);
572  free(B21);
573  free(B22);
574  free(P1);
575  free(P2);
576  free(P3);
577  free(P4);
578  free(P5);
579  free(P6);
580  free(P7);
581  free(C11);
582  free(C12);
583  free(C21);
584  free(C22);
585 
586  return C;
587  }
int LEAF_SIZE
Definition: matrix.h:332
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345
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
Data1 ** StrassenMultiply(Data1 **A, Data1 **B, int n)
The main Strassen&#39;s Algorithm function implemented using recursion. Takes in square Matrices A and B...
Definition: matrix.h:405

◆ sub()

template<typename Data1>
Data1** MATOPS::BigMatrix< Data1 >::sub ( Data1 **  M1,
Data1 **  M2,
int  n 
)
inlineprivate

Function to Subtract 2 square Matrices of size n.

Parameters
M1= Pointer pointing to BigMatrix M1 loaded into memory
M2= Pointer pointing to BigMatrix M2 loaded into memory
n= Size of the Matrices
Returns
Returns a pointer pointing to a memory location containing the difference of M1 and M2
383  {
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];
388  return temp;
389  }
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345

◆ Transpose() [1/2]

template<typename Data1>
void MATOPS::BigMatrix< Data1 >::Transpose ( std::string  path,
std::string  str_path 
)
inline

This is a function to find the Transpose of a BigMatrix and stores it in a csv file.

Parameters
path= "path/to/A.csv"
str_path= path to store the Transpose of BigMatrix A.
841  {
842  std::vector<std::vector<Data1>> MAT= load_CSV(path); // Load the Matrix from CSV file
843 
844  Data1** A= (Data1 **)calloc(MAT[0].size(),sizeof(Data1*));
845 
846  for(int i=0;i<MAT[0].size();i++)
847  {
848  A[i]=(Data1*)calloc(MAT.size(),sizeof(Data1));
849  }
850 
851  for(int i=0;i<MAT.size();i++)
852  {
853  for(int j=0;j<MAT[0].size();j++)
854  {
855  A[j][i]=MAT[i][j];
856  }
857  }
858 
859  store_csv<Data1>(A,MAT[0].size(),MAT.size(),str_path);
860  for(int i=0;i<MAT[0].size();i++)
861  {
862  free(A[i]);
863  }
864  free(A);
865  }
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

◆ Transpose() [2/2]

template<typename Data1>
void MATOPS::BigMatrix< Data1 >::Transpose ( std::string  path)
inline

This is a function to find the Transpose of a BigMatrix and stores it in the same csv file (In-palce transpose).

Parameters
path= "path/to/A.csv"
872  {
873  std::vector<std::vector<Data1>> MAT= load_CSV(path); // Load the Matrix from CSV file
874 
875  Data1** A= (Data1 **)calloc(MAT[0].size(),sizeof(Data1*));
876 
877  for(int i=0;i<MAT[0].size();i++)
878  {
879  A[i]=(Data1*)calloc(MAT.size(),sizeof(Data1));
880  }
881 
882  for(int i=0;i<MAT.size();i++)
883  {
884  for(int j=0;j<MAT[0].size();j++)
885  {
886  A[j][i]=MAT[i][j];
887  }
888  }
889 
890  store_csv<Data1>(A,MAT[0].size(),MAT.size(),path);
891  store_csv<Data1>(A,MAT[0].size(),MAT.size(),path);
892 
893  for(int i=0;i<MAT[0].size();i++)
894  {
895  free(A[i]);
896  }
897  free(A);
898 
899  }
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

Member Data Documentation

◆ LEAF_SIZE

template<typename Data1>
int MATOPS::BigMatrix< Data1 >::LEAF_SIZE
private

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