pub fn mean<T>(inp: &ndarray::NdArray<T>) -> Result<ndarray::NdArray<T>, NdArrayError>
where
    T: Copy + Default + std::iter::Sum + std::ops::Div<f32, Output = T>,
{
    match inp.shape() {
        Shape::Scalar(_) => Ok(inp.clone()),
        Shape::Vector([n]) => {
            let s: T = inp.as_slice().iter().cloned().sum();
            let res = s / *n as f32;
            let mut values = ndarray::Data::new();
            values.push(res);
            ndarray::NdArray::new_with_values(0, values)
        }
        Shape::Tensor(_) | Shape::Matrix([_, _]) => {
            let mut values = Vec::with_capacity(inp.shape().col_span());
            for col in inp.iter_rows() {
                let s: T = col.iter().cloned().sum();
                let res = s / (col.len() as f32);
                values.push(res)
            }
            let mut res = ndarray::NdArray::new_vector(values);
            let mut shape = inp.shape().clone();
            let l = shape.as_slice().len();
            shape.as_mut_slice()[l - 1] = 1;
            res.reshape(shape);
            Ok(res)
        }
    }
}
/// Calculate the column-wise mean.
///
/// Scalars will return themselves. While others will collapse the last column into a 1D vector.
///
/// To calculate the mean of all items reshape the array into a vector first.
///
///
/// ## Mean of tensor
///
/// ```
/// use facet_core::prelude::*;
///
/// let a = NdArray::new_with_values(&[2u32, 2, 3][..], smallvec![
///  1.0f32, 2.0f32, 3.0f32, 4.0f32, 5.0f32, 6.0f32, 7.0f32, 8.0f32, 9.0f32, 10.0f32, 11.0f32,
///  12.0f32
/// ]).unwrap();
///
/// let m = mean(&a).unwrap();
///
/// assert_eq!(a.shape(), &Shape::from(&[2, 2, 3][..]));
/// assert_eq!(m.shape(), &Shape::from(&[2, 2, 1][..]));
/// assert_eq!(m.as_slice(), &[2.0, 5.0, 8.0, 11.0]);
///
/// ```
///
/// ## Mean of vector
///
/// ```
/// use facet_core::prelude::*;
///
/// let a = NdArray::new_vector(smallvec![ 1.0f32, 2.0f32, 3.0f32, 4.0f32, 5.0f32, 6.0f32, 7.0f32, 8.0f32, 9.0f32, 10.0f32, 11.0f32, 12.0 ]);
///
/// let m = mean(&a).unwrap();
///
/// assert_eq!(a.shape(), &Shape::from(&[12][..]));
/// assert_eq!(m.shape(), &Shape::from(&[0][..]));
/// assert_eq!(m.as_slice(), &[6.5]);
///
/// ```
Calculate the column-wise mean.
Scalars will return themselves. While others will collapse the last column into a 1D vector.

To calculate the mean of all items reshape the array into a vector first.
Calculate the column - wise mean . Scalars will return themselves . While others will collapse the last column into a 1D vector . To calculate the mean of all items reshape the array into a vector first .
pub fn mean<T>(inp: &ndarray::NdArray<T>) -> Result<ndarray::NdArray<T>, NdArrayError>
where
    T: Copy + Default + std::iter::Sum + std::ops::Div<f32, Output = T>,
{
    match inp.shape() {
        Shape::Scalar(_) => Ok(inp.clone()),
        Shape::Vector([n]) => {
            let s: T = inp.as_slice().iter().cloned().sum();
            let res = s / *n as f32;
            let mut values = ndarray::Data::new();
            values.push(res);
            ndarray::NdArray::new_with_values(0, values)
        }
        Shape::Tensor(_) | Shape::Matrix([_, _]) => {
            let mut values = Vec::with_capacity(inp.shape().col_span());
            for col in inp.iter_rows() {
                let s: T = col.iter().cloned().sum();
                let res = s / (col.len() as f32);
                values.push(res)
            }
            let mut res = ndarray::NdArray::new_vector(values);
            let mut shape = inp.shape().clone();
            let l = shape.as_slice().len();
            shape.as_mut_slice()[l - 1] = 1;
            res.reshape(shape);
            Ok(res)
        }
    }
}
pub fn mean < T > ( inp : & ndarray :: NdArray < T > ) -> Result < ndarray :: NdArray < T > , NdArrayError > where T : Copy + Default + std :: iter :: Sum + std :: ops :: Div < f32 , Output = T > , { match inp . shape ( ) { Shape :: Scalar ( _ ) => Ok ( inp . clone ( ) ) , Shape :: Vector ( [ n ] ) => { let s : T = inp . as_slice ( ) . iter ( ) . cloned ( ) . sum ( ) ; let res = s / * n as f32 ; let mut values = ndarray :: Data :: new ( ) ; values . push ( res ) ; ndarray :: NdArray :: new_with_values ( 0 , values ) } Shape :: Tensor ( _ ) | Shape :: Matrix ( [ _ , _ ] ) => { let mut values = Vec :: with_capacity ( inp . shape ( ) . col_span ( ) ) ; for col in inp . iter_rows ( ) { let s : T = col . iter ( ) . cloned ( ) . sum ( ) ; let res = s / ( col . len ( ) as f32 ) ; values . push ( res ) } let mut res = ndarray :: NdArray :: new_vector ( values ) ; let mut shape = inp . shape ( ) . clone ( ) ; let l = shape . as_slice ( ) . len ( ) ; shape . as_mut_slice ( ) [ l - 1 ] = 1 ; res . reshape ( shape ) ; Ok ( res ) } } }
Calculate the column-wise mean.
Calculate the column - wise mean .
inp
&ndarray::NdArray<T>
mean of tensor
```
use facet_core::prelude::*.



let m = mean(&a).unwrap().




` ` ` use facet_core :: prelude :: * . let m = mean ( &a ) . unwrap () .
mean of vector
```
use facet_core::prelude::*.



let m = mean(&a).unwrap().




` ` ` use facet_core :: prelude :: * . let m = mean ( &a ) . unwrap () .