Trait rustml::math::sum::Sum [] [src]

pub trait Sum<T> {
    fn sum(&self, dim: Dimension) -> T;

Trait to compute the sum of values.

Required Methods

fn sum(&self, dim: Dimension) -> T

Computes the sum over all elements of the specified dimension.

When computing the sum of a vector or a slice both are interpreted as a row vector. Thus, the only valid dimension for vectors or slices is Dimension::Row. For other dimensions the function will always return zero.


Compute the sum of all elements of a vector.

use rustml::*;

let v = vec![1.0, 5.0, 9.0];
assert_eq!(v.sum(), 15.0);

To compute the sum of all elements in each column or each row of a matrix you have to specify the correct dimension.

use rustml::*;

let m = mat![
    1.0, 2.0; 
    5.0, 10.0

assert_eq!(m.sum(Dimension::Row), vec![3.0, 15.0]);
assert_eq!(m.sum(Dimension::Column), vec![6.0, 12.0]);

Implementation details

Currently, the following code is used to compute the sum of a row:

self.iter().fold(T::zero(), |init, &val| init + val)

In the future this may be replaced by a more efficient implementation.