-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
98 lines (93 loc) · 2.99 KB
/
main.cpp
File metadata and controls
98 lines (93 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include "matrix.hpp"
template <typename T>
Matrix<T> parseMatrix(const std::string& str) {
std::istringstream iss(str);
std::vector<std::vector<T>> data;
std::string line;
while (std::getline(iss, line, ';')) {
std::istringstream lineStream(line);
std::vector<T> row;
T value;
while (lineStream >> value) {
row.push_back(value);
}
data.push_back(row);
}
int rows = data.size();
int cols = data[0].size();
Matrix<T> matrix(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
matrix.assignElement(i, j, data[i][j]);
}
}
return matrix;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <method> <matrix1> [<matrix2>]" << std::endl;
return 1;
}
std::string method = argv[1];
Matrix<double> matrix1 = parseMatrix<double>(argv[2]);
Matrix<double> matrix2;
if (argc == 4) {
matrix2 = parseMatrix<double>(argv[3]);
}
if (method == "add" && argc == 4) {
Matrix<double> result = matrix1 + matrix2;
result.print();
} else if (method == "subtract" && argc == 4) {
Matrix<double> result = matrix1 - matrix2;
result.print();
} else if (method == "dot" && argc == 4) {
Matrix<double> result = matrix1 * matrix2;
result.print();
} else if (method == "pow" && argc == 4) {
Matrix<double> result = matrix1.pow(matrix2[0][0]);
result.print();
} else if (method == "determinant") {
double result = matrix1.determinant();
std::cout << "Determinant: " << result << std::endl;
} else if (method == "inverse") {
Matrix<double> result = matrix1.inverse();
result.print();
} else if (method == "transpose") {
Matrix<double> result = matrix1.transpose();
result.print();
} else if (method == "ref") {
Matrix<double> result = matrix1.ref();
result.print();
} else if (method == "rref") {
Matrix<double> result = matrix1.rref();
result.print();
} else if (method == "eigen") {
Matrix<double>::Eigen result = matrix1.eigen();
result.vectorVec.print();
std::cout << "\n";
for (const auto& val : result.valueVec) {
std::cout << val << " ";
}
std::cout << std::endl;
} else if (method == "qr") {
Matrix<double>::Qr result = matrix1.qr();
result.orthogonal.print();
std::cout << "\n";
result.upper.print();
} else if (method == "plu") {
Matrix<double>::Plu result = matrix1.plu();
result.permutation.print();
std::cout << "\n";
result.lower.print();
std::cout << "\n";
result.upper.print();
} else {
std::cerr << "Unknown method or incorrect number of arguments" << std::endl;
return 1;
}
return 0;
}