Skip to main content

NDArray Methods

The NDArray class (from the default numpy-ts entry point) provides chainable instance methods that delegate to the corresponding standalone functions. Each method below calls its standalone equivalent under the hood.
import * as np from 'numpy-ts';

// Method chaining
const result = np.arange(12)
  .reshape(3, 4)
  .multiply(2)
  .sum(0);

// Equivalent standalone calls
const a = np.arange(12);
const b = np.reshape(a, [3, 4]);
const c = np.multiply(b, 2);
const d = np.sum(c, 0);
These methods are only available on NDArray (the full entry point). NDArrayCore from numpy-ts/core does not include them. See the NDArray Class page for core methods available on both classes.

Arithmetic methods

add

add(other: NDArray | number): NDArray
Element-wise addition. See add.

subtract

subtract(other: NDArray | number): NDArray
Element-wise subtraction. See subtract.

multiply

multiply(other: NDArray | number): NDArray
Element-wise multiplication. See multiply.

divide

divide(other: NDArray | number): NDArray
Element-wise division. See divide.

floor_divide

floor_divide(other: NDArray | number): NDArray
Element-wise floor division. See floor_divide.

mod

mod(other: NDArray | number): NDArray
Element-wise modulo. See mod.

remainder

remainder(divisor: NDArray | number): NDArray
Element-wise remainder (same as mod). See remainder.

fmod

fmod(divisor: NDArray | number): NDArray
Element-wise C-style remainder. See fmod.

power

power(exponent: NDArray | number): NDArray
Raise elements to the given power. See power.

negative

negative(): NDArray
Element-wise numerical negation (-x). See negative.

positive

positive(): NDArray
Element-wise numerical positive (+x). Returns a copy. See positive.

absolute

absolute(): NDArray
Element-wise absolute value. See absolute.

reciprocal

reciprocal(): NDArray
Element-wise reciprocal (1/x). See reciprocal.

square

square(): NDArray
Element-wise square (x**2). See square.

sqrt

sqrt(): NDArray
Element-wise square root. See sqrt.

cbrt

cbrt(): NDArray
Element-wise cube root. See cbrt.

fabs

fabs(): NDArray
Element-wise absolute value (always returns float). See fabs.

sign

sign(): NDArray
Element-wise sign indication (-1, 0, or 1). See sign.

heaviside

heaviside(x2: NDArray | number): NDArray
Heaviside step function. See heaviside.

divmod

divmod(divisor: NDArray | number): [NDArray, NDArray]
Returns both floor quotient and remainder. See divmod.

Exponential and logarithmic methods

exp

exp(): NDArray
Natural exponential (e^x). See exp.

exp2

exp2(): NDArray
Base-2 exponential (2^x). See exp2.

expm1

expm1(): NDArray
e^x - 1, accurate for small x. See expm1.

log

log(): NDArray
Natural logarithm. See log.

log2

log2(): NDArray
Base-2 logarithm. See log2.

log10

log10(): NDArray
Base-10 logarithm. See log10.

log1p

log1p(): NDArray
ln(1 + x), accurate for small x. See log1p.

logaddexp

logaddexp(x2: NDArray | number): NDArray
log(exp(x1) + exp(x2)), numerically stable. See logaddexp.

logaddexp2

logaddexp2(x2: NDArray | number): NDArray
log2(2^x1 + 2^x2), numerically stable. See logaddexp2.

Trigonometric methods

sin

sin(): NDArray
Sine (radians). See sin.

cos

cos(): NDArray
Cosine (radians). See cos.

tan

tan(): NDArray
Tangent (radians). See tan.

arcsin

arcsin(): NDArray
Inverse sine. See arcsin.

arccos

arccos(): NDArray
Inverse cosine. See arccos.

arctan

arctan(): NDArray
Inverse tangent. See arctan.

arctan2

arctan2(other: NDArray | number): NDArray
Element-wise arc tangent of this/other, choosing the correct quadrant. See arctan2.

hypot

hypot(other: NDArray | number): NDArray
Hypotenuse given two right-triangle legs. See hypot.

degrees

degrees(): NDArray
Convert radians to degrees. See degrees.

radians

radians(): NDArray
Convert degrees to radians. See radians.

Hyperbolic methods

sinh

sinh(): NDArray
Hyperbolic sine. See sinh.

cosh

cosh(): NDArray
Hyperbolic cosine. See cosh.

tanh

tanh(): NDArray
Hyperbolic tangent. See tanh.

arcsinh

arcsinh(): NDArray
Inverse hyperbolic sine. See arcsinh.

arccosh

