-
Notifications
You must be signed in to change notification settings - Fork 2
/
mandelbrot.R
82 lines (68 loc) · 1.73 KB
/
mandelbrot.R
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
# Set arguments ----
resolution <- as.integer(400) # Resolution of output image. This increases the exponentially x^2.
max.iter <- as.integer(100)
# Defining functions ----
mandelbrot <- function(c, max.iter = 100) {
z <- 0
for (i in 1:max.iter) {
z <- z ^ 2 + c
if (abs(z) > 2) {
return(i)
}
}
return(-1)
}
# For loop function definition
for_loop <- function(points,
max.iter = 100) {
result_for <- matrix(NA,
nrow = dim(points)[1],
ncol = dim(points)[2])
for (x in 1:dim(points)[1]) {
for (y in 1:dim(points)[2]) {
result_for[x, y] <- mandelbrot(points[x, y],
max.iter)
}
}
return(result_for)
}
# Prepare input pairs ----
x <- seq(-2, 1, length.out = resolution)
y <- seq(-1.5, 1.5, length.out = resolution)
points <-
outer(x, y, function(x, y)
complex(real = x, imaginary = y))
# Benchmark ----
print(microbenchmark::microbenchmark(
apply = apply(
X = points,
MARGIN = c(1, 2),
FUN = mandelbrot
),
for_loop = for_loop(points),
times=5
))
# Check the results correctness ----
out_for <- for_loop(points)
out_apply <- apply(X = points,
MARGIN = c(1, 2),
FUN = mandelbrot)
all.equal(out_for,
out_apply)
# Visualize ----
# Base R solution
# image(out_for)
#
# # ggplot2 solution
# data.frame(
# value = as.vector(out_for),
# y = rep(1:resolution, each = resolution),
# x = rep(1:resolution, rep = resolution)
# ) |>
# ggplot2::ggplot() +
# ggplot2::geom_tile(ggplot2::aes(x = x, y = y, fill = value)) +
# ggplot2::scale_fill_viridis_c()
#
# # Plotly solution
# plotly::plot_ly(z = t(out_for),
# type = "heatmap")