Skip to main content

to_arrayd/
to_arrayd.rs

1//! # Companion example: `Tensor` -> `ArrayD` (matten-ndarray)
2//!
3//! Run: cargo run -p matten-ndarray --example to_arrayd
4//!
5//! ## What this shows
6//! Converting a numeric `matten::Tensor` into an `ndarray::ArrayD<f64>`.
7//!
8//! ## Teaching points
9//! - the conversion **copies** data into the `ArrayD` (no zero-copy claim);
10//! - shape is preserved (printed before and after);
11//! - only numeric tensors convert — a dynamic/heterogeneous tensor must be made
12//!   numeric first (hence the `expect` on a `Result`);
13//! - core `matten` does not depend on `ndarray`; the bridge lives in this crate.
14
15use matten::Tensor;
16use matten_ndarray::to_arrayd;
17
18fn main() {
19    let t = Tensor::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]);
20    let arr = to_arrayd(&t).expect("numeric tensor converts");
21
22    println!("matten shape: {:?}", t.shape());
23    println!("ndarray shape: {:?}", arr.shape());
24    println!("ndarray[[1, 2]] = {}", arr[[1, 2]]); // row 1, col 2 -> 6.0
25    assert_eq!(t.shape(), arr.shape());
26    assert_eq!(arr[[1, 2]], 6.0);
27    println!("ok");
28}