This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Posts

A post is one program that tests its own result; demos and articles also draw a figure. It is posted only if its tests pass, except a problem, whose tests must fail until someone solves it.

articleAINumericswritten with an agent#1173edd4ea
Adam Zettel@adam2 min read

Finite differences and spectral methods

Two ways to differentiate a function you only know at grid points. A central difference uses the two neighbors; a spectral derivative uses every point. On a smooth periodic function the spectral one reaches rounding error with a few dozen points. Put a jump in the function and, away from the jump, the central difference is the accurate one.

Largest derivative error against the number of points
spectral, exp(sin x)central difference, exp(sin x)spectral, sin x + step, far pointscentral difference, sin x + step, far points
10⁻¹²10⁻⁹10⁻⁶10⁻³10⁰10¹10²Nlargest error

10 paragraphs · 8 functions · 2 min

✓ 3 tests passchecked when the site was built#numerics#fourier#derivatives

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIStatistical mechanicswritten with an agent#fb6a40dd60

A random walk spreads as 2Dt and turns Gaussian

4000 walkers each take 200 random steps, drawn evenly from an interval. Their mean squared distance from the start grows in a straight line, , with the diffusion constant set by the step size; a line through the measurements gives . After one step the walkers are spread flat, and by step 200 they are spread as the Gaussian that solves the diffusion equation: the flatness measure called excess kurtosis falls as , from to zero within the noise.

✓ 8 tests passchecked when the site was built#random-walk#diffusion#central-limit-theorem#monte-carlo#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIOptimizationwritten with an agent#e075db35de

BFGS against gradient descent on the Rosenbrock valley

Both methods start at on Rosenbrock's function and use the same backtracking line search; they differ only in the direction they search along. BFGS is within of the minimum at after 35 iterations and has a zero gradient to after 36. Gradient descent takes 22,962 iterations to get within , and near the minimum it gains a factor of only 0.99919 per step, close to for the Hessian's condition number .

✓ 9 tests passchecked when the site was built#optimization#bfgs#quasi-newton#gradient-descent#line-search#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIFinancewritten with an agent#011222bf7f

Black-Scholes by Monte Carlo, against the closed form

The price of a one-year call option on a $100 stock with strike $100, 5% interest and 20% volatility is $10.4506 by the Black-Scholes formula. Simulating the stock price 131072 times and averaging the discounted payoff gives $10.371, 1.95 standard errors away. The error shrinks as : its measured slope on a log-log plot is , against , and the standard error comes from an exact formula for the payoff's spread.

✓ 10 tests passchecked when the site was built#finance#options#black-scholes#monte-carlo#convergence#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#7389123e4c

Burgers' equation in nonconservative form puts the shock in the wrong place

For smooth solutions, and are the same equation. For a shock from to they are not. A scheme built on the first form, Godunov's, moves the shock at 0.625, the Rankine-Hugoniot speed , to rounding. Upwinding the second form moves it at 0.5132, on 400, 800 and 1600 cells alike, so refining the grid does not fix it. With the nonconservative shock stays at , with speed 0.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIQuantum chemistrywritten with an agent#002d1b455a

Excitation energies of water from the Casida equation, with and without de-excitations

Water in the 6-31G basis, from its Hartree-Fock ground state to all 40 singlet and 40 triplet excitation energies, two ways. Time-dependent Hartree-Fock (the random-phase approximation) keeps the coupling between excitations and de-excitations; the Tamm-Dancoff approximation drops it and diagonalizes alone. The lowest singlet is at 0.344074 Ha (9.363 eV) in TDHF and 0.346161 Ha in TDA, and every one of the 160 energies matches PySCF's to Ha.

✓ 11 tests passchecked when the site was built#quantum-chemistry#excited-states#tdhf#rpa#tamm-dancoff#cis#casida#pyscf
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • Explore

    Run it and step through what each part did, with the code, values and plots beside it.

The double scroll of Chua's circuit

Two capacitors, an inductor, a resistor and one diode whose current is a three-piece straight line. The voltages never settle and never repeat: they wind around two centers, hopping between them at irregular times. Nearby starts separate like with , which is what a separate scipy calculation gives within the scatter expected of a run this long.

✓ 5 tests passchecked when the site was built#chaos#circuits#lyapunov-exponents#odes
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#9458e4aca8

Crank-Nicolson is second order in time, backward Euler first

The heat equation from , stepped to with six time steps from 0.02 down to 0.000625. Each halving cuts the Crank-Nicolson error by a factor of 4.0 to 4.7 (measured order 2.09 to 2.23, then 2.000) and the backward Euler error by 1.9 to 2.0 (order 0.94 to 0.996). The errors are measured against the exact solution of the space-discretized equation, a discrete sine series. Against the exact Fourier series of the PDE, the Crank-Nicolson error stops falling at the space error, .

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#ca6b837769

Electron mobility fitted to NIST's certified digits

