You are here

public static function MatrixUtility::cumulativeSum in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Component/MatrixUtility.php \Drupal\image_effects\Component\MatrixUtility::cumulativeSum()
  2. 8.2 src/Component/MatrixUtility.php \Drupal\image_effects\Component\MatrixUtility::cumulativeSum()

Calculates the cumulative sum matrix of a bi-dimensional matrix.

Parameters

array $matrix: A simple array of matrix rows values. Each row is a simple array of numeric values.

Return value

array A matrix representing the cumulative sum of the input matrix, in the form of a simple array of matrix rows values. Each row is a simple array of numeric values.

See also

https://stackoverflow.com/questions/39937212/maximum-subarray-of-size-hx...

2 calls to MatrixUtility::cumulativeSum()
GDOperationTrait::getEntropyCropByGridding in src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php
Computes the entropy crop of an image, using recursive gridding.
MatrixUtilityTest::testCumulativeSum in tests/src/Unit/MatrixUtilityTest.php
@covers ::cumulativeSum @dataProvider cumulativeSumProvider

File

src/Component/MatrixUtility.php, line 24

Class

MatrixUtility
Matrix handling methods for image_effects.

Namespace

Drupal\image_effects\Component

Code

public static function cumulativeSum(array $matrix) {
  $matrix_rows = count($matrix);
  $matrix_columns = count($matrix[0]);
  $cumulative_sum_matrix = [];
  for ($r = 0; $r < $matrix_rows; $r++) {
    for ($c = 0; $c < $matrix_columns; $c++) {
      if ($r === 0 && $c === 0) {
        $cumulative_sum_matrix[$r][$c] = $matrix[$r][$c];
      }
      elseif ($r == 0) {
        $cumulative_sum_matrix[$r][$c] = $cumulative_sum_matrix[$r][$c - 1] + $matrix[$r][$c];
      }
      elseif ($c == 0) {
        $cumulative_sum_matrix[$r][$c] = $cumulative_sum_matrix[$r - 1][$c] + $matrix[$r][$c];
      }
      else {
        $cumulative_sum_matrix[$r][$c] = $cumulative_sum_matrix[$r - 1][$c] + $cumulative_sum_matrix[$r][$c - 1] - $cumulative_sum_matrix[$r - 1][$c - 1] + $matrix[$r][$c];
      }
    }
  }
  return $cumulative_sum_matrix;
}