This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##' 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(...) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# "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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
OlderNewer