Skip to main content

zeros

Create an array filled with zeros.
function zeros(shape: number[], dtype?: DType): NDArray
NameTypeDefaultDescription
shapenumber[]-Shape of the new array, e.g. [2, 3].
dtypeDType'float64'Desired data type.
Returns: NDArray — Array of zeros with the given shape.
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])

ones

Create an array filled with ones.
function ones(shape: number[], dtype?: DType): NDArray
NameTypeDefaultDescription
shapenumber[]-Shape of the new array.
dtypeDType'float64'Desired data type.
Returns: NDArray — Array of ones with the given shape.
import * as np from 'numpy-ts';

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

empty

Create an uninitialized array. In JavaScript, this is equivalent to zeros since typed arrays are zero-initialized.
function empty(shape: number[], dtype?: DType): NDArray
NameTypeDefaultDescription
shapenumber[]-Shape of the new array.
dtypeDType'float64'Desired data type.
Returns: NDArray — Uninitialized array (zero-filled in practice).
import * as np from 'numpy-ts';

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

full

Create an array filled with a constant value.
function full(shape: number[], fill_value: number | bigint | boolean, dtype?: DType): NDArray
NameTypeDefaultDescription
shapenumber[]-Shape of the new array.
fill_valuenumber | bigint | boolean-Value to fill the array with.
dtypeDTypeinferredData type. Inferred from fill_value if omitted (int32 for integers, float64 for floats, bool for booleans, int64 for bigints).
Returns: NDArray — Array filled with fill_value.
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])

array

Create an array from nested JavaScript arrays, typed arrays, or existing arrays.
function array(data: NestedArray | TypedArray, dtype?: DType): NDArray
NameTypeDefaultDescription
dataNestedArray | TypedArray-Input data. Can be a nested JS array, a TypedArray, or an existing NDArray.
dtypeDTypeinferredData type. Inferred from data if omitted.
Returns: NDArray — An ndarray constructed from the input data.
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])

asarray

Convert the input to an array. If the input is already an NDArray with the correct dtype, it is returned without copying.
function asarray(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Input data, in any form that can be converted to an array.
dtypeDTypeinferredData 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.
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

asanyarray

Convert input to an array, passing through subclasses. Behaves identically to asarray in numpy-ts.
function asanyarray(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Input data.
dtypeDTypeinferredDesired data type.
Returns: NDArray — Array interpretation of a.
import * as np from 'numpy-ts';

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

asarray_chkfinite

Convert input to an array, raising an error if the input contains Inf or NaN.
function asarray_chkfinite(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Input data.
dtypeDTypeinferredDesired data type.
Returns: NDArray — Array interpretation of a. Throws: Error if the array contains Inf or NaN.
import * as np from 'numpy-ts';

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

ascontiguousarray

Return a contiguous array (C order) in memory. In numpy-ts, this always returns a copy.
function ascontiguousarray(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Input data.
dtypeDTypeinferredDesired data type.
Returns: NDArray — Contiguous array with C-order memory layout.
import * as np from 'numpy-ts';

const a = np.ascontiguousarray([[1, 2], [3, 4]]);
// array([[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).
function asfortranarray(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Input data.
dtypeDTypeinferredDesired data type.
Returns: NDArray — Array copy (C-order in practice).
import * as np from 'numpy-ts';

const a = np.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.
function require(a: ArrayLike, dtype?: DType, _requirements?: string | string[]): NDArray
NameTypeDefaultDescription
aArrayLike-Input array.
dtypeDTypeundefinedDesired data type. A cast is performed if needed.
_requirementsstring | string[]undefinedMemory layout requirements (e.g., ['C']). Mostly no-ops in numpy-ts.
Returns: NDArray — Array satisfying the given requirements.
import * as np from 'numpy-ts';

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

copy

Return a deep copy of an array.
function copy(a: ArrayLike): NDArray
NameTypeDefaultDescription
aArrayLike-Input array to copy.
Returns: NDArray — A copy of the input array. Modifications to the copy do not affect the original.
import * as np from 'numpy-ts';

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

zeros_like

Return an array of zeros with the same shape and dtype as a given array.
function zeros_like(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Reference array.
dtypeDTypea.dtypeOverride data type.
Returns: NDArray — Array of zeros matching the shape (and optionally dtype) of a.
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]])

ones_like

Return an array of ones with the same shape and dtype as a given array.
function ones_like(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Reference array.
dtypeDTypea.dtypeOverride data type.
Returns: NDArray — Array of ones matching the shape of a.
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]])

empty_like

Return an uninitialized array with the same shape and dtype as a given array.
function empty_like(a: ArrayLike, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Reference array.
dtypeDTypea.dtypeOverride data type.
Returns: NDArray — Uninitialized array (zero-filled in practice) matching the shape of a.
import * as np from 'numpy-ts';

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

full_like

Return a filled array with the same shape and dtype as a given array.
function full_like(a: ArrayLike, fill_value: number | bigint | boolean, dtype?: DType): NDArray
NameTypeDefaultDescription
aArrayLike-Reference array.
fill_valuenumber | bigint | boolean-Fill value.
dtypeDTypea.dtypeOverride data type.
Returns: NDArray — Array filled with fill_value, matching the shape of a.
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]])