Skip to content

Instantly share code, notes, and snippets.

@mdnestor
mdnestor / reaction-diffusion-wave-formulas.md
Created September 20, 2025 11:10
Deriving formulas for reaction diffusion waves

In a recent post I showed that whenever the reaction diffusion equation

$$ \dot u = \Delta u + f(u) $$

has traveling wave solutions connecting $1$ to $0$ with speed $c$, then you can determine the sign of $c$ by computing the integral of $f$:

$$ \mathrm{sign}(c) = \mathrm{sign}(\int_0^1 f(u) du) $$

(and this also holds when $\Delta$ is replaced by some other types of linear operator $L$, like convolution for integro-differential equations).

@mdnestor
mdnestor / reaction-diffusion-wave-speed-sign.md
Last active September 21, 2025 19:45
Quick derivations of bistable wave speed sign in reaction-diffusion equations

In many variants of scalar reaction-diffusion equations with monotone bistable traveling waves, there is a concise formula for computing the sign of the wave speed.

First I will prove a general version of this formula that applies to continuous time, and show how it applies specifically to both PDE and integro-difference equations. Then I will prove a discrete-time variation for integrodifference equations.

The classic references on this specifically for reaction-diffusion PDE are:

@mdnestor
mdnestor / scalar-reaction-diffusion.md
Last active September 13, 2025 22:53
Some notes on scalar reaction diffusion equations

How good is your (my) intuition for reaction diffusion equations? I mean equations in this form:

$$ \dot u = \Delta u + f(u) $$

where $u=u(x,t)$. Let me test you with examples. I give you the reaction term $f$ and initial data $u_0$ and you tell me what happens.

Note: you should always first consider the "local dynamics" given by the ODE

@mdnestor
mdnestor / ornstein_uhlenbeck.py
Created April 12, 2025 18:30
Ornstein-Uhlenbeck process
import numpy
import matplotlib.pyplot
def ornstein_uhlenbeck(tmax, dt, x0=0, r=1, sigma=1, mu=0):
T = numpy.arange(0, tmax + dt, dt)
X = numpy.zeros(T.shape[0])
X[0] = x0
for i in range(T.shape[0] - 1):
dWt = numpy.random.normal(scale=dt**0.5)
X[i+1] = X[i] + (-r) * (X[i] - mu) * dt + sigma * dWt
@mdnestor
mdnestor / brownian_bridge_recursive.py
Created April 12, 2025 18:24
recursive sampling of brownian bridge
import numpy
def bridge(x0 = 0, x1 = 0, dt = 1, n = 0):
if n == 0:
return [x0, x1]
m = numpy.random.normal((x0 + x1) / 2, dt/4)
bridge1 = bridge(x0, m, dt/2, n-1)
bridge2 = bridge(m, x1, dt/2, n-1)
return bridge1[:-1] + bridge2
@mdnestor
mdnestor / spigot.py
Last active March 18, 2025 22:12
spigot algorithm for euler's constant
# http://stanleyrabinowitz.com/bibliography/spigot.pdf
from typing import *
def espigot(n: int) -> List[int]:
A = [1 for _ in range(n + 4)]
q = 0
digits = []
for _ in range(n + 2):
A = [10*a for a in A]
for i in range(n + 4):
@mdnestor
mdnestor / diffe-hellman.py
Created May 9, 2024 09:25
Diffe-Hellman key exchange in Python 3
# Diffe-Hellman key exchange
# https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
import math
def least_prime_factor(n: int) -> int:
assert(n > 1)
for d in range(2, int(n**(1/2))):
if n % d == 0:
return d
# Algorithm Minecraft uses to convert world seeds to integers
# Shows why `creashaks organzine` yields seed 0, whereas by design manually inputting seed 0 uses random seed.
def seed(x: str) -> int:
if isinstance(x, int):
return x
s = 0
for c in x:
s = ((31*s + ord(c)) ^ 0x80000000) & 0xFFFFFFFF - 0x80000000
return s
import numpy as np
import pygame
from scipy.ndimage import convolve
def game_of_life(state):
kernel = np.array([[2,2,2],[2,1,2],[2,2,2]])
activation = np.vectorize(lambda x: int(5 <= x <= 7))
return activation(convolve(state, kernel, mode='wrap'))
pygame.init()