Skip to content

Instantly share code, notes, and snippets.

@yangyushi
yangyushi / join_pairs.cpp
Last active July 13, 2020 15:19
Join overlapped labels recursively, eg [(2, 3), (3, 5), (2, 6), (8, 9), (9, 10)] → [(2, 3, 5, 6), (8, 9, 10)]
#include <iostream>
#include <vector>
#include <set>
using namespace std;
using Pair = vector<int>;
using Pairs = vector<Pair>;
bool should_join(Pair p1, Pair p2){
for (int n1 : p1){
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
b1 = np.array([
[1, 0, 0],
[0, 1, 0],
@yangyushi
yangyushi / random_sphere_surface.py
Last active May 13, 2020 22:56
generate random points on the surface of a sphere
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot, scatter, imshow
from mpl_toolkits.mplot3d import Axes3D
N = 1000
z = np.random.uniform(-1, 1, N)
phi = np.random.uniform(-np.pi, np.pi, N)
rxy = np.sqrt(1 - z**2)
@yangyushi
yangyushi / parse_xyz.cpp
Last active April 7, 2022 14:55
Parse .xyz file and load to std::vector
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
using XYZ = std::array<double, 3>;
using Coord3D = std::vector<XYZ>;
@yangyushi
yangyushi / rotation_opt.py
Last active March 31, 2020 12:15
This gist shows a `numba.njit` accelerated code is faster than its `einsum` equivalent for composing/applying a lot of rotation matrices
from time import time
import numpy as np
from numba import njit, prange
@njit(parallel=True)
def ndot1(A, B):
"""
Calculate C with
C[i, j, k] = sum_q( A[i, j, q] · B[i, q, k] )
@yangyushi
yangyushi / neighbour_list.py
Last active April 3, 2020 18:07
Algorithm of neighbour list introduced in book "computer simulation of liquids" chapter 5
#!/usr/bin/env python3
import math
from itertools import product
import numpy as np
from numba import njit
from numba.typed import List
@njit
def build_verlet_nlist(nlist: List, point: List, particles: np.ndarray, rl:float):
@yangyushi
yangyushi / check_tracking_example.py
Last active August 31, 2020 14:58
Handy function to check 3D particle tracking results
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from matplotlib.pyplot import plot, scatter, imshow
def get_fake(size, number, radius, blur):
"""
Generate a fake image for ideal gas (random positions).
./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-python3interp=yes \
--with-python3-config-dir=$(python3-config --configdir) \
--enable-perlinterp=yes \
--enable-luainterp=yes \
--enable-cscope \
--prefix=/usr/local
@yangyushi
yangyushi / reform.py
Last active May 16, 2020 11:35
format the entry key names in your bib file to please your supervisor
#!/usr/bin/env python
import re
import sys
from textwrap import dedent
STOPWORDS = [ # common stop words from package nltk
"i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "you're", "you've", "you'll",
"you'd", "your", "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "she's",
"her", "hers", "herself", "it", "it's", "its", "itself", "they", "them", "their", "theirs",
"themselves", "what", "which", "who", "whom", "this", "that", "that'll", "these", "those", "am",
ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers/X11 /usr/local/include/X11
./configure\
--enable-optimizations\
--prefix=/usr/local\
--enable-shared\
--with-openssl=$(brew --prefix openssl)\
CPPFLAGS="-I/usr/local/opt/openssl/include"\
LDFLAGS="-L/usr/local/opt/openssl/lib"