pub fn from_arrayd(array: ArrayD<f64>) -> Result<Tensor, MattenNdarrayError>Expand description
Converts an ndarray::ArrayD<f64> into a Tensor.
Conversion preserves logical element order: an ArrayD may be in
non-standard (transposed / sliced / non-standard-stride) layout, so the raw
backing buffer is not read directly — that would silently transpose the
data. A shape with any zero-length axis is rejected, because core matten
does not support zero-sized dimensions.
use matten_ndarray::from_arrayd;
use ndarray::{ArrayD, IxDyn};
// A transposed (non-standard-layout) array still converts by logical order.
let a = ArrayD::from_shape_vec(IxDyn(&[2, 3]), vec![1., 2., 3., 4., 5., 6.]).unwrap();
let t = from_arrayd(a.t().to_owned()).unwrap(); // logical shape [3, 2]
assert_eq!(t.shape(), &[3, 2]);
assert_eq!(t.as_slice(), &[1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);Examples found in repository?
examples/from_arrayd.rs (line 13)
10fn main() {
11 let arr = ArrayD::from_shape_vec(IxDyn(&[2, 3]), vec![1., 2., 3., 4., 5., 6.]).unwrap();
12
13 let t = from_arrayd(arr.clone()).expect("contiguous converts");
14 println!(
15 "from contiguous: shape {:?} data {:?}",
16 t.shape(),
17 t.as_slice()
18 );
19
20 // Transposed input is non-standard layout; conversion preserves logical order.
21 let tt = from_arrayd(arr.t().to_owned()).expect("transposed converts");
22 println!(
23 "from transposed: shape {:?} data {:?}",
24 tt.shape(),
25 tt.as_slice()
26 );
27 assert_eq!(tt.shape(), &[3, 2]);
28 assert_eq!(tt.as_slice(), &[1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
29 println!("ok");
30}