Skip to content

Instantly share code, notes, and snippets.

@arshednabeel
arshednabeel / animate_flock.py
Created April 24, 2024 08:35
Animate collective movement of flocks from trajectories.
'''
A simple animation function to animate collective movement of flocks, given trajectory data.
(c) Arshed Nabeel, 2024
'''
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
@arshednabeel
arshednabeel / vicsek.py
Last active May 6, 2024 05:22
A minimal implementation of the Vicsek model in ~50 lines of code
import numpy as np
from tqdm import trange
def get_neighbour_matrix(x, L, R):
dx = np.subtract.outer(x[:, 0], x[:, 0])
dy = np.subtract.outer(x[:, 1], x[:, 1])
dx[dx > (L / 2) ** 2] -= (L / 2) ** 2
dy[dy > (L / 2) ** 2] -= (L / 2) ** 2
pair_dist = dx ** 2 + dy ** 2
@arshednabeel
arshednabeel / numpy_flatten.md
Last active November 29, 2017 20:29
NumPy Noob Trick: Flatten out all except the last dimension of an ndarray.

The following command flattens out all except the last dimension of an n-d array; useful, for example, to create a feature vector. W_flat = W.reshape((-1,W.shape[-1]))