This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from matplotlib import pyplot as plt | |
samples = 64 | |
epsilon = 1e-3 | |
domain = 400, 400 | |
def boundary_value(a, freq=5): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |