use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Neg, Sub};
use super::Shape;
pub trait Differentiable:
Clone
+ Debug
+ Send
+ Sync
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ Neg<Output = Self>
{
type Accumulator: Clone
+ Debug
+ Send
+ Sync
+ Add<Output = Self::Accumulator>
+ Mul<Output = Self::Accumulator>;
fn promote(&self) -> Self::Accumulator;
fn demote(accumulated: Self::Accumulator) -> Self;
fn zero_like(&self) -> Self;
fn one_like(&self) -> Self;
fn counted(shape: Shape, count: usize) -> Self;
fn shape(&self) -> Shape;
}
impl Differentiable for f32 {
type Accumulator = Self;
fn promote(&self) -> Self {
*self
}
fn demote(accumulated: Self) -> Self {
accumulated
}
fn zero_like(&self) -> Self {
0.0
}
fn one_like(&self) -> Self {
1.0
}
fn counted(_shape: Shape, count: usize) -> Self {
count as f32
}
fn shape(&self) -> Shape {
Shape::scalar()
}
}
impl Differentiable for f64 {
type Accumulator = Self;
fn promote(&self) -> Self {
*self
}
fn demote(accumulated: Self) -> Self {
accumulated
}
fn zero_like(&self) -> Self {
0.0
}
fn one_like(&self) -> Self {
1.0
}
fn counted(_shape: Shape, count: usize) -> Self {
count as f64
}
fn shape(&self) -> Shape {
Shape::scalar()
}
}