Skip to content

Instantly share code, notes, and snippets.

@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];
@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>
@slwu89
slwu89 / bridge1.cpp
Created August 20, 2019 01:01
bridge-ish pattern with c++ when you want to have the different implementations store variable data using void*
#include <iostream>
#include <iomanip>
#include <string>
#include <string>
#include <memory>
class impl_base {
public:
/* ctor & dtor */
@slwu89
slwu89 / melancholia_i.R
Created August 8, 2019 02:04
Melancholia I has a matrix on the wall. This finds its stationary distribution. (https://en.wikipedia.org/wiki/Melencolia_I)
xx <- matrix(c(16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1),nrow = 4,ncol = 4,byrow = T)
xxx <- xx/rowSums(xx)
xeig <- eigen(xxx)
xvecs <- xeig$vectors
lvecs <- MASS::ginv(xvecs)
pi_eq <- lvecs[1,]/sum(lvecs[1,])
pi_eq
@slwu89
slwu89 / mvhyper.cpp
Created May 28, 2019 21:24
multivar hypergeometric in rcpp
// destination: array to fill the drawn "balls"
// source: number of "balls" in each "urn"
// n: number of draws to take
// k: number of "urns"
void rmhyper(int* destination, int const* source, int n, int k){
int sum, x, y;
size_t i;
if(n < 0 || k < 0){Rcpp::stop("Invalid parameters of distribution");}
// total number of "balls"
@slwu89
slwu89 / widgetlist.cpp
Last active April 30, 2019 03:19
unique_ptr<widget> in a list, modify them
// Example program
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <memory>
typedef struct widget {
size_t i;
@slwu89
slwu89 / llist-attr.cpp
Created April 25, 2019 17:38
store humans in linked-list and some attributes of them outside, delete multiple at once
//
// main.cpp
// test_humans
//
// Created by Sean Wu on 5/31/18.
// Copyright © 2018 Sean Wu. All rights reserved.
//
#include <iostream>
#include <string>