Skip to content

Instantly share code, notes, and snippets.

@vrld
vrld / Table.py
Created September 30, 2013 14:24
Lua-Table like dictionary for Python
class Table(dict):
def __getattr__(self, name):
return self[name] if name in self else None
def __setattr__(self, name, value):
if value is not None:
self[name] = value
elif name in self:
del self[name]
def __delattr__(self, name):
self.__setattr__(name, None)
@vrld
vrld / module-pipeline-example.py
Last active December 24, 2015 12:09
Simple Module Pipeline Thingy
class Constant(Module):
def __init__(self, val = 0):
self.out = val
def __repr__(self):
return 'Constant({})'.format(self.out)
class Mul(Module):
def __repr__(self):
return 'Mul()'
@vrld
vrld / iutf8.lua
Last active December 30, 2015 08:29
Simple, non-standard compliant UTF8 iterator for vanilla Lua
local function utf8_iter(s, i)
if i >= #s then return end
local b, nbytes = s:byte(i+1,i+1), 1
-- determine width of the codepoint by counting the number if set bits
-- from top to bottom. not 100% to standard, but it works well enough
if b/4 >= 63 then nbytes = 6
elseif b/8 >= 31 then nbytes = 5
elseif b/16 >= 15 then nbytes = 4
elseif b/32 >= 7 then nbytes = 3
@vrld
vrld / buddha.cpp
Last active August 29, 2015 13:56
Buddhabrot Renderer
#include <omp.h>
#include <vector>
#include <complex>
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
#include <string>
#include <cstdint>
@vrld
vrld / rythm.lua
Created June 3, 2014 12:31
Euclidean Rythms in Lua
-- Euclidean rythm (http://en.wikipedia.org/wiki/Euclidean_Rhythm)
function rythm(k,n)
local r = {}
for i = 1,n do
r[i] = {i <= k}
end
local function cat(i,k)
for _,v in ipairs(r[k]) do
r[i][#r[i]+1] = v
module Crossvalidation
export KFold, StratifiedKFold, LOO, RandomSplit
#==
K-Fold cross validation
Example (5-Fold CV):
KFold(X, y, 5) do X_tr, y_tr, X_te, y_te
train!(model, X_tr, y_tr)
@vrld
vrld / ContourDescriptors.jl
Created October 30, 2014 12:34
Various contour descriptors
function forward_difference(c::Matrix)
out = zeros(c)
@simd for i = 1:size(c,1)-1
@inbounds out[i,:] = c[i+1,:] - c[i,:]
end
@inbounds out[size(c,1),:] = c[1,:] - c[size(c,1),:]
out
end
function at(c::Matrix, t::Number, Δc::Matrix, cumlen::Vector{Float64})
@vrld
vrld / OnlineLearning.jl
Created October 31, 2014 12:00
Online Learning Algorithms
type Perceptron
w::Vector{Float64}
Perceptron(d::Int) = new(zeros(d))
Perceptron(w::Vector{Float64}) = new(w)
end
type Winnow
w::Vector{Float64}
η::Float64
Perceptron(d::Int, η::Float64) = new(ones(d)./d, η)
@vrld
vrld / cholesky.jl
Last active August 29, 2015 14:14
Cholesky Decomposition and simple (!) gaussian process regression
function chol_(A::Matrix, L::Matrix)
for i = 1:size(A,1)
s = 0
@simd for j = 1:i-1
@inbounds L[i,j] = (A[i,j] - sum([L[i,k]*L[j,k] for k = 1:j-1])) / L[j,j]
@inbounds s += L[i,j]^2
end
L[i,i] = sqrt(A[i,i] - s)
end
L
@vrld
vrld / dirble.py
Created April 2, 2015 23:52
Python Dirble API wrapper
import requests
from collections import namedtuple
class Dirble(object):
Category = namedtuple("Category", "id name description")
Station = namedtuple("Station", "id name streamurl country bitrate status")
StationDetails = namedtuple("StationDetails", "id name streamurl country bitrate status description added urlid website songhistory")
Song = namedtuple("Song", "artist title")
def __init__(self, key, base_url):