Grok 4.1 Fast· xai
Frontier Reasoning Traces
Generate Python code to calculate and visualize the Mandelbrot set.
- User
Generate Python code to calculate and visualize the Mandelbrot set.
- Assistant
import numpy as np import matplotlib.pyplot as plt def mandelbrot_set(width, height, x_min=-2.5, x_max=1.0, y_min=-1.5, y_max=1.5, max_iter=256): """ Compute the Mandelbrot set iteration counts for a given grid. Args: - width, height: Dimensions of the output image. - x_min, x_max, y_min, y_max: Boundaries of the complex plane region. - max_iter: Maximum number of iterations per point. Returns: - 2D numpy array of iteration counts. """ # Create coordinate grids y, x = np.ogrid[y_min:y_max:height*1j, x_min:x_max:width*1j] c = x + y*1j # Complex plane grid z = c # Initial z_0 = 0, but we start with z = c for efficiency div_time = max_iter + np.zeros(z.shape, dtype=int) for i in range(max_iter): z = z**2 + c diverge = abs(z) > 2 # |z| > 2 indicates escape now_diverge = diverge & (div_time == max_iter) # First divergence time div_time[now_diverge] = i z[diverge] = 2 # Cap z to prevent overflow return div_time def visualize_mandelbrot(div_time, cmap='hot', title='Mandelbrot Set'): """ Visualize the Mandelbrot set using matplotlib. """ plt.figure(figsize=(10, 10)) plt.imshow(div_time, cmap=cmap, extent=[-2.5, 1.0, -1.5, 1.5], origin='lower') plt.colorbar(label='Iteration Count') plt.title(title) plt.xlabel('Re(c)') plt.ylabel('Im(c)') plt.show() # Example usage if __name__ == "__main__": # Generate the set (800x800 for good resolution) mandelbrot_image = mandelbrot_set(800, 800, max_iter=128) # Visualize visualize_mandelbrot(mandelbrot_image, cmap='inferno', title='Mandelbrot Set (128 iterations)')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.