Skip to content

Instantly share code, notes, and snippets.

View ziap's full-sized avatar

Zap ziap

View GitHub Profile
@ziap
ziap / qrls.py
Last active August 5, 2025 04:41
Exact rational least squares using Gram-Schmidt orthogonalization
from fractions import Fraction
def least_squares(A, b):
b = list(map(Fraction, b))
m = len(A)
n = len(A[0]) if m else 0
R = [[Fraction(0) for _ in range(n)] for _ in range(n)]
Q_cols = []
@ziap
ziap / fft-blur.py
Created July 26, 2025 06:43
Efficient binomial-approximated gaussian blur with FFT
def binomial_filter_fft(filter_size, fft_size):
k = np.arange(fft_size)
z = -2j * np.pi * k / fft_size
L = np.log1p(np.exp(z)) - np.log(2)
logH = (filter_size - 1) * L
return np.exp(logH)
def conv_separable_fft(img2d, filter_size, filter):
H, W, C = img2d.shape
@ziap
ziap / eigh6.py
Last active July 4, 2025 14:13
robust, analytic eigensolver for symmetric 3x3 matrix, faster than np.linalg.eigh, based on <https://www.geometrictools.com/Documentation/RobustEigenSymmetric3x3.pdf> with some modifications
"""
eigh6.py — Fast symmetric 3×3 eigen‐decomposition with Numba
This module provides a highly optimized, stand-alone (no Numpy) implementation
of the eigenvalue decomposition for real symmetric 3×3 matrices. It returns the
three real eigenvalues in ascending order along with an orthonormal basis of
eigenvectors. The implementation is designed for use with Numba’s @njit
decorator and features:
- Analytic solution for eigenvalues via closed‐form formulas (trigonometric
@ziap
ziap / pcg32.zig
Created February 23, 2025 10:49
PCG32 RSH-RR with vectorized 64-bit generation
const Pcg32 = struct {
state: u64,
fn next_u32(self: *Pcg32) u32 {
const s = self.state;
self.state = s *% 0x5851f42d4c957f2d +% 0x14057b7ef767814f;
const xorshifted: u32 = @truncate((s ^ (s >> 18)) >> 27);
const rot: u5 = @intCast(s >> 59);
return (xorshifted >> rot) | (xorshifted << -% rot);
@ziap
ziap / zxhash.c
Last active February 23, 2025 07:24
A custom fast, non-cryptographic hash function
#include <stdint.h>
static inline uint64_t multiply_mix(uint64_t x, uint64_t y) {
__uint128_t m = (__uint128_t)x * (__uint128_t)y;
uint64_t hi = m >> 64;
uint64_t lo = m;
return lo ^ hi;
}
@ziap
ziap / example.zig
Last active February 27, 2025 14:20
Algorithm for initializing large RNGs from strings and other low-entropy sources
const std = @import("std");
const seeder = @import("seeder.zig");
const Xoshiro256 = struct {
s: [4]u64,
pub fn next(self: *Xoshiro256) u64 {
const S = struct {
inline fn rotl(data: u64, rot: u6) u64 {
import asyncio
from base64 import urlsafe_b64encode
from hashlib import sha384
import os
import aiofiles
from aiohttp.client import ClientSession
# Fake user agent to trick the google font api to give us the .woff2 fonts
# Retrive one by typing `navigator.userAgent` in the browser's console
@ziap
ziap / inverse-index.cpp
Last active October 17, 2024 07:34
Compile time inverse indexing in C++
#include <cstdint>
#include <string_view>
#include <memory>
constexpr uint32_t fnv_1a(std::string_view sv) {
uint32_t hash = 0x811c9dc5;
for (char c : sv) {
hash ^= (uint8_t)c;
hash *= 0x01000193;
}
@ziap
ziap / readline.c
Created October 14, 2024 07:59
Read a line from stdin into a malloc'd buffer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *read_line_malloc(void) {
size_t sz = 64;
char *buf = malloc(sz);
if (!fgets(buf, sz, stdin)) {
*buf = '\0';
@ziap
ziap / fixed-bigint.cpp
Created October 7, 2024 08:39
Fixed-width big integer in C++
#include <cstdint>
#include <cstring>
#include <charconv>
#include <iostream>
template<const int N>
struct BigInt {
static constexpr size_t digit_count = 9;
static constexpr uint32_t base = 1000000000;
uint32_t limbs[N];