Есть какая-то возможность ускорить этот код?
def mandelbrot(filename='fractal.png'):
def get_fractal(pmin, pmax, ppoints, qmin, qmax, qpoints, max_iterations, infinity_border):
image = np.zeros((ppoints, qpoints))
p, q = np.mgrid[pmin:pmax:(ppoints * 1j), qmin:qmax:(qpoints * 1j)]
c = p + 1j * q
z = np.zeros_like(c)
for k in range(max_iterations):
z **= 2 + c
mask = (np.abs(z) > infinity_border) & (image == 0)
image[mask] = k
z[mask] = np.nan
return -image.T
image = get_fractal(-2.5, 1.5, 1000, -2, 2, 1000, 200, 20)
plt.xticks([])
plt.yticks([])
plt.imshow(image, cmap='flag', interpolation='none')
fig = plt.gcf()
fig.set_size_inches(20, 20)
plt.axis('off')
plt.savefig(filename, format='png', bbox_inches='tight')
Само долго исполняется функция get_fractal. Можно как-то ускорить всё это дело через какой-нибудь numba.njit, multiprocessing или что-то вроде этого?
Ответы (1 шт):
Автор решения: КИТ KIT
→ Ссылка
@jit(nopython=True)
def draw_fractal(image, z, c, p, q, max_iterations, infinity_border):
for k in range(max_iterations):
z = calculate_z(z, c, p, q, k)
mask = (np.abs(z) > infinity_border) & (image == 0)
image[mask] = k
z[mask] = np.nan
return image
Получилось написать такую функцию, но вот проблема.
No implementation of function Function(<built-in function setitem>) found for signature:
>>> setitem(array(float64, 2d, C), array(bool, 2d, C), int64)
There are 10 candidate implementations:
- Of which 8 did not match due to:
Overload of function 'setitem': File: <numerous>: Line N/A.
With argument(s): '(array(float64, 2d, C), array(bool, 2d, C), int64)':
No match.
- Of which 2 did not match due to:
Overload in function 'SetItemBuffer.generic': File: numba\core\typing\arraydecl.py: Line 171.
With argument(s): '(array(float64, 2d, C), array(bool, 2d, C), int64)':
Rejected as the implementation raised a specific error:
TypeError: unsupported array index type array(bool, 2d, C) in [array(bool, 2d, C)]
raised from C:\Users\andre\AppData\Local\Programs\Python\Python37\lib\site-packages\numba\core\typing\arraydecl.py:69
During: typing of setitem at D:/Islam/Python/Islam/test/test.py (16)
File "test.py", line 16:
def draw_fractal(image, z, c, p, q, max_iterations, infinity_border):
<source elided>
mask = (np.abs(z) > infinity_border) & (image == 0)
image[mask] = k
^