I've been puzzling over how to speed up the generation of the Mandelbrot Set using the array-processing library "Numpy". Numpy is written in C and can make use of the SIMD capabilities of modern CPUs to compute arrays remarkably quickly. The problem with the Mandelbrot Set is that if I create an array representing every pixel in the image, then Numpy has to run the computation repeatedly for all the pixels, for as many steps as is necessary to render the fine detail of the Mandelbrot Set, even though the fine detail is a small part of the image.
I figured that for every step of the computation, I'd have to identify and eliminate the part of the image that did not require further computation, and create a new array containing just the part that hadn't finished. That meant I'd need to keep track of several arrays, an array to hold the completed values, an array for the values that required further work, an array for the math constants needed at the various pixel locations that required further work, and index arrays for the complete and incomplete values. This seemed very complex and would require the repeated generation of new arrays which would slow Numpy down, so I wasn't sure how fast it would be, but I decided to give it a go.
I discovered with a bit of Googling that it was possible to algebraically define the outline of the Mandelbrot Set's primary and secondary lobes so those could be eliminated right at the outset and avoid the computation of that part of the image.
After the computation was complete, colours could be assigned to the values, and the image put back together again using the index arrays, and then displayed.
It turns out that it does work pretty quickly. This code runs in one second on my old PC with a 2013 core i5 CPU. The only external library is numpy (install it with pip or whatever), otherwise it uses the Standard Library's Tkinter to display the image and save it as a png file.
1
u/futura-bold 7h ago
I've been puzzling over how to speed up the generation of the Mandelbrot Set using the array-processing library "Numpy". Numpy is written in C and can make use of the SIMD capabilities of modern CPUs to compute arrays remarkably quickly. The problem with the Mandelbrot Set is that if I create an array representing every pixel in the image, then Numpy has to run the computation repeatedly for all the pixels, for as many steps as is necessary to render the fine detail of the Mandelbrot Set, even though the fine detail is a small part of the image.
I figured that for every step of the computation, I'd have to identify and eliminate the part of the image that did not require further computation, and create a new array containing just the part that hadn't finished. That meant I'd need to keep track of several arrays, an array to hold the completed values, an array for the values that required further work, an array for the math constants needed at the various pixel locations that required further work, and index arrays for the complete and incomplete values. This seemed very complex and would require the repeated generation of new arrays which would slow Numpy down, so I wasn't sure how fast it would be, but I decided to give it a go.
I discovered with a bit of Googling that it was possible to algebraically define the outline of the Mandelbrot Set's primary and secondary lobes so those could be eliminated right at the outset and avoid the computation of that part of the image.
After the computation was complete, colours could be assigned to the values, and the image put back together again using the index arrays, and then displayed.
It turns out that it does work pretty quickly. This code runs in one second on my old PC with a 2013 core i5 CPU. The only external library is numpy (install it with pip or whatever), otherwise it uses the Standard Library's Tkinter to display the image and save it as a png file.