<jupyter_start><jupyter_text>Graficar Fractales Complejos<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML

import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Mandelbrot<jupyter_code>def mandelbrot(x, y, threshold):
    
    # Condiciones iniciales
    c = complex(x, y)
    z = complex(0, 0)
    
    for i in range(threshold):
        z = z**2 + c
        if abs(z) > 4.:  # diverge
            return i
        
    return threshold - 1  # no diverge
x_start, y_start = -2, -1.5  # inicio
width, height = 3, 3
density_per_unit = 250  # pixeles por unidad

# eje real e imaginario
re = np.linspace(x_start, x_start + width, width * density_per_unit )
im = np.linspace(y_start, y_start + height, height * density_per_unit)

def animate(i):
    ax.clear()
    ax.set_xticks([], [])
    ax.set_yticks([], [])
    
    X = np.empty((len(re), len(im)))
    threshold = round(1.15**(i + 1))
    
    for i in range(len(re)):
        for j in range(len(im)):
            X[i, j] = mandelbrot(re[i], im[j], threshold)
    
    img = ax.imshow(X.T, interpolation="bicubic", cmap='magma')
    return [img]
fig = plt.figure(figsize=(10, 10))
ax = plt.axes()

anim_mandelbrot = animation.FuncAnimation(fig, animate, frames=45, interval=120, blit=True)
HTML(anim_mandelbrot.to_html5_video())<jupyter_output><empty_output><jupyter_text>Julia<jupyter_code>def julia_quadratic(zx, zy, cx, cy, threshold):
    # condiciones iniciales
    z = complex(zx, zy)
    c = complex(cx, cy)
    
    for i in range(threshold):
        z = z**2 + c
        if abs(z) > 4.:  # diverge
            return i
        
    return threshold - 1  # no diverge
x_start, y_start = -2, -2
width, height = 4, 4
density_per_unit = 200

# eje real e imaginario
re = np.linspace(x_start, x_start + width, width * density_per_unit )
im = np.linspace(y_start, y_start + height, height * density_per_unit)


threshold = 20  # iteraciones máximas
frames = 100  # frames por animación

# c = r*cos(a) + i*r*sin(a) = r*e^{i*a}
r = 0.7885
a = np.linspace(0, 2*np.pi, frames)


def animate(i):
    ax.clear()
    ax.set_xticks([], [])
    ax.set_yticks([], [])
    
    X = np.empty((len(re), len(im)))
    cx, cy = r * np.cos(a[i]), r * np.sin(a[i])
    
    for i in range(len(re)):
        for j in range(len(im)):
            X[i, j] = julia_quadratic(re[i], im[j], cx, cy, threshold)
    
    img = ax.imshow(X.T, interpolation="bicubic", cmap='magma')
    return [img]
fig = plt.figure(figsize=(10, 10))
ax = plt.axes()

anim_julia = animation.FuncAnimation(fig, animate, frames=frames, interval=50, blit=True)
HTML(anim_julia.to_html5_video())<jupyter_output><empty_output><jupyter_text>Guardar en formato .gif<jupyter_code>anim_julia.save('julia.gif', writer='imagemagick')
anim_mandelbrot.save('mandelbrot.gif', writer='imagemagick')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14546684709066307, 'fraction_numerical': 0.018944519621109608, 'mean_word_length': 3.521406727828746, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '272146', 'license': 'MIT', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1160, 'n_words': 251, 'path': 'Complex_Fractals.ipynb', 'repo_name': 'felipesanma/fractales-Complejos', 'size': '2956'}