Skip to content

Instantly share code, notes, and snippets.

View frostburn's full-sized avatar

Lumi Pakkanen frostburn

View GitHub Profile
@frostburn
frostburn / fractions_bitwise.py
Created December 19, 2011 16:36
Bitwise operations for fractions in Python.
"""This module defines bitwise operations for fractions.
In the same way that rational numbers have repeating decimal expansions ie. 1/3 = 0.333...
they also have repeating binary expansions: 1/3 = 0.010101...
Bitwise operators can be extended to such expansions and then converted back to rational numbers.
Just like we usually don't write 1 as 0.999... this module follows the convention that
1 = 1.000... even when expanded in binary.
The inversion operator '~' is not defined as ~1 = ...1110.111... would violate this convention.
For the same reason bitwise operations between negative numbers result in undefined behaviour.
The option to use one's complement for negative numbers ie. -1 = ...1110.111... = ~1
@frostburn
frostburn / Cbrt.hs
Last active April 9, 2018 05:02
Cube root in Haskell
--{-# LANGUAGE TemplateHaskell #-}
--import Test.QuickCheck
--import Test.QuickCheck.All
--prop_cbrt :: Double -> Bool
--prop_cbrt x = abs (x - cbrt x ^ 3) < 1e-12
--main = ($quickCheckAll)
module Cbrt (cbrt) where
@frostburn
frostburn / ball.py
Last active December 17, 2015 17:49
Just a little physics demo
from __future__ import division
"""
This module defines a Ball object for performing lossless kinematics over two dimensional Galilean space.
Author: Pyry Pakkanen
"""
__author__ = "Pyry Pakkanen"
__copyright__ = "Copyright 2013"
__credits__ = ["Pyry Pakkanen"]
@frostburn
frostburn / stable_filters.py
Created November 29, 2014 14:48
Stable variable filters
from __future__ import division
from math import pi, sin, cos, sqrt
from sys import version_info
if version_info < (3,):
from itertools import izip as zip
__author__ = "Lumi Pakkanen"
@frostburn
frostburn / lissajous.py
Created December 16, 2014 16:56
Lissajous waveforms up to ratio 5/6.
from __future__ import division
from math import pi, floor, sin, cos, atan2
__author__ = "Lumi Pakkanen"
__copyright__ = "Copyright 2014, Lumi Pakkanen"
__credits__ = ["Lumi Pakkanen"]
__license__ = "MIT"
__version__ = "1.0"
__maintainer__ = "Lumi Pakkanen"
@frostburn
frostburn / theta_formant.py
Created December 26, 2014 14:53
Jacobi theta function and associated formant waveforms
from __future__ import division
from math import pi, exp, cosh, cos, log, floor
from cmath import rect as from_polar, exp as cexp
__author__ = "Lumi Pakkanen"
__copyright__ = "Copyright 2014, Lumi Pakkanen"
__credits__ = ["Lumi Pakkanen"]
__license__ = "MIT"
__version__ = "1.0"
@frostburn
frostburn / additive.py
Created January 3, 2015 10:55
Algorithms for Additive Synthesis
from __future__ import division
from math import pi, exp, sin, cos
from cmath import rect as from_polar
__author__ = "Lumi Pakkanen"
__copyright__ = "Copyright 2015, Lumi Pakkanen"
__credits__ = ["Lumi Pakkanen"]
__license__ = "MIT"
__version__ = "1.0"
@frostburn
frostburn / mirror.d
Last active August 29, 2015 14:22
The diagonal mirror routine for the 11x10 board in my go toolkit
void mirror_d() pure nothrow @nogc @safe
in
{
assert(valid);
assert(can_rotate);
}
out
{
assert(valid);
assert(can_rotate);
@frostburn
frostburn / complex_rationals.py
Created November 28, 2015 21:12
Plot complex rational numbers grouped by the absolute value of the denominator in reduced form.
from __future__ import division
from argparse import ArgumentParser
from itertools import cycle
from pylab import *
tau = 2 * pi
if __name__ == '__main__':
parser = ArgumentParser(description='Plot complex rational numbers')
#!/usr/bin/env python3
import argparse
BAILOUT = 16
MAX_ITER = 56
def escape_time(c):
z = 0j # Squaring accumulator