concision_linear/
utils.rs

1/*
2    Appellation: utils <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::params::Biased;
6use nd::{ArrayBase, Axis, Dimension, RawData, RemoveAxis};
7
8/// A utilitarian funciton for building bias tensors.
9pub(crate) fn build_bias<S, D, E, F>(biased: bool, dim: D, builder: F) -> Option<ArrayBase<S, E>>
10where
11    D: RemoveAxis<Smaller = E>,
12    E: Dimension,
13    F: Fn(E) -> ArrayBase<S, E>,
14    S: RawData,
15{
16    if biased {
17        Some(builder(bias_dim::<D, E>(dim)))
18    } else {
19        None
20    }
21}
22
23pub(crate) fn bias_dim<D, E>(dim: D) -> E
24where
25    D: RemoveAxis<Smaller = E>,
26    E: Dimension,
27{
28    if dim.ndim() == 1 {
29        dim.remove_axis(Axis(0))
30    } else {
31        dim.remove_axis(Axis(1))
32    }
33}
34
35/// A utilitarian function for checking if a type is [Biased]; returns false otherwise.
36/// Compares the [TypeId](core::any::TypeId) of `K` to the [TypeId](core::any::TypeId) of [Biased].
37pub fn is_biased<K: 'static>() -> bool {
38    use core::any::TypeId;
39    TypeId::of::<K>() == TypeId::of::<Biased>()
40}