-
Notifications
You must be signed in to change notification settings - Fork 1
/
fractal.py
46 lines (40 loc) · 1.07 KB
/
fractal.py
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
import sys
import copperhead as cpp
extra_compile_args = "'/std:c++14'" if sys.version.split('[')[1].startswith('MSC') else "'-std=c++14'"
config = {
'extra_compile_args': extra_compile_args
}
mandelbrot_cpp = r'''
#include <cmath>
#include <complex>
#include <fstream>
inline int mandelbrot(const std::complex<double> &c)
{
const int max_iter {100};
int i {0};
std::complex<double> z {0.0, 0.0};
while (std::abs(z) < 2.0 && i < max_iter)
{
z = z * z + c;
++i;
}
return max_iter - i;
}
void compute(std::string filename, double x, double y, double h)
{
const auto n {std::lround(2.5 / h)};
std::ofstream f(filename);
for (long yidx {0}; yidx < n; ++yidx)
{
for (long xidx {0}; xidx < n; ++xidx)
{
f << mandelbrot(std::complex<double>(x, y)) << " ";
x += h;
}
f << "\n";
y -= h;
x -= 2.5;
}
}'''
compute = cpp.generate('compute', 'void(std::string, double, double, double)', mandelbrot_cpp, config=config)
compute('fractal.dat', -2.0, 1.25, 0.005)