# Numeric algorithms and constants module#include "diplib/accumulators.h" dip::MomentAccumulator class

MomentAccumulator accumulates the zeroth order moment, the first order normalized moments, and the second order central normalized moments, in N dimensions.

### Contents

• Reference

Samples are added one by one, using the Push method. Other members are used to retrieve the moments.

It is possible to accumulate samples in different objects (e.g. when processing with multiple threads), and add the accumulators together using the + operator.

## Constructors, destructors, assignment and conversion operators

The constructor determines the dimensionality for the object.

## Functions

void Reset()
Reset the accumulator, leaving it as if newly allocated.
void Push(dip::FloatArray const& pos, dip::dfloat weight)
Add a sample to the accumulator. pos must have N dimensions.
auto Sum() const -> dip::dfloat
Sum of weights (zeroth order moment)
auto FirstOrder() const -> dip::FloatArray
First order moments, normalized
auto SecondOrder() const -> dip::FloatArray
Second order central moment tensor, normalized

## Operators

auto operator+=(dip::MomentAccumulator const& b) -> dip::MomentAccumulator&
Combine two accumulators

## Function documentation

### dip::FloatArray SecondOrder() const

Second order central moment tensor, normalized

The moments are stored in the same order as symmetric tensors are stored in an image (see dip::Tensor::Shape). That is, fist are the main diagonal elements, then the elements above the diagonal, column-wise. This translates to:

• 2D: xx, yy, xy
• 3D: xx, yy, zz, xy, xz, yz
• 4D: xx, yy, zz, tt, xy, xz, yz, xt, yt, zt
• etc.

The second order moment tensor is defined as:

where is the identity matrix ( ), is the weight of point , and is its position. In 2D, this leads to: