Skip to main content

Documentation Index

Fetch the complete documentation index at: https://numpyts.dev/llms.txt

Use this file to discover all available pages before exploring further.

tile

Construct an array by tiling a according to the repetition counts in reps. The result has the same dtype as a.
function tile(a: ArrayLike, reps: number | number[]): NDArray
NameTypeDefaultDescription
aArrayLikeInput array.
repsnumber | number[]Number of repetitions along each axis. A single integer repeats along all axes equally. An array specifies repetitions per axis (padded with 1s on the left when shorter than a.ndim).
Returns: NDArray — The tiled array.
import * as np from 'numpy-ts';

const a = np.array([1, 2, 3]);

// Repeat 3 times along the single axis
const b = np.tile(a, 3);
console.log(b.toArray());  // [1, 2, 3, 1, 2, 3, 1, 2, 3]

// Tile a 1-D array into a 2-D grid
const c = np.tile(a, [2, 3]);
console.log(c.shape);     // [2, 9]
console.log(c.toArray());
// [[1, 2, 3, 1, 2, 3, 1, 2, 3],
//  [1, 2, 3, 1, 2, 3, 1, 2, 3]]

// Tile a 2-D array
const d = np.array([[1, 2], [3, 4]]);
const e = np.tile(d, [2, 3]);
console.log(e.shape);  // [4, 6]

repeat

Repeat each element of an array a given number of times.
function repeat(
  a: ArrayLike,
  repeats: number | number[],
  axis?: number
): NDArray
NameTypeDefaultDescription
aArrayLikeInput array.
repeatsnumber | number[]Number of repetitions for each element. If a single integer, all elements are repeated equally. If an array, its length must match the size of a (when axis is undefined) or the length of the specified axis.
axisnumberundefinedAxis along which to repeat. If undefined, the array is flattened first and the result is 1-D.
Returns: NDArray — Array with repeated elements.
import * as np from 'numpy-ts';

const a = np.array([1, 2, 3]);

// Repeat each element 3 times
const b = np.repeat(a, 3);
console.log(b.toArray());  // [1, 1, 1, 2, 2, 2, 3, 3, 3]

// Different repeat counts per element
const c = np.repeat(a, [2, 1, 3]);
console.log(c.toArray());  // [1, 1, 2, 3, 3, 3]

// Repeat along a specific axis
const d = np.array([[1, 2], [3, 4]]);
const e = np.repeat(d, 2, 0);
console.log(e.toArray());
// [[1, 2], [1, 2], [3, 4], [3, 4]]

const f = np.repeat(d, 2, 1);
console.log(f.toArray());
// [[1, 1, 2, 2], [3, 3, 4, 4]]

pad

Pad an array with values along each dimension.
function pad(
  arr: ArrayLike,
  pad_width: PadWidthArg,
  mode?: 'constant' | 'edge' | 'linear_ramp' | 'maximum' | 'mean' | 'median' | 'minimum' | 'reflect' | 'symmetric' | 'wrap' | 'empty',
  constant_values?: PadValueArg
): NDArray

type PadWidthArg = number | [number, number] | (number | [number, number])[];
type PadValueArg = number | [number, number] | (number | [number, number])[];
NameTypeDefaultDescription
arrArrayLikeInput array.
pad_widthPadWidthArgNumber of values padded to each edge. Accepts the full NumPy broadcast forms (see below).
modestring'constant'Padding mode. Only 'constant' is currently supported; other modes throw.
constant_valuesPadValueArg0Fill value(s) when mode is 'constant'. Same broadcast forms as pad_width.
Returns: NDArray — Padded array. Both pad_width and constant_values accept these broadcast forms:
  • scalar n[n, n] for every axis
  • [n] — broadcast [n, n] to every axis
  • [before, after] — same pair on every axis
  • [n0, n1, …, n_{ndim-1}] — per-axis scalars (each becomes [n_i, n_i])
  • [[b, a]] — broadcast pair to every axis
  • [[b0, a0], …, [b_{ndim-1}, a_{ndim-1}]] — per-axis pairs
  • mixed: [n0, [b1, a1], …] — scalars expand to [n, n]
When constant_values varies by axis, corner cells follow NumPy’s “highest-axis-wins” rule: the value from the largest-indexed axis whose pad region covers the cell is used.
import * as np from 'numpy-ts';

const a = np.array([[1, 2], [3, 4]]);

// Pad 1 element on all sides with zeros
const b = np.pad(a, 1);
console.log(b.toArray());
// [[0, 0, 0, 0],
//  [0, 1, 2, 0],
//  [0, 3, 4, 0],
//  [0, 0, 0, 0]]

// Asymmetric padding per axis
const c = np.pad(a, [[1, 0], [0, 2]]);
console.log(c.shape);  // [3, 4]

// Custom fill value
const d = np.pad(a, 1, 'constant', -1);
console.log(d.toArray());
// [[-1, -1, -1, -1],
//  [-1,  1,  2, -1],
//  [-1,  3,  4, -1],
//  [-1, -1, -1, -1]]

// Per-axis fill values: axis 0 fills with 9, axis 1 with 7
// (corner cells go to the highest-indexed axis -> 7)
const e = np.pad(a, 1, 'constant', [9, 7]);
console.log(e.toArray());
// [[7, 9, 9, 7],
//  [7, 1, 2, 7],
//  [7, 3, 4, 7],
//  [7, 9, 9, 7]]