Skip to content

Instantly share code, notes, and snippets.

@mrocklin
mrocklin / goal-ordering-failure.clj
Last active December 11, 2015 01:48
A simple script to demonstrate the unfortunate importance of goal ordering in `core.logic`.
(use `clojure.core.logic)
;; Runs quickly. Prints (1 2,3).
(clojure.pprint/pprint (run* [q] (fresh [x] (== x [1,2,3])
(membero q x))))
;; Hangs
(clojure.pprint/pprint (run* [q] (fresh [x] (membero q x)
(== x [1,2,3]))))
@mrocklin
mrocklin / conditional.py
Created February 18, 2013 23:48
Example showing densities of random variables in conditional joint probability space.
from sympy.stats import *
from sympy import Tuple, Eq, simplify, Symbol, pprint
X = Normal('x', 3, 4)
Y = Normal('y', 5, 6)
Z = Normal('z', 0, 1)
# X, Y, and Z given that Z+Y == X
XX, YY, ZZ = given(Tuple(X, Y, Z), Eq(Z+Y, X))
w = Symbol('w')
@mrocklin
mrocklin / kalman.f90
Last active August 27, 2023 20:21
Kalman filter in BLAS/LAPACK Fortran
subroutine f(mu, Sigma, H, INFO, R, Sigmavar_2, data, muvar_2, k, n)
implicit none
integer, intent(in) :: k
integer, intent(in) :: n
real*8, intent(in) :: Sigma(n, n) ! Sigma
real*8, intent(in) :: H(k, n) ! H
real*8, intent(in) :: mu(n) ! mu
real*8, intent(in) :: R(k, k) ! R, H*Sigma*H' + R
real*8, intent(in) :: data(k) ! (H*Sigma*H' + R)^-1*((-1)*data + H*mu), data, (-1)* data + H*mu
@mrocklin
mrocklin / theano_logpy.py
Created April 1, 2013 19:32
Quick demonstration of using LogPy to simplify Theano graphs.
from logpy import eq, run, var, facts, Relation
from logpy.variables import variables
import theano
from theano import tensor
# Define relevant fields for theano objects
# LogPy will traverse these fields to match expressions
important_attrs = {theano.Apply: ['op', 'inputs'],
tensor.TensorVariable: ['type', 'owner', 'name'],
@mrocklin
mrocklin / wrf-snippet.f
Created April 10, 2013 22:34
A snippet showing code from the meteorological code WRF Taken from phys/module_mp_wsm5_accel.F:644 Version 3.4
! phys/module_mp_wsm5_accel.F:644 Version 3.4
do k = kte, kts, -1
if(t(i,k,j).gt.t0c.and.qrs(i,k,2).gt.0.) then
!----------------------------------------------------------------
! psmlt: melting of snow [HL A33] [RH83 A25]
! (T>T0: S->R)
!----------------------------------------------------------------
xlf = xlf0
! work2(i,k)= venfac(p(i,k),t(i,k,j),den(i,k,j))
@mrocklin
mrocklin / groupby.py
Last active December 17, 2015 13:39
# Passes https://gist.github.com/mrocklin/5722155
def groupby(f, coll):
""" Group elements in collection by ``f`` """
d = dict()
for item in coll:
key = f(item)
if key not in d:
d[key] = []
d[key].append(item)
@mrocklin
mrocklin / merge.py
Created May 23, 2013 17:41
Merge sorted function. This is half of the classic MergeSort algorithm.
def test_merge_sorted():
assert list(merge_sorted([1,2,3], [1,2,3])) == [1,1,2,2,3,3]
assert list(merge_sorted([1,3,5], [2,4,6])) == [1,2,3,4,5,6]
from Queue import PriorityQueue
def merge_sorted(*iters, **kwargs):
""" Merge a collection of sorted collections
>>> list(merge_sorted([1, 3, 5], [2, 4, 6]))
[1, 2, 3, 4, 5, 6]
def test_groupby():
names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
assert groupby(len, names) == {3: ['Bob', 'Dan'],
5: ['Alice', 'Edith', 'Frank'],
7: ['Charlie']}
even = lambda x: x % 2 == 0
assert groupby(even, range(10)) == {False: [1, 3, 5, 7, 9],
True: [0, 2, 4, 6, 8]}
@mrocklin
mrocklin / hist.py
Last active December 18, 2015 04:10
Histogram
# Depends on GroupBy https://gist.github.com/mrocklin/5618992
identity = lambda x: x
def hist(coll):
""" Histogram - count occurrences of items in collection
>>> hist('aabbc')
{'a': 2, 'b': 2, 'c': 1}
"""
d = groupby(identity, coll)
# http://stackoverflow.com/questions/8972866/correct-way-to-use-super-argument-passing
class Base(object):
def __init__(self, *args, **kwargs): pass
class Slotted(Base):
""" Boilerplate for standard Python class
>>> class Person(Slotted):
... __slots__ = ['name', 'age']