Skip to content

Instantly share code, notes, and snippets.

View johntyree's full-sized avatar

iyknk johntyree

View GitHub Profile
@johntyree
johntyree / codepad.py
Created October 17, 2012 16:54
Post to codepad.org from file or stdin.
#!/usr/bin/env python
import urllib
import urllib2
import sys
if len(sys.argv) > 1:
lang = sys.argv[1]
else:
function webview.init_funcs.mailto_hook (view, w)
view:add_signal("navigation-request", function (v, uri)
if string.match(string.lower(uri), "^mailto:") then
luakit.spawn(string.format("%s %q", "urxvtc -title mutt -e mutt", uri))
return false
end
end)
end
@johntyree
johntyree / AD_Greeks.hs
Last active October 12, 2015 02:18
Using Auto Differentiation to compute the Greeks.
module Main where
import Control.Comonad.Cofree
import Control.Lens
import Numeric.AD
import Numeric.AD.Types
import Text.Printf (printf)
def crank(V, L1, R1x, L2, R2x, dt, n, crumbs=[], callback=None):
V = V.copy()
dt *= 0.5
L1e = flatten_tensor(L1)
L1i = L1e.copy()
R1 = np.array(R1x).T
L2e = flatten_tensor(L2)
L2i = L2e.copy()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@johntyree
johntyree / pymode.vim
Created December 13, 2012 19:26
Pass by 'reference' in VimL
fun! pymode#Toggle(toggle, msg) "{{{
let {a:toggle} = {a:toggle} ? 0 : 1
if {a:toggle}
echomsg a:msg." enabled"
else
echomsg a:msg." disabled"
endif
endfunction "}}}
@johntyree
johntyree / ansi-attributes.py
Created December 19, 2012 12:27
ANSI attributes in Python
'''
Set ANSI Terminal Color and Attributes.
'''
from sys import stdout
esc = '%s['%chr(27)
reset = '%s0m'%esc
format = '1;%dm'
fgoffset, bgoffset = 30, 40
for k, v in dict(
@johntyree
johntyree / arr.c
Created December 19, 2012 13:02
3D array indexing.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
int main(int argc, char* argv[]) {
int idim = 3;
int jdim = 4;
@johntyree
johntyree / cyhash.pyx
Created December 20, 2012 10:54
Cython with Numpy. From 2.2s to 0.4s.
# cython: infer_types=True
# Use the C math library to avoid Python overhead.
from libc cimport math
# For boundscheck below.
import cython
# We're lazy so we'll let Numpy handle our array memory management.
import numpy as np
# You would normally also import the Numpy pxd to get faster access to the Numpy
# API, but it requires some fancier compilation options so I'll leave it out for
@johntyree
johntyree / a.pyx
Last active May 17, 2018 12:26
C vs. C++ in Cython.
# coding: utf8
# distutils: language = c++
# distutils: sources = b.cpp
# Using C calling convention works with "cdef extern ..." directly; no
# hpp header.
# Without it we need to use the cdef extern from "b.hpp": ... declaration.
# Implicitly forces C calling convenction?
cdef extern void c()