-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.cpp
More file actions
72 lines (66 loc) · 2.04 KB
/
solve.cpp
File metadata and controls
72 lines (66 loc) · 2.04 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
#include <fstream>
#include <iostream>
#include <string>
#include "matrix"
using namespace std;
matrix solve_conglomerate(matrix const& block) {
size_t rows = block.rows;
size_t columns = block.columns;
if (rows == columns)
return block.inverse();
if (rows > columns)
return solve_conglomerate(block.transposed()).transpose();
matrix system = matrix(rows, rows);
for (size_t idx = 0; idx < rows; ++idx)
for (size_t jdx = 0; jdx < rows; ++jdx)
system(idx, jdx) = block(idx, jdx);
size_t width = columns - rows;
matrix evaluation = matrix(rows, width);
for (size_t idx = 0; idx < rows; ++idx)
for (size_t jdx = 0; jdx < width; ++jdx)
evaluation(idx, jdx) = block(idx, rows + jdx);
return system.solve(evaluation);
}
int main(int argc, char** argv) {
try {
if (argc < 2 || argc > 3) {
cout << "Conglomerate Matrix Solver" << endl;
cout << "Usage: " << argv[0] << " INFILE OUTFILE" << endl;
cout << " Note:" << endl;
cout << "Read from standard input if INFILE is set to `stdin` (or "
"unspecified)"
<< endl;
cout << "Written to standard output if OUTFILE is set to `stdout` (or "
"unspecified)"
<< endl;
cout << " Example: solve 2x + 5y = 41, 7x + 11y = 98" << endl << endl;
cout << argv[0] << endl;
cout << "2 5 41 [ENTER]" << endl;
cout << "7 11 98 [ENTER]" << endl;
cout << "[ENTER]" << endl;
cout << "3 <- x" << endl;
cout << "7 <- y" << endl << endl;
}
string infile = (argc > 1) ? argv[1] : "stdin";
ifstream in;
istream* pin = ∈
if (infile == "stdin")
pin = &cin;
else
in.open(infile);
string outfile = (argc > 2) ? argv[2] : "stdout";
ofstream out;
ostream* pout = &out;
if (outfile == "stdout")
pout = &cout;
else
out.open(outfile);
matrix input;
*pin >> input;
*pout << solve_conglomerate(input) << endl;
} catch (exception const& error) {
cerr << "Error: " << error.what() << endl;
return 1;
}
return 0;
}