Skip to content

Instantly share code, notes, and snippets.

@bschneidr
Created May 9, 2025 14:47
Show Gist options
  • Select an option

  • Save bschneidr/fa7c14c5aca641d777db1d22aa313836 to your computer and use it in GitHub Desktop.

Select an option

Save bschneidr/fa7c14c5aca641d777db1d22aa313836 to your computer and use it in GitHub Desktop.
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
X <- cov(matrix(rnorm(n = X_dim^2), X_dim, X_dim))
eigen_ndarray(X)
use extendr_api::prelude::*;
use ndarray::ArrayView2;
use ndarray_linalg::{Eigh, UPLO};
#[extendr]
fn eigen_ndarray(x: ArrayView2<f64>) -> List {
let Ok((eigvals, eigvecs)) = x.eigh(UPLO::Lower) else {panic!("Eigendecomposition failed!")};
let eigvecs: Robj = eigvecs.try_into().unwrap();
let eigvals: Robj = eigvals.try_into().unwrap();
list!(values = eigvals, vectors = eigvecs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment