Skip to content

Instantly share code, notes, and snippets.

@bschneidr
bschneidr / r-ndarray-eigendecomposition.R
Created May 9, 2025 14:47
Eigendecomposition with Rust ndarray in R via 'extendr'
library(rextendr)
rust_source(
code = readLines("r-ndarray-eigendecomposition.rs"),
dependencies = list("ndarray-linalg" = "0.17.0"),
features = "ndarray",
profile = "dev"
)
X_dim <- 5
@bschneidr
bschneidr / r-faer-eigendecomposition.R
Last active May 9, 2025 00:14
Eigendecomposition using 'faer' with R (via 'rextendr')
library(rextendr)
rust_source(
code = readLines("r-faer-eigendecomposition.rs"),
dependencies = list("faer" = "0.22.6"),
features = "faer",
profile = "release"
)
X <- matrix(rnorm(n = 25), 5, 5)
@bschneidr
bschneidr / round-trip-r-matrix-to-faer-matrix.R
Last active May 7, 2025 14:10
Round trip conversion for an R matrix to a 'faer' matrix in Rust
library(rextendr) # Version 0.4.0
rust_code <- r"(
use faer::prelude::*;
use extendr_api::prelude::*;
#[extendr]
fn return_matrix(X: RMatrix<f64>) -> RMatrix<f64> {
let A: Mat<f64> = Mat::from_fn(X.nrows(), X.ncols(), |row, col| X[[row, col]]);
let result: RMatrix<f64> = RMatrix::new_matrix(A.nrows(), A.ncols(), |row, col| A[(row, col)]);
import polars as pl
import pandas as pd
# Read input data as a data frame with two columns
input = pl.from_pandas(
pd.read_table("day-1/input", sep = " ", header = None,
names = ["list_1", "list_2"])
)
# Sort each column separately,
read.table("day-1/input", col.names = c("list_1", "list_2")) |>
apply(MARGIN = 2, sort) |>
apply(MARGIN = 1, FUN = \(x) abs(x[2] - x[1])) |>
sum()
@bschneidr
bschneidr / gist:dbc2eb6a1e64d6408ef3281bb2c589d1
Last active September 30, 2024 04:08
Install cmdstanr with WSL and OpenBLAS
sudo apt-get install libopenblas-dev
sudo apt-get install liblapacke-dev
sudo apt-get install liblapacke
sudo apt-get install libopenblas-serial-dev
sudo apt-get install libopenblas0-serial
sudo apt-get install libopenblas0
@bschneidr
bschneidr / sample-groups-with-dplyr-and-tidyr.R
Created May 23, 2024 17:59
Sampling groups with 'dplyr' + 'tidyr'
library(dplyr)
library(tidyr)
library(survey)
# Load example data
# 'apipop' has one row per school
data('api', package = 'survey')
# Draw a sample of school districts
library(dplyr)
library(broom)
library(tidyr)
# Make example data
my_data <- data.frame(x = sample(1:5, size = 10, replace = TRUE),
y = sample(1:5, size = 10, replace = TRUE))
# Generate every pair of variables
var_list <- colnames(my_data)
@bschneidr
bschneidr / alt-design-syntax.R
Created December 11, 2022 14:51
Alternative survey design syntax
describe_design(
srs_stage(id = "PSU_ID", method = "SRS",
stratum = "PSU_STRATUM_ID"),
nonresponse_stage(response_indicator = "PSU_RESPONDENT"),
pps_stage(id = "SSU_ID", method = "PPS",
stratum = "SSU_STRATUM_ID"),
nonresponse_stage(response_indicator = "SSU_RESPONDENT")
)
@bschneidr
bschneidr / sample-variance-as-quadratic-form.R
Last active October 28, 2022 12:33
Expresses sample variance as a quadratic form
n = 5
# Establish quadratic form
quad_form_matrix <- matrix(nrow = n, ncol = n)
for (i in seq(n)) {
for (j in seq(n)) {
if (i == j) {
quad_form_matrix[i,j] <- (1/n)
} else {