> ## Documentation Index
> Fetch the complete documentation index at: https://numpyts.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Tiling & Repeating

> Repeat and pad array contents.

### tile

Construct an array by tiling `a` according to the repetition counts in `reps`. The result has the same dtype as `a`.

```typescript theme={null}
function tile(a: ArrayLike, reps: number | number[]): NDArray
```

| Name   | Type                 | Default | Description                                                                                                                                                                              |
| ------ | -------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `a`    | `ArrayLike`          | --      | Input array.                                                                                                                                                                             |
| `reps` | `number \| 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.

```typescript theme={null}
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.

```typescript theme={null}
function repeat(
  a: ArrayLike,
  repeats: number | number[],
  axis?: number
): NDArray
```

| Name      | Type                 | Default     | Description                                                                                                                                                                                                          |
| --------- | -------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `a`       | `ArrayLike`          | --          | Input array.                                                                                                                                                                                                         |
| `repeats` | `number \| 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. |
| `axis`    | `number`             | `undefined` | Axis along which to repeat. If `undefined`, the array is flattened first and the result is 1-D.                                                                                                                      |

**Returns:** `NDArray` -- Array with repeated elements.

```typescript theme={null}
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.

```typescript theme={null}
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])[];
```

| Name              | Type          | Default      | Description                                                                               |
| ----------------- | ------------- | ------------ | ----------------------------------------------------------------------------------------- |
| `arr`             | `ArrayLike`   | --           | Input array.                                                                              |
| `pad_width`       | `PadWidthArg` | --           | Number of values padded to each edge. Accepts the full NumPy broadcast forms (see below). |
| `mode`            | `string`      | `'constant'` | Padding mode. Only `'constant'` is currently supported; other modes throw.                |
| `constant_values` | `PadValueArg` | `0`          | Fill 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.

```typescript theme={null}
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]]
```
