Skip to content

Instantly share code, notes, and snippets.

View GregTJ's full-sized avatar
💻

Gregory Johnson GregTJ

💻
  • Montana
View GitHub Profile
@GregTJ
GregTJ / sort.py
Created October 8, 2019 08:03
Pixel sorting with Numpy
import numpy as np
from PIL import Image
def sort_pixels(image, condition, rotation=0):
pixels = np.rot90(np.array(image), rotation)
luminosity = np.sum(pixels, axis=2) / (255 * 3)
mask = np.zeros_like(luminosity)
mask[condition(luminosity)] = 1
@GregTJ
GregTJ / jacobian.py
Last active May 7, 2024 15:35
Affine Map, Jacobian, Curl, and Divergence from Vector Field with Numpy
import numpy as np
from matplotlib import pyplot as plt
# Arbitrary vector field example.
shape = 11, 11
dims = len(shape)
domain = -2, 2
x, y = np.meshgrid(*(np.linspace(*domain, num=d) for d in shape))
field = np.stack((-2 * x * y, x ** 2 + 2 * y - 4))
@GregTJ
GregTJ / potential.py
Last active June 10, 2021 20:36
Newtonian Kernel: Green's Function of The Poisson Equation
import numpy as np
from scipy.special import gamma
def n_ball_volume(n):
return np.pi ** (n := (n - 1) / 2) / gamma(n + 1)
def newtonian_potential(shape, ds=1):
kernel = np.indices(shape) - np.floor_divide(shape, 2).reshape(-1, *(1,) * len(shape))
@GregTJ
GregTJ / max_cliques.py
Last active April 8, 2021 02:08
Naïve Bron-Kerbosch algorithm for finding all maximal cliques of an undirected graph.
from typing import Dict, Set, List, Any
def maximal_cliques(g: Dict[Any, Set]) -> List[Set]:
def bron_kerbosch(r, p, x):
if not p | x:
cliques.append(r)
for v in p.copy():
bron_kerbosch(r | {v}, p & g[v], x & g[v])
p.remove(v)
@GregTJ
GregTJ / WoS.py
Last active September 29, 2023 01:37
Naïve Walk On Spheres implementation for the Laplace equation on a disk with sinusoidal boundary values.
import numpy as np
from matplotlib import pyplot as plt
samples = 64
epsilon = 1e-3
domain = 400, 400
def boundary_value(a, freq=5):
@GregTJ
GregTJ / local_variance.py
Last active April 16, 2021 00:33
Selecting regions of an image based on local variance.
import cv2
import numpy as np
window = 100, 100 # Window size.
number = 3 # Number of boxes.
padding = 2 # Minimum spacing between boxes.
source = 'input.jpg'
destination = 'output.jpg'
# Read the image and initialize variance array.
@GregTJ
GregTJ / GPU_WoS.py
Last active August 22, 2023 19:49
GPU Walk On Spheres Implementation with CuPy.
import cupy as cp
from matplotlib import pyplot as plt
domain = 500, 500
wos_samples = 200
# Use step count instead of epsilon threshold for efficiency.
# This will result in a different bias than traditional WoS.
wos_steps = 5
@GregTJ
GregTJ / levi_civita.py
Last active June 16, 2022 00:52
Levi-Civita symbol in n-dimensions.
import sparse
from typing import Tuple, Generator
def sjt_permutations(n: int) -> Generator[Tuple[int], None, None]:
"""An implementation of the Steinhaus–Johnson–Trotter
algorithm with Even's speedup for generating
permutations in order of alternating parity."""
# Each element of the permutation is