Skip to content

Reduction

Reduction refers to a generalized mechanism of transformations over values that, when applied to tensors yield a type reduced from the original. Reduced type is either a scalar or a tensor with smaller nesting depth.

Only built-in functions min, max, all and any support reduction. For more details about these functions, see Built-in functions.

Reduction supports tensors of arbitrary nesting depth and dimensions, but it is not applied recursively. If nesting depth exceeds minimal level needed for the chosen reduction type, reduction is performed on the deepest levels of nesting.

All reduction functions support scalar inputs and behave as identity functions in those cases.

Innermost reduction

Innermost reduction is a generalized mechanism of transforming a tensor to a single value. Innermost reduction is performed following these rules:

  1. If a scalar is passed, it behaves as identity function
  2. If a tensor (i.e. nesting depth is greater or equal to 1) is passed:
  3. Scalars on the deepest level of nesting are selected
  4. Operation is performed on all scalars that belong to the same tensor
  5. Result is formed and it has the same type as base scalar type
  6. Target tensor type that was reduced is replaced by the base scalar type
  7. If nesting depth is greater than 1, outer tensor types are not affected

Innermost reduction examples and syntax are shown below.

a = [1, 2, 3]

x = [[1, 2, 3], [-1, 5, -3]]

y = [
        [[1, 2, 3], [-1, 2, -3]],
        [[1, 2, 3], [-1, 2, -3]]
]

c = min(a) # 1

d = min(x) # [1, -3]

e = min(y) # [[1, -3], [1, -3]]

Overlapping reduction

Overlapping reduction is a generalized mechanism of transforming a tensor of tensors to a tensor formed from all corresponding index values. Overlapping reduction is performed following these rules:

  1. If a scalar is passed, it behaves as identity function
  2. If a tensor with scalars is passed, it behaves the same as innermost reduction
  3. If a tensor of tensors (i.e. nesting depth is greater or equal to 2) is passed:
  4. Tensors on the deepest level of nesting are selected
  5. Operation is performed accross all scalars with corresponding indices and the result is stored to the result on the same index as input values
  6. Result is formed and it has the same type as base type of the reduced tensor
  7. Target tensor type that was reduced is replaced by the base tensor type
  8. If nesting depth is greater than 2, outer tensor types are not affected

Overlapping reduction is signified in the name of a function by the ! ending. E.g. min uses innermost reduction while min! uses overlapping reduction.

Overlapping reduction examples and syntax are shown below.

a = [1, 2, 3]

b = [[1, 2, 3]]

x = [[1, 2, 3], [-1, 5, -3]]

y = [
        [[1, 2, 3], [-1, 2, -3]],
        [[1, 2, 3], [-1, 2, -3]]
]

c = min!(a) # 1 - same as innermost reduction!

d = min!(b) # [1, 2, 3] - no other tensors to overlap with!

e = min!(x) # [-1, 2, -3]

f = min!(y) # [[-1, 2, -3], [-1, 2, -3]]