-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenergy_values.cpp
165 lines (133 loc) · 4.2 KB
/
energy_values.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Coursera/Advanced Algorithms and Complexity/Week 2/Problem 1:
* Infer Energy Values of Ingredients (Gaussian Elimination)
* Grading: 'Good job! (Max time used: 0.01/1.00, max memory used: 9535488/536870912.)'
* Author: Andrii Shostatskyi
* Email: [email protected]
* Respect Coursera Honor Code
* Copyright © 2018. All rights reserved
*/
#include <cmath>
#include <iostream>
#include <vector>
#include <iomanip>
constexpr int PRECISION = 10;
using Column = std::vector<double>;
using Row = std::vector<double>;
using Matrix = std::vector<Row>;
struct Equation {
Matrix a;
Column b;
};
struct Position {
short column;
short row;
};
Equation read_equation()
{
int size;
std::cin >> size;
Matrix a(size, std::vector<double>(size, 0.0));
Column b(size, 0.0);
for (int row = 0; row < size; ++row) {
for (int column = 0; column < size; ++column)
std::cin >> a[row][column];
std::cin >> b[row];
}
return Equation{ std::move(a), std::move(b) };
}
Position select_pivot(const Matrix& a, std::vector<bool>& used_rows, std::vector<bool>& used_columns)
{
// First, select the first free element.
Position pivot_element{ 0, 0 };
while (used_rows[pivot_element.row])
++pivot_element.row;
while (used_columns[pivot_element.column])
++pivot_element.column;
// Then, amongs free elements, select a pivot with the largest absolute value
double max = 0.0;
for (int from = pivot_element.row, size = a.size(); from < size; ++from) {
if (std::fabs(a[from][pivot_element.column]) > std::fabs(max)) {
max = a[from][pivot_element.column];
pivot_element.row = from;
}
}
return pivot_element;
}
inline void swap_lines(Matrix& a, Column& b, std::vector<bool>& used_rows, Position& pivot_element)
{
std::swap(a[pivot_element.column], a[pivot_element.row]);
std::swap(b[pivot_element.column], b[pivot_element.row]);
std::swap(used_rows[pivot_element.column], used_rows[pivot_element.row]);
pivot_element.row = pivot_element.column;
}
void back_substitution(Matrix& a, Column& b)
{
for (int i = a.size() - 1; i; --i) {
double v = b[i];
for (int j = 0; j != i; ++j) {
b[j] -= a[j][i] * v;
a[j][i] = 0;
}
}
}
inline void scale_pivot(Matrix& a, Column& b, const Position& pivot_element)
{
const double divisor = a[pivot_element.row][pivot_element.column];
const int size = a.size();
for (int j = pivot_element.column; j < size; ++j) {
a[pivot_element.row][j] /= divisor;
}
b[pivot_element.row] /= divisor;
}
void process_pivot(Matrix& a, Column& b, const Position& pivot_element)
{
const int size = a.size();
double multiple{ 0.0 };
scale_pivot(a, b, pivot_element);
for (int i = pivot_element.row + 1; i < size; ++i) {
multiple = a[i][pivot_element.column];
for (int j = pivot_element.column; j < size; ++j) {
a[i][j] -= (a[pivot_element.row][j] * multiple);
}
b[i] -= (b[pivot_element.row] * multiple);
}
}
inline void mark_pivot_used(const Position& pivot, std::vector<bool>& used_rows, std::vector<bool>& used_columns)
{
used_rows[pivot.row] = true;
used_columns[pivot.column] = true;
}
Column solve_equation(Equation equation)
{
Matrix& a = equation.a;
Column& b = equation.b;
std::vector<bool> used_columns(a.size(), false);
std::vector<bool> used_rows(a.size(), false);
for (int steps = a.size(); steps; --steps) {
Position pivot_element = select_pivot(a, used_rows, used_columns);
swap_lines(a, b, used_rows, pivot_element);
process_pivot(a, b, pivot_element);
mark_pivot_used(pivot_element, used_rows, used_columns);
}
back_substitution(a, b);
return b;
}
void print_column(const Column& column)
{
std::cout.precision(PRECISION);
for (const auto s : column) {
std::cout << s << ' ';
}
std::cout << std::endl;
}
int main()
{
std::ios_base::sync_with_stdio(false);
Equation equation = read_equation();
if (equation.a.size()) {
Column solution = solve_equation(equation);
print_column(solution);
}
return 0;
}