Last active
May 7, 2025 14:10
-
-
Save bschneidr/5ba9790961411618a1ad6476fa292fe2 to your computer and use it in GitHub Desktop.
Round trip conversion for an R matrix to a 'faer' matrix in Rust
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) # 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)]); | |
return result; | |
} | |
)" | |
rust_source( | |
code = rust_code, | |
dependencies = list(`faer` = '0.22.6') | |
) | |
X <- diag(3) | |
return_matrix(X) | |
#> [,1] [,2] [,3] | |
#> [1,] 1 0 0 | |
#> [2,] 0 1 0 | |
#> [3,] 0 0 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment