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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <stdexcept>
#include <algorithm>
#include <cassert>
#include "eqsystem_solve.h"
const Product Product::times(double x) const {
return Product{cnst * x, vars};
}
void Product::times_inplace(double x) {
cnst *= x;
}
const Product Product::times(const Product &p) const {
std::vector<std::string> vars2{vars};
vars2.insert(vars2.end(), p.vars.begin(), p.vars.end());
return Product{cnst * p.cnst, vars2};
}
bool Product::contains(const std::string &var) const {
for (const std::string &v : vars) {
if (v == var) return true;
}
return false;
}
const Product Product::without(const std::string &var) const {
Product result{cnst, {}};
result.vars.reserve(vars.size());
for (const std::string &v : vars) {
if (v != var) result.vars.push_back(v);
}
return result;
}
bool Product::operator==(const Product &other) const {
if (cnst != other.cnst) return false;
if (vars.size() != other.vars.size()) return false;
std::unordered_set<std::string> seen(vars.begin(), vars.end());
for (const std::string &v : other.vars) {
auto it = seen.find(v);
if (it == seen.end()) return false;
seen.erase(it);
}
return true;
}
const Sum Sum::times(double x) const {
Sum result;
result.terms.reserve(terms.size());
for (const Product &p : terms) result.terms.push_back(p.times(x));
return result;
}
const Sum Sum::substitute(const std::string &var, Sum sum) const {
Sum result;
for (const Product &p : terms) {
if (p.contains(var)) {
Product rest = p.without(var);
for (const Product &term : sum.terms) {
result.terms.push_back(rest.times(term));
}
} else {
result.terms.push_back(p);
}
}
return result;
}
void Sum::simplify() {
double tot = 0.0;
std::vector<Product> terms2;
for (Product &p : terms) {
if (p.vars.empty()) {
tot += p.cnst;
} else {
sort(p.vars.begin(), p.vars.end());
bool added = false;
for (Product &q : terms2) {
if (q.vars == p.vars) {
q.cnst += p.cnst;
added = true;
break;
}
}
if (!added) terms2.push_back(std::move(p));
}
}
if (tot != 0.0) terms2.push_back(Product{tot, {}});
terms = terms2;
}
std::optional<double> Sum::evaluate() const {
double tot = 0.0;
for (const Product &p : terms) {
if (!p.vars.empty()) return std::nullopt;
tot += p.cnst;
}
return tot;
}
std::optional<std::string> Equation::validate() const {
std::unordered_set<std::string> seen;
for (int i = 0; i < 2; i++) {
const Sum &sum = i == 0 ? lhs : rhs;
for (const Product &p : sum.terms) {
for (const std::string &var : p.vars) {
auto it = seen.find(var);
if (it != seen.end()) {
std::stringstream ss;
ss << "In equation " << *this << ": variable " << var << " occurs multiple times";
return ss.str();
}
seen.insert(var);
}
}
}
return std::nullopt;
}
const Equation Equation::substitute(const std::string &var, Sum sum) const {
return Equation{lhs.substitute(var, sum), rhs.substitute(var, sum)};
}
void Equation::substitute_inplace(const std::string &var, Sum sum) {
*this = substitute(var, sum);
}
int Equation::num_vars() const {
int count = 0;
for (const Product &p : lhs.terms) count += p.vars.size();
for (const Product &p : rhs.terms) count += p.vars.size();
return count;
}
std::vector<std::string> Equation::all_vars() const {
std::vector<std::string> result;
for (const Product &p : lhs.terms) result.insert(result.end(), p.vars.begin(), p.vars.end());
for (const Product &p : rhs.terms) result.insert(result.end(), p.vars.begin(), p.vars.end());
return result;
}
bool Equation::isolatable(const std::string &target) const {
int num_terms = 0;
for (int i = 0; i < 2; i++) {
const Sum &side = i == 0 ? lhs : rhs;
for (const Product &p : side.terms) {
if (p.contains(target)) {
num_terms++;
if (num_terms > 1) return false;
if (p.vars.size() > 1) return false;
}
}
}
return true;
}
void Equation::isolate_left(const std::string &target) {
Sum newlhs, newrhs;
for (int i = 0; i < 2; i++) {
const Sum &side = i == 0 ? lhs : rhs;
for (const Product &p : side.terms) {
if (p.contains(target)) newlhs.terms.push_back(p.times(i == 0 ? 1 : -1));
else newrhs.terms.push_back(p.times(i == 1 ? 1 : -1));
}
}
lhs = newlhs;
rhs = newrhs;
if (lhs.terms.size() == 1) {
const double factor = lhs.terms.at(0).cnst;
if (factor != 0) {
for (Product &p : rhs.terms) p.times_inplace(1.0 / factor);
}
lhs.terms.at(0).cnst = 1.0;
}
}
void Equation::simplify() {
lhs.simplify();
rhs.simplify();
}
// xmax = xmin + cplxwidth
// ymax = ymin + cplxheight
// xmin + xmax = 2 * cx
// ymin + ymax = 2 * cy
// imgwidth * cplxheight = cplxwidth * imgheight
std::variant<
std::string, // description of the error
std::vector<std::pair<std::string, double>> // result assignment
> System::solve_inplace(bool debug) {
for (const Equation &eq : eqs) {
if (auto err = eq.validate()) {
throw std::invalid_argument("Invalid equation passed to System::solve(): " + *err);
}
}
std::vector<std::pair<std::string, double>> assignment;
std::vector<bool> assigned(eqs.size());
std::vector<bool> substituted(eqs.size());
success_try_again:
if (debug) {
std::cerr << std::endl << "\x1B[1mSolving:\x1B[0m" << std::endl;
std::cerr << *this << std::endl << std::endl;
}
for (int stage = 1; stage <= 2; stage++) {
for (size_t i = 0; i < eqs.size(); i++) {
if (assigned[i] || (stage == 2 && substituted[i])) continue;
Equation &eq = eqs[i];
if (stage >= 2 || eq.num_vars() == 1) {
const std::string var = eq.all_vars().at(0);
if (eq.isolatable(var)) {
eq.isolate_left(var);
eq.rhs.simplify();
if (eq.lhs.terms.size() == 1 &&
eq.lhs.terms.at(0) == Product{1.0, std::vector<std::string>{var}}) {
if (debug) std::cerr << "\x1B[1m[" << stage << "] Substituting " << var;
if (auto value = eq.rhs.evaluate()) {
if (debug) std::cerr << " = " << *value;
assignment.emplace_back(
eq.lhs.terms.at(0).vars.at(0),
*value
);
assigned[i] = true;
}
if (debug) std::cerr << "\x1B[0m" << std::endl;
for (size_t j = 0; j < eqs.size(); j++) {
if (j == i || assigned[j]) continue;
eqs[j].substitute_inplace(var, eq.rhs);
eqs[j].simplify();
}
substituted[i] = true;
goto success_try_again;
}
}
}
}
}
std::stringstream errmsg;
errmsg << "Equations could not be solved:\n";
bool have_error = false;
// If we arrive here, no more equations could be solved
for (size_t i = 0; i < eqs.size(); i++) {
if (assigned[i]) continue;
have_error = true;
errmsg << " " << eqs[i] << '\n';
}
if (have_error) return errmsg.str();
else return assignment;
}
void System::substitute_inplace(const std::string &var, Sum sum) {
for (Equation &eq : eqs) {
eq.substitute_inplace(var, sum);
}
}
std::ostream& operator<<(std::ostream &os, const Product &p) {
bool first = true;
if (p.cnst != 1 || p.vars.empty()) {
os << p.cnst;
first = false;
}
for (const std::string &v : p.vars) {
if (first) first = false;
else os << "*";
os << v;
}
return os;
}
std::ostream& operator<<(std::ostream &os, const Sum &s) {
if (s.terms.empty()) {
os << "0";
return os;
}
bool first = true;
for (const Product &p : s.terms) {
if (first) first = false;
else os << " + ";
os << p;
}
return os;
}
std::ostream& operator<<(std::ostream &os, const Equation &eq) {
os << eq.lhs << " = " << eq.rhs;
return os;
}
std::ostream& operator<<(std::ostream &os, const System &sys) {
bool first = true;
for (const Equation &eq : sys.eqs) {
if (first) first = false;
else os << '\n';
os << eq;
}
return os;
}
// vim: set sw=4 ts=4 noet:
|