Skip to content

Instantly share code, notes, and snippets.

View jdpage's full-sized avatar
💭
random walk

Jonathan David Page jdpage

💭
random walk
View GitHub Profile
@jdpage
jdpage / linalg.py
Created September 1, 2011 21:05
Python module for vector and matrix handling
# linalg.py - vector and matrix handling
#
# Copyright 2011 Jonathan D. Page. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
@jdpage
jdpage / interp-tarpit.py
Created January 29, 2012 23:49
Brainf*** interpreter
#!/usr/bin/env python
import sys
from collections import deque
SINGLE, SHIFT_LEFT_SEQ, SHIFT_RIGHT_SEQ, INC_SEQ, DEC_SEQ = range(5)
SHIFT_RIGHT = ord('>') # increment the data pointer
SHIFT_LEFT = ord('<') # decrement the data pointer
INC = ord('+') # increment the current byte
@jdpage
jdpage / magfieldcurl.py
Created February 17, 2012 00:44
Magnetic field sim
from __future__ import division
from visual import *
from math import *
PROTON_CHARGE = 100
MU_ZERO = 1.256e-6
VEL = vector(.01, 0, 0) # m/s
SCALE = 20
observation_points = [vector(x * .01 - .1, .01 * cos(pi/4 * t), .01 * sin(pi/4 * t)) for x in range(20) for t in range(8)]
@jdpage
jdpage / approx.py
Created March 15, 2012 20:22
Rational Approximation Finder
from __future__ import division
from math import pi
def grader(target):
return lambda (p1, q1), (p2, q2): cmp(abs(target - p1 / q1), abs(target - p2 / q2))
def gcd(a, b):
# euclid's algorithm
while a != b:
if b < a:
@jdpage
jdpage / FooTest.java
Created March 27, 2012 08:32
Evil Java File
import java.lang.reflect.Method;
import java.lang.reflect.Field;
public class FooTest {
static class Foo {
private int hehe = 0;
private void doSomething() {
System.out.println("Foo!");
}
@jdpage
jdpage / FooTest.java
Created March 27, 2012 08:32
Evil Java File
import java.lang.reflect.Method;
import java.lang.reflect.Field;
public class FooTest {
static class Foo {
private int hehe = 0;
private void doSomething() {
System.out.println("Foo!");
}
@jdpage
jdpage / sort.fs
Created March 28, 2012 02:58
Quicksort in F#
open System
let rec sorted = function
| (x :: xs) ->
let left, right = List.partition (fun i -> i < x) xs
let sleft = sorted left
let sright = sorted right
sleft @ (x :: sright)
| [] -> []
@jdpage
jdpage / sort.py
Created March 28, 2012 03:13
Quicksort in Python
#!/usr/bin/python3
import random
def sorted(l):
if len(l) == 0:
return l
else:
x, *xs = l
left, right = [], []
@jdpage
jdpage / Add.java
Created June 30, 2012 18:42
Playing Haskell (in Java)
class Add extends Func2<Integer, Integer, Integer> {
@Override
public Integer apply(Integer a, Integer b) {
return a + b;
}
}
@jdpage
jdpage / approx_cofr.py
Created December 20, 2012 04:55
Better way of getting fractional pi approximations. See the corresponding blog post at http://sleepingcyb.org/2012/12/20/approximating-pi-redux for an explanation.
from __future__ import division
from fractions import gcd
from itertools import izip, tee, izip_longest
import sys
def pi_noncanon_seq():
"""
produces a generalized continued fraction from of pi, starting with
(0 4) (1 1) (3 4) (5 9) ...
"""