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

# Shape Operations

> Reshape, flatten, and modify array dimensions.

### reshape

Change the shape of an array without changing its data. Returns a view if the array is C-contiguous, otherwise returns a copy. Use `-1` for one dimension to have its size inferred automatically.

```typescript theme={null}
function reshape(a: ArrayLike, newShape: number[]): NDArray
```

| Name       | Type        | Default | Description                                                                             |
| ---------- | ----------- | ------- | --------------------------------------------------------------------------------------- |
| `a`        | `ArrayLike` | --      | Input array.                                                                            |
| `newShape` | `number[]`  | --      | New shape. One dimension may be `-1`, in which case it is inferred from the total size. |

**Returns:** `NDArray` -- Array with the specified shape. View when possible, copy otherwise.

```typescript theme={null}
import * as np from 'numpy-ts';

const a = np.arange(12);

// Reshape to 3x4 matrix
const b = np.reshape(a, [3, 4]);
console.log(b.shape);  // [3, 4]

// Use -1 to auto-infer one dimension
const c = np.reshape(a, [2, -1]);
console.log(c.shape);  // [2, 6]

// Method chaining
const d = np.arange(24).reshape([2, 3, 4]);
console.log(d.shape);  // [2, 3, 4]
```

***

### flatten

Return a 1-D copy of the array. Unlike `ravel`, `flatten` always allocates new memory.

```typescript theme={null}
function flatten(a: ArrayLike): NDArray
```

| Name | Type        | Default | Description  |
| ---- | ----------- | ------- | ------------ |
| `a`  | `ArrayLike` | --      | Input array. |

**Returns:** `NDArray` -- A 1-D array containing all elements in row-major (C) order. Always a copy.

```typescript theme={null}
import * as np from 'numpy-ts';

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

const flat = np.flatten(a);
console.log(flat.shape);    // [6]
console.log(flat.toArray()); // [1, 2, 3, 4, 5, 6]

// Modifying the flattened array does not affect the original
flat.set([0], 99);
console.log(a.item(0, 0));  // 1 (unchanged)
```

***

### ravel

Return a contiguous flattened 1-D array. Returns a view if the array is already C-contiguous, otherwise returns a copy (same data as `flatten` in that case).

```typescript theme={null}
function ravel(a: ArrayLike): NDArray
```

| Name | Type        | Default | Description  |
| ---- | ----------- | ------- | ------------ |
| `a`  | `ArrayLike` | --      | Input array. |

**Returns:** `NDArray` -- A 1-D array. View when the input is C-contiguous, copy otherwise.

```typescript theme={null}
import * as np from 'numpy-ts';

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

// ravel returns a view when possible
const r = np.ravel(a);
console.log(r.shape);     // [4]
console.log(r.toArray());  // [1, 2, 3, 4]

// For C-contiguous arrays, ravel shares memory with the original
console.log(r.base !== null);  // true (it is a view)
```

***

### squeeze

Remove axes of length 1 from the array shape. Returns a view (no data is copied).

```typescript theme={null}
function squeeze(a: ArrayLike, axis?: number): NDArray
```

| Name   | Type                 | Default     | Description                                                                                                                           |
| ------ | -------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `a`    | `ArrayLike`          | --          | Input array.                                                                                                                          |
| `axis` | `number \| number[]` | `undefined` | Axis or axes to squeeze. If `undefined`, all length-1 axes are removed. Raises an error if the specified axis does not have length 1. |

**Returns:** `NDArray` -- View of `a` with the specified length-1 dimensions removed.

```typescript theme={null}
import * as np from 'numpy-ts';

const a = np.array([[[1], [2], [3]]]);
console.log(a.shape);  // [1, 3, 1]

// Remove all size-1 dimensions
const b = np.squeeze(a);
console.log(b.shape);  // [3]

// Remove only axis 0
const c = np.squeeze(a, 0);
console.log(c.shape);  // [3, 1]
```

***

### expand\_dims

Insert a new axis (dimension of length 1) at the given position. Returns a view.

```typescript theme={null}
function expand_dims(a: ArrayLike, axis: number): NDArray
```

| Name   | Type                 | Default | Description                                                                     |
| ------ | -------------------- | ------- | ------------------------------------------------------------------------------- |
| `a`    | `ArrayLike`          | --      | Input array.                                                                    |
| `axis` | `number \| number[]` | --      | Position(s) where the new axis is inserted. Negative values count from the end. |

**Returns:** `NDArray` -- View of `a` with an additional dimension inserted.

```typescript theme={null}
import * as np from 'numpy-ts';

const a = np.array([1, 2, 3]);
console.log(a.shape);  // [3]

// Add a leading dimension (row vector -> 2D)
const b = np.expand_dims(a, 0);
console.log(b.shape);  // [1, 3]

// Add a trailing dimension (column vector -> 2D)
const c = np.expand_dims(a, 1);
console.log(c.shape);  // [3, 1]

// Negative axis
const d = np.expand_dims(a, -1);
console.log(d.shape);  // [3, 1]
```

***

### resize

Return a new array with the given shape. If the new size is larger, the data is repeated (cycled). If smaller, the data is truncated. Always returns a new array (not a view).

```typescript theme={null}
function resize(a: ArrayLike, new_shape: number[]): NDArray
```

| Name        | Type        | Default | Description                |
| ----------- | ----------- | ------- | -------------------------- |
| `a`         | `ArrayLike` | --      | Input array.               |
| `new_shape` | `number[]`  | --      | Shape of the output array. |

**Returns:** `NDArray` -- New array with the specified shape, filled by repeating or truncating elements from `a`.

```typescript theme={null}
import * as np from 'numpy-ts';

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

// Grow: elements repeat to fill the new shape
const b = np.resize(a, [2, 4]);
console.log(b.toArray());
// [[1, 2, 3, 1],
//  [2, 3, 1, 2]]

// Shrink: elements are truncated
const c = np.resize(a, [2]);
console.log(c.toArray());  // [1, 2]
```
