Skip to content

Instantly share code, notes, and snippets.

@pschulam
Created August 17, 2013 06:19
Show Gist options
  • Save pschulam/6255545 to your computer and use it in GitHub Desktop.
Save pschulam/6255545 to your computer and use it in GitHub Desktop.
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)
nZ <- t(replicate(nrow(X)))
nX + nZ - 2 (X %*% t(Z))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment