Skip to main content

to_arrayd

Function to_arrayd 

Source
pub fn to_arrayd(tensor: &Tensor) -> Result<ArrayD<f64>, MattenNdarrayError>
Expand description

Converts a numeric Tensor into an ndarray::ArrayD<f64>.

The result is standard (row-major) layout. A dynamic tensor returns MattenNdarrayError::DynamicTensor rather than panicking. This guard is unconditional — it does not depend on the companion dynamic feature being enabled (RFC-031).

use matten::Tensor;
use matten_ndarray::to_arrayd;

let t = Tensor::new(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]);
let arr = to_arrayd(&t).unwrap();
assert_eq!(arr.shape(), &[2, 2]);
assert_eq!(arr[[1, 0]], 3.0);
Examples found in repository?
examples/to_arrayd.rs (line 20)
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}