pub struct Sum<T>(pub T);Expand description
Wrapper type for the addition monoid.
Sum<T> forms a monoid under addition (+), making it useful for
accumulating numeric values, counting, or gradient accumulation in
automatic differentiation.
§Examples
use algebra_core::{Monoid, Semigroup, Sum};
let a = Sum(5);
let b = Sum(3);
let c = a.combine(&b);
assert_eq!(c, Sum(8));
assert_eq!(Sum::<i32>::empty(), Sum(0));§Use in Autodiff
Gradient accumulation in reverse-mode automatic differentiation
is fundamentally Sum<f64>:
use algebra_core::{Semigroup, Sum};
let grad1 = Sum(0.5);
let grad2 = Sum(0.3);
let total_grad = grad1.combine(&grad2);
assert_eq!(total_grad.0, 0.8);Tuple Fields§
§0: TTrait Implementations§
Source§impl<T: Ord> Ord for Sum<T>
impl<T: Ord> Ord for Sum<T>
Source§impl<T: PartialOrd> PartialOrd for Sum<T>
impl<T: PartialOrd> PartialOrd for Sum<T>
impl<T: Add<Output = T> + Clone + Zero> CommutativeMonoid for Sum<T>
impl<T: Copy> Copy for Sum<T>
impl<T: Eq> Eq for Sum<T>
impl<T> StructuralPartialEq for Sum<T>
Auto Trait Implementations§
impl<T> Freeze for Sum<T>where
T: Freeze,
impl<T> RefUnwindSafe for Sum<T>where
T: RefUnwindSafe,
impl<T> Send for Sum<T>where
T: Send,
impl<T> Sync for Sum<T>where
T: Sync,
impl<T> Unpin for Sum<T>where
T: Unpin,
impl<T> UnwindSafe for Sum<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more