use ndarray::iter as nditer;
use ndarray::{ArrayBase, Data, DataMut, Dimension, RawData};
pub trait Weighted<S, D, A = <S as RawData>::Elem>: Sized
where
D: Dimension,
S: RawData<Elem = A>,
{
type Tensor<_S, _D, _A>
where
_D: Dimension,
_S: RawData<Elem = _A>;
fn weights(&self) -> &Self::Tensor<S, D, A>;
fn weights_mut(&mut self) -> &mut Self::Tensor<S, D, A>;
fn replace_weights(&mut self, weights: Self::Tensor<S, D, A>) -> Self::Tensor<S, D, A> {
core::mem::replace(self.weights_mut(), weights)
}
fn set_weights(&mut self, weights: Self::Tensor<S, D, A>) -> &mut Self {
*self.weights_mut() = weights;
self
}
}
pub trait Biased<S, D, A = <S as RawData>::Elem>: Weighted<S, D, A>
where
D: Dimension,
S: RawData<Elem = A>,
{
fn bias(&self) -> &ArrayBase<S, D::Smaller, A>;
fn bias_mut(&mut self) -> &mut ArrayBase<S, D::Smaller, A>;
fn assign_bias(&mut self, bias: &ArrayBase<S, D::Smaller, A>) -> &mut Self
where
S: DataMut,
S::Elem: Clone,
{
self.bias_mut().assign(bias);
self
}
fn replace_bias(&mut self, bias: ArrayBase<S, D::Smaller, A>) -> ArrayBase<S, D::Smaller, A> {
core::mem::replace(self.bias_mut(), bias)
}
fn set_bias(&mut self, bias: ArrayBase<S, D::Smaller, A>) -> &mut Self {
*self.bias_mut() = bias;
self
}
fn iter_bias<'a>(&'a self) -> nditer::Iter<'a, S::Elem, D::Smaller>
where
S: Data + 'a,
D: 'a,
{
self.bias().iter()
}
fn iter_bias_mut<'a>(&'a mut self) -> nditer::IterMut<'a, S::Elem, D::Smaller>
where
S: DataMut + 'a,
D: 'a,
{
self.bias_mut().iter_mut()
}
}