arccosh(): NDArray
Inverse hyperbolic cosine. See arccosh.

arctanh

arctanh(): NDArray
Inverse hyperbolic tangent. See arctanh.

Rounding methods

ceil

ceil(): NDArray
Ceiling of each element. See ceil.

floor

floor(): NDArray
Floor of each element. See floor.

fix

fix(): NDArray
Round towards zero. See fix.

rint

rint(): NDArray
Round to nearest integer. See rint.

trunc

trunc(): NDArray
Truncate to integer part. See trunc.

around

around(decimals?: number): NDArray
Round to the given number of decimals (default: 0). See around.

round

round(decimals?: number): NDArray
Alias for around. See around.

clip

clip(a_min: number | NDArray | null, a_max: number | NDArray | null): NDArray
Clip values to [a_min, a_max]. Pass null for no bound on either side. See clip.

Comparison methods

equal

equal(other: NDArray | number): NDArray
Element-wise equality. Returns boolean array. See equal.

not_equal

not_equal(other: NDArray | number): NDArray
Element-wise inequality. Returns boolean array. See not_equal.

greater

greater(other: NDArray | number): NDArray
Element-wise greater than. Returns boolean array. See greater.

greater_equal

greater_equal(other: NDArray | number): NDArray
Element-wise greater than or equal. Returns boolean array. See greater_equal.

less

less(other: NDArray | number): NDArray
Element-wise less than. Returns boolean array. See less.

less_equal

less_equal(other: NDArray | number): NDArray
Element-wise less than or equal. Returns boolean array. See less_equal.

allclose

allclose(other: NDArray | number, rtol?: number, atol?: number): boolean
Returns true if all elements are equal within tolerance. Default rtol=1e-5, atol=1e-8. See allclose.

isclose

isclose(other: NDArray | number, rtol?: number, atol?: number): NDArray
Element-wise comparison within tolerance. Returns boolean array. See isclose.

isnan

isnan(): NDArray
Test element-wise for NaN. Returns boolean array. See isnan.

isinf

isinf(): NDArray
Test element-wise for infinity. Returns boolean array. See isinf.

isfinite

isfinite(): NDArray
Test element-wise for finiteness. Returns boolean array. See isfinite.

isnat

isnat(): NDArray
Test element-wise for NaT (Not a Time). Always returns false (no datetime support). See isnat.

Logic methods

logical_and

logical_and(other: NDArray | number): NDArray
Element-wise logical AND. Returns boolean array. See logical_and.

logical_or

logical_or(other: NDArray | number): NDArray
Element-wise logical OR. Returns boolean array. See logical_or.

logical_not

logical_not(): NDArray
Element-wise logical NOT. Returns boolean array. See logical_not.

logical_xor

logical_xor(other: NDArray | number): NDArray
Element-wise logical XOR. Returns boolean array. See logical_xor.

invert

invert(): NDArray
Bitwise NOT (alias for bitwise_not). See invert.

bitwise_and

bitwise_and(other: NDArray | number): NDArray
Element-wise bitwise AND. Requires integer dtype. See bitwise_and.

bitwise_or

bitwise_or(other: NDArray | number): NDArray
Element-wise bitwise OR. Requires integer dtype. See bitwise_or.

bitwise_xor

bitwise_xor(other: NDArray | number): NDArray
Element-wise bitwise XOR. Requires integer dtype. See bitwise_xor.

bitwise_not

bitwise_not(): NDArray
Element-wise bitwise NOT. Requires integer dtype. See bitwise_not.

left_shift

left_shift(shift: NDArray | number): NDArray
Element-wise left bit shift. See left_shift.

right_shift

right_shift(shift: NDArray | number): NDArray
Element-wise right bit shift. See right_shift.

copysign

copysign(x2: NDArray | number): NDArray
Change the sign of this to that of x2, element-wise. See copysign.

signbit

signbit(): NDArray
Returns true where the sign bit is set (negative). See signbit.

nextafter

nextafter(x2: NDArray | number): NDArray
Return the next floating-point value after this towards x2. See nextafter.

spacing

spacing(): NDArray
Return the distance between each element and the nearest adjacent number. See spacing.

Reduction methods

sum

sum(axis?: number, keepdims?: boolean): NDArray | number | bigint | Complex
Sum of elements along an axis. Returns a scalar when axis is undefined. See sum.

prod

prod(axis?: number, keepdims?: boolean): NDArray | number | bigint | Complex
Product of elements along an axis. See prod.

mean

mean(axis?: number, keepdims?: boolean): NDArray | number | Complex
Arithmetic mean along an axis. See mean.

std

