Skip to content

Instantly share code, notes, and snippets.

@doobwa
doobwa / log.sum.exp.R
Created April 25, 2011 20:15
Computing log-sum-exponential
log.sum.exp<- function(x) {
# Computes log(sum(exp(x))
# Uses offset trick to avoid numeric overflow: http://jblevins.org/notes/log-sum-exp
offset <- x[which(x == max(abs(x)))]
log(sum(exp(x - offset))) + offset
}
@doobwa
doobwa / dfapply
Created October 3, 2011 04:03
dfapply: apply an expression to each row of a data.frame
# Chris DuBois
# October 2, 2011
# dfapply: apply an expression to each row of a data.frame
# This can be especially helpful when organizing experimental setups and you want to plot the results from a data.frame.
# s: data.frame, where each row has argument values
# expr: expression to evaluate on each row of s
@doobwa
doobwa / rcpp.modules.r
Created January 9, 2012 19:35
Rcpp Module example
# Borrowed from http://dirk.eddelbuettel.com/papers/rcpp_workshop_part_3_advanced.pdf
require(Rcpp)
require(inline)
require(testthat)
fx <- cxxfunction(,"",includes=
'
double norm( double x, double y ){
return sqrt( x*x + y*y) ;
@doobwa
doobwa / example.openmp.r
Created February 1, 2012 05:35
OpenMP Rcpp example
#!/usr/bin/r
library(inline)
library(rbenchmark)
## lastly via OpenMP for parallel use
openMPCode <- '
// assign to C++ vector
std::vector<double> x = Rcpp::as<std::vector< double > >(xs);
@doobwa
doobwa / example.openmp.r
Created February 1, 2012 05:35
OpenMP Rcpp example
#!/usr/bin/r
library(inline)
library(rbenchmark)
## openMPCode example from Rcpp/examples/OpenMP/ by Dirk E.
openMPCode <- '
// assign to C++ vector
std::vector<double> x = Rcpp::as<std::vector< double > >(xs);
@doobwa
doobwa / listn()
Created May 27, 2012 04:56
Create named lists quickly in R
##' Quickly create a named list using one or more variables
##'
##' Example:
##' > a <- 1; b <- 2
##' > listn(a,b)
##' $a
##' [1] 1
##' $b
##' [1] 2
listn <- function(...) {
@doobwa
doobwa / matmult.jl
Created June 26, 2012 04:45
Sparse matrix multiplication in julia
# "Fast matrix multiply" written in Julia
#
# Method below implements the algorithm described in [Gustavson, 1978]:
# http://www.cse.iitb.ac.in/graphics/~anand/website/include/papers/matrix/fast_matrix_mul.pdf
#
# Copyright (C) 2012 Christopher DuBois
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
@doobwa
doobwa / dlm2dict.jl
Created September 25, 2012 01:01
An attempt at faster Julia IO
# Chris DuBois
# September 2012
# Inspired by: http://vijayinterviewquestions.blogspot.com/2007/07/write-your-own-c-program-to-implement.html
function atoi(s)
i = 0
L = strlen(s)
# skip preceding whitespaece
a = 1
@doobwa
doobwa / mbandit.r
Created April 7, 2013 07:05
Quick example of multi-armed bandits.
# Multi-armed bandit example
# Each arm has some (unknown) probability of reward=1.
# Using beta priors we can update our posterior after each pull
# The following uses Thompson sampling, where we sample probabilities
# from our posterior and choose the arm with the largest expected reward.
set.seed(2)
J <- 3
probs <- rbeta(J,1,1) # True probability
@doobwa
doobwa / gdb_backtrace_julia_segfault_139
Created September 29, 2013 14:32
GDB Backtrace for segfault during bootstrap (sysimg.jl) on Ubuntu
NU gdb (GDB) 7.5.91.20130417-cvs-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
[Thread debugging using libthread_db enabled]