function diff
diff(x: array): array

Differences between adjacent elements.

Computes the differences between adjacent elements in an array or along a specified dimension of a matrix.

Examples

Differences in a 1D array

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

assertEquals(diff([5, 6, 3]), [1, -3]);

Differences in a matrix along columns (dim=1)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 1), [[2], [2], [-6]]);

Differences in a matrix along rows (dim=0)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 0), [[1, -1], [1, -9]]);

Parameters

The input array or matrix of values

Return Type

The differences between adjacent elements

Throws

If no input is provided

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

Differences between adjacent elements.

Computes the differences between adjacent elements in an array or along a specified dimension of a matrix.

Examples

Differences in a 1D array

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

assertEquals(diff([5, 6, 3]), [1, -3]);

Differences in a matrix along columns (dim=1)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 1), [[2], [2], [-6]]);

Differences in a matrix along rows (dim=0)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 0), [[1, -1], [1, -9]]);

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 differences between adjacent elements

Throws

If no input is provided

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

Differences between adjacent elements.

Computes the differences between adjacent elements in an array or along a specified dimension of a matrix.

Examples

Differences in a 1D array

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

assertEquals(diff([5, 6, 3]), [1, -3]);

Differences in a matrix along columns (dim=1)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 1), [[2], [2], [-6]]);

Differences in a matrix along rows (dim=0)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 0), [[1, -1], [1, -9]]);

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 differences between adjacent elements

Throws

If no input is provided

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

Differences between adjacent elements.

Computes the differences between adjacent elements in an array or along a specified dimension of a matrix.

Examples

Differences in a 1D array

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

assertEquals(diff([5, 6, 3]), [1, -3]);

Differences in a matrix along columns (dim=1)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 1), [[2], [2], [-6]]);

Differences in a matrix along rows (dim=0)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 0), [[1, -1], [1, -9]]);

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 differences between adjacent elements

Throws

If no input is provided

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

Differences between adjacent elements.

Computes the differences between adjacent elements in an array or along a specified dimension of a matrix.

Examples

Differences in a 1D array

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

assertEquals(diff([5, 6, 3]), [1, -3]);

Differences in a matrix along columns (dim=1)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 1), [[2], [2], [-6]]);

Differences in a matrix along rows (dim=0)

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

assertEquals(diff([[5, 6, 5], [7, 8, -1]], 0), [[1, -1], [1, -9]]);

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 differences between adjacent elements

Throws

If no input is provided