Skip to content

Instantly share code, notes, and snippets.

View Sleepingwell's full-sized avatar

Simon Knapp Sleepingwell

  • Canberra, ACT, Australia
View GitHub Profile
@Sleepingwell
Sleepingwell / gist:8896424
Created February 9, 2014 09:05
Some thoughts on a spatial temporal container.
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <boost/ref.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range/iterator_range.hpp>
using namespace std;
@Sleepingwell
Sleepingwell / gist:8908692
Created February 10, 2014 01:19
Some comparative timing for various iterators and loops.
#include <boost/iterator/iterator_categories.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/any_range.hpp>
#include <boost/range/numeric.hpp>
#include <boost/timer/timer.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <numeric>
@Sleepingwell
Sleepingwell / deep_thought.R
Last active August 29, 2015 13:56
An example how to make R code hard to find using C.
setwd("c:/dir/containing/compiled/binary")
dyn.load("deep_thought")
.External('the_meaning_of_life', environment())
.External('the_meaning_of_multiple_lives', 42, environment())
dyn.unload("deep_thought")
@Sleepingwell
Sleepingwell / gist:9612041
Created March 18, 2014 01:44
Timings of data.table Vs by for a specific problem
library(data.table)
nl <- 100
stp <- 50000
ss <- stp
ss <- seq(stp, length.out=20, by=stp)
times <- sapply(ss, function(ss) {
mydata <- data.frame(
to_location_id = as.factor(sample(nl, ss, T)),
@Sleepingwell
Sleepingwell / gist:8588c5ee844ce0242d05
Created November 3, 2014 03:50
Holding a large number of SEXPs in C++
#include <vector>
#include <utility>
#include <iterator>
#include <R.h>
#include <Rdefines.h>
template<typename Iter>
SEXP makePolygon(Iter b, Iter e) {
SEXP
coords;
@Sleepingwell
Sleepingwell / boost_mpl_pair_sequence.cpp
Last active August 29, 2015 14:25
Create a sequence of types (std::pair in this dummy example) containing the outer product of two input sequences of types.
/*
* A 'functor descriptor' that can be used to generate a runtime polymorphic functor
* for a set of input/ouput types. To be used to create python extensions in C++ for
* wrapping with boost::python, such that the functors can be configured in Python
* then applied to a number (greater than 1) of non-polymorphic input/output types
* in C++ (the types used in practice will not be those in first_types and second
* types, which are just for convenience).
*/
#include <new> // placement new
@Sleepingwell
Sleepingwell / gist:5076345
Created March 3, 2013 14:38
A comparison of some hypothesis tests for a specific two sample problem (for a forum).
pre <- scan()
51.11
64.44
73.33
77.78
66.67
84.44
68.89
57.78
64.44
@Sleepingwell
Sleepingwell / gist:5843755
Last active December 18, 2015 20:59
A wrapper around Rcartogram::cartogram for shape files.
# A wrapper around Rcartogram::cartogram for shape files.
#
# @param polys: Either a SpatialPolygonsDataFrame or the path to a shape file.
# @param variable: Either a string giving the name of the variable to used in polys@data, or a vector
# of the same length as polys@polygons.
# @param nx: The number of columns in the raster used to represent the region.
# @param ny: The number of rows in the raster used to represent the region.
# @param buffer.by: The proportion width of the buffer to add to the region (see ?Rcartogram::addBoundary).
#
# @return The new boundaries as A SpatialPolygonsDataFrame with the attribute 'cartogram.scaling.var'.
@Sleepingwell
Sleepingwell / rng_tests.R
Last active December 19, 2015 23:39
Comparison of performance (speed) or R's Gausian random number generators.
test.func <- function(n.samples) {
nms <- c("Wichmann-Hill", "Marsaglia-Multicarry", "Super-Duper", "Mersenne-Twister", "Knuth-TAOCP-2002", "Knuth-TAOCP", "L'Ecuyer-CMRG")
tmp <- lapply(nms, function(unif.rng.name) {
RNGkind(unif.rng.name)
nms <- c("Kinderman-Ramage", "Ahrens-Dieter", "Box-Muller", "Inversion")
tmp <- sapply(nms, function(norm.rng.name) {
RNGkind(, norm.rng.name)
system.time(rnorm(n.samples))
})[1:3,]
# included here because I'm not sure if rziggurat uses the unif rng internally.
@Sleepingwell
Sleepingwell / ca_rng.cpp
Last active December 19, 2015 23:49
A cellular automata random uniform number generator (with R interface).
/*
Copyright (C) 2006 Anthony M. Pasqualoni
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of