# Add support for `mean` calculations

Issue #243 resolved

Although blaze has covered several fundamental operations, it does not have an operation to calculate the average. I believe having `mean` calculations can be really helpful. As an example, I need to implement some pooling operation. In the simplest case, I have:

``````blaze::DynamicMatrix<int> m{
{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},
{-1, -2, -3, -4}, {-5, -6, -7, -8}, {-9, -10, -11, -12}};

std::size_t filter_height = 2;
std::size_t filter_width = 2;

blaze::DynamicMatrix<double> result(
m.rows() - filter_height + 1, m.columns() - filter_width + 1);

for (std::size_t r = 0; r != result.rows(); ++r)
for (std::size_t c = 0; c != result.columns(); ++c)

result(r, c) = blaze::sum(
blaze::submatrix(m, r, c, filter_height, filter_width));

result /= (filter_height * filter_width);
``````

I need to use `blaze::sum` and a division. In more complex cases, the submatrix has a different size for each group of elements of my result matrix and I have to calculate divisions for each case separately.

Hi Bita!

Thanks for your proposal. We agree that a `mean()` function might be useful for a lot of people and thus would be a reasonable addition. We will consider it for one of the next releases.

One of the primary design goals of Blaze is to enable you to extend the functionality easily. For instance, the following function implements a reasonable `mean()` for your purposes:

``````template< typename MT, bool SO >
decltype(auto) mean( const Matrix<MT,SO>& m )
{
return sum( ~m ) / static_cast<double>( size( ~m ) );
}
``````

Please also take a look at the wiki, which might give you some further insight into how to extend Blaze with custom functionality. Thanks again for the proposal,

Best regards,

Klaus!

1. Although blaze has covered several fundamental operations, it does not have an operation to calculate the average. I believe having `mean` calculations can be really helpful. As an example, I need to implement some pooling operation. In the simplest case, I have:

``````blaze::DynamicMatrix<int> m{
{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},
{-1, -2, -3, -4}, {-5, -6, -7, -8}, {-9, -10, -11, -12}};

std::size_t filter_height = 2;
std::size_t filter_width = 2;

blaze::DynamicMatrix<double> result(
m.rows() - filter_height + 1, m.columns() - filter_width + 1);

for (std::size_t r = 0; r != result.rows(); ++r)
for (std::size_t c = 0; c != result.columns(); ++c)

result(r, c) = blaze::sum(
blaze::submatrix(m, r, c, filter_height, filter_width));

result /= (filter_height * filter_width);
``````

I need to use `blaze::sum` and a division. In more complex cases, the submatrix has a different size for each group of elements of my result matrix and I have to calculate divisions for each case separately.

• changed status to open
2. ## Summary

The feature has been implemented, tested, optimized, and documented as required. It is immediately available via cloning the Blaze repository and will be officially released in Blaze 3.6.

## mean()

The (arithmetic) mean of a dense or sparse vector or matrix can be computed via the `mean()` function. In case of a sparse vector or matrix, both the non-zero and zero elements are taken into account.

#### Vector Example

``````blaze::DynamicVector<int> v{ 1, 4, 3, 6, 7 };

const double m = mean( v );  // Results in 4.2 (i.e. 21/5)
``````

#### Matrix Example

``````blaze::DynamicMatrix<int> A{ { 1, 4, 3, 6, 7 }
, { 2, 6, 3, 1, 0 } };

const double m = mean( A );  // Results in 3.3 (i.e. 33/10)

blaze::DynamicVector<double,blaze::columnVector> rm;
blaze::DynamicVector<double,blaze::rowVector> cm;

rm = mean<rowwise>( A );     // Results in ( 4.2  2.4 )
cm = mean<columnwise>( A );  // Results in ( 1.5  5.0  3.0  3.5  3.5 )
``````