C.W.K.
Stream
Lesson 01 of 05 · published

Why JAX Excels at Scientific Computing

~8 min · scientific, jax, tutorial

Level 0Curious
0 XP0/73 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

JAX was born from the scientific computing community, and its core features — automatic differentiation, JIT compilation, and vectorization — are exactly what scientists need. While PyTorch and TensorFlow were designed primarily for neural networks, JAX was designed for differentiable computation in general. This makes it uniquely powerful for scientific applications.

Consider what a scientist might need:

  • Gradients of any computation — not just neural network loss functions, but physical simulations, statistical models, optimization objectives
  • Fast numerical code — JIT compilation gives you C++/Fortran-level speed from Python
  • Vectorization — run the same simulation with 1000 different initial conditions, simultaneously
  • Hardware acceleration — GPUs and TPUs for massive parallelism
import jax
import jax.numpy as jnp

# Example: differentiable physics simulation
def simulate_spring(k, m, x0, v0, dt, num_steps):
    """Simulate a damped spring system: m*x'' + 0.1*x' + k*x = 0"""
    def step(state, _):
        x, v = state
        a = (-k * x - 0.1 * v) / m  # spring force + damping
        v_new = v + a * dt
        x_new = x + v_new * dt
        return (x_new, v_new), x_new

    init_state = (x0, v0)
    _, trajectory = jax.lax.scan(step, init_state, None, length=num_steps)
    return trajectory

# Simulate
traj = simulate_spring(k=2.0, m=1.0, x0=1.0, v0=0.0, dt=0.01, num_steps=1000)

# Gradient: how does the final position change with spring constant?
@jax.jit
def final_position(k):
    return simulate_spring(k, 1.0, 1.0, 0.0, 0.01, 1000)[-1]

dk = jax.grad(final_position)(2.0)
print(f"d(final_pos)/dk = {dk:.6f}")

# Vectorize: simulate 100 different spring constants at once
ks = jnp.linspace(0.5, 5.0, 100)
all_trajectories = jax.vmap(lambda k: simulate_spring(k, 1.0, 1.0, 0.0, 0.01, 1000))(ks)
print(f"Batch trajectories shape: {all_trajectories.shape}")  # (100, 1000)

💡 Why This Matters

In traditional scientific computing (Fortran, C++, MATLAB), getting gradients means either deriving them by hand, using finite differences (slow and inaccurate), or using complex AD tools. JAX gives you exact gradients of arbitrary computations for free, with GPU acceleration and vectorization on top. This has opened entirely new research directions: differentiable physics, learned simulators, and inverse problems that were previously intractable.

Code

import jax
import jax.numpy as jnp

# Example: differentiable physics simulation
def simulate_spring(k, m, x0, v0, dt, num_steps):
    """Simulate a damped spring system: m*x'' + 0.1*x' + k*x = 0"""
    def step(state, _):
        x, v = state
        a = (-k * x - 0.1 * v) / m  # spring force + damping
        v_new = v + a * dt
        x_new = x + v_new * dt
        return (x_new, v_new), x_new

    init_state = (x0, v0)
    _, trajectory = jax.lax.scan(step, init_state, None, length=num_steps)
    return trajectory

# Simulate
traj = simulate_spring(k=2.0, m=1.0, x0=1.0, v0=0.0, dt=0.01, num_steps=1000)

# Gradient: how does the final position change with spring constant?
@jax.jit
def final_position(k):
    return simulate_spring(k, 1.0, 1.0, 0.0, 0.01, 1000)[-1]

dk = jax.grad(final_position)(2.0)
print(f"d(final_pos)/dk = {dk:.6f}")

# Vectorize: simulate 100 different spring constants at once
ks = jnp.linspace(0.5, 5.0, 100)
all_trajectories = jax.vmap(lambda k: simulate_spring(k, 1.0, 1.0, 0.0, 0.01, 1000))(ks)
print(f"Batch trajectories shape: {all_trajectories.shape}")  # (100, 1000)

External links

Exercise

List five domains where 'differentiable + accelerated' beats classical scientific Python (NumPy/SciPy/Numba). Pick one you'd use yourself and write a 3-line justification. The framing matters more than the list.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.