Skip to content

Instantly share code, notes, and snippets.

@slwu89
slwu89 / R_C_API2.c
Created June 26, 2018 00:59
how to call multiple functions with parameters and ... in C using R's C API
/* evaluate 2 functions with ... (but must take same named args) */
SEXP C_example(SEXP call, SEXP rho){
SEXP args = CDR(call);
/* make up some 'arguments' for our two functions we want to call */
SEXP a1 = PROTECT(allocVector(REALSXP,1));
SEXP b1 = PROTECT(allocVector(REALSXP,1));
REAL(a1)[0] = 5.;
REAL(b1)[0] = 10.;
@slwu89
slwu89 / getOmega.R
Last active October 25, 2018 17:42
test omega for mgdrive
get_omega1 <- function(mu,lifespanReduction){
lifespan <- (1/mu) * lifespanReduction
get_omega_f <- function(omega,mu,lifespan){
(1/(1-omega+(omega*mu)))-lifespan
}
return(uniroot(f=get_omega_f,interval=c(0,1),lifespan=lifespan,mu=mu,maxiter=1e4)$root)
}
# 10-25-2018
get_omega <- function(mu,lifespan){
@slwu89
slwu89 / multinom.cpp
Last active June 20, 2018 23:07
testing various flavors of multinomial sampling algorithms
#include <Rcpp.h>
#include <random>
#include <vector>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
typedef std::mt19937 RNG;
@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++)
@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 / 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 / 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 / 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 / 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 / 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