#![forbid(unsafe_code)]
pub trait Differentiable {
type Tangent;
type Cotangent;
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct NoTangent;
impl Differentiable for f32 {
type Tangent = f32;
type Cotangent = f32;
}
impl Differentiable for f64 {
type Tangent = f64;
type Cotangent = f64;
}
impl Differentiable for () {
type Tangent = NoTangent;
type Cotangent = NoTangent;
}
impl<A, B> Differentiable for (A, B)
where
A: Differentiable,
B: Differentiable,
{
type Tangent = (A::Tangent, B::Tangent);
type Cotangent = (A::Cotangent, B::Cotangent);
}
impl<T, const N: usize> Differentiable for [T; N]
where
T: Differentiable,
{
type Tangent = [T::Tangent; N];
type Cotangent = [T::Cotangent; N];
}
impl<T> Differentiable for Vec<T>
where
T: Differentiable,
{
type Tangent = Vec<T::Tangent>;
type Cotangent = Vec<T::Cotangent>;
}
#[cfg(feature = "num-complex")]
impl<T> Differentiable for num_complex::Complex<T>
where
T: Differentiable,
{
type Tangent = num_complex::Complex<T::Tangent>;
type Cotangent = num_complex::Complex<T::Cotangent>;
}
pub trait JvpRule<Args>
where
Args: Differentiable,
{
type Output: Differentiable;
type Error;
#[allow(clippy::type_complexity)]
fn jvp(
&self,
args: &Args,
tangent: &Args::Tangent,
) -> Result<(Self::Output, <Self::Output as Differentiable>::Tangent), Self::Error>;
}
pub trait Pullback<OutputCotangent, InputCotangent> {
type Error;
fn apply(self, cotangent: OutputCotangent) -> Result<InputCotangent, Self::Error>;
}
impl<F, OutputCotangent, InputCotangent, Error> Pullback<OutputCotangent, InputCotangent> for F
where
F: FnOnce(OutputCotangent) -> Result<InputCotangent, Error>,
{
type Error = Error;
fn apply(self, cotangent: OutputCotangent) -> Result<InputCotangent, Self::Error> {
self(cotangent)
}
}
pub trait VjpRule<Args>
where
Args: Differentiable,
{
type Output: Differentiable;
type Error;
type Pullback<'a>: Pullback<<Self::Output as Differentiable>::Cotangent, Args::Cotangent, Error = Self::Error>
where
Self: 'a,
Args: 'a;
fn vjp<'a>(&'a self, args: &'a Args)
-> Result<(Self::Output, Self::Pullback<'a>), Self::Error>;
}
pub mod testing {
#[must_use]
pub fn central_difference<F>(f: F, x: f64, direction: f64, step: f64) -> f64
where
F: Fn(f64) -> f64,
{
assert_valid_step(step);
(f(x + step * direction) - f(x - step * direction)) / (2.0 * step)
}
pub fn directional_central_difference<Output, Error, Evaluate, Difference>(
mut evaluate_at_offset: Evaluate,
step: f64,
difference: Difference,
) -> Result<Output, Error>
where
Evaluate: FnMut(f64) -> Result<Output, Error>,
Difference: FnOnce(Output, Output, f64) -> Output,
{
assert_valid_step(step);
let positive = evaluate_at_offset(step)?;
let negative = evaluate_at_offset(-step)?;
Ok(difference(positive, negative, 2.0 * step))
}
pub fn assert_close(actual: f64, expected: f64, relative: f64, absolute: f64) {
assert!(
relative.is_finite() && relative >= 0.0,
"relative tolerance must be finite and nonnegative"
);
assert!(
absolute.is_finite() && absolute >= 0.0,
"absolute tolerance must be finite and nonnegative"
);
let tolerance = absolute + relative * actual.abs().max(expected.abs());
assert!(
(actual - expected).abs() <= tolerance,
"values differ: actual={actual}, expected={expected}, tolerance={tolerance}"
);
}
#[allow(clippy::too_many_arguments)]
pub fn assert_adjoint_identity<
OutputTangent,
OutputCotangent,
InputTangent,
InputCotangent,
OutputPairing,
InputPairing,
>(
jvp: &OutputTangent,
output_cotangent: &OutputCotangent,
input_tangent: &InputTangent,
input_cotangent: &InputCotangent,
output_pairing: OutputPairing,
input_pairing: InputPairing,
relative: f64,
absolute: f64,
) where
OutputPairing: FnOnce(&OutputTangent, &OutputCotangent) -> f64,
InputPairing: FnOnce(&InputTangent, &InputCotangent) -> f64,
{
assert_close(
output_pairing(jvp, output_cotangent),
input_pairing(input_tangent, input_cotangent),
relative,
absolute,
);
}
pub fn assert_scalar_adjoint_identity(
jvp: f64,
output_cotangent: f64,
input_tangent: f64,
input_cotangent: f64,
tolerance: f64,
) {
assert_adjoint_identity(
&jvp,
&output_cotangent,
&input_tangent,
&input_cotangent,
|left, right| left * right,
|left, right| left * right,
tolerance,
tolerance,
);
}
fn assert_valid_step(step: f64) {
assert!(
step.is_finite() && step > 0.0,
"finite-difference step must be finite and positive"
);
}
}