pub fn to_dim<R, T, B, D, D2>(
tensor: &TensorBase<Storage<R, T, B>, D>,
) -> TensorBase<Storage<DataRef<'_, <B as DeviceRawAPI<T>>::Raw>, T, B>, D2>where
D: DimAPI + DimIntoAPI<D2>,
D2: DimAPI,
R: DataAPI<Data = <B as DeviceRawAPI<T>>::Raw>,
B: DeviceAPI<T>,Expand description
Convert layout to the other dimension.
This function is intended to be used as associated method TensorAny::into_dim. We just
provide the API document here.
§Parameters
-
tensor:&TensorAny<R, T, B, D>- The input tensor.
-
generic parameter
D2:DimAPI- Target dimension type.
IxDorIxDyn(equilvalent toVec<usize>) can be used for dynamic dimension.Ix<N>(givenconst N: usize, equilvalent to[usize; N]) can be used for static dimension.- Dynamic dimensionality is usually more preferred for general use cases, while static dimensionality is more suitable for performance-critical code, especially frequent indexing and non-contiguous memory access.
§Returns
-
- A view of the input tensor with the layout converted to the target dimension type.
- The underlying data is not copied; only the layout of the view is modified.
- If you want to convert the tensor itself (taking the ownership instead of returning view),
use
into_diminstead.
§Examples
In most cases, RSTSR will generate dynamic dimension (IxD) in most cases, including indexing
slice, reshaping reshape, creation asarray.
You can debug print tensor or it’s layout to verify the dimensionality, or call const_ndim to
check whether the dimension is static or dynamic.
use rstsr::prelude::*;
let a = rt::arange(6).into_shape([2, 3]); // shape: (2, 3), IxD
println!("a: {:?}", a);
// `None` here indicates dynamic dimension
assert_eq!(a.shape().const_ndim(), None);You can convert the dimension to static dimension by associate method:
let b = a.to_dim::<Ix2>(); // shape: (2, 3), Ix2
assert_eq!(b.shape().const_ndim(), Some(2));You can use .to_dim::<IxD>() or .to_dyn() to convert back to dynamic dimension:
let c = b.to_dyn(); // shape: (2, 3), IxD
assert_eq!(c.shape().const_ndim(), None);§Panics
- The shape of the tensor is not compatible with the target dimension type.
use rstsr::prelude::*;
let a = rt::arange(6).into_shape([2, 3]); // shape: (2, 3), IxD
let b = a.to_dim::<Ix3>(); // shape: (2, 3), Ix3, panics§See also
§Similar functions from other crates/libraries
- ndarray:
into_dimensionality - ndarray:
into_dyn
§Variants of this function
-
to_dim: Borrowing version. -
to_dim_f: Fallible borrowing version. -
into_dim: Consuming version. -
into_dim_f: Consuming and fallible version, actual implementation. -
to_dyn: Convert to dynamic dimension, borrowing version (infallible by definition). -
into_dyn: Convert to dynamic dimension, consuming version (infallible by definition). -
Associated methods on
TensorAny: