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 (under the dynamic feature) returns MattenNdarrayError::DynamicTensor rather than panicking.

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 11)
9fn main() {
10    let t = Tensor::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]);
11    let arr = to_arrayd(&t).expect("numeric tensor converts");
12
13    println!("matten shape: {:?}", t.shape());
14    println!("ndarray shape: {:?}", arr.shape());
15    println!("ndarray[[1, 2]] = {}", arr[[1, 2]]); // row 1, col 2 -> 6.0
16    assert_eq!(arr[[1, 2]], 6.0);
17    println!("ok");
18}