Well, I solved the endless loop in Justin Peel's code, adding the maximum iteration condition to the code, now the code exposes polynomials such as z ^ 4-1, and it does not go into the endless loop. If anyone knows how to improve this error, let us know. My solution may slow down code execution, but it works. This is the code:
#!/usr/bin/python # -*- coding: utf-8 -*- import numpy as np import itertools import matplotlib.pyplot as plt __author__ = 'Tobal' __version__ = 1.0 def newton_fractal(f, xmin, xmax, ymin, ymax, xres, yres, tolerance, maxiters): yarr, xarr = np.meshgrid(np.linspace(xmin, xmax, xres), np.linspace(ymin, ymax, yres) * 1j) arr = yarr + xarr ydim, xdim = arr.shape arr = arr.flatten() fp = np.polyder(f, m=1) counts = np.zeros(shape=arr.shape) unconverged = np.ones(shape=arr.shape, dtype=bool) indices = np.arange(len(arr)) iters = 0 for i in itertools.count(): f_g = f(arr[unconverged]) new_unconverged = np.abs(f_g) > tolerance counts[indices[unconverged][~new_unconverged]] = i if not np.any(new_unconverged) or iters >= maxiters: return counts.reshape((ydim, xdim)) iters += 1 unconverged[unconverged] = new_unconverged arr[unconverged] -= f_g[new_unconverged] / fp(arr[unconverged]) pic = newton_fractal(np.poly1d([1., 0., 0., 0., -1.]), -10, 10, -10, 10, 1000, 1000, 0.00001, 1000) plt.imshow(pic, cmap=plt.get_cmap('gnuplot')) plt.title(r'$Newton^{\prime} s\;\;Fractal\;\;Of\;\;P\,(z) =z^4-1$', fontsize=18, y=1.03) plt.tight_layout() plt.show()
I am using Pycharm 5 with Anaconda Python 3 and this IDE reports a warning in non np.any code (new_unconverged)
The expected 'type Union [ndarray, iterable]', instead got "bool"
This warning appears in the source code of Justin Peel; and I donβt know how to solve it. I am very interested in this issue. 