A seven-parameter rational model fitted to NIST's semiconductor electron-mobility data by lmder, the Levenberg-Marquardt routine of MINPACK, translated to Viv step by step. Every trial step matches a gfortran build of the original Fortran to the last bit, the answer matches scipy.optimize.leastsq, and the parameters land within 0.001 standard deviations of NIST's certified values.

✓ 8 tests passchecked when the site was built#least-squares#levenberg-marquardt#minpack#curve-fitting#nist
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • Explore

    Run it and step through what each part did, with the code, values and plots beside it.

demoAINumericswritten with an agent#82acda210a

Forward and backward Euler on either side of h = 2/λ

The exact solution of this equation is , which a step of 0.1 would follow to a few digits. With , forward Euler is within of it at with , and off by at . A disturbance is multiplied by each step, and the measured factor matches to at 36 step sizes. Backward Euler's factor is , below 1 at every step size, and at it is still within of .

✓ 9 tests passchecked when the site was built#ode#stiffness#forward-euler#backward-euler#stability#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#0b475cd75c

Gauss-Legendre quadrature against tanh-sinh on a smooth and a singular integral

Five Gauss-Legendre points integrate every polynomial of degree 9 or less exactly, and miss degree 10 by exactly what the error formula says. On seven points reach rounding. On , which is infinite at both ends of the interval, 128 points still miss by , and the error only halves when the points double. The tanh-sinh rule gets that integral to rounding with 37 points.

✓ 13 tests passchecked when the site was built#quadrature#integration#gauss-legendre#tanh-sinh#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIPlasma physicswritten with an agent#ff0590fe04

Grad-Shafranov by finite differences, against Solov'ev's closed form

A tokamak equilibrium solved on a grid and graded against an exact one. Solov'ev's solves the Grad-Shafranov equation with constant pressure gradient; the five-point solver, relaxed by SOR until the residual is below , misses it by , and on grids of spacing 0.1, 0.05 and 0.025, an observed order of 1.995 and then 1.994. Those errors are scipy's direct solve of the same equations, to .

✓ 7 tests passchecked when the site was built#plasma#mhd#tokamak#equilibrium#grad-shafranov#finite-differences#sor
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Graphene's Dirac cones

An electron hopping between neighboring carbon atoms of a honeycomb has two energy bands, and they touch at the corners of the Brillouin zone. Near a corner the energy rises in a straight line in every direction, a cone, with slope . A centered finite difference at the corner gives to eight digits in every direction.

✓ 6 tests passchecked when the site was built#graphene#tight-binding#band-structure#dirac-cones#fermi-velocity
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIQuantum chemistrywritten with an agent#635f66f63b

RHF, UHF and full CI for H₂ in STO-3G along the bond

Pull the two atoms of H₂ apart in the smallest basis there is, and compute its energy three ways. Restricted Hartree-Fock is off by Ha at the equilibrium bond length and heads for the wrong limit, 0.39 Ha too high. Unrestricted Hartree-Fock splits from it at bohr and reaches two hydrogen atoms. Full CI is exact in this basis. Every energy matches PySCF to Ha.

✓ 10 tests passchecked when the site was built#hartree-fock#full-ci#correlation-energy#bond-breaking#sto-3g#pyscf
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

ladderAIPlasma physicswritten with an agent#b7c5ad40e0

The Hasegawa-Wakatani drift-wave model

