Skip to content

Instantly share code, notes, and snippets.

@slwu89
slwu89 / ricker.jl
Created December 4, 2021 01:00
the same example as s3inheritance.R in julia
using Random, Distributions, Plots
# the parent type
abstract type ricker end
# derived, concrete types
mutable struct ricker_deterministic <: ricker
x::Float64
r::Float64
k::Float64
@slwu89
slwu89 / s3inherit.R
Last active December 4, 2021 00:25
S3 inheritance mocking dispatch on inherited types
# a simple Ricker model, which may be stochastic if you provide `sd` or deterministic if not
setup_ricker <- function(x0, r, k, sd = NULL) {
type <- c("ricker")
if (is.null(sd)) {
type <- c(type, "ricker_deterministic")
} else {
type <- c(type, "ricker_stochastic")
}
mod <- structure(new.env(), class = type)
mod$x <- x0
@slwu89
slwu89 / mem_copy.R
Created November 5, 2021 21:54
when do things get copied in R?
fn_list <- function(y){
y$a <- y$a + 1
return(y)
}
fn_env <- function(y){
y$a <- y$a + 1
}
x <- list(a=matrix(1:9,3,3),b=matrix(1:36,6,6))
@slwu89
slwu89 / float_matching.R
Last active September 30, 2021 17:50
operator to check if floats are in sets of other floats, for R
# for more info on comparing floats, this blog post is very informative: https://bitbashing.io/comparing-floats.html
# for more info on how floating point mathematics compares to "real" mathematics, see https://www.tandfonline.com/doi/full/10.1080/10724117.2019.1611061
# helper function for approximate equality between floats
approx_equal <- function(a, b, tol = sqrt(.Machine$double.eps)) {
abs(a - b) <= tol
}
# use instead of the %in% operator to check if floats are in sets of other floats
`%in_approx%` <- function(x, table) {
@slwu89
slwu89 / netreg.R
Created December 29, 2020 01:49
comparison of sna::lnam to spatial regression approaches
rm(list=ls());gc();dev.off()
library(sna)
set.seed(4395834L)
n <- 100
w1<-rgraph(n) #Draw the AR matrix
w2<-w1 #Draw the MA matrix
x<-matrix(rnorm(n*5),n,5) #Draw some covariates
r1<-0.2 #Set the model parameters
@slwu89
slwu89 / std_function_RcppXPtr.cpp
Last active March 19, 2020 01:38
for when you want to return a std::function to R wrapped in a struct to be evaluated later
#include <functional>
#include <Rcpp.h>
// [[Rcpp::plugins(cpp14)]]
using my_lambda = std::function<double(const double)>;
typedef struct lambda_st {
my_lambda func;
@slwu89
slwu89 / xorshift.cpp
Last active March 17, 2020 23:36
the PRNG is from code here: http://prng.di.unimi.it ; information on how to code URNG template class for STL's random header is here https://stackoverflow.com/questions/25129500/using-stdshuffle-with-custom-rng and https://www.boost.org/doc/libs/1_72_0/doc/html/boost_random/reference.html ; useful discussion is found on the dev's boards https://…
#include <random>
#include <array>
#include <iostream>
#include <limits>
#include <random>
#include <Rcpp.h>
// [[Rcpp::plugins(cpp14)]]
@slwu89
slwu89 / unique_array.cpp
Created March 12, 2020 19:43
how to get unique elements and how many times they appear in an array of ints (for C, but compiled with C++)
// Example program
#include <iostream>
#include <string>
int main(void) {
int arr[13] = {1, 2, 2, 123121, 123121, 3, 5, 6 , 7, 7, 14, 2, 16};
int len = 13;
int unique[len];

1-20-2019

Avery arrives at a coffee shop at a random time between 7:00 and 9:00, and Bruno at a random time between 6:00 and 10:00. The time one person arrives does not affect the other's.

Integrate[
 PDF[UniformDistribution[{7, 9}], a + 0.5]*
  PDF[UniformDistribution[{6, 10}], b], {b, 6, 10}, {a, 7, b}]
@slwu89
slwu89 / shared_pointer_test.cpp
Created December 4, 2019 20:32
checking that shared pointers are working right when mtple objects are sharing one resource, even when the first goes out of scope
//
// main.cpp
// test_simple
//
// Created by Sean Wu on 4/10/19.
// Copyright © 2019 Sean Wu. All rights reserved.
//
#include <memory>
#include <functional>