Skip to main content

apply_along_axis

Apply a function to 1-D slices of an array along the given axis. The function is called once for each slice, and the results are assembled into an output array.
function apply_along_axis(
  func1d: (arr: NDArray) => NDArray | number,
  axis: number,
  arr: ArrayLike
): NDArray
ParameterTypeDefaultDescription
func1d(arr: NDArray) => number | NDArrayFunction that operates on a 1-D array and returns a scalar or 1-D array.
axisnumberAxis along which to apply func1d.
arrArrayLikeInput array.
Returns: NDArray — The result of applying func1d along the specified axis.
import { array, apply_along_axis, sort } from 'numpy-ts';

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

// Sort each row
const sorted = apply_along_axis(
  (row) => sort(row),
  1,
  a
);
// array([[1, 2, 3],
//        [4, 5, 6]])

// Sum each column (returns scalars)
const colSums = apply_along_axis(
  (col) => col.toArray().reduce((a, b) => a + b, 0),
  0,
  a
);
// array([9, 5, 7])

apply_over_axes

Apply a function repeatedly over multiple axes. After each application, the result is kept with the reduced axis preserved as a size-1 dimension, so subsequent axes remain valid.
function apply_over_axes(
  func: (a: NDArray, axis: number) => NDArray,
  a: ArrayLike,
  axes: number[]
): NDArray
ParameterTypeDefaultDescription
func(a: NDArray, axis: number) => NDArrayFunction that takes an array and an axis, returning an array with that axis reduced to size 1.
aArrayLikeInput array.
axesnumber[]Axes over which to apply func, in order.
Returns: NDArray — The result of applying func over all specified axes.
import { array, apply_over_axes, sum } from 'numpy-ts';

const a = array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
// shape: [2, 2, 2]

// Sum over axes 0 and 2
const result = apply_over_axes(
  (arr, axis) => sum(arr, axis, true),
  a,
  [0, 2]
);
// shape: [1, 2, 1]
// array([[[10], [22]]])

shares_memory

Determine whether two arrays share the same underlying data buffer. Returns true only when the arrays definitely reference overlapping memory.
function shares_memory(a: ArrayLike, b: ArrayLike): boolean
ParameterTypeDefaultDescription
aArrayLikeFirst input array.
bArrayLikeSecond input array.
Returns: booleantrue if both arrays share memory.
import { array, reshape, copy, shares_memory } from 'numpy-ts';

const a = array([1, 2, 3, 4]);
const b = reshape(a, [2, 2]);   // view
const c = copy(a);              // independent copy

shares_memory(a, b);  // true  -- b is a view of a
shares_memory(a, c);  // false -- c is a copy

may_share_memory

Determine whether two arrays might share memory. This is a less strict check than shares_memory — it may return true even when the arrays do not actually overlap, but it never returns false when they do.
function may_share_memory(a: ArrayLike, b: ArrayLike): boolean
ParameterTypeDefaultDescription
aArrayLikeFirst input array.
bArrayLikeSecond input array.
Returns: booleantrue if the arrays might share memory.
import { array, reshape, copy, may_share_memory } from 'numpy-ts';

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

may_share_memory(a, b);  // true
may_share_memory(a, copy(a));  // false

ndim

Return the number of dimensions of an array.
function ndim(a: ArrayLike | number | bigint | boolean): number
ParameterTypeDefaultDescription
aArrayLikeInput array or array-like.
Returns: number — The number of dimensions (axes).
import { array, ndim } from 'numpy-ts';

ndim(array([1, 2, 3]));             // 1
ndim(array([[1, 2], [3, 4]]));      // 2
ndim(array([[[1]]]));               // 3

shape

Return the shape of an array as a number array.
function shape(a: ArrayLike | number | bigint | boolean): number[] | readonly number[]
ParameterTypeDefaultDescription
aArrayLikeInput array or array-like.
Returns: number[] — The dimensions of the array.
import { array, zeros, shape } from 'numpy-ts';

shape(array([1, 2, 3]));             // [3]
shape(array([[1, 2], [3, 4]]));      // [2, 2]
shape(zeros([3, 4, 5]));             // [3, 4, 5]

size

Return the total number of elements in an array.
function size(a: ArrayLike | number | bigint | boolean): number
ParameterTypeDefaultDescription
aArrayLikeInput array or array-like.
Returns: number — The total number of elements (product of shape dimensions).
import { array, zeros, size } from 'numpy-ts';

size(array([1, 2, 3]));            // 3
size(array([[1, 2], [3, 4]]));     // 4
size(zeros([3, 4, 5]));            // 60

geterr

Get the current error handling settings for floating-point operations.
function geterr(): FloatErrorState
Returns: object — An object with keys divide, over, under, and invalid, each set to one of 'warn', 'raise', 'ignore', or 'print'.
import { geterr } from 'numpy-ts';

const settings = geterr();
console.log(settings);
// { divide: 'warn', over: 'warn', under: 'ignore', invalid: 'warn' }

seterr

Set how floating-point errors are handled. Returns the previous settings so you can restore them later.
function seterr(
  all?: ErrorMode,
  divide?: ErrorMode,
  over?: ErrorMode,
  under?: ErrorMode,
  invalid?: ErrorMode
): FloatErrorState
ParameterTypeDefaultDescription
allErrorModeundefinedDefault mode applied to all error categories unless overridden by specific args.
divideErrorModeundefinedDivision-by-zero handling mode.
overErrorModeundefinedOverflow handling mode.
underErrorModeundefinedUnderflow handling mode.
invalidErrorModeundefinedInvalid operation handling mode.
Returns: FloatErrorState — The previous error handling settings.
import { seterr, geterr } from 'numpy-ts';

// Suppress all warnings
const old = seterr(undefined, 'ignore', 'ignore');

// ... perform operations that may divide by zero ...

// Restore previous settings
seterr(undefined, old.divide, old.over, old.under, old.invalid);

item

Get a single scalar element from an array.
function item(a: ArrayLike, ...args: number[]): number | bigint | boolean | Complex

tolist

Convert an array to nested JavaScript arrays (or a scalar for 0-D arrays).
function tolist(a: ArrayLike): unknown

tobytes

Return the raw bytes of an array in C-order by default.
function tobytes(a: ArrayLike, order?: 'C' | 'F'): Uint8Array

byteswap

Swap the byte order of each array element.
function byteswap(a: ArrayLike, inplace?: boolean): NDArray

view

Create a view of an array (same dtype view currently supported).
function view(a: ArrayLike, dtype?: DType): NDArray

tofile

Write an array to a file (Node.js-oriented API; see note in implementation for environment limits).
function tofile(_a: ArrayLike, _file: string, _sep?: string, _format?: string): void

fill

Fill an array in place with a scalar value.
function fill(a: NDArray, value: number | bigint | boolean | Complex): void