Skip to main content

from_arrayd

Function from_arrayd 

Source
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 22)
19fn main() {
20    let arr = ArrayD::from_shape_vec(IxDyn(&[2, 3]), vec![1., 2., 3., 4., 5., 6.]).unwrap();
21
22    let t = from_arrayd(arr.clone()).expect("contiguous converts");
23    println!(
24        "from contiguous: shape {:?} data {:?}",
25        t.shape(),
26        t.as_slice()
27    );
28
29    // Transposed input is non-standard layout; conversion preserves logical order.
30    let tt = from_arrayd(arr.t().to_owned()).expect("transposed converts");
31    println!(
32        "from transposed: shape {:?} data {:?}",
33        tt.shape(),
34        tt.as_slice()
35    );
36    assert_eq!(tt.shape(), &[3, 2]);
37    assert_eq!(tt.as_slice(), &[1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
38    println!("ok");
39}