Created
May 9, 2025 14:47
-
-
Save bschneidr/fa7c14c5aca641d777db1d22aa313836 to your computer and use it in GitHub Desktop.
Eigendecomposition with Rust ndarray in R via 'extendr'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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