Skip to content

Instantly share code, notes, and snippets.

@yangyushi
yangyushi / shortest_length.py
Created January 26, 2021 18:49
find the shortest path in a graph, applied to real-life train station problem.
from itertools import combinations
stations = [
"a", "b", "c", "d", "e",
"f", "g", "h", "i", "j",
"k", "l", "m", "n"
]
line1 = ["a", "b", "f", "d", "e"]
line2 = ["c", "e", "j", "g", "i"]
@yangyushi
yangyushi / alignment_sphere.py
Created November 30, 2020 21:17
applying alignment spherical boundary
import numpy as np
import matplotlib.pyplot as plt
N = 5
v = np.random.random((N, 3))
v = v / np.linalg.norm(v, axis=1)[:, None]
r = np.random.randn(N, 3)
r = r / np.linalg.norm(r, axis=1)[:, None]
angle = np.arccos(np.einsum('ij,ij->i', v, r))
@yangyushi
yangyushi / pcross.py
Last active August 3, 2021 14:42
pairwise dot product with einsum
#!/usr/bin/env python3
import numpy as np
from time import time
a = np.random.random((10000, 3))
b = np.random.random((10000, 3))
t0 = time()
c = np.array([np.cross(x, y) for x, y in zip(a, b)])
@yangyushi
yangyushi / pdist_pbc.py
Created November 2, 2020 14:36
Different ways to get pairwise distance in cubic box with PBC in Python
"""
Different functions to get pairwise distance in cubic box with PBC
Args:
positions: the positions of particles, shape (N, dim)
box (int or float): the size of the box
Return:
np.ndarray: the pair-wise distances, shape (N, N)
"""
@yangyushi
yangyushi / normal_equation.py
Created October 23, 2020 16:14
estimate polynomial fit with the normal equation
import numpy as np
n_sample = 8
x = np.linspace(0, 10, n_sample)
par = [1e-4, 2e-3, -3e-2, -2e-1, 1]
order = len(par) - 1
# the shape of x_poly is (order + 1, n_sample)
x_poly = x[None, :] ** np.arange(order + 1)[:, None]
y = np.polyval(par, x)
@yangyushi
yangyushi / positions_hard_sphere.xyz
Last active October 23, 2020 12:15
calculate the [r]adial [d]istribution [f]unction (g or r) in orthogonal PBC box with arbitrarily large cutoff distance in all the normal dimensions
125
H 16.658345122844334 5.438348200947194 15.278503165303777
H 11.904690376717243 5.1529666697956795 13.25948396116124
H 9.62667890619329 7.438653597799468 5.290790255780805
H 6.017600501383954 7.235017543794983 12.626298470129054
H 9.33671886855014 10.014970018631432 4.270413237226138
H 2.168893723778388 0.9019879254927321 7.552264051940874
H 10.211873888717818 10.346039720521441 13.019842912794617
H 7.797419763761877 17.778647693825153 15.882609513949191
@yangyushi
yangyushi / explore-xray.ipynb
Last active October 19, 2020 22:12
RANSAC Spine from X-Ray
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yangyushi
yangyushi / computational-nanoscience-concrete-ideas.ipynb
Created October 13, 2020 19:10
computational-nanoscience-concrete-ideas.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yangyushi
yangyushi / nplocate_demo.ipynb
Created September 6, 2020 21:53
demonstrating my particle tracking idea
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yangyushi
yangyushi / lz77.py
Created September 3, 2020 11:46
the LZ77 dictionary transformation for data compression
def get_longest_token(search_buffer, look_ahead_buffer, offset):
"""
Get the longest duplicated token in both search buffer and look ahead buffer
given a constant offset value
Args:
search_buffer (list): the search buffer
look_ahead_buffer (list): the look ahead buffer
offset (int): the offset, starting from tail of the search buffer