concision_data/params/
parameter.rs

1/*
2    Appellation: param <mod>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use super::{Param, ParamKind};
6use ndarray::linalg::Dot;
7use ndarray::prelude::{Array, Dimension, Ix2};
8use ndarray::IntoDimension;
9use num::Float;
10
11#[derive(Clone, Debug, Eq, PartialEq)]
12#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
13pub struct Parameter<T = f64, D = Ix2>
14where
15    D: Dimension,
16{
17    pub(crate) features: D,
18    pub(crate) kind: ParamKind,
19    pub(crate) name: String,
20    pub(crate) value: Array<T, D>,
21}
22
23impl<T, D> Parameter<T, D>
24where
25    D: Dimension,
26{
27    pub fn new(features: impl IntoDimension<Dim = D>, kind: ParamKind, name: impl ToString) -> Self
28    where
29        T: Clone + Default,
30    {
31        let features = features.into_dimension();
32        Self {
33            features: features.clone(),
34            kind,
35            name: name.to_string(),
36            value: Array::default(features),
37        }
38    }
39
40    pub fn kind(&self) -> &ParamKind {
41        &self.kind
42    }
43
44    pub fn kind_mut(&mut self) -> &mut ParamKind {
45        &mut self.kind
46    }
47
48    pub fn name(&self) -> &str {
49        &self.name
50    }
51
52    pub fn name_mut(&mut self) -> &mut String {
53        &mut self.name
54    }
55
56    pub fn data(&self) -> &Array<T, D> {
57        &self.value
58    }
59
60    pub fn params_mut(&mut self) -> &mut Array<T, D> {
61        &mut self.value
62    }
63
64    pub fn set_kind(&mut self, kind: ParamKind) {
65        self.kind = kind;
66    }
67
68    pub fn set_name(&mut self, name: String) {
69        self.name = name;
70    }
71
72    pub fn set_params(&mut self, params: Array<T, D>) {
73        self.value = params;
74    }
75
76    pub fn with_kind(self, kind: ParamKind) -> Self {
77        Self { kind, ..self }
78    }
79
80    pub fn with_name(self, name: impl ToString) -> Self {
81        Self {
82            name: name.to_string(),
83            ..self
84        }
85    }
86
87    pub fn with_params(self, params: Array<T, D>) -> Self {
88        Self {
89            value: params,
90            ..self
91        }
92    }
93}
94
95impl<T, D> Param for Parameter<T, D>
96where
97    T: Float,
98    D: Dimension,
99{
100    type Key = ParamKind;
101    type Value = Array<T, D>;
102}
103
104impl<S, T, D, O> Dot<S> for Parameter<T, D>
105where
106    Array<T, D>: Dot<S, Output = O>,
107    D: Dimension,
108    T: Float,
109{
110    type Output = O;
111
112    fn dot(&self, rhs: &S) -> Self::Output {
113        self.value.dot(rhs)
114    }
115}