Skip to content

Instantly share code, notes, and snippets.

@slwu89
slwu89 / threadSafeSingleton.cpp
Created January 13, 2018 06:42
thread safe c++11 singleton
#include <iostream>
using namespace std;
class CSingleton final
{
public:
static CSingleton* GetInstance();
void set_x(const int& x_){x = x_;};
int get_x(){return x;};
@slwu89
slwu89 / R_C_example1.c
Last active March 17, 2018 00:58
example of using named arguments and ... in R's C API
SEXP call_function(SEXP call, SEXP rho){
/* advance to 2nd element (CAR) of call pairlist; 1st element is just the call_function function */
SEXP args = CDR(call);
/* get function (2rd element) and advance to 3th element */
SEXP funSymbol = install("FUN");
args = CDR(args);
/* make the function call */
@slwu89
slwu89 / eapply.c
Last active March 17, 2018 04:13
rewrite of eapply without memory allocation for return values (for calling functions just for side effects)
/* ################################################################################
* BEGIN UTILITY FUNCTIONS
################################################################################ */
#define NONEMPTY_(_FRAME_) \
CHAR(PRINTNAME(TAG(_FRAME_)))[0] != '.' && CAR(_FRAME_) != R_UnboundValue
static int FrameSize(SEXP frame, int all)
{
int count = 0;
@slwu89
slwu89 / simpleGillespie.R
Last active April 8, 2018 18:29
simple example of using a gillespie algorithm to simulate a ctmc with 2 events
events = c(
birth = 1/1.5,
death = 1/2.5
)
state = c(
animals = 5
)
tmax = 1e3
@slwu89
slwu89 / hypercubeWalk.R
Created April 24, 2018 00:21
walk on hypercube
n_iter = 100
n_nodes = 10
trace = vector(mode="list",length = n_iter)
trace[[1]] = sample(x = 1:n_nodes,size = n_nodes,replace = TRUE)
for(i in 2:n_iter){
trace[[i]] = trace[[i-1]]
j = sample(x = 1:n_nodes,size = 1) # sample index
trace[[i]][j] = sample(x = 1:n_nodes,size = 1) # flip n-bit
}
@slwu89
slwu89 / lumpedAgeModel.R
Created April 25, 2018 20:26
lumpedAgeModel.R
library(deSolve)
mod <- function(t,y,par){
with(as.list(c(y,par)),{
# lagged state variables
if((t - TO) < 0){
S_TO <- S_init
@slwu89
slwu89 / classWithMapFunctions.cpp
Created April 30, 2018 17:58
example of using std::map to store string keys and values that are bound member functions with 1 parameters
#ifndef human_hpp
#define human_hpp
#include <stdio.h>
#include <iostream>
#include <string>
#include <random>
#include <functional>
#include <map>
using namespace std::placeholders;
@slwu89
slwu89 / pf_fsm.tex
Created May 9, 2018 20:25
how to make finite state machine in tikz (latex)
\documentclass[12pt]{article}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
semithick]
@slwu89
slwu89 / lscape.R
Created May 16, 2018 20:58
make a point set, put a parameteric movement kernel on it and plot
library(viridis)
library(spatstat)
library(truncdist)
# source: https://github.com/slwu89/PlosCompBio_2013/blob/master/fig2/code/functionsModel.R
points.clustered = function(n, meanParents = 10, clusteredness = .25, ...){
meanDist = clusteredness / sqrt(meanParents)
meanChildren = n / meanParents
ps = rMatClust(meanParents, meanDist, meanChildren, ...)
while(ps$n != n){
@slwu89
slwu89 / harmonicMean.cpp
Created May 30, 2018 17:05
quick test that templated harmonic mean works with int and float
// CPP program to find harmonic mean of numbers.
#include <bits/stdc++.h>
using namespace std;
template <typename T>
float harmonicMean(T arr[], int n){
// Declare sum variables and initialize
// with zero.
float sum = 0;
for (int i = 0; i < n; i++)