std(axis?: number, ddof?: number, keepdims?: boolean): NDArray | number
Standard deviation along an axis. ddof defaults to 0. See std.

var

var(axis?: number, ddof?: number, keepdims?: boolean): NDArray | number
Variance along an axis. ddof defaults to 0. See var.

min

min(axis?: number, keepdims?: boolean): NDArray | number | Complex
Minimum value along an axis. See min.

max

max(axis?: number, keepdims?: boolean): NDArray | number | Complex
Maximum value along an axis. See max.

argmin

argmin(axis?: number): NDArray | number
Index of the minimum value along an axis. See argmin.

argmax

argmax(axis?: number): NDArray | number
Index of the maximum value along an axis. See argmax.

all

all(axis?: number, keepdims?: boolean): NDArray | boolean
Test whether all elements evaluate to true. See all.

any

any(axis?: number, keepdims?: boolean): NDArray | boolean
Test whether any element evaluates to true. See any.

ptp

ptp(axis?: number, keepdims?: boolean): NDArray | number | Complex
Peak-to-peak (maximum minus minimum) along an axis. See ptp.

median

median(axis?: number, keepdims?: boolean): NDArray | number
Median along an axis. See median.

percentile

percentile(q: number, axis?: number, keepdims?: boolean): NDArray | number
Compute the q-th percentile (0—100). See percentile.

quantile

quantile(q: number, axis?: number, keepdims?: boolean): NDArray | number
Compute the q-th quantile (0—1). See quantile.

average

average(weights?: NDArray, axis?: number): NDArray | number | Complex
Weighted average along an axis. See average.

cumsum

cumsum(axis?: number): NDArray
Cumulative sum along an axis. See cumsum.

cumprod

cumprod(axis?: number): NDArray
Cumulative product along an axis. See cumprod.

diff

diff(n?: number, axis?: number): NDArray
Calculate the n-th discrete difference along an axis. See diff.

nansum

nansum(axis?: number, keepdims?: boolean): NDArray | number | Complex
Sum treating NaNs as zero. See nansum.

nanprod

nanprod(axis?: number, keepdims?: boolean): NDArray | number | Complex
Product treating NaNs as one. See nanprod.

nanmean

nanmean(axis?: number, keepdims?: boolean): NDArray | number | Complex
Mean ignoring NaNs. See nanmean.

nanstd

nanstd(axis?: number, ddof?: number, keepdims?: boolean): NDArray | number
Standard deviation ignoring NaNs. See nanstd.

nanvar

nanvar(axis?: number, ddof?: number, keepdims?: boolean): NDArray | number
Variance ignoring NaNs. See nanvar.

nanmin

nanmin(axis?: number, keepdims?: boolean): NDArray | number | Complex
Minimum ignoring NaNs. See nanmin.

nanmax

nanmax(axis?: number, keepdims?: boolean): NDArray | number | Complex
Maximum ignoring NaNs. See nanmax.

nanargmin

nanargmin(axis?: number): NDArray | number
Index of minimum ignoring NaNs. See nanargmin.

nanargmax

nanargmax(axis?: number): NDArray | number
Index of maximum ignoring NaNs. See nanargmax.

nanmedian

nanmedian(axis?: number, keepdims?: boolean): NDArray | number
Median ignoring NaNs. See nanmedian.

nancumsum

nancumsum(axis?: number): NDArray
Cumulative sum treating NaNs as zero. See nancumsum.

nancumprod

nancumprod(axis?: number): NDArray
Cumulative product treating NaNs as one. See nancumprod.

nanpercentile

nanpercentile(q: number, axis?: number, keepdims?: boolean): NDArray | number
Percentile ignoring NaNs. See nanpercentile.

nanquantile

nanquantile(q: number, axis?: number, keepdims?: boolean): NDArray | number
Quantile ignoring NaNs. See nanquantile.

Shape methods

reshape

reshape(...shape: number[]): NDArray
Return a new array with the given shape. Use -1 for one inferred dimension. See reshape.

flatten

flatten(): NDArray
Return a flattened copy (always 1D). See flatten.

ravel

ravel(): NDArray
Return a flattened array. Returns a view when possible, otherwise a copy. See ravel.

squeeze

squeeze(axis?: number): NDArray
Remove axes of length one. See squeeze.

expand_dims

expand_dims(axis: number): NDArray
Insert a new axis of length one at the given position. See expand_dims.

transpose

transpose(axes?: number[]): NDArray
Permute the dimensions. With no arguments, reverses all axes. See transpose.

swapaxes

swapaxes(axis1: number, axis2: number): NDArray
Interchange two axes. See swapaxes.

