Matrix  0.1.0
Easy-to-use Scientific Computing library in/for C++
matrix_basic.hpp
1 #ifndef _matrix_hpp_
2 #define _matrix_hpp_
3 
4 #include <algorithm>
5 #include <cassert>
6 #include <cmath>
7 #include <fstream>
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 
13 class Matrix {
14  private:
15  Matrix scalar_to_mat(double);
16  Matrix vector_to_mat(Matrix);
17 
18  public:
19  std::vector<std::vector<double>> double_mat;
20  std::vector<std::vector<std::string>> str_mat;
21  bool if_double = false;
22 
23  // Member functions
24  std::vector<std::vector<double>> get();
25  std::vector<double> get_row(int);
26  std::vector<double> get_col(int);
27  int col_length();
28  int row_length();
29  void print();
30  void head();
31  void tail();
32  void view(int, int);
33  void view(int, int, int, int);
34  Matrix slice(int, int, int, int);
35  Matrix T();
36  void to_double();
37  void to_string();
38 
39  // Overloaded Operators
41  Matrix operator+(double);
43  Matrix operator-(double);
45  Matrix operator*(double);
47  Matrix operator/(double);
48  double &operator()(int, int);
49  Matrix operator-();
50 };
51 
53 std::vector<std::vector<double>> Matrix::get() {
54  bool error = if_double;
55  if (!error)
56  assert(("The Matrix should be first converted to double using to_double() method", error));
57  return double_mat;
58 }
59 
61 std::vector<double> Matrix::get_row(int row) {
62  bool error = if_double;
63  if (!error)
64  assert(("The Matrix should be first converted to double using to_double() method", error));
65 
66  bool is_within_range = (double_mat.size() >= row);
67  if (!is_within_range) {
68  assert(("The row parameter is out of bounds of the matrix size.", is_within_range));
69  }
70 
71  std::vector<double> row_vec;
72  for (int i = 0; i < col_length(); i++)
73  row_vec.push_back(double_mat[row][i]);
74  return row_vec;
75 }
76 
78 std::vector<double> Matrix::get_col(int col) {
79  bool error = if_double;
80  if (!error)
81  assert(("The Matrix should be first converted to double using to_double() method", error));
82 
83  bool is_within_range = (double_mat[0].size() >= col);
84  if (!is_within_range) {
85  assert(("The col parameter is out of bounds of the matrix size.", is_within_range));
86  }
87 
88  std::vector<double> col_vec;
89  for (int i = 0; i < row_length(); i++)
90  col_vec.push_back(double_mat[i][col]);
91  return col_vec;
92 }
93 
95 int Matrix::col_length() { return str_mat[0].size(); }
96 
98 int Matrix::row_length() { return str_mat.size(); }
99 
102  for (int i = 0; i < str_mat.size(); i++) {
103  for (int j = 0; j < str_mat[i].size(); j++)
104  std::cout << str_mat[i][j] << "\t";
105  std::cout << std::endl;
106  }
107 }
108 
110 void Matrix::view(int row, int col) { std::cout << str_mat[row][col] << std::endl; }
111 
113 void Matrix::view(int row_start, int row_end, int col_start, int col_end) {
114  bool is_within_range = (str_mat.size() >= row_end) && (str_mat[0].size() >= col_end);
115  if (!is_within_range) {
116  assert(("The slicing parameters are out of bounds of the matrix size.", is_within_range));
117  }
118 
119  for (int i = row_start; i < row_end; i++) {
120  for (int j = col_start; j < col_end; j++)
121  std::cout << str_mat[i][j] << "\t";
122  std::cout << std::endl;
123  }
124 }
125 
127 void Matrix::head() {
128  int row = row_length() < 5 ? row_length() : 5;
129  for (int i = 0; i < row; i++) {
130  for (int j = 0; j < str_mat[i].size(); j++)
131  std::cout << str_mat[i][j] << "\t";
132  std::cout << std::endl;
133  }
134 }
135 
137 void Matrix::tail() {
138  int row = row_length() < 5 ? row_length() : 5;
139  for (int i = str_mat.size() - row; i < str_mat.size(); i++) {
140  for (int j = 0; j < str_mat[i].size(); j++)
141  std::cout << str_mat[i][j] << "\t";
142  std::cout << std::endl;
143  }
144 }
145 
150 Matrix Matrix::slice(int row_start, int row_end, int col_start, int col_end) {
151  bool is_within_range = (str_mat.size() >= row_end) && (str_mat[0].size() >= col_end);
152  if (!is_within_range) {
153  assert(("The slicing parameters are out of bounds of the matrix size.", is_within_range));
154  }
155  Matrix mat;
156  std::vector<std::string> row;
157  for (int i = row_start; i < row_end; i++) {
158  for (int j = col_start; j < col_end; j++)
159  row.push_back(str_mat[i][j]);
160  mat.str_mat.push_back(row);
161  row.clear();
162  }
163  if (if_double) {
164  mat.to_double();
165  }
166 
167  return mat;
168 }
169 
174  Matrix mat;
175  std::vector<std::string> row;
176 
177  for (int i = 0; i < col_length(); i++) {
178  for (int j = 0; j < row_length(); j++)
179  row.push_back(str_mat[j][i]);
180  mat.str_mat.push_back(row);
181  row.clear();
182  }
183  if (if_double) {
184  mat.to_double();
185  }
186 
187  return mat;
188 }
189 
192  std::vector<double> row;
193  double_mat.clear();
194  for (int i = 0; i < str_mat.size(); i++) {
195  for (int j = 0; j < str_mat[i].size(); j++)
196  row.push_back(std::stod(str_mat[i][j]));
197  double_mat.push_back(row);
198  row.clear();
199  }
200  if_double = true;
201 }
202 
205  std::vector<std::string> row;
206  str_mat.clear();
207  for (int i = 0; i < double_mat.size(); i++) {
208  for (int j = 0; j < double_mat[i].size(); j++)
209  row.push_back(std::to_string(double_mat[i][j]));
210  str_mat.push_back(row);
211  row.clear();
212  }
213  if_double = true;
214 }
215 
216 // Operator overloading functions
217 
219  bool error1 = ((if_double) && (mat.if_double));
220  if (!error1)
221  assert(("The Matrix objects should be first converted to double using to_double() method",
222  error1));
223 
224  if ((row_length() == mat.row_length()) && (col_length() == mat.col_length())) {
225  Matrix result;
226  std::vector<std::string> row;
227 
228  for (int i = 0; i < row_length(); i++) {
229  for (int j = 0; j < col_length(); j++)
230  row.push_back(std::to_string(double_mat[i][j] + mat.double_mat[i][j]));
231  result.str_mat.push_back(row);
232  row.clear();
233  }
234  result.to_double();
235  return result;
236  } else if ((row_length() == mat.row_length()) && (mat.col_length() == 1)) {
237  mat = vector_to_mat(mat);
238  Matrix result;
239  std::vector<std::string> row;
240 
241  for (int i = 0; i < row_length(); i++) {
242  for (int j = 0; j < col_length(); j++)
243  row.push_back(std::to_string(double_mat[i][j] + mat.double_mat[i][j]));
244  result.str_mat.push_back(row);
245  row.clear();
246  }
247  result.to_double();
248  return result;
249  } else if ((col_length() == mat.col_length()) && (mat.row_length() == 1)) {
250  mat = vector_to_mat(mat);
251  Matrix result;
252  std::vector<std::string> row;
253 
254  for (int i = 0; i < row_length(); i++) {
255  for (int j = 0; j < col_length(); j++)
256  row.push_back(std::to_string(double_mat[i][j] + mat.double_mat[i][j]));
257  result.str_mat.push_back(row);
258  row.clear();
259  }
260  result.to_double();
261  return result;
262  } else {
263  assert(("The Matrix objects should be of compatible dimensions", false));
264  }
265 }
266 
268  bool error = if_double;
269  if (!error)
270  assert(("The Matrix should be first converted to double using to_double() method", error));
271 
272  Matrix mat_val = scalar_to_mat(val);
273 
274  Matrix result;
275  std::vector<std::string> row;
276 
277  for (int i = 0; i < row_length(); i++) {
278  for (int j = 0; j < col_length(); j++)
279  row.push_back(std::to_string(double_mat[i][j] + mat_val.double_mat[i][j]));
280  result.str_mat.push_back(row);
281  row.clear();
282  }
283  result.to_double();
284  return result;
285 }
286 
288  bool error1 = ((if_double) && (mat.if_double));
289  if (!error1)
290  assert(("The Matrix objects should be first converted to double using to_double() method",
291  error1));
292 
293  if ((row_length() == mat.row_length()) && (col_length() == mat.col_length())) {
294  Matrix result;
295  std::vector<std::string> row;
296 
297  for (int i = 0; i < row_length(); i++) {
298  for (int j = 0; j < col_length(); j++)
299  row.push_back(std::to_string(double_mat[i][j] - mat.double_mat[i][j]));
300  result.str_mat.push_back(row);
301  row.clear();
302  }
303  result.to_double();
304  return result;
305  } else if ((row_length() == mat.row_length()) && (mat.col_length() == 1)) {
306  mat = vector_to_mat(mat);
307  Matrix result;
308  std::vector<std::string> row;
309 
310  for (int i = 0; i < row_length(); i++) {
311  for (int j = 0; j < col_length(); j++)
312  row.push_back(std::to_string(double_mat[i][j] - mat.double_mat[i][j]));
313  result.str_mat.push_back(row);
314  row.clear();
315  }
316  result.to_double();
317  return result;
318  } else if ((col_length() == mat.col_length()) && (mat.row_length() == 1)) {
319  mat = vector_to_mat(mat);
320  Matrix result;
321  std::vector<std::string> row;
322 
323  for (int i = 0; i < row_length(); i++) {
324  for (int j = 0; j < col_length(); j++)
325  row.push_back(std::to_string(double_mat[i][j] - mat.double_mat[i][j]));
326  result.str_mat.push_back(row);
327  row.clear();
328  }
329  result.to_double();
330  return result;
331  } else {
332  assert(("The Matrix objects should be of compatible dimensions", false));
333  }
334 }
335 
337  bool error = if_double;
338  if (!error)
339  assert(("The Matrix should be first converted to double using to_double() method", error));
340 
341  Matrix mat_val = scalar_to_mat(val);
342 
343  Matrix result;
344  std::vector<std::string> row;
345 
346  for (int i = 0; i < row_length(); i++) {
347  for (int j = 0; j < col_length(); j++)
348  row.push_back(std::to_string(double_mat[i][j] - mat_val.double_mat[i][j]));
349  result.str_mat.push_back(row);
350  row.clear();
351  }
352  result.to_double();
353  return result;
354 }
355 
357  bool error1 = ((if_double) && (mat.if_double));
358  if (!error1)
359  assert(("The Matrix objects should be first converted to double using to_double() method",
360  error1));
361 
362  if ((row_length() == mat.row_length()) && (col_length() == mat.col_length())) {
363  Matrix result;
364  std::vector<std::string> row;
365 
366  for (int i = 0; i < row_length(); i++) {
367  for (int j = 0; j < col_length(); j++)
368  row.push_back(std::to_string(double_mat[i][j] * mat.double_mat[i][j]));
369  result.str_mat.push_back(row);
370  row.clear();
371  }
372  result.to_double();
373  return result;
374  } else if ((row_length() == mat.row_length()) && (mat.col_length() == 1)) {
375  mat = vector_to_mat(mat);
376  Matrix result;
377  std::vector<std::string> row;
378 
379  for (int i = 0; i < row_length(); i++) {
380  for (int j = 0; j < col_length(); j++)
381  row.push_back(std::to_string(double_mat[i][j] * mat.double_mat[i][j]));
382  result.str_mat.push_back(row);
383  row.clear();
384  }
385  result.to_double();
386  return result;
387  } else if ((col_length() == mat.col_length()) && (mat.row_length() == 1)) {
388  mat = vector_to_mat(mat);
389  Matrix result;
390  std::vector<std::string> row;
391 
392  for (int i = 0; i < row_length(); i++) {
393  for (int j = 0; j < col_length(); j++)
394  row.push_back(std::to_string(double_mat[i][j] * mat.double_mat[i][j]));
395  result.str_mat.push_back(row);
396  row.clear();
397  }
398  result.to_double();
399  return result;
400  } else {
401  assert(("The Matrix objects should be of compatible dimensions", false));
402  }
403 }
404 
406  bool error = if_double;
407  if (!error)
408  assert(("The Matrix should be first converted to double using to_double() method", error));
409 
410  Matrix mat_val = scalar_to_mat(val);
411 
412  Matrix result;
413  std::vector<std::string> row;
414 
415  for (int i = 0; i < row_length(); i++) {
416  for (int j = 0; j < col_length(); j++)
417  row.push_back(std::to_string(double_mat[i][j] * mat_val.double_mat[i][j]));
418  result.str_mat.push_back(row);
419  row.clear();
420  }
421  result.to_double();
422  return result;
423 }
424 
426  bool error1 = ((if_double) && (mat.if_double));
427  if (!error1)
428  assert(("The Matrix objects should be first converted to double using to_double() method",
429  error1));
430 
431  if ((row_length() == mat.row_length()) && (col_length() == mat.col_length())) {
432  Matrix result;
433  std::vector<std::string> row;
434 
435  for (int i = 0; i < row_length(); i++) {
436  for (int j = 0; j < col_length(); j++) {
437  double val;
438  if (mat.double_mat[i][j] == 0)
439  val = double_mat[i][j];
440  else
441  val = double_mat[i][j] / mat.double_mat[i][j];
442  row.push_back(std::to_string(val));
443  }
444  result.str_mat.push_back(row);
445  row.clear();
446  }
447  result.to_double();
448  return result;
449  } else if ((row_length() == mat.row_length()) && (mat.col_length() == 1)) {
450  mat = vector_to_mat(mat);
451  Matrix result;
452  std::vector<std::string> row;
453 
454  for (int i = 0; i < row_length(); i++) {
455  for (int j = 0; j < col_length(); j++) {
456  double val;
457  if (mat.double_mat[i][j] == 0)
458  val = double_mat[i][j];
459  else
460  val = double_mat[i][j] / mat.double_mat[i][j];
461  row.push_back(std::to_string(val));
462  }
463  result.str_mat.push_back(row);
464  row.clear();
465  }
466  result.to_double();
467  return result;
468  } else if ((col_length() == mat.col_length()) && (mat.row_length() == 1)) {
469  mat = vector_to_mat(mat);
470  Matrix result;
471  std::vector<std::string> row;
472 
473  for (int i = 0; i < row_length(); i++) {
474  for (int j = 0; j < col_length(); j++) {
475  double val;
476  if (mat.double_mat[i][j] == 0)
477  val = double_mat[i][j];
478  else
479  val = double_mat[i][j] / mat.double_mat[i][j];
480  row.push_back(std::to_string(val));
481  }
482  result.str_mat.push_back(row);
483  row.clear();
484  }
485  result.to_double();
486  return result;
487  } else {
488  assert(("The Matrix objects should be of compatible dimensions", false));
489  }
490 }
491 
493  bool error = if_double;
494  if (!error)
495  assert(("The Matrix should be first converted to double using to_double() method", error));
496 
497  Matrix mat_val = scalar_to_mat(val);
498 
499  Matrix result;
500  std::vector<std::string> row;
501 
502  for (int i = 0; i < row_length(); i++) {
503  for (int j = 0; j < col_length(); j++) {
504  double val;
505  if (mat_val.double_mat[i][j] == 0)
506  val = double_mat[i][j];
507  else
508  val = double_mat[i][j] / mat_val.double_mat[i][j];
509  row.push_back(std::to_string(val));
510  }
511  result.str_mat.push_back(row);
512  row.clear();
513  }
514  result.to_double();
515  return result;
516 }
517 
518 double &Matrix::operator()(int row, int col) {
519  bool error1 = if_double;
520  if (!error1)
521  assert(("The Matrix should be first converted to double using to_double() method", error1));
522  bool error2 = (((row >= 0) && (row < row_length())) && ((col >= 0) && (col < col_length())));
523  if (!error2)
524  assert(("Index is out of range", false));
525 
526  to_string();
527  return double_mat[row][col];
528 }
529 
531  bool error1 = if_double;
532  if (!error1)
533  assert(("The Matrix should be first converted to double using to_double() method", error1));
534 
535  Matrix result;
536  std::vector<double> row;
537 
538  for (int i = 0; i < row_length(); i++) {
539  for (int j = 0; j < col_length(); j++)
540  row.push_back(-double_mat[i][j]);
541  result.double_mat.push_back(row);
542  row.clear();
543  }
544  result.to_string();
545  return result;
546 }
547 
548 // Helper methods
549 
550 Matrix Matrix::scalar_to_mat(double val) {
551  Matrix result;
552  std::vector<std::string> row(col_length(), std::to_string(val));
553  std::vector<std::vector<std::string>> mat(row_length(), row);
554  result.str_mat = mat;
555  result.to_double();
556  return result;
557 }
558 
559 Matrix Matrix::vector_to_mat(Matrix vec) {
560  Matrix result;
561  std::vector<std::string> row;
562  if (row_length() == vec.row_length()) {
563  for (int i = 0; i < row_length(); i++) {
564  row.assign(col_length(), vec.str_mat[i][0]);
565  result.str_mat.push_back(row);
566  row.clear();
567  }
568  } else if (col_length() == vec.col_length()) {
569  for (int i = 0; i < row_length(); i++) {
570  result.str_mat.push_back(vec.str_mat[0]);
571  }
572  }
573  result.to_double();
574  return result;
575 }
576 
577 #endif /* _matrix_hpp_ */
Matrix::slice
Matrix slice(int, int, int, int)
Method to slice a Matrix object The method will return a Matrix object whose dimensions will be (row_...
Definition: matrix_basic.hpp:150
Matrix::if_double
bool if_double
Definition: matrix_basic.hpp:21
Matrix::double_mat
std::vector< std::vector< double > > double_mat
Definition: matrix_basic.hpp:19
Matrix::operator()
double & operator()(int, int)
Definition: matrix_basic.hpp:518
Matrix::print
void print()
Method to print a Matrix object.
Definition: matrix_basic.hpp:101
Matrix::col_length
int col_length()
Method to return the number of columns.
Definition: matrix_basic.hpp:95
Matrix::operator*
Matrix operator*(Matrix)
Definition: matrix_basic.hpp:356
Matrix::operator-
Matrix operator-()
Definition: matrix_basic.hpp:530
Matrix::operator+
Matrix operator+(Matrix)
Definition: matrix_basic.hpp:218
Matrix::str_mat
std::vector< std::vector< std::string > > str_mat
Definition: matrix_basic.hpp:20
Matrix::head
void head()
Method to print first 5 rows of a Matrix object.
Definition: matrix_basic.hpp:127
Matrix::T
Matrix T()
Method to return the Tranpose of a Matrix The method will return a Matrix object whose dimensions wil...
Definition: matrix_basic.hpp:173
Matrix::to_double
void to_double()
Method convert the elements of a Matrix from std::string to double.
Definition: matrix_basic.hpp:191
Matrix::get_row
std::vector< double > get_row(int)
Method to return a row of the matrix in the form of a vector.
Definition: matrix_basic.hpp:61
Matrix::to_string
void to_string()
Method convert the elements of a Matrix from double to std::string.
Definition: matrix_basic.hpp:204
Matrix::get_col
std::vector< double > get_col(int)
Method to return a column of the matrix in the form of a vector.
Definition: matrix_basic.hpp:78
Matrix::view
void view(int, int)
Method to print a single cell (row, col) of a Matrix object.
Definition: matrix_basic.hpp:110
Matrix
Definition: matrix_basic.hpp:13
Matrix::get
std::vector< std::vector< double > > get()
Method to return the matrix in the form of vector.
Definition: matrix_basic.hpp:53
Matrix::tail
void tail()
Method to print last 5 rows of a Matrix object.
Definition: matrix_basic.hpp:137
Matrix::row_length
int row_length()
Method to return the number of rows.
Definition: matrix_basic.hpp:98
Matrix::operator/
Matrix operator/(Matrix)
Definition: matrix_basic.hpp:425