Skip to content

Instantly share code, notes, and snippets.

@steinelu
steinelu / segment_tree.py
Last active July 15, 2023 17:59
Segment Tree Python
import math
class SegmentTree:
def __init__(self, arr, function, neutral_element):
self.m = len(arr)
k = math.ceil(math.log2(self.m)) + 1
self.n = 2**k
self.arr = arr
self.tree = [neutral_element for _ in range(self.n)]
self.foo = function
self.neutral_element = neutral_element
@steinelu
steinelu / CartesianTree.c
Last active August 25, 2023 09:02
Small C program that constructs a Cartesian tree from an array (and generates a Graphviz dot file from it)
#include <stdio.h>
#include <stdlib.h>
struct Node{
struct Node *parent;
struct Node *left;
struct Node *right;
int index;
int value;
};
@steinelu
steinelu / argsort.zig
Last active January 1, 2025 23:19
zig argsort example
const std = @import("std");
const builtin = @import("builtin");
pub fn main() !void {
const n = 20;
var idx : [n]u8 = undefined;
for (&idx, 0..) |*id, i| {
id.* = @intCast(i);
}
@steinelu
steinelu / gauss_wasserstein.py
Last active January 20, 2026 13:09
Python function calculating the Wasserstein distance over two Gaussian distributions
def wasserstein_distance_gaussian_analytical(G1, G2):
mu1, sigma1 = G1
mu2, sigma2 = G2
diff_mean = mu1 - mu2
squared_diff_mean = np.dot(diff_mean, diff_mean) # euclidean norm
sigma1_sqrt = linalg.sqrtm(sigma1)
sigma_product = np.dot(sigma1_sqrt, np.dot(sigma2, sigma1_sqrt))
sigma_product_sqrt = scipy.linalg.sqrtm(sigma_product)