pub trait SwappedOperations<Lhs = Self> {
    type Output;

    // Required methods
    fn sub_swapped(self, lhs: Lhs) -> Self::Output;
    fn div_swapped(self, lhs: Lhs) -> Self::Output;
}
Expand description

A trait which defines subtraction and division with the arguments swapped around, ie 5.sub_swapped(7) would equal 2. This trait is only implemented for Records and constant operations.

Addition and Multiplication are not included because argument order doesn’t matter for those operations, so you can just swap the left and right and get the same result.

Implementations for Trace are not included because you can just lift a constant to a Trace with ease. While you can lift constants to Records with ease too, these operations allow for the avoidance of storing the constant on the WengertList which saves memory.

use easy_ml::differentiation::{Record, RecordTensor, WengertList};
use easy_ml::differentiation::record_operations::SwappedOperations;
use easy_ml::tensors::Tensor;

let history = WengertList::new();

let x = Record::variable(-1.0, &history);
let z = x.sub_swapped(10.0);
assert_eq!(z.number, 11.0);

let X = RecordTensor::variables(
    &history,
    Tensor::from_fn([("x", 2), ("y", 2)], |[r, c]| ((r + 4) * (c + 1)) as f64)
);
let Z = X.div_swapped(100.0);
assert_eq!(
    Z.view().map(|(x, _)| x),
    Tensor::from([("x", 2), ("y", 2)], vec![ 25.0, 12.5, 20.0, 10.0 ])
);

Required Associated Types§

Required Methods§

source

fn sub_swapped(self, lhs: Lhs) -> Self::Output

source

fn div_swapped(self, lhs: Lhs) -> Self::Output

Implementors§

source§

impl<'a, T, S> SwappedOperations<&T> for &RecordMatrix<'a, T, S>

§

type Output = RecordContainer<'a, T, MatrixView<(T, usize), Matrix<(T, usize)>>, 2>

source§

impl<'a, T, S> SwappedOperations<&T> for RecordMatrix<'a, T, S>

§

type Output = RecordContainer<'a, T, MatrixView<(T, usize), Matrix<(T, usize)>>, 2>

source§

impl<'a, T, S> SwappedOperations<T> for &RecordMatrix<'a, T, S>

§

type Output = RecordContainer<'a, T, MatrixView<(T, usize), Matrix<(T, usize)>>, 2>

source§

impl<'a, T, S> SwappedOperations<T> for RecordMatrix<'a, T, S>

§

type Output = RecordContainer<'a, T, MatrixView<(T, usize), Matrix<(T, usize)>>, 2>

source§

impl<'a, T, S, const D: usize> SwappedOperations<&T> for &RecordTensor<'a, T, S, D>
where T: Numeric + Primitive, for<'t> &'t T: NumericRef<T>, S: TensorRef<(T, Index), D>,

§

type Output = RecordContainer<'a, T, TensorView<(T, usize), Tensor<(T, usize), D>, D>, D>

source§

impl<'a, T, S, const D: usize> SwappedOperations<&T> for RecordTensor<'a, T, S, D>
where T: Numeric + Primitive, for<'t> &'t T: NumericRef<T>, S: TensorRef<(T, Index), D>,

§

type Output = RecordContainer<'a, T, TensorView<(T, usize), Tensor<(T, usize), D>, D>, D>

source§

impl<'a, T, S, const D: usize> SwappedOperations<T> for &RecordTensor<'a, T, S, D>
where T: Numeric + Primitive, for<'t> &'t T: NumericRef<T>, S: TensorRef<(T, Index), D>,

§

type Output = RecordContainer<'a, T, TensorView<(T, usize), Tensor<(T, usize), D>, D>, D>

source§

impl<'a, T, S, const D: usize> SwappedOperations<T> for RecordTensor<'a, T, S, D>
where T: Numeric + Primitive, for<'t> &'t T: NumericRef<T>, S: TensorRef<(T, Index), D>,

§

type Output = RecordContainer<'a, T, TensorView<(T, usize), Tensor<(T, usize), D>, D>, D>

source§

impl<'a, T: Numeric + Primitive> SwappedOperations<&T> for &Record<'a, T>
where for<'t> &'t T: NumericRef<T>,

§

type Output = Record<'a, T>

source§

impl<'a, T: Numeric + Primitive> SwappedOperations<&T> for Record<'a, T>
where for<'t> &'t T: NumericRef<T>,

§

type Output = Record<'a, T>

source§

impl<'a, T: Numeric + Primitive> SwappedOperations<T> for &Record<'a, T>
where for<'t> &'t T: NumericRef<T>,

§

type Output = Record<'a, T>

source§

impl<'a, T: Numeric + Primitive> SwappedOperations<T> for Record<'a, T>
where for<'t> &'t T: NumericRef<T>,

§

type Output = Record<'a, T>