This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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])))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'] |