Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
chris-taylor / Dual.m
Created March 9, 2012 10:19
Matlab dual number class (for automatic differentiation)
classdef Dual
% DUAL Dual numbers class for automatic differentiation.
%
% To do automatic differentiation we introduce a number d such that d^2 == 0.
% Then by a simple application of Taylor's theorem we have
%
% f(x + d) = f(x) + f'(x) d
%
% with all higher-order terms vanishing. Therefore applying a function to a dual
% number x + d generates both the value f(x) *and* the derivative f'(x).
@chris-taylor
chris-taylor / isdual.m
Created March 9, 2012 10:25
Test if a Matlab object is a Dual
function b = isdual(a)
%ISDUAL Return true if a is of class Dual, else return false
b = strcmp(class(a),'Dual');
end
@chris-taylor
chris-taylor / SetDefault.m
Created March 23, 2012 09:23
Set default values for Matlab function arguments
function SetDefault(argname,value)
%SETDEFAULT Provide a default value of VALUE for the argument ARGNAME.
%
%USAGE
% SetDefault('x',1)
% If no variable 'x' exists in the calling function, or if the variable exists
% but it is empty, then this call assigns the value 1 to 'x'. If the variable
% 'x' already exists, and is non-empty, then no action is taken.
%
@chris-taylor
chris-taylor / sicp3.3.hs
Created April 9, 2012 21:40
Exercise in 3.3 of SICP
import Control.Monad.ST
import Data.STRef
-- Primitive actions
getSignal wire = wire GetSignal
setSignal wire x = wire (SetSignal x)
addAction wire proc = wire (AddAction proc)
@chris-taylor
chris-taylor / ffi.py
Created April 29, 2012 11:38
Quick and dirty implementation of first fit decreasing algo for bin packing
import random
import time
N = 200
binsize = 8000;
xs = [100 * random.randint(1,40) for i in range(N)]
print '\nObjects to be packed:\n'
print xs
@chris-taylor
chris-taylor / t.hs
Created May 1, 2012 14:43
alternative implementation of floating point abs and num
data NewFloat a = F a deriving (Eq,Ord,Show)
instance RealFloat a => Num (NewFloat a) where
F x + F y = F (x + y)
F x - F y = F (x - y)
F x * F y = F (x * y)
fromInteger n = F (fromInteger n)
abs = newAbs
signum = newSignum
@chris-taylor
chris-taylor / trib.hs
Created May 22, 2012 17:26
Finding tribonacci pseudoprimes
-- A semi-fast isPrime algorithm
noDivs factors n = foldr (\f r -> f*f > n || (rem n f /= 0 && r))
True factors
primesTD = 2 : 3 : filter (noDivs $ tail primesTD) [5,7..]
isPrime n = n > 1 && noDivs primesTD n
divides a b = b `mod` a == 0
@chris-taylor
chris-taylor / 2cat.hs
Created May 22, 2012 23:34
Playing around with ways of defining a group as a 2-category in Haskell
{-# LANGUAGE MultiParamTypeClasses #-}
import Data.Maybe
import Data.List as List
-- Group stuff
class Group g where
eye :: g
(%) :: g -> g -> g
@chris-taylor
chris-taylor / jsmath.js
Created June 11, 2012 21:41
jsMath inclusion
/**********************************************************************
*
* Customize the values given below to suit your needs.
* You can make additional copies of this file with
* different customizated settings if you need to load
* jsMath with different parameters.
*
* Load this page via:
*
* <SCRIPT SRC="path-to-jsMath/easy/load.js"></SCRIPT>
@chris-taylor
chris-taylor / evolution.m
Created June 12, 2012 13:02
Relative entropy as applied to an evolutionary competition between two species
function [T Y] = relent(Tspan)
%% Configuration
leg = {'Species 1','Species 2'};
opts = odeset();
opts.AbsTol = 1e-8;
opts.RelTol = 1e-4;