Mandelbrot set: Difference between revisions
m
→{{header|Python}}: formatted code and changed the linspace formula to ensure readability and to be in sync with the numpy implementation
No edit summary |
m (→{{header|Python}}: formatted code and changed the linspace formula to ensure readability and to be in sync with the numpy implementation) |
||
Line 6,393:
Actually the same, but more suitable for teaching. First without Numpy, only with floating point numbers.
<lang python>import matplotlib.pyplot as plt
def linspace(start, stop, num):
return [
def zeros(rows, cols):
return [[0 for j in range(cols)] for i in range(rows)]
d, n = 100, 50 # pixel density & number of iterations
r = 2.5
x = linspace(-2.5, 1.5, 4 * d + 1)
y = linspace(-1.5, 1.5, 3 * d + 1)
T = zeros(len(y), len(x))
Line 6,412 ⟶ 6,415:
u, v = 0.0, 0.0
for k in range(n):
u, v = u ** 2 - v ** 2 + a, 2 * u * v + b
if not u ** 2 + v ** 2 < r ** 2:
break
T[i][j] = k + 1
plt.imshow(T, cmap=plt.cm.twilight_shifted)
plt.savefig(
Second with Numpy and complex matrices.
<lang python>import numpy as np
Line 6,424 ⟶ 6,427:
d, n = 100, 50 # pixel density & number of iterations
r = 2.5
x = np.linspace(-2.5, 1.5, 4 * d + 1)
y = np.linspace(-1.5, 1.5, 3 * d + 1)
A, B = np.meshgrid(x, y)
C = A + B * 1j
Z = np.zeros_like(C)
Line 6,437 ⟶ 6,440:
for k in range(n):
M = abs(Z) < r
Z[M] = Z[M] ** 2 + C[M]
T[M] = k + 1
plt.imshow(T, cmap=plt.cm.twilight_shifted)
plt.savefig(
=={{header|R}}==
|