function cumsum
cumsum(x: array): array

Cumulative sum of array elements.

Computes the cumulative sum of elements in an array or matrix along a specified dimension.

Examples

Cumulative sum of a 1D array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([5, 6, 3]), [5, 11, 14]);

Cumulative sum of a matrix along columns (dim=1)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 1), [[5, 12], [6,14], [5, 4]]);

Cumulative sum of a matrix along rows (dim=0)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 0), [[5, 11, 16], [7, 15, 14]]);

Parameters

The input array or matrix of values

Return Type

The cumulative sum of the input values

Throws

If no input is provided

cumsum(
x: array,
dim: 0,
): array

Cumulative sum of array elements.

Computes the cumulative sum of elements in an array or matrix along a specified dimension.

Examples

Cumulative sum of a 1D array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([5, 6, 3]), [5, 11, 14]);

Cumulative sum of a matrix along columns (dim=1)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 1), [[5, 12], [6,14], [5, 4]]);

Cumulative sum of a matrix along rows (dim=0)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 0), [[5, 11, 16], [7, 15, 14]]);

Parameters

The input array or matrix of values

dim: 0

The dimension along which to calculate, 1 for columns, 0 for rows (defaults to 1)

Return Type

The cumulative sum of the input values

Throws

If no input is provided

cumsum(
x: array,
dim: 1,
): array

Cumulative sum of array elements.

Computes the cumulative sum of elements in an array or matrix along a specified dimension.

Examples

Cumulative sum of a 1D array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([5, 6, 3]), [5, 11, 14]);

Cumulative sum of a matrix along columns (dim=1)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 1), [[5, 12], [6,14], [5, 4]]);

Cumulative sum of a matrix along rows (dim=0)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 0), [[5, 11, 16], [7, 15, 14]]);

Parameters

The input array or matrix of values

dim: 1

The dimension along which to calculate, 1 for columns, 0 for rows (defaults to 1)

Return Type

The cumulative sum of the input values

Throws

If no input is provided

cumsum(
x: matrix,
dim: 0,
): matrix

Cumulative sum of array elements.

Computes the cumulative sum of elements in an array or matrix along a specified dimension.

Examples

Cumulative sum of a 1D array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([5, 6, 3]), [5, 11, 14]);

Cumulative sum of a matrix along columns (dim=1)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 1), [[5, 12], [6,14], [5, 4]]);

Cumulative sum of a matrix along rows (dim=0)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 0), [[5, 11, 16], [7, 15, 14]]);

Parameters

The input array or matrix of values

dim: 0

The dimension along which to calculate, 1 for columns, 0 for rows (defaults to 1)

Return Type

The cumulative sum of the input values

Throws

If no input is provided

cumsum(
x: matrix,
dim: 1,
): matrix

Cumulative sum of array elements.

Computes the cumulative sum of elements in an array or matrix along a specified dimension.

Examples

Cumulative sum of a 1D array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([5, 6, 3]), [5, 11, 14]);

Cumulative sum of a matrix along columns (dim=1)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 1), [[5, 12], [6,14], [5, 4]]);

Cumulative sum of a matrix along rows (dim=0)

import { assertEquals } from "jsr:@std/assert";

assertEquals(cumsum([[5, 6, 5], [7, 8, -1]], 0), [[5, 11, 16], [7, 15, 14]]);

Parameters

The input array or matrix of values

dim: 1

The dimension along which to calculate, 1 for columns, 0 for rows (defaults to 1)

Return Type

The cumulative sum of the input values

Throws

If no input is provided