concision_traits/tensor/
dimensionality.rs

1/*
2    Appellation: dimensionality <module>
3    Created At: 2025.12.09:10:03:43
4    Contrib: @FL03
5*/
6
7pub trait DimConst<const N: usize> {}
8
9/// the [`Dim`] trait is used to define a type that can be used as a raw dimension.
10/// This trait is primarily used to provide abstracted, generic interpretations of the
11/// dimensions of the [`ndarray`] crate to ensure long-term compatibility.
12pub trait Dim {
13    type Shape;
14
15    private! {}
16
17    /// returns the rank of the dimension; the rank essentially speaks to the total number of
18    /// axes defined by the dimension.
19    fn rank(&self) -> usize;
20    /// returns the total number of elements considered by the dimension
21    fn size(&self) -> usize;
22}
23
24use ndarray::{Axis, Dimension, RemoveAxis};
25
26/// The [`IntoAxis`] trait is used to define a conversion routine that takes a type and wraps
27/// it in an [`Axis`] type.
28pub trait IntoAxis {
29    fn into_axis(self) -> Axis;
30}
31
32/// The [`AsBiasDim`] trait is used to define a type that can be used to get the bias dimension
33/// of the parameters.
34pub trait AsBiasDim<D: Dimension> {
35    /// returns the bias dimension of the parameters
36    fn as_bias_dim(&self) -> D;
37}
38
39/*
40 ************* Implementations *************
41*/
42
43impl<S> IntoAxis for S
44where
45    S: AsRef<usize>,
46{
47    fn into_axis(self) -> Axis {
48        Axis(*self.as_ref())
49    }
50}
51
52impl<A, B> AsBiasDim<B> for A
53where
54    A: RemoveAxis<Smaller = B>,
55    B: Dimension,
56{
57    /// returns the bias dimension of the parameters by removing the "zero-th" axis from the
58    /// given dimension
59    fn as_bias_dim(&self) -> B {
60        self.remove_axis(Axis(0))
61    }
62}
63
64/*
65 ************* Implementations *************
66*/
67
68impl<D> Dim for D
69where
70    D: ndarray::Dimension,
71{
72    type Shape = D::Pattern;
73
74    seal! {}
75
76    fn rank(&self) -> usize {
77        self.ndim()
78    }
79
80    fn size(&self) -> usize {
81        self.size()
82    }
83}