from_arrayd/from_arrayd.rs
1//! # Companion example: `ArrayD` -> `Tensor` (matten-ndarray)
2//!
3//! Run: cargo run -p matten-ndarray --example from_arrayd
4//!
5//! ## What this shows
6//! Converting an `ndarray::ArrayD<f64>` into a `matten::Tensor`, including a
7//! non-standard-layout (transposed) input.
8//!
9//! ## Teaching points
10//! - the conversion **copies** data into the `Tensor` (no zero-copy / borrow);
11//! - logical shape and element order are preserved, even for a transposed
12//! (non-contiguous) input;
13//! - the dependency direction is one-way: `matten-ndarray` depends on `matten`
14//! and `ndarray`, while core `matten` depends on neither.
15
16use matten_ndarray::from_arrayd;
17use ndarray::{ArrayD, IxDyn};
18
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}