1 #ifndef _matrix_operations_hpp_
2 #define _matrix_operations_hpp_
4 #include <matrix_basic.hpp>
15 Matrix init(std::vector<std::vector<std::string>>);
46 std::ifstream file(filename);
47 std::string line, cell;
48 std::vector<std::string> cells;
50 while (std::getline(file, line)) {
51 std::stringstream ss(line);
52 while (std::getline(ss, cell, delim)) {
53 cells.push_back(cell);
80 if (dim ==
"column") {
82 assert((
"The Matrix objects should be of compatible dimensions",
false));
87 }
else if (dim ==
"row") {
89 assert((
"The Matrix objects should be of compatible dimensions",
false));
93 assert((
"Concatenate dimension wrong",
false));
105 assert((
"The Matrix should be first converted to double using to_double() method", error));
108 assert((
"The Matrix objects should be of compatible dimensions",
false));
112 std::vector<double> row;
118 for (
unsigned long int i = 0; i < mat1.
row_length(); i++) {
119 for (
unsigned long int j = 0; j < mat2.
col_length(); j++) {
120 for (
unsigned long int k = 0; k < mat2.
row_length(); k++) {
132 std::vector<std::string> vec(col, std::to_string(0));
133 std::vector<std::vector<std::string>> mat(row, vec);
142 std::vector<std::string> vec(col, std::to_string(1));
143 std::vector<std::vector<std::string>> mat(row, vec);
152 for (
int i = 0; i < size; i++) {
163 assert((
"The Matrix should be first converted to double using to_double() method", error));
166 assert((
"The Matrix must be a square matrix",
false));
175 for (
int f = 0; f < n; f++) {
176 temp = cofactor(mat, temp, 0, f);
188 assert((
"The Matrix should be first converted to double using to_double() method", error));
191 assert((
"The Matrix must be a square matrix",
false));
194 assert((
"The Matrix is singular",
false));
196 Matrix adj = adjoint(mat);
197 Matrix result = adj / det;
205 assert((
"The Matrix should be first converted to double using to_double() method", error));
208 if (dim ==
"column") {
212 result(0, j) = result(0, j) + mat(i, j);
216 }
else if (dim ==
"row") {
220 result(j, 0) = result(j, 0) + mat(j, i);
225 assert((
"Second parameter 'dimension' wrong",
false));
234 assert((
"The Matrix should be first converted to double using to_double() method", error));
237 if (dim ==
"column") {
239 result =
sum(mat, dim) / n;
240 }
else if (dim ==
"row") {
242 result =
sum(mat, dim) / n;
244 assert((
"Second parameter 'dimension' wrong",
false));
253 assert((
"The Matrix should be first converted to double using to_double() method", error));
256 if (dim ==
"column") {
260 result =
sum((temp * temp), dim) / n;
261 }
else if (dim ==
"row") {
265 result =
sum((temp * temp), dim) / n;
267 assert((
"Second parameter 'dimension' wrong",
false));
276 assert((
"The Matrix should be first converted to double using to_double() method", error));
278 if (dim ==
"column") {
279 std::vector<double> min_indices;
281 std::vector<double> vec = mat.
get_col(i);
282 min_indices.push_back(*std::min_element(vec.begin(), vec.end()));
284 std::vector<std::vector<double>> res;
285 res.push_back(min_indices);
286 return matrix.
init(res);
288 }
else if (dim ==
"row") {
289 std::vector<double> temp;
290 std::vector<std::vector<double>> min_indices;
292 std::vector<double> vec = mat.
get_row(i);
293 temp.push_back(*std::min_element(vec.begin(), vec.end()));
294 min_indices.push_back(temp);
297 return matrix.
init(min_indices);
300 assert((
"Second parameter 'dimension' wrong",
false));
308 assert((
"The Matrix should be first converted to double using to_double() method", error));
310 if (dim ==
"column") {
311 std::vector<double> max_indices;
313 std::vector<double> vec = mat.
get_col(i);
314 max_indices.push_back(*std::max_element(vec.begin(), vec.end()));
316 std::vector<std::vector<double>> res;
317 res.push_back(max_indices);
318 return matrix.
init(res);
320 }
else if (dim ==
"row") {
321 std::vector<double> temp;
322 std::vector<std::vector<double>> max_indices;
324 std::vector<double> vec = mat.
get_row(i);
325 temp.push_back(*std::max_element(vec.begin(), vec.end()));
326 max_indices.push_back(temp);
329 return matrix.
init(max_indices);
332 assert((
"Second parameter 'dimension' wrong",
false));
340 assert((
"The Matrix should be first converted to double using to_double() method", error));
342 if (dim ==
"column") {
343 std::vector<double> min_indices;
345 std::vector<double> vec = mat.
get_col(i);
346 min_indices.push_back(std::min_element(vec.begin(), vec.end()) - vec.begin());
348 std::vector<std::vector<double>> res;
349 res.push_back(min_indices);
350 return matrix.
init(res);
352 }
else if (dim ==
"row") {
353 std::vector<double> temp;
354 std::vector<std::vector<double>> min_indices;
356 std::vector<double> vec = mat.
get_row(i);
357 temp.push_back(std::min_element(vec.begin(), vec.end()) - vec.begin());
358 min_indices.push_back(temp);
361 return matrix.
init(min_indices);
364 assert((
"Second parameter 'dimension' wrong",
false));
372 assert((
"The Matrix should be first converted to double using to_double() method", error));
374 if (dim ==
"column") {
375 std::vector<double> max_indices;
377 std::vector<double> vec = mat.
get_col(i);
378 max_indices.push_back(std::max_element(vec.begin(), vec.end()) - vec.begin());
380 std::vector<std::vector<double>> res;
381 res.push_back(max_indices);
382 return matrix.
init(res);
384 }
else if (dim ==
"row") {
385 std::vector<double> temp;
386 std::vector<std::vector<double>> max_indices;
388 std::vector<double> vec = mat.
get_row(i);
389 temp.push_back(std::max_element(vec.begin(), vec.end()) - vec.begin());
390 max_indices.push_back(temp);
393 return matrix.
init(max_indices);
396 assert((
"Second parameter 'dimension' wrong",
false));
403 assert((
"The Matrix should be first converted to double using to_double() method", error));
405 std::vector<double> row;
406 std::vector<std::vector<double>> res;
409 row.push_back(std::sqrt(mat.
double_mat[i][j]));
414 return matrix.
init(res);
420 assert((
"The Matrix objects should be first converted to double using to_double() method",
424 std::vector<double> row;
425 std::vector<std::vector<double>> res;
432 return matrix.
init(res);
435 mat2 = vector_to_mat(mat1, mat2);
437 std::vector<double> row;
438 std::vector<std::vector<double>> res;
445 return matrix.
init(res);
447 assert((
"The Matrix objects should be of compatible dimensions",
false));
454 assert((
"The Matrix should be first converted to double using to_double() method", error));
456 Matrix mat_val = scalar_to_mat(mat, val);
458 std::vector<double> row;
459 std::vector<std::vector<double>> res;
466 return matrix.
init(res);
476 if (!is_compatible) {
477 assert((
"The Matrix objects should be of same dimensions", is_compatible));
479 std::vector<std::vector<double>> res;
499 }
else if (dim ==
"column") {
509 assert((
"Second parameter 'dimension' wrong",
false));
516 assert((
"The Matrix should be first converted to double using to_double() method", error1));
518 std::vector<double> row;
519 std::vector<std::vector<double>> res;
523 row.push_back(std::exp(mat.
double_mat[i][j]));
535 assert((
"The Matrix should be first converted to double using to_double() method", error1));
537 std::vector<double> row;
538 std::vector<std::vector<double>> res;
542 row.push_back(std::log(mat.
double_mat[i][j]));
554 assert((
"The Matrix should be first converted to double using to_double() method", error1));
556 std::vector<double> row;
557 std::vector<std::vector<double>> res;
561 row.push_back(std::abs(mat.
double_mat[i][j]));
573 assert((
"The Matrix should be first converted to double using to_double() method", error1));
575 std::vector<double> row;
576 std::vector<std::vector<double>> res;
593 for (
int row = 0; row < mat.
row_length(); row++) {
594 for (
int col = 0; col < mat.
col_length(); col++) {
595 if (row != p && col != q) {
622 temp = cofactor(mat, temp, i, j);
623 sign = ((i + j) % 2 == 0) ? 1 : -1;
631 Matrix MatrixOp::scalar_to_mat(
Matrix mat,
double val) {
632 std::vector<std::string> row(mat.
col_length(), std::to_string(val));
633 std::vector<std::vector<std::string>> res(mat.
row_length(), row);
639 std::vector<std::string> row;
640 std::vector<std::vector<std::string>> res;