Skip to content

Instantly share code, notes, and snippets.

# Experimental Python implementation of Poisson disk sampling with O(n) space and time complexity.
# Reference: https://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf
import random
import numpy as np
import matplotlib.pyplot as plt
S2 = np.sqrt(2)
def runit(r, shape, k=10):
@mbillingr
mbillingr / main.rs
Created July 19, 2018 16:20
Simple Ambisonics on top of CPAL (and inspired by Rodio)
extern crate cpal;
extern crate hound;
use std::f32;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::ops;
use cpal::{StreamData, UnknownTypeOutputBuffer};
@mbillingr
mbillingr / autodiff.rs
Created November 7, 2018 15:35
Symbolic Match with Rust's type system
use std::ops::{Add, Mul};
use std::fmt::{self, Debug};
trait Expression: Sized + Clone {
fn add<Other: Expression>(self, rhs: Other) -> Addition<Self, Other> {
Addition {
lhs: self,
rhs,
}
}
@mbillingr
mbillingr / autodiff.rs
Created November 7, 2018 16:13
Symbolic Math dynamically at runtime with Rust
use std::ops::{Add, Sub, Mul, Div};
use std::fmt::{self, Debug};
#[derive(Clone)]
enum Expression {
Zero,
One,
Constant(f64),
Variable(&'static str),
Add(Box<Expression>, Box<Expression>),
@mbillingr
mbillingr / friendly_iter.py
Created December 6, 2018 15:23
Python class that wraps any iterable to enable iterator composition by chaining method calls (similar to Rust's iterators).
import itertools
import functools
class FriendlyIter:
def __init__(self, it):
self.it = iter(it)
def __iter__(self):
return self
@mbillingr
mbillingr / policy_supervised.py
Created January 4, 2019 12:52
Supervised Policy Learning
import numpy as np
import scipy.ndimage as ndimage
import random
import matplotlib.pyplot as plt
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
@mbillingr
mbillingr / movement_solver.py
Created January 7, 2019 17:09
Resolve multiple agents' weighted movement choices to avoid collisions (=assignment problem) or to allow collisions (=something else, seems solvable with simulated annealing...)
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from heapq import *
from scipy.optimize import linear_sum_assignment
MOVE_FACTOR = 10
@mbillingr
mbillingr / fft.rs
Created January 8, 2019 15:32
Simple unoptimized FFT implementation
use num::complex::Complex;
use std::f64;
fn ditfft2(x: &[Complex<f64>], n: usize, s: usize) -> Vec<Complex<f64>> {
if n == 1 {
return vec![x[0]];
}
let mut out: Vec<_> = ditfft2(x, n / 2, s * 2)
.into_iter()
@mbillingr
mbillingr / diffusion.py
Created January 14, 2019 17:02
diffusion with differential equations
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from scipy import integrate as spi
from scipy import signal
DIFFUSION_COEFFICIENT = 10
EVAPORATION_RATE = 1
DECAY_RATE = 0.25
@mbillingr
mbillingr / obj_vm.rs
Last active March 5, 2019 15:23
Stack-based Object VM
// `error_chain!` can recurse deeply
#![recursion_limit = "1024"]
#[macro_use]
extern crate error_chain;
use std::collections::HashMap;
use std::fmt::Debug;
use std::rc::Rc;