tensor_from_nested

Macro tensor_from_nested 

Source
macro_rules! tensor_from_nested {
    ([$([$([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*], $device:expr) => { ... };
    ([$([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*], $device:expr) => { ... };
    ([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*], $device:expr) => { ... };
    ([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*], $device:expr) => { ... };
    ([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*], $device:expr) => { ... };
    ([$([$($x:expr),* $(,)*]),+ $(,)*], $device:expr) => { ... };
    ([$($x:expr),* $(,)*], $device:expr) => { ... };
    ([$([$([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]) => { ... };
    ([$([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]) => { ... };
    ([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]) => { ... };
    ([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]) => { ... };
    ([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]) => { ... };
    ([$([$($x:expr),* $(,)*]),+ $(,)*]) => { ... };
    ([$($x:expr),* $(,)*]) => { ... };
}
Expand description

Create a tensor from a nested array literal (for development purposes).

§Examples

use rstsr::prelude::*;

let tsr = rt::tensor_from_nested!(
    [[1, 2, 3],
     [4, 5, 6]]
);
println!("{tsr:?}");
// [[ 1 2 3]
//  [ 4 5 6]]
// 2-Dim (dyn), contiguous: Cc

let tsr = rt::tensor_from_nested!(
    [[[1, 2],
      [3, 4]],
     [[5, 6],
      [7, 8]]]
);
println!("{tsr:?}");
// [[[ 1 2]
//   [ 3 4]]
//  [[ 5 6]
//   [ 7 8]]]
// 3-Dim (dyn), contiguous: Cc

If you want to specify the device, you can do it like this:

use rstsr::prelude::*;

let device = DeviceFaer::default(); // or other devices
let tsr = rt::tensor_from_nested!([[1, 2, 3], [4, 5, 6]], &device);
println!("{tsr:?}"); // you will get a tensor on DeviceFaer

§Notes on usage

  • This macro is used for testing or prototyping purposes. For production code, it is recommended to use other methods to create tensors (e.g., asarray) to have better control and performance.
  • This macro only gives row-major (C-contiguous) layout tensors, even if the device’s default order is column-major.
  • The macro supports up to 6 dimensions. For 7 or more dimensions, use other methods to create tensors.
  • The created tensor is on the DeviceCpu (depends on crate feature faer_as_default) and has dynamic dimension type IxD.
  • It is adviced to use brackets [] to denote each dimension level, instead of parentheses ().

§See also