Skip to content

Instantly share code, notes, and snippets.

# Simple root finding using Newton's method
#
# Function to be optimized: f(x) = x^2 - 4 * sin(x)
import math
def objfun(x):
'''Compute the value and gradient of our function at x.'''
f = x**2 - 4 * math.sin(x)
fp = 2 * x - 4 * math.cos(x)
# Script to help understanding of S3 object-oriented programming
# in R using classes and methods
# Constructor functions for various classes of animal
pig <- function() structure(list(), class=c("pig", "animal"))
dog <- function() structure(list(), class=c("dog", "animal"))
cat <- function() structure(list(), class=c("cat", "animal"))
makeSound <- function(x) UseMethod("makeSound")
log.sum <- function(x) {
m <- max(x)
r <- sum(exp(x - m))
return m + log(r)
}
cdist <- function(X, Z) {
nX <- rowSums(X ** 2)
nX <- replicate(nrow(Z))
nZ <- rowSums(Z ** 2)
@pschulam
pschulam / numtricks.py
Created August 1, 2013 20:22
Python numeric tricks.
import numpy as np
def log_sum(X):
m = X.max(0)
r = np.exp(X - m).sum(0)
return m + np.log(r)
archive: stuff you want to archive
$(eval DATE := $(shell date +'%F.%R'))
@mkdir -p .archive/$(DATE)
@rm -f .archive/$(DATE)/options.txt
@$(foreach V,$(sort $(.VARIABLES)),$(if $(filter-out environment% default automatic,$(origin $V)),echo '$V=$(value $V)' >> .archive/$(DATE)/options.txt;))
@cp $^ .archive/$(DATE)