Skip to content

Instantly share code, notes, and snippets.

View orlp's full-sized avatar
⚠️
This user's social credit score is unknown.

Orson Peters orlp

⚠️
This user's social credit score is unknown.
  • Leiden, Netherlands
View GitHub Profile
// Original idea by Mikkel Gjoel (https://computergraphics.stackexchange.com/a/5952/10515),
// simplified to one readable function.
// Demo at https://www.shadertoy.com/view/Wts3zH.
// Dithers and quantizes color value c in [0, 1] to the given color depth.
// It's expected that rng contains a uniform random variable on [0, 1].
uint dither_quantize(float c, uint depth, float rng) {
float cmax = float(depth) - 1.0;
float ci = c * cmax;
@orlp
orlp / gen.py
Last active July 26, 2018 00:50
from collections import namedtuple, defaultdict
EarleyItem = namedtuple("EarleyItem", ["rule", "dot", "origin"])
def item_advance(item):
return EarleyItem(rule=item.rule, dot=item.dot + 1, origin=item.origin)
class Generator:
def __init__(self, G, terminals):
self.G = [["S'", G[0][0]]] + list(G)
@orlp
orlp / gf.py
Created November 18, 2017 11:57
import sympy as sp
import itertools as it
from sympy.physics.quantum import TensorProduct
def companion_matrix(p):
"""Gives companion matrix for polynomial p."""
p = sp.Poly(p)
assert p.is_monic
// Pattern-defeating quicksort
// Copyright Orson Peters 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/sort/ for library home page.
@orlp
orlp / genfilter.py
Last active February 7, 2017 20:30
# Standard, Hardcore, tmpstandard, tmphardcore
league = "tmpstandard"
categories = [
"DivinationCards",
"Essence",
"UniqueMap",
"UniqueFlask",
"UniqueWeapon",
"UniqueArmour",
"UniqueMap",
#IfWinActive, Path of Exile ahk_class POEWindowClass
#MaxHotkeysPerInterval 500
#SingleInstance force
SendMode Input
; Install the Fontin font http://www.exljbris.com/fontin.html.
; Edit log_file to your Client.txt path.
; Edit overlay_x, overlay_y to fit your desire. Default is right under the timer for 1920x1080.
; Your game needs to be in windowed fullscreen mode.
@orlp
orlp / peg.py
Last active July 3, 2018 02:10
PEG parser in Python.
from __future__ import unicode_literals
import sys
# We only use unicode in our parser, except for __repr__, which must return str.
if sys.version_info.major == 2:
repr_str = lambda s: s.encode("utf-8")
str = unicode
else:
repr_str = lambda s: s
from sympy.ntheory import factorint, multiplicity
import operator as op, functools, itertools, fractions
factorlist = lambda n: sum(([p] * k for p, k in factorint(n).items()), [])
totient_factors = lambda n: sum((factorlist(p-1) + [p]*(k-1)
for p, k in factorint(n).items()), [])
divisors = lambda fs: [functools.reduce(op.mul, c, 1)
for l in range(len(fs) + 1) for c in itertools.combinations(fs, l)]
leadzero = lambda n, l: str(n).zfill(l) if l else ""
import bisect
import io
# We use 17-bit internal values with 15-bit probabilities.
CODE_VALUE_BITS = 17
FP_MAX = (1 << CODE_VALUE_BITS) - 1
FP_QUARTER = FP_MAX//4 + 1
FP_HALF = 2*FP_QUARTER
FP_3QUARTER = 3*FP_QUARTER
FREQ_BITS = CODE_VALUE_BITS - 2
import hashlib
import math
def prf_under_n(N, key, tweak):
b = math.ceil(math.log(N, 2))
c = 0
while True:
h = ""
while len(h) * 4 < b: