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

# Basic Creation

> Create arrays filled with zeros, ones, constants, or from existing data.

### zeros

Create an array filled with zeros.

```typescript theme={null}
function zeros(shape: number[], dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `shape` | `number[]` | - | Shape of the new array, e.g. `[2, 3]`. |
| `dtype` | `DType` | `'float64'` | Desired data type. |

**Returns:** `NDArray` -- Array of zeros with the given shape.

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

const a = np.zeros([2, 3]);
// array([[0, 0, 0],
//        [0, 0, 0]])

const b = np.zeros([3], 'int32');
// array([0, 0, 0])
```

```typescript Core theme={null}
import { zeros } from 'numpy-ts/core';

const a = zeros([2, 3]);
const b = zeros([3], 'int32');
```

***

### ones

Create an array filled with ones.

```typescript theme={null}
function ones(shape: number[], dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `shape` | `number[]` | - | Shape of the new array. |
| `dtype` | `DType` | `'float64'` | Desired data type. |

**Returns:** `NDArray` -- Array of ones with the given shape.

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

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

```typescript Core theme={null}
import { ones } from 'numpy-ts/core';

const a = ones([2, 3]);
```

***

### empty

Create an uninitialized array. In JavaScript, this is equivalent to `zeros` since typed arrays are zero-initialized.

```typescript theme={null}
function empty(shape: number[], dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `shape` | `number[]` | - | Shape of the new array. |
| `dtype` | `DType` | `'float64'` | Desired data type. |

**Returns:** `NDArray` -- Uninitialized array (zero-filled in practice).

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

const a = np.empty([3, 2]);
// array([[0, 0],
//        [0, 0],
//        [0, 0]])
```

```typescript Core theme={null}
import { empty } from 'numpy-ts/core';

const a = empty([3, 2]);
```

***

### full

Create an array filled with a constant value.

```typescript theme={null}
function full(shape: number[], fill_value: number | bigint | boolean, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `shape` | `number[]` | - | Shape of the new array. |
| `fill_value` | `number \| bigint \| boolean` | - | Value to fill the array with. |
| `dtype` | `DType` | inferred | Data type. Inferred from `fill_value` if omitted: `bool` for booleans, `int64` for bigints, `int32` for integers fitting in `[-2³¹, 2³¹-1]`, `int64` for larger integers, `float64` for everything else. |

**Returns:** `NDArray` -- Array filled with `fill_value`.

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

const a = np.full([2, 2], 7);
// array([[7, 7],
//        [7, 7]])

const b = np.full([3], 3.14, 'float32');
// array([3.14, 3.14, 3.14])
```

```typescript Core theme={null}
import { full } from 'numpy-ts/core';

const a = full([2, 2], 7);
const b = full([3], 3.14, 'float32');
```

***

### array

Create an array from nested JavaScript arrays, typed arrays, or existing arrays.

```typescript theme={null}
function array(data: NestedArray | TypedArray, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `data` | `NestedArray \| TypedArray` | - | Input data. Can be a nested JS array, a `TypedArray`, or an existing `NDArray`. |
| `dtype` | `DType` | inferred | Data type. Inferred from `data` if omitted. |

**Returns:** `NDArray` -- An ndarray constructed from the input data.

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

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

const b = np.array([[1, 2], [3, 4]], 'int32');
// array([[1, 2],
//        [3, 4]])

const c = np.array(new Float32Array([1.5, 2.5, 3.5]));
// array([1.5, 2.5, 3.5])
```

```typescript Core theme={null}
import { array } from 'numpy-ts/core';

const a = array([1, 2, 3]);
const b = array([[1, 2], [3, 4]], 'int32');
```

***

### asarray

Convert the input to an array. If the input is already an `NDArray` with the correct dtype, it is returned without copying.

```typescript theme={null}
function asarray(a: ArrayLike, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `ArrayLike` | - | Input data, in any form that can be converted to an array. |
| `dtype` | `DType` | inferred | Data type. If specified and differs from the input, a cast is performed. |

**Returns:** `NDArray` -- Array interpretation of `a`. No copy is made if `a` is already an ndarray of the matching dtype.

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

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

const existing = np.array([4, 5, 6]);
const b = np.asarray(existing); // no copy
```

```typescript Core theme={null}
import { asarray } from 'numpy-ts/core';

const a = asarray([1, 2, 3]);
```

***

### asanyarray

Convert input to an array, passing through subclasses. Behaves identically to `asarray` in numpy-ts.

```typescript theme={null}
function asanyarray(a: ArrayLike, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `ArrayLike` | - | Input data. |
| `dtype` | `DType` | inferred | Desired data type. |

**Returns:** `NDArray` -- Array interpretation of `a`.

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

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

```typescript Core theme={null}
import { asanyarray } from 'numpy-ts/core';

const a = asanyarray([1, 2, 3]);
```

***

### asarray\_chkfinite

Convert input to an array, raising an error if the input contains `Inf` or `NaN`.

```typescript theme={null}
function asarray_chkfinite(a: ArrayLike, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `ArrayLike` | - | Input data. |
| `dtype` | `DType` | inferred | Desired data type. |

**Returns:** `NDArray` -- Array interpretation of `a`.

**Throws:** `Error` if the array contains `Inf` or `NaN`.

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

const a = np.asarray_chkfinite([1, 2, 3]); // OK
// np.asarray_chkfinite([1, NaN, 3]);       // Throws!
```

```typescript Core theme={null}
import { asarray_chkfinite } from 'numpy-ts/core';

const a = asarray_chkfinite([1, 2, 3]);
```

***

### ascontiguousarray

Return a contiguous array (C order) in memory. In numpy-ts, this always returns a copy.

```typescript theme={null}
function ascontiguousarray(a: ArrayLike, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `ArrayLike` | - | Input data. |
| `dtype` | `DType` | inferred | Desired data type. |

**Returns:** `NDArray` -- Contiguous array with C-order memory layout.

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

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

```typescript Core theme={null}
import { ascontiguousarray } from 'numpy-ts/core';

const a = ascontiguousarray([[1, 2], [3, 4]]);
```

***

### asfortranarray

Return an array laid out in Fortran order in memory. In numpy-ts, this returns a C-order copy (Fortran order is not natively supported).

```typescript theme={null}
function asfortranarray(a: ArrayLike, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `ArrayLike` | - | Input data. |
| `dtype` | `DType` | inferred | Desired data type. |

**Returns:** `NDArray` -- Array copy (C-order in practice).

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

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

```typescript Core theme={null}
import { asfortranarray } from 'numpy-ts/core';

const a = asfortranarray([[1, 2], [3, 4]]);
```

***

### require

Return an array that satisfies requirements. In numpy-ts, requirements like `'C'`, `'F'`, `'A'`, `'W'`, `'O'`, `'E'` are mostly no-ops.

```typescript theme={null}
function require(a: NDArray, dtype?: DType, _requirements?: string | string[]): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `NDArray` | - | Input array. |
| `dtype` | `DType` | `undefined` | Desired data type. A cast is performed if needed. |
| `_requirements` | `string \| string[]` | `undefined` | Memory layout requirements (e.g., `['C']`). Mostly no-ops in numpy-ts. |

**Returns:** `NDArray` -- Array satisfying the given requirements.

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

const a = np.array([1, 2, 3]);
const b = np.require(a, 'float32');
console.log(b.dtype); // 'float32'
```

```typescript Core theme={null}
import { require, array } from 'numpy-ts/core';

const a = array([1, 2, 3]);
const b = require(a, 'float32');
```

***

### copy

Return a deep copy of an array.

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

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `NDArray` | - | Input array to copy. |

**Returns:** `NDArray` -- A copy of the input array. Modifications to the copy do not affect the original.

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

const a = np.array([1, 2, 3]);
const b = np.copy(a);
// b is independent of a
```

```typescript Core theme={null}
import { copy, array } from 'numpy-ts/core';

const a = array([1, 2, 3]);
const b = copy(a);
```

***

### zeros\_like

Return an array of zeros with the same shape and dtype as a given array.

```typescript theme={null}
function zeros_like(a: NDArray, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `NDArray` | - | Reference array. |
| `dtype` | `DType` | `a.dtype` | Override data type. |

**Returns:** `NDArray` -- Array of zeros matching the shape (and optionally dtype) of `a`.

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

const a = np.array([[1, 2, 3], [4, 5, 6]]);
const b = np.zeros_like(a);
// array([[0, 0, 0],
//        [0, 0, 0]])
```

```typescript Core theme={null}
import { zeros_like, array } from 'numpy-ts/core';

const a = array([[1, 2], [3, 4]]);
const b = zeros_like(a);
```

***

### ones\_like

Return an array of ones with the same shape and dtype as a given array.

```typescript theme={null}
function ones_like(a: NDArray, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `NDArray` | - | Reference array. |
| `dtype` | `DType` | `a.dtype` | Override data type. |

**Returns:** `NDArray` -- Array of ones matching the shape of `a`.

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

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

```typescript Core theme={null}
import { ones_like, array } from 'numpy-ts/core';

const a = array([[1, 2], [3, 4]]);
const b = ones_like(a);
```

***

### empty\_like

Return an uninitialized array with the same shape and dtype as a given array.

```typescript theme={null}
function empty_like(a: NDArray, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `NDArray` | - | Reference array. |
| `dtype` | `DType` | `a.dtype` | Override data type. |

**Returns:** `NDArray` -- Uninitialized array (zero-filled in practice) matching the shape of `a`.

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

const a = np.array([1, 2, 3]);
const b = np.empty_like(a);
// array([0, 0, 0])
```

```typescript Core theme={null}
import { empty_like, array } from 'numpy-ts/core';

const a = array([1, 2, 3]);
const b = empty_like(a);
```

***

### full\_like

Return a filled array with the same shape and dtype as a given array.

```typescript theme={null}
function full_like(a: NDArray, fill_value: number | bigint | boolean, dtype?: DType): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `a` | `NDArray` | - | Reference array. |
| `fill_value` | `number \| bigint \| boolean` | - | Fill value. |
| `dtype` | `DType` | `a.dtype` | Override data type. |

**Returns:** `NDArray` -- Array filled with `fill_value`, matching the shape of `a`.

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

const a = np.array([[1, 2], [3, 4]]);
const b = np.full_like(a, 99);
// array([[99, 99],
//        [99, 99]])
```

```typescript Core theme={null}
import { full_like, array } from 'numpy-ts/core';

const a = array([[1, 2], [3, 4]]);
const b = full_like(a, 99);
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.