The Hasegawa-Wakatani equations are the simplest model of drift-wave turbulence at the edge of a tokamak that contains the instability driving it. Five rungs build their linear theory: the drift-wave frequency, the growth rate as the eigenvalue of a 2×2 matrix (0.098684 at , as numpy's eigensolver gives), the return to the drift wave as at the predicted rates and , a zonal flow that keeps of its start as it relaxes, and the one-line change in the modified model that leaves it untouched.

  1. 1The drift wave+1 function · +3 tests
  2. 2The growth rate is an eigenvalue+3 functions · +3 tests
  3. 3Stiff coupling gives back the drift wave+2 tests
  4. 4A zonal flow relaxes to k^2 / (1 + k^2) of its start+1 function · +2 tests
  5. 5The modified model leaves the band alone+1 function · +2 tests
✓ 12 tests passon 5 rungs, each rerunning every earlier test · checked when the site was built#plasma#drift-waves#turbulence#zonal-flows#eigenvalues#ladder

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#295447f910

Jacobi's eigenvalue method

Jacobi's method diagonalizes a symmetric matrix by plane rotations, each of which zeroes one off-diagonal pair. On an 8 by 8 matrix the off-diagonal norm goes 1.33, 0.453, 0.0299, , over four sweeps, and after the fifth it is under , rounding error. The diagonal is then the spectrum, and it matches numpy.linalg.eigh to .

✓ 10 tests passchecked when the site was built#eigenvalues#linear-algebra#jacobi#convergence#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#cec2f50a64

Jacobi, SOR and conjugate gradients on the Poisson equation

The five-point Poisson equation on a 15 by 15 grid, solved to a relative residual of four ways. Jacobi takes 948 sweeps, Gauss-Seidel 475, SOR with the optimal factor 60, and conjugate gradients 25 iterations. Each count follows from one number the theory predicts: Jacobi's error shrinks by per sweep once the smoothest mode dominates, Gauss-Seidel's by its square, and SOR and CG depend on . Halve and Jacobi takes four times as many sweeps; SOR and CG take twice as many.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#5589c09a31

Three floating-point failures in summation, cancellation and RANDU

Adding for up to a million left to right ends 414 units in the last place (ulps) from the correctly rounded sum; Kahan's compensated sum is correctly rounded at all 31 checkpoints from 1 to . Evaluating from its expanded coefficients near gives rounding noise: 89 sign changes among 401 points, where the polynomial has one, and 8.5 times the true value at . The factored form is within everywhere. The random number generator RANDU puts every triple of successive draws on one of 15 planes.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIStatistical mechanicswritten with an agent#45c11d9591

Kinetic Monte Carlo against the master equation

A molecule hops between three states, A to B to C and back, at fixed rates. The master equation gives the exact probability of each state at every time; kinetic Monte Carlo instead follows 10000 molecules one random jump at a time. At 25 times the fraction of molecules in each state matches the exact probability within four standard errors, the intermediate B rises to 0.460 and falls back, and the mean time to first reach C, exactly 1.75, is measured as .

✓ 7 tests passchecked when the site was built#kinetic-monte-carlo#gillespie#master-equation#markov-chains#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#d003d94d29

Lanczos finds the largest eigenvalue three times

Sixty steps of Lanczos on a 200 by 200 symmetric matrix whose eigenvalues are known exactly. Both ends of the spectrum, , come out right to . But in floating point the basis stops being orthogonal once the top eigenvalue has converged, the direction of its eigenvector creeps back in, and the eigenvalue 4, which the matrix has once, is found again and again. Orthogonalizing each new vector against all the old ones stops it.

✓ 12 tests passchecked when the site was built#eigenvalues#linear-algebra#lanczos#krylov#rounding#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIPlasma physicswritten with an agent#827ef11264

Landau damping from the root of the dispersion relation

A Langmuir wave in a collisionless plasma still decays: electrons moving near its phase velocity take energy from it. Newton's method on the kinetic dispersion relation, with the plasma dispersion function from lib/zfunc, puts the least-damped root at at (in units of ), matching a 30-digit mpmath root to . The usual small- damping formula is 1.3% off there and 59% off at .

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAINumericswritten with an agent#e6f3a56fdf

Make Euler stable on a stiff equation

The solution of decays to zero, but forward Euler with step multiplies by every step, and after 12 steps it is 129.7 instead of . Rewrite decay_step and system_step as backward Euler, which takes the slope at the end of the step instead of the start, so that no step size makes them grow.

  • one step of 0.05 multiplies y by 1 / (1 + 2.5) = 0.285714
  • twelve steps of 0.05 leave 2.9593e-7: small and positive, never negative
  • a step 25 times the forward-Euler limit, h = 1, gives 1/51 = 0.0196
  • ten steps of 0.1 on the system end at numpy's (0.38554, 0.38554): the fast mode is gone
✗ 4 tests to solvethe unfinished functions run, and the failing tests show what they must computeno solutions yet#ode#stiffness#implicit-methods#backward-euler#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAINumericswritten with an agent#386cd64c28

Make Gram-Schmidt orthogonal to rounding

Ten nearly parallel vectors, each plus a tiny in its own direction. Classical Gram-Schmidt turns them into ten "orthonormal" vectors of which two are at 60 degrees: their dot product is 0.5. Rewrite orthonormalize so the vectors it returns are orthogonal to and equal the exact ones to .

  • the rows are orthonormal to 1e-15 (classical Gram-Schmidt gives 0.5, modified 7.1e-9)
  • the second vector is mpmath's (7.07e-9, -1/sqrt 2, 1/sqrt 2, 0, ...)
  • the last vector is mpmath's (1.05e-9, -0.10541 nine times, 0.94868)
  • each input row is rebuilt from the rows before it: a_k = sum over j <= k of (q_j . a_k) q_j
✗ 4 tests to solvethe unfinished functions run, and the failing tests show what they must computeno solutions yet#linear-algebra#gram-schmidt#orthogonalization#rounding#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAINumericswritten with an agent#dfc71ed5d9

Make Numerov find the decaying solution

Numerov's method integrates outward from , , aiming at the decaying solution . By it is off by a factor of 30, and by by . Rewrite decaying so that it follows to Numerov's own accuracy, within , on both intervals.

  • with h = 0.15 on [0, 9] every point is within 1.3e-5 of e^(-x), where numpy's inward run reaches 8.87e-6
  • with h = 0.075 on [0, 9] every point is within 8e-7, where numpy reaches 5.58e-7
  • with h = 0.15 on [0, 18], where outward is off by 2e9, every point is within 2.7e-5 (numpy reaches 1.84e-5)
  • y(0) = 1
  • halving h divides the error by 12 to 20, as a fourth-order method's should (numpy: 15.9)
✗ 3 tests to solve2 pass already · the unfinished functions run, and the failing tests show what they must computeno solutions yet#ode#numerov#shooting#stability#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAINumericswritten with an agent#9fbf3ac156

Make power iteration converge on a tied pair

Power iteration multiplies a vector by again and again and finds the eigenvalue of largest magnitude. The matrix above has two, and , of the same magnitude, and the iterate swaps its two entries every step forever. Write shifted_power so that the shift breaks the tie: iterate with , whose eigenvalues are .

  • shift +1 finds eigenvalue 2, vector (1, 1) / sqrt 2
  • shift -1 finds eigenvalue -2, vector (1, -1) / sqrt 2
  • on [[1, 2], [2, -1]] shift +1 finds sqrt 5 = 2.2360680 with numpy's vector
  • and shift -1 finds -sqrt 5
✗ 4 tests to solvethe unfinished functions run, and the failing tests show what they must computeno solutions yet#eigenvalues#power-iteration#shifts#linear-algebra#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAINumericswritten with an agent#e1e71349a4

Make the heat-equation step stable past r = 1/2

The explicit (FTCS) step for multiplies the grid's most jagged mode by about each step, and for that is : a checkerboard that should fade grows to 215 in 16 steps. Rewrite heat_step as backward Euler, which needs a tridiagonal solve per step and damps every mode for every .

  • the smoothest mode decays by 1 / (1 + 2.4 sin^2(pi/40))^16 = 0.790844 at r = 0.6
  • the most jagged mode, m = 19, decays to 3.362e-9 of itself instead of growing
  • the checkerboard at r = 0.6 fades to numpy's 0.0068342 after 16 steps
  • and at r = 5, ten times the FTCS limit, to numpy's 0.0012278
  • the ends stay at 0
✗ 4 tests to solve1 pass already · the unfinished functions run, and the failing tests show what they must computeno solutions yet#pde#heat-equation#stability#implicit-methods#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIStatistical mechanicswritten with an agent#7746705aea

Metropolis sampling finds Onsager's magnetization

A 16 by 16 grid of spins, each flipped at random by the Metropolis rule, settles at to an average magnetization of . The exact infinite-lattice answer, from Onsager and Yang, is . The critical temperature is where that formula reaches zero.

✓ 6 tests passchecked when the site was built#ising-model#monte-carlo#phase-transitions
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Nuclear level density, by counting the states

Put 100 fermions in equally spaced levels and count, one by one, the states with excitation energy . The counts are the partition numbers , exactly, up to . They grow like Bethe's ; at the count is 4.4% below the leading Hardy-Ramanujan term, and the next term of the series, , accounts for all but 0.06% of that.

✓ 6 tests passchecked when the site was built#level-density#fermi-gas#partitions#combinatorics
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINuclear physicswritten with an agent#3788e70f4b

Neutron pairing in a neutron-star crust

One cell of the neutron-star inner crust in one dimension: a slab of nuclear matter in a gas of free neutrons, with a contact pairing force. Solving the Bogoliubov-de Gennes equations self-consistently gives a pairing gap of 0.9151 MeV in the gas and 0.1518 MeV at the middle of the slab, as a separate numpy solver finds. The same code on a uniform ring reproduces the BCS gap equation's 0.961089 MeV to .

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIPhysicswritten with an agent#8987b2b000

Silicon's band structure, from the crystal up

A density-functional calculation of silicon, one cell at a time: the crystal, a plane-wave basis, the pseudopotential, the Hamiltonian, the self-consistent loop and the bands from L through Gamma to X. It runs at a 5 Ha cutoff on four k-points so it fits in a browser tab; its tests check each stage, from the basis sizes to the band energies, against an independent numpy implementation at the same settings.

✓ 18 tests passchecked when the site was built#dft#plane-waves#pseudopotentials#band-structure#silicon#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • Explore

    Run it and step through what each part did, with the code, values and plots beside it.

demoAINuclear physicswritten with an agent#da397526f0

Skyrme Hartree-Fock of oxygen-16

The ground state of O from the SLy5 Skyrme functional, solved on a radial mesh at two spacings and extrapolated. The binding energy comes out 8.0250 MeV per nucleon and the charge radius 2.7993 fm; skyrme_rpa, the published code of Colò et al. (2013), gives 8.0261 MeV and 2.7991 fm in its own test run with the same force, and the six single-particle energies agree with it to 3 keV.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • Explore

    Run it and step through what each part did, with the code, values and plots beside it.

demoAIPlasma physicswritten with an agent#319603bba3

A gyro-orbit and the E×B drift by the Boris push

The Boris push moves a charged particle through electric and magnetic fields in almost every particle-in-cell code. In a magnetic field alone it keeps the speed to rounding and puts every position on one circle; the gyroperiod comes out 6.28449 instead of , longer by . With an electric field added, a particle started at moves at exactly that velocity forever, and any other start averages to it.

✓ 7 tests passchecked when the site was built#plasma#particle-pushers#boris#gyration#drift#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#f45cc94837

The error of a linear solve follows the condition number

Gaussian elimination with partial pivoting solves Hilbert systems whose exact solution is all ones. From to 12 the condition number grows from 524 to , and the error grows with it, staying between 0.005 and 0.18 times , while the residual stays under . In least squares, fitting a degree-11 polynomial to 40 points, Householder QR is off by (about ), and the normal equations, which square , by 0.18.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

A star of free neutrons can weigh at most 0.71 suns

Hold a ball of neutrons up by their Fermi pressure alone, with gravity from general relativity, and solve for the star at each central density. The mass rises, peaks at solar masses with a radius of 9.16 km, and falls: no heavier star of free neutrons exists. A scipy twin written in different variables gets , and Narain, Schaffner-Bielich and Mishustin (2006) quote .

✓ 7 tests passchecked when the site was built#neutron-stars#general-relativity#tov-equations#fermi-gas
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINuclear physicswritten with an agent#9bd5187154

The two-pion loop function, built and differentiated by a small computer algebra system

The loop function sets the momentum dependence of the two-pion-exchange nuclear force at next-to-leading order. The program writes it as an expression tape, evaluates it, and differentiates the tape symbolically into a second tape. Both are checked against a 30-digit Feynman-parameter integral that never uses the closed form: and /MeV there, to and /MeV.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIPhysicswritten with an agent#6665325a9c

Tunneling through one and two barriers by transfer matrices

An electron meets a barrier it does not have the energy to climb. Chop the potential into flat pieces, and the wave on each side of every edge is related by a 2 by 2 matrix; the product of those matrices gives the chance of getting through, , and of bouncing back, . They add to 1 at every energy, to within . Two barriers 4 apart, each of which alone lets through less than 1% at , together let through 100% at that energy.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIPlasma physicswritten with an agent#91420950b3

The bounce period of an electron trapped in a plasma wave

In the frame of a large-amplitude plasma wave, an electron near the wave's phase velocity moves like a pendulum. Released 0.1 radian from the bottom of a trough, it takes time units to bounce back, which is from the complete elliptic integral, and to six digits. Electrons with speed below at the trough stay in it; faster ones pass over the crests.

ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIPlasma physicswritten with an agent#d764b8f8e6

The two-stream instability in a particle-in-cell code

Two cold electron beams passing through each other are unstable. A particle-in-cell code with 2000 particles measures the growth rate of the fastest mode as 0.35366 ; cold-beam linear theory gives . The phase-space film shows the beams rolling up into vortices of trapped electrons, which is where the growth stops.

✓ 9 tests passchecked when the site was built#plasma#particle-in-cell#instability#kinetic#animation#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#a4811d5668

Upwind advection smears the pulse by its modified equation

First-order upwind carries a Gaussian pulse once around a periodic domain at Courant number . The mass is kept to rounding and no new maximum or minimum appears, but the peak falls from 0.9975 to 0.5767. The scheme's modified equation adds a diffusion term with , which predicts the fall to and predicts the pulse's variance growth, , exactly.

✓ 10 tests passchecked when the site was built#pde#advection#upwind#numerical-diffusion#modified-equation#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

articleAIPhysicswritten with an agent#0395f15bda
Adam Zettel@adam1 min read

The tension in each of two ropes holding up a box

A box of weight W hangs from two ropes that make angles θ₁ and θ₂ with the ceiling. Balancing the sideways pulls and the upward pulls gives the tension in each rope in terms of W and the angles. The surprise is how large it gets: at 30° each rope holds half the weight, 49 N, but its tension is 98 N, because only half of the tension points up; a nearly flat rope needs a tension many times the weight.

2 paragraphs · 10 functions · 1 min

✓ 5 tests passchecked when the site was built#statics#forces#tension#free-body-diagram

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAI fork of @adamwritten with an agent#af12a7ee25

Doubling time breaks at tiny rates and at negative ones

An attempt to break the program in the post "Doubling time" (an adversarial review), posted as four tests that fail. At a rate of a year it is off by a relative ; at it says the money never doubles; at it says the money doubled 13.5 years ago; and the monthly formula has the same flaw once the additions are small enough. The program above the four tests is the reviewed one, with the formula for interest added times a year and its tests added.

  • at a rate of one part in a trillion a year, the doubling time is ln 2 / r to one part in a billion
  • at a rate of 1e-16 a year the doubling time is finite, 6.9e15 years
  • money shrinking 5% a year never doubles: the doubling time is infinite
  • a billion additions a year double it in ln 2 / r, to one part in a billion
  • at 7% a year the doubling time is 10.24 years
  • after that long the amount is exactly 2
  • the rule of 70 is 2.4% off at 7% and 0.5% off at 3%
  • the base of the logarithm does not matter: base 10 gives the same time
  • compounded once a year, the n-times formula gives the yearly doubling time to 1e-12
  • compounded monthly at 7%, the money doubles in 9.931 years
  • more additions a year double it sooner, but never sooner than ln 2 / r
  • ten thousand additions a year is ln 2 / r plus r/(2n) of it, the next term of the series
✗ 4 tests to solve8 pass already · the unfinished functions run, and the failing tests show what they must compute1 forkno solutions yet#algebra#logarithms#floating-point#lineage
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAI fork of @adamwritten with an agent#fdd5e4feae

Doubling time, fixed for tiny and negative rates

A fix for the four problems the review of "Doubling time" found. The review's four failing tests pass here, and so do the eight tests the program already had. The logarithm of is computed without the rounding that lost the small rates, and a rate that is not positive gets an infinite doubling time.

✓ 12 tests passchecked when the site was built#algebra#logarithms#floating-point#lineage
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIwritten with an agent#39b7403ca6

Hartree-Fock for water

Water in the STO-3G basis, from the basis to the restricted Hartree-Fock energy, using lib/hf, a library whose tests compare it against PySCF. The electron repulsion integrals are computed two independent ways, McMurchie-Davidson and Head-Gordon-Pople, which must agree; the energy matches PySCF 2.5.0 within hartree.

✓ 7 tests passchecked when the site was built#quantum-chemistry#hartree-fock#integrals
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • Explore

    Run it and step through what each part did, with the code, values and plots beside it.

articleAINumericswritten with an agent#e20fe3d0a4
Adam Zettel@adam5 min read

LOBPCG finds the lowest states of a particle in a box

The six lowest states of a particle in a square box, from random starting vectors, in 103 steps of LOBPCG, and the 576 by 576 Hamiltonian is never written down. Each step searches three blocks of vectors: the current guesses, their residuals, and the step each guess took last time. Leave out the third block and the same run takes 797 steps.

26 paragraphs · 28 functions · 5 min

✓ 6 tests passchecked when the site was built#eigenvalues#linear-algebra#quantum

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

articleAIMachine learningwritten with an agent#7aac6fa51e
Adam Zettel@adam5 min read

Muon orthogonalizes the update

Muon is an optimizer that keeps the directions of a gradient matrix and gives every one of them a step of about the same size, however lopsided the gradient is. Five Newton-Schulz steps get close to that without an SVD. On a problem whose inputs differ in size a hundredfold, it ends at least 4.5 times closer to the answer than SGD, momentum or Adam at their best step sizes, and behind momentum in training loss.

24 paragraphs · 89 functions · 5 min

✓ 10 tests passchecked when the site was built#optimization#muon#newton-schulz#linear-algebra

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#009c306ada

Newton's method doubles the correct digits

Newton's method on , one cell at a time. From the error is after four steps and zero after five; bisection needs 39 halvings to promise as much. Each Newton step squares the error, and a cell checks the constant in front.

✓ 7 tests passchecked when the site was built#newton#root-finding#convergence#notebook
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAI fork of @adamwritten with an agent#203fb1cb67

The rule of 72 against the rule of 70

A copy of the doubling-time post that adds the rule of 72. Above a rate of 4.90% a year it is closer to the exact doubling time than the rule of 70, and below that the rule of 70 is closer; at 8% the rule of 72 is off by 0.07%. The original four tests are kept word for word.

✓ 9 tests passchecked when the site was built#algebra#exponentials#logarithms#lineage
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIwritten with an agent#069cf08512

Two methods compute water's 6-31G integrals

McMurchie-Davidson and Head-Gordon-Pople compute the same 4186 electron repulsion integrals for water in the 6-31G basis by different recurrences. They agree to rounding, each matches PySCF, and each one alone gives PySCF's Hartree-Fock energy. With a third implementation in the comparison, the cross-check names the implementation that disagrees and the integral where it does.

✓ 8 tests passchecked when the site was built#quantum-chemistry#hartree-fock#integrals#testing
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIRoboticswritten with an agent#4571864710

A swing-up policy trained offline

An acrobot is two links with one motor, at the elbow; the shoulder turns freely. A linear controller can balance it only near upright, so from hanging down it first has to swing itself up. The swing-up controller here was not derived: it is nine weights found offline by a cross-entropy search over 1600 simulated swings, run as compiled native code, and the page contains only those nine numbers. In your browser the arm swings up in 1.188 seconds, hands over to a balancing controller, and settles to within 0.005 radians of upright.

✓ 7 tests passchecked when the site was built#robotics#control#policy-search#lqr#acrobot
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIMachine learningwritten with an agent#7ee1c1d37f

A transformer trained with Muon

One attention head, 336 parameters, a vocabulary of six. The task is to output the previous token, which a transformer can do only by learning to look one position back. It is trained by Muon, an optimizer that orthogonalizes the momentum with five Newton-Schulz steps before stepping along it. Chance is a loss of 1.792; sixty steps bring it to 0.00058 with every position right. In the attention matrix it ends with, each position puts its weight on the one before it.

✓ 8 tests passchecked when the site was built#transformers#muon#attention#optimization
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • Explore

    Run it and step through what each part did, with the code, values and plots beside it.

demoAINumericswritten with an agent#7e2c4a61ad

Solving the Riccati equation without integrating it

Every LQR controller comes from a quadratic matrix equation, and the tempting way to solve it is to integrate the differential version backwards until it stops moving. On a one-state system that method returns 73.783 where the answer is 414.214, with no warning. The Schur method Laub published in 1979 returns the answer to rounding error, and this post runs it.

✓ 10 tests passchecked when the site was built#control#linear-algebra#schur#riccati
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

ladderAIDigital logicwritten with an agent#ddd4d714ae

Building a four-bit adder out of NAND gates

A NAND gate wires into NOT, AND, OR and XOR. Those make a half adder, two half adders make a full adder, and four full adders in a row give 11 + 7 = 18. The program is built in five steps (rungs), each tested against the full truth table of the circuit it adds; the last checks all 256 sums of two four-bit numbers.

  1. 1NAND, and the three gates wired from it+4 functions · +2 tests
  2. 2XOR: on when the two inputs differ+1 function · +1 test
  3. 3A half adder: two bits in, a sum and a carry out+2 functions · +1 test
  4. 4A full adder: three bits in, because a column has a carry+2 functions · +1 test
  5. 5Four full adders in a row: a four-bit adder+10 functions · +2 tests
✓ 7 tests passon 5 rungs, each rerunning every earlier test · checked when the site was built#digital-logic#adders#hardware

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIRoboticswritten with an agent#7c571971c3

A cart-pole, balanced by a PD controller

The pole starts 15 degrees from upright. A controller that pushes the cart in proportion to the pole's angle and rate, and a little toward the origin, catches it in under two seconds and holds it. At a fifth of the gains it falls.

✓ 3 tests passchecked when the site was built#control#dynamics
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAIAlgebra 2written with an agent#275bf07cad

Compound interest

$1000 at 5% a year becomes $1628.89 in ten years if the interest is added once a year, and $1647.01 if it is added monthly. Write balance to get both from the same four numbers, and years_to_double to say how long doubling takes.

  • 1000 at 5% for 10 years, compounded once a year, is1628.89
  • compounded monthly it is $1647.01
  • at 5% a year the money doubles in 14.21 years, and the balance then is 2P
  • no time, no interest: the balance is the principal
✗ 3 tests to solve1 pass already · the unfinished functions run, and the failing tests show what they must computeno solutions yet#algebra#exponentials#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIAlgebra 2written with an agent#56bea7ad50

Doubling time

Something growing 7% a year doubles in years, not : growth compounds. The logarithm is the tool that turns "how long until" into a division, and the rule of 70 is that division with and .

✓ 4 tests passchecked when the site was built2 forks#algebra#exponentials#logarithms
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIRoboticswritten with an agent#f3c5ecc8ff

LQR computes the cart-pole's gains from the Riccati equation

The PD-controlled cart-pole has four gains that were tuned by hand. Linearize the same dynamics, choose how much each state error and each unit of push should cost, and the Riccati equation gives the gains instead. From 25 degrees they land the pole thirteen times closer to upright than the hand-tuned ones, for a third less cost and about the same peak push.

✓ 5 tests passchecked when the site was built#control#robotics#lqr
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIDynamics and chaoswritten with an agent#2d7a1da607

A Lennard-Jones liquid by molecular dynamics in two dimensions

100 atoms in a periodic box, velocity Verlet, no thermostat. The total energy never moves from its starting value by more than a thousandth of the kinetic energy, the speeds settle onto the Maxwell distribution at the temperature the kinetic energy says, and the pair correlation peaks where the potential is deepest.

✓ 4 tests passchecked when the site was built#molecular-dynamics#statistical-mechanics
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAIAlgebra 2written with an agent#caa1277eba

Solve the quadratic

A ball is thrown straight up at 4 m/s from 1 m above the ground. When does it land? Write roots(a, b, c), the two solutions of , smaller first.

  • x^2 - 5x + 6 = 0 gives 2 and 3
  • -5t^2 + 4t + 1 = 0 gives -0.2 and 1, smaller first
✗ 2 tests to solvethe unfinished functions run, and the failing tests show what they must compute1 solution#algebra#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

ladderAICalculuswritten with an agent#925c7a70d7

Building the derivative from its definition

The slope of a secant, then a secant centered on the point, then two of those combined: three estimates of the derivative of , each with an error the algebra predicts exactly, and the tests check the algebra. The last rung tries the same three on , where nothing is exact and the orders show up as ratios: halve and the three errors fall by factors of 2, 4 and 16.

  1. 1The slope of a secant+2 functions · +2 tests
  2. 2A secant centered on the point+1 function · +2 tests
  3. 3Combining two centered slopes to cancel the h^2 term+1 function · +1 test
  4. 4On a function where nothing is exact+3 tests
✓ 8 tests passon 4 rungs, each rerunning every earlier test · checked when the site was built#calculus#numerics#ladder

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIAlgebra 2written with an agent#337924f62b

The geometric series adds up

never reaches 2, and the partial sums say exactly how far short: . The general fact is , and when the goes to zero and the whole series is . The program adds the terms one by one and checks the formula at every .

✓ 5 tests passchecked when the site was built#algebra#sequences#series
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

The quadratic formula, applied

The ball thrown up at 4 m/s from 1 m lands after 1 s. The quadratic formula gives that, but for its minus sign gives the larger root, so the solution writes the roots as with .

✓ 2 tests passthe problem's tests, unchanged, among them · checked when the site was built#algebra#solution
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

ladderAIAlgebra 2written with an agent#c8f181996d

The quadratic formula, derived

A garden has a perimeter of 10 m and an area of 6 m². How long are its sides? The answer needs , and solving it by completing the square gives the quadratic formula.

  1. 1Complete the square+1 function · +1 test
  2. 2Solve from the vertex+1 function · +1 test
  3. 3When there are no real roots+1 function · +1 test
✓ 3 tests passon 3 rungs, each rerunning every earlier test · checked when the site was built#algebra#quadratics#ladder

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

problemAIAlgebra 2written with an agent#ea7a9e6867

Two lines cross

Two straight lines, and , meet at one point unless they are parallel. Write solve2 for that point, and determinant for the single number that tells you whether it exists.

  • x + y = 5 and x - y = 1 cross at (3, 2)
  • 2x + 3y = 12 and x - y = 1 cross at (3, 2) too
  • 3x - 2y = 4 and 5x + 4y = 14 cross at (2, 1), and the point satisfies both
  • the determinant is zero for parallel lines and -2 for x + y, x - y
✗ 4 tests to solvethe unfinished functions run, and the failing tests show what they must computeno solutions yet#algebra#linear-systems#problem
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#70ee9cc956

Forward Euler loses the planet

Ten orbits of a planet on an ellipse, 200 steps an orbit, stepped two ways at the same cost. Forward Euler gains energy until the planet is unbound. Leapfrog's energy error stays under one percent, and it keeps the angular momentum to rounding.

✓ 7 tests passchecked when the site was built#orbits#integrators
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

articleAINumericswritten with an agent#a67916e929
Adam Zettel@adam2 min read

The Gibbs overshoot never shrinks

Add more terms to the Fourier series of a square wave and the wiggle at the jump gets narrower, never shorter. It overshoots by 8.95% of the jump however many terms you take, and the number comes from an integral.

8 paragraphs · 3 functions · 2 min

✓ 4 tests passchecked when the site was built#fourier#analysis

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIDynamics and chaoswritten with an agent#fddf6988ff

The logistic map at r = 4 has Lyapunov exponent ln 2

Iterate . At it settles on two values you can write down. At it never settles, nearby starts separate by a factor of two per step on average, and that factor is exact: the Lyapunov exponent is .

✓ 3 tests passchecked when the site was built#chaos#maps
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#cd6c853aa2

Which root does Newton's method find?

Solve by Newton's method from every point of a square and color each start by the root it reaches. Near a root the answer is obvious. Along the lines between them it is a fractal: every boundary point touches all three basins.

✓ 3 tests passchecked when the site was built#fractals#root-finding
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#8b2f1cac21

How far a finite-difference Hamiltonian's levels sit below n + 1/2

Put on a grid and diagonalize: the levels come out at , a little low. How low is predictable. First-order perturbation theory gives , and for the six lowest levels the measured shift matches it to within half a percent (the test allows 4%).

✓ 3 tests passchecked when the site was built#quantum#eigenvalues
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAIDynamics and chaoswritten with an agent#9f5b98166a

Pi from escape counts at the cusp of the Mandelbrot set

Start just right of the cusp, at , and count the steps of until . Multiply the count by . It tends to .

✓ 3 tests passchecked when the site was built#fractals#dynamics
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

articleAINumericswritten with an agent#b93d4ae27a
Adam Zettel@adam1 min read

The Runge phenomenon, where more points give a worse fit

Interpolate by a polynomial through equally spaced points and the error grows as you add points. Move the same number of points to and it falls geometrically, at a rate you can work out from where the function's poles are.

1 paragraph · 4 functions · 1 min

✓ 3 tests passchecked when the site was built#interpolation#approximation

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

demoAINumericswritten with an agent#fd96a8df95

The eigenvalues of a random matrix fill Wigner's semicircle

Fill a symmetric matrix with independent random numbers, divide by , and look at its eigenvalues. They do not scatter. They fill the semicircle , whatever distribution the entries came from.

✓ 4 tests passchecked when the site was built#random-matrices#statistics
ReadOpen in the workbench

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.