| Crates.io | matamorph |
| lib.rs | matamorph |
| version | 0.1.2 |
| created_at | 2025-12-12 12:09:26.134663+00 |
| updated_at | 2025-12-19 11:15:26.556253+00 |
| description | Seamless conversions between Rust’s major matrix libraries: ndarray, faer, nalgebra, and mdarray. |
| homepage | |
| repository | https://gitlab.com/starcluster/matamorph |
| max_upload_size | |
| id | 1981447 |
| size | 119,270 |
A Rust library for seamless conversions between popular matrix libraries: ndarray, nalgebra, faer, and mdarray.
The crate provides three types of conversions:
Add this to your Cargo.toml (for conversion between ndarray and faer):
[dependencies]
matamorph = { git = "https://gitlab.com/starcluster/matamorph", features = ["with-ndarray", "with-faer"]}
use matamorph::own::MataConvertOwn;
use matamorph::ref_::MataConvertRef;
use matamorph::mut_::MataConvertMut;
// Owned conversion (copies data): ndarray → nalgebra
let a = ndarray::array![[1.0, 2.0], [3.0, 4.0]];
let b = a.to_nalgebra();
assert_eq!(b[(0, 1)], 2.0);
// Reference conversion (zero-copy): mdarray → faer
let c = mdarray::tensor![[5.0, 6.0], [7.0, 8.0]];
let d = c.view(.., ..).to_faer();
assert_eq!(d[(1, 0)], 7.0);
// Mutable reference conversion (zero-copy): ndarray → faer
let mut e = ndarray::array![[9.0, 10.0], [11.0, 12.0]];
let mut f = e.view_mut().to_faer();
f[(0, 0)] = 0.0;
assert_eq!(e[[0, 0]], 0.0); // Original is modified
use matamorph::ref_::MataConvertRef;
// ⚠️ Warning: conversions to nalgebra from row-major references are transposed!
let g = ndarray::array![[1.0, 2.0], [3.0, 4.0]];
let h = g.view().to_nalgebra();
assert_eq!(h[(1, 0)], 2.0); // h is transposed: h[(row, col)] = g[(col, row)]
// But conversions FROM nalgebra preserve layout (no transposition)
let i = nalgebra::dmatrix![1.0, 2.0; 3.0, 4.0];
let i_view = i.view((0,0), (2,2));
let j = i_view.to_ndarray();
assert_eq!(j[[0, 1]], 2.0); // No transposition
assert_eq!(i[(0, 1)], 2.0); // Same element accessed
// Similarly, conversions between column-major crates (faer ↔ nalgebra) preserve layout
let k = faer::mat![[1.0, 2.0], [3.0, 4.0]];
let l = k.as_ref().to_nalgebra();
assert_eq!(l[(1, 0)], 3.0); // No transposition
assert_eq!(k[(1, 0)], 3.0); // Same element accessed
MIT OR Apache-2.0