qfeval_functions.functions.mmax

mmax(x, span, dim=-1)[source]

Compute the moving (sliding window) maximum of a tensor.

This function calculates the maximum value within a sliding window of size span along the specified dimension. The output tensor has the same shape as the input tensor. For positions where the sliding window cannot fully cover preceding elements (i.e., the first span - 1 elements along the selected dimension), the result is computed using available values by padding with the first element.

The moving maximum is computed as:

\[\text{MMAX}[i] = \max_{j=\max(0, i-\text{span}+1)}^{i} x[j]\]
Parameters:
  • x (Tensor) – The input tensor containing values.

  • span (int) – The size of the sliding window. Must be positive.

  • dim (int) – The dimension along which to compute the moving maximum. Default is -1 (the last dimension).

Returns:

A tensor of the same shape as the input, containing the moving maximum values.

Return type:

Tensor

Example

>>> # Simple moving maximum with window size 3
>>> x = torch.tensor([1.0, 5.0, 3.0, 8.0, 2.0])
>>> QF.mmax(x, span=3)
tensor([1., 5., 5., 8., 8.])
>>> # 2D tensor with moving maximum along columns
>>> x = torch.tensor([[1.0, 4.0, 2.0, 6.0],
...                   [3.0, 1.0, 5.0, 2.0],
...                   [2.0, 7.0, 3.0, 4.0]])
>>> QF.mmax(x, span=2, dim=1)
tensor([[1., 4., 4., 6.],
        [3., 3., 5., 5.],
        [2., 7., 7., 4.]])
>>> # Moving maximum along the first dimension
>>> x = torch.tensor([[1.0, 2.0],
...                   [4.0, 1.0],
...                   [2.0, 5.0],
...                   [3.0, 2.0]])
>>> QF.mmax(x, span=3, dim=0)
tensor([[1., 2.],
        [4., 2.],
        [4., 5.],
        [4., 5.]])
>>> # Handling negative values
>>> x = torch.tensor([-2.0, -1.0, -5.0, -3.0, -1.0])
>>> QF.mmax(x, span=2)
tensor([-2., -1., -1., -3., -1.])

See also

mmin(): Moving minimum function. msum(): Moving sum function.