function sum
sum(x: array): number

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

Return Type

number

The sum of the elements

sum(
x: array,
dim: 0 | 1,
): number

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

dim: 0 | 1

The dimension to sum across. Use 0 for rows and 1 for columns (defaults to 0)

Return Type

number

The sum of the elements

sum(
x: matrix,
dim: 0,
): array

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

dim: 0

The dimension to sum across. Use 0 for rows and 1 for columns (defaults to 0)

Return Type

The sum of the elements

sum(
x: matrix,
dim: 1,
): array

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

dim: 1

The dimension to sum across. Use 0 for rows and 1 for columns (defaults to 0)

Return Type

The sum of the elements

sum(x: matrix): array

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

Return Type

The sum of the elements

sum(
x: matrix,
dim: 0 | 1,
): array

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

dim: 0 | 1

The dimension to sum across. Use 0 for rows and 1 for columns (defaults to 0)

Return Type

The sum of the elements

sum(
dim?: 0 | 1,
): number | array

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

optional
dim: 0 | 1

The dimension to sum across. Use 0 for rows and 1 for columns (defaults to 0)

Return Type

number | array

The sum of the elements

sum(
dim: 0 | 1,
): number | array

Sum of array elements.

Computes the sum of array or matrix elements. If a matrix is provided, the sum can be calculated across rows or columns based on the specified dimension.

Examples

Sum of elements in a 1D array

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

assertEquals(sum([5, 6, 3]), 14);

Sum of elements across rows in a 2D array

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

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

Sum of elements across columns in a 2D array

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

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

Parameters

The input array or matrix of values

dim: 0 | 1

The dimension to sum across. Use 0 for rows and 1 for columns (defaults to 0)

Return Type

number | array

The sum of the elements