import jax
import jax.numpy as jnp
import brainstate
import braintools.surrogate as surrogate
import matplotlib.pyplot as plt
xs = jnp.linspace(-3, 3, 1000)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
for alpha in [0.3, 0.5, 1.0]:
    for width in [1.0, 2.0]:
        rg_fn = surrogate.ReluGrad(alpha=alpha, width=width)
        grads = jax.vmap(jax.grad(rg_fn))(xs)
        ax1.plot(xs, grads, label=rf'$\alpha={alpha}, w={width}$')
ax1.set_xlabel('Input (x)')
ax1.set_ylabel('Gradient')
ax1.set_title('ReLU Surrogate Gradients')
ax1.legend()
ax1.grid(True, alpha=0.3)
alpha_fixed = 0.5
for width in [0.5, 1.0, 1.5, 2.0]:
    rg_fn = surrogate.ReluGrad(alpha=alpha_fixed, width=width)
    grads = jax.vmap(jax.grad(rg_fn))(xs)
    ax2.plot(xs, grads, label=rf'$width={width}$')
ax2.set_xlabel('Input (x)')
ax2.set_ylabel('Gradient')
ax2.set_title(f'Width Effect (α={alpha_fixed})')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
