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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <array>
#include <string>
#include <complex>
#include <utility>
#include <tuple>
#include <algorithm>
#include <numeric>
#include <thread>
#include <mutex>
#include <cstdlib>
#include <cstdint>
#include <cassert>
#include "../lodepng.h"
using namespace std;
constexpr const int N = 18;
using Com = complex<double>;
using Poly = array<int, N + 1>;
using AApprox = array<Com, N>;
template <typename T>
constexpr static T clearLowestBit(T value) {
return value & (value - 1);
}
template <typename T>
constexpr static bool ispow2(T value) {
return clearLowestBit(value) == 0;
}
template <typename T>
constexpr static T ceil2(T value) {
T value2 = clearLowestBit(value);
if (value2 == 0) return value;
while (true) {
value = value2;
value2 = clearLowestBit(value);
if (value2 == 0) return value << 1;
}
}
__attribute__((unused))
static ostream& operator<<(ostream &os, const Poly &p) {
static const char *supers[10] = {
"⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"
};
os << p[0];
for (int i = 1; i < (int)p.size(); i++) {
if (p[i] < 0) os << " - " << -p[i];
else if (p[i] > 0) os << " + " << p[i];
else continue;
os << "x";
if (i == 1) continue;
ostringstream ss;
ss << i;
string s = ss.str();
for (char c : s) os << supers[c - '0'];
}
return os;
}
template <typename T>
static T eval(const Poly &p, int nterms, T pt) {
T value = p[nterms - 1];
for (int i = nterms - 2; i >= 0; i--) {
value = pt * value + (double)p[i];
}
return value;
}
template <typename T>
static T eval(const Poly &p, T pt) {
return eval(p, p.size(), pt);
}
static Poly derivative(const Poly &p) {
Poly res;
for (int i = res.size() - 2; i >= 0; i--) {
res[i] = (i+1) * p[i+1];
}
return res;
}
static double maxRootNorm(const Poly &poly) {
// Cauchy's bound: https://en.wikipedia.org/wiki/Geometrical_properties_of_polynomial_roots#Lagrange's_and_Cauchy's_bounds
double value = 0;
double last = (double)poly.back();
for (int i = 0; i < (int)poly.size() - 1; i++) {
value = max(value, abs(poly[i] / last));
}
return 1 + value;
}
struct AberthState {
const Poly &poly;
Poly deriv;
Poly boundPoly;
AApprox approx;
double radius;
void regenerate() {
auto genCoord = [this]() { return (double)random() / INT_MAX * 2 * radius - radius; };
for (int i = 0; i < N; i++) {
approx[i] = Com(genCoord(), genCoord());
}
}
// boundPoly is 's' in the stop condition formulated at p.189-190 of
// https://link.springer.com/article/10.1007%2FBF02207694
AberthState(const Poly &poly)
: poly(poly), deriv(derivative(poly)), radius(maxRootNorm(poly)) {
regenerate();
for (int i = 0; i <= N; i++) {
boundPoly[i] = abs(poly[i]) * (4 * i + 1);
}
}
// Gauss-Seidel-style step where the updated values are already used in the current iteration
bool step() {
array<Com, N * N> pairs;
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
pairs[N * i + j] = 1.0 / (approx[i] - approx[j]);
}
}
bool allConverged = true;
AApprox offsets;
for (int i = 0; i < N; i++) {
Com pval = eval(poly, approx[i]);
Com derivval = eval(deriv, poly.size() - 1, approx[i]);
Com quo = pval / derivval;
Com sum = 0;
for (int j = 0; j < i; j++) sum += pairs[N * j + i];
for (int j = i + 1; j < N; j++) sum += pairs[N * i + j];
offsets[i] = quo / (1.0 - quo * sum);
approx[i] -= offsets[i];
double sval = eval(boundPoly, abs(approx[i]));
if (abs(pval) > 1e-9 * sval) allConverged = false;
}
return allConverged;
}
void iterate() {
int tries = 1, stepIdx = 1;
while (!step()) {
stepIdx++;
if (stepIdx > tries * 100) {
regenerate();
stepIdx = 0;
tries++;
}
}
}
};
static AApprox aberth(const Poly &poly) {
AberthState state(poly);
state.iterate();
return state.approx;
}
// Set the constant coefficient to 1; nextDerbyshire will never change it
static Poly initDerbyshire() {
Poly poly;
poly[0] = 1;
fill(poly.begin() + 1, poly.end(), -1);
return poly;
}
// Returns whether we just looped around
static bool nextDerbyshire(Poly &poly) {
for (int i = 1; i < (int)poly.size(); i++) {
if (poly[i] == -1) {
poly[i] = 1;
return false;
}
poly[i] = -1;
}
return true;
}
static Poly derbyshireAtIndex(int index) {
Poly poly;
poly[0] = 1;
for (int i = 1; i <= N; i++) {
poly[i] = index & 1 ? 1 : -1;
index >>= 1;
}
assert(index == 0);
return poly;
}
struct Job {
Poly init;
int numItems;
};
static vector<Job> derbyshireJobs(int targetJobs) {
int njobs = min(1 << N, ceil2(targetJobs));
int jobsize = (1 << N) / njobs;
vector<Job> jobs(njobs);
for (int i = 0; i < njobs; i++) {
jobs[i].init = derbyshireAtIndex(i * jobsize);
jobs[i].numItems = jobsize;
}
return jobs;
}
static tuple<int, int, vector<int>> computeCounts() {
constexpr const int W = 900;
constexpr const int H = 900;
constexpr const int numThreads = 4;
static_assert(ispow2(numThreads));
constexpr const Com bottomLeft = Com(-1.5, -1.5);
constexpr const Com topRight = Com(1.5, 1.5);
vector<int> counts(W * H);
mutex countsMutex;
vector<Job> jobs = derbyshireJobs(numThreads);
assert(jobs.size() == numThreads);
vector<thread> threads(jobs.size());
for (int i = 0; i < (int)jobs.size(); i++) {
threads[i] = thread([&counts, &countsMutex, job = jobs[i], bottomLeft, topRight]() {
auto calcIndex = [](double value, double left, double right, int steps) -> int {
return (value - left) / (right - left) * (steps - 1) + 0.5;
};
auto calcPos = [bottomLeft, topRight, &calcIndex](Com z) -> pair<int, int> {
return make_pair(
calcIndex(z.real(), bottomLeft.real(), topRight.real(), W),
calcIndex(z.imag(), bottomLeft.imag(), topRight.imag(), H)
);
};
vector<int> localCounts(W * H);
Poly poly = job.init;
for (int i = 0; i < job.numItems; i++) {
for (Com z : aberth(poly)) {
int x, y;
tie(x, y) = calcPos(z);
if (0 <= x && x < W && 0 <= y && y < H) {
localCounts[W * y + x]++;
}
}
nextDerbyshire(poly);
}
lock_guard guard(countsMutex);
for (int i = 0; i < W * H; i++) counts[i] += localCounts[i];
});
}
for (thread &th : threads) th.join();
return make_tuple(W, H, counts);
}
static void writeCounts(int W, int H, const vector<int> &counts, const char *fname) {
ofstream f(fname);
f << W << ' ' << H << '\n';
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
if (x != 0) f << ' ';
f << counts[W * y + x];
}
f << '\n';
}
}
static tuple<int, int, vector<int>> readCounts(const char *fname) {
ifstream f(fname);
int W, H;
f >> W >> H;
vector<int> counts(W * H);
for (int &v : counts) f >> v;
return make_tuple(W, H, counts);
}
static int rankCounts(vector<int> &counts) {
int maxcount = reduce(counts.begin(), counts.end(), 0, [](int a, int b) -> int { return max(a, b); });
vector<int> cumul(maxcount + 1, 0);
for (int v : counts) cumul[v]++;
cumul[0] = 0;
for (int i = 1; i < (int)cumul.size(); i++) cumul[i] += cumul[i-1];
// assert(cumul[maxcount + 1] == (int)counts.size());
for (int &v : counts) v = cumul[v];
return cumul[maxcount];
}
static vector<uint8_t> drawImage(int W, int H, const vector<int> &counts, int maxcount) {
vector<uint8_t> image(3 * W * H);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
double value = (double)counts[W * y + x] / maxcount * 255;
image[3 * (W * y + x) + 0] = value;
image[3 * (W * y + x) + 1] = value;
image[3 * (W * y + x) + 2] = value;
}
}
return image;
}
int main(int argc, char **argv) {
srandomdev();
int W, H;
vector<int> counts;
if (argc <= 1) {
tie(W, H, counts) = computeCounts();
writeCounts(W, H, counts, "out.txt");
} else if (argc == 2) {
tie(W, H, counts) = readCounts(argv[1]);
} else {
cerr << "Usage: " << argv[0] << " -- compute and draw" << endl;
cerr << "Usage: " << argv[0] << " <out.txt> -- draw already-computed data" << endl;
return 1;
}
int maxcount = rankCounts(counts);
vector<uint8_t> image = drawImage(W, H, counts, maxcount);
assert(lodepng_encode24_file("out.png", image.data(), W, H) == 0);
}
|