moveaxis

moveaxis(source: number | number[], destination: number | number[]): NDArray
Move axes to new positions. See moveaxis.

T

get T(): NDArray
Transpose property (no parentheses). Returns a view with reversed axes. See NDArray Class.

Linear algebra methods

dot

dot(other: NDArray): NDArray | number | bigint | Complex
Dot product following NumPy semantics (inner product for 1D, matrix multiply for 2D). See dot.

matmul

matmul(other: NDArray): NDArray
Matrix multiplication (@ operator equivalent). See matmul.

inner

inner(other: NDArray): NDArray | number | bigint | Complex
Inner product (contracts over last axes of both arrays). See inner.

outer

outer(other: NDArray): NDArray
Outer product (flattens inputs, computes a[i] * b[j]). See outer.

tensordot

tensordot(other: NDArray, axes?: number | [number[], number[]]): NDArray | number | bigint | Complex
Tensor dot product along specified axes. Default contracts 2 axes. See tensordot.

trace

trace(): number | bigint | Complex
Sum of diagonal elements. See trace.

diagonal

diagonal(offset?: number, axis1?: number, axis2?: number): NDArray
Return specified diagonals. See diagonal.

Sorting and searching methods

sort

sort(axis?: number): NDArray
Return a sorted copy. Default sorts along the last axis. See sort.

argsort

argsort(axis?: number): NDArray
Return indices that would sort the array. See argsort.

partition

partition(kth: number, axis?: number): NDArray
Partially sort so that the element at kth position is in its final sorted position. See partition.

argpartition

argpartition(kth: number, axis?: number): NDArray
Return indices that would partition the array. See argpartition.

searchsorted

searchsorted(v: NDArray, side?: 'left' | 'right'): NDArray
Find indices where elements should be inserted to maintain order. See searchsorted.

nonzero

nonzero(): NDArray[]
Return indices of non-zero elements, one array per dimension. See nonzero.

argwhere

argwhere(): NDArray
Find indices of non-zero elements, returned as a 2D array of shape (N, ndim). See argwhere.

Indexing methods

take

take(indices: number[], axis?: number): NDArray
Take elements along an axis. See take.

put

put(indices: number[], values: NDArray | number | bigint): void
Set values at flat indices (in-place). See put.

choose

choose(choices: NDArray[]): NDArray
Construct an array by choosing from a list of arrays using this as the index. See choose.

compress

compress(condition: NDArray | boolean[], axis?: number): NDArray
Return selected slices along an axis where condition is true. See compress.

repeat

repeat(repeats: number | number[], axis?: number): NDArray
Repeat elements of the array. See repeat.

resize

resize(newShape: number[]): NDArray
Return a new array with the given shape. Repeats data if the new shape is larger. See resize.

bindex

bindex(mask: NDArray, axis?: number): NDArray
Boolean array indexing. Select elements where mask is true. See compress.

iindex

iindex(indices: NDArray | number[] | number[][], axis?: number): NDArray
Integer array indexing (fancy indexing). Select elements by an array of indices. See take.

Complex number methods

conj

conj(): NDArray
Return the complex conjugate, element-wise. For real arrays, returns a copy. See conj.

conjugate

conjugate(): NDArray
Alias for conj. See conj.

Other methods

copy

copy(): NDArray
Return a deep copy. See NDArray Class.

astype

astype(dtype: DType, copy?: boolean): NDArray
Cast to a new dtype. See NDArray Class.

fill

fill(value: number | bigint): void
Fill array in-place with a scalar. See NDArray Class.

toArray

toArray(): NestedArray
Convert to nested JavaScript array. See NDArray Class.

tolist

tolist(): NestedArray
Same as toArray. See NDArray Class.

tobytes

tobytes(): ArrayBuffer
Serialize to raw bytes. See NDArray Class.

get

get(indices: number[]): number | bigint | Complex
Get element by multi-dimensional indices. See NDArray Class.

set

set(indices: number[], value: number | bigint | Complex): void
Set element by multi-dimensional indices. See NDArray Class.

slice

slice(...sliceStrs: string[]): NDArray
NumPy-style string slicing. See NDArray Class.

row

row(i: number): NDArray
Get row i. See NDArray Class.

col

col(j: number): NDArray
Get column j. See NDArray Class.

rows

rows(start: number, stop: number): NDArray
Get a range of rows. See NDArray Class.

cols

cols(start: number, stop: number): NDArray
Get a range of columns. See NDArray Class.

item

item(...args: number[]): number | bigint | Complex
Extract a scalar value. See NDArray Class.