Skip to content

Instantly share code, notes, and snippets.

View fangzhou-xie's full-sized avatar

Fangzhou Xie fangzhou-xie

View GitHub Profile
@fangzhou-xie
fangzhou-xie / sqlite_config.R
Created June 1, 2023 14:04
sqlite parameters
# optimization parameters
DBI::dbExecute(con, "PRAGMA journal_mode = wal;")
DBI::dbExecute(con, "PRAGMA synchronous = normal;")
DBI::dbExecute(con, "PRAGMA cache_size = -128000;")
DBI::dbExecute(con, "PRAGMA page_size = 4096;")
DBI::dbExecute(con, "PRAGMA locking_mode = EXCLUSIVE;")
DBI::dbExecute(con, "PRAGMA temp_store = MEMORY;")
# duckdb import tsv snippet
duckdb_fp
tsv_fp
con <- DBI::dbConnect(duckdb::duckdb(), duckdb_fp)
# config duckdb
DBI::dbExecute(con, "SET memory_limit='20GB';")
DBI::dbExecute(con, "SET threads TO 20;")
@fangzhou-xie
fangzhou-xie / convert.R
Created March 22, 2022 00:18
format latex to vscode snippet
fp <- "proposal.tex"
lines <- readLines(fp)
for (i in 1:length(lines)) {
lines[i] = paste0("\"", gsub("\\", "\\\\", lines[i], fixed = TRUE), "\",")
}
writeLines(lines)
@fangzhou-xie
fangzhou-xie / install.sh
Created October 27, 2021 01:22
install R package for Microsoft Open R
# thanks to https://social.technet.microsoft.com/Forums/en-US/b4335755-68a4-4acc-8d70-54414b40c987/microsoft-r-open-line-breaks-during-package-compilation?forum=ropen
/opt/microsoft/ropen/4.0.2/lib64/R/bin/R --vanilla
@fangzhou-xie
fangzhou-xie / tor.R
Last active October 2, 2021 01:25
Tor in R
# set fresh_connect to force not using old connection (so that ip address could refresh per 10secs)
# depends on how you set up the refresh ip for Tor service
h <- curl::new_handle(proxy = "socks5://localhost:9050", maxage_conn = 10) # , fresh_connect = 1)
curl::handle_setheaders(h, "Cache-Control" = "no-cache", "User-Agent" = Randomuseragent::random_useragent())
req <- curl::curl_fetch_memory("https://icanhazip.com/", handle = h)
rawToChar(req$content)
@fangzhou-xie
fangzhou-xie / partial_gradient.cpp
Created September 22, 2021 17:28
gradient from Eigen
// test the partial derivative in one go
#include "adcpp.h"
#include <iostream>
using namespace adcpp;
static bwd::Double mysq(const bwd::Double &x, const bwd::Double &y) {
return bwd::pow(x, 2) + bwd::pow(y, 3);
}
@fangzhou-xie
fangzhou-xie / sinkhorn.cpp
Last active September 22, 2021 01:36
Eigen Sinkhorn
// write the sinkhorn algorithm and compare with the POT result
#include "Eigen/Eigen"
// #include "unsupported/Eigen/MatrixFunctions"
#include <iostream>
void sinkhorn(Eigen::VectorXd a, Eigen::VectorXd b, Eigen::MatrixXd M,
const double reg, const int numItermax = 1000,
const double stopThr = 1e-9) {
// only compute 1d to 1d case