Skip to main content

clip

Clip (limit) the values in an array to a given range.
function clip(a: ArrayLike, a_min: ArrayLike | number | null, a_max: ArrayLike | number | null): NDArray
NameTypeDefaultDescription
aArrayLike-Input array.
a_minnumber | null-Minimum value. Elements below this are set to a_min. Pass null to skip the lower bound.
a_maxnumber | null-Maximum value. Elements above this are set to a_max. Pass null to skip the upper bound.
Returns: NDArray — Array with values clipped to [a_min, a_max].
import * as np from 'numpy-ts';

const a = np.clip(np.array([1, 5, 10, 15, 20]), 3, 12);
// array([3, 5, 10, 12, 12])

// One-sided clipping
const b = np.clip(np.array([-5, 0, 5, 10]), 0, null);
// array([0, 0, 5, 10])

maximum

Element-wise maximum of two arrays. Propagates NaN.
function maximum(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise maximum. If either element is NaN, the result is NaN.
import * as np from 'numpy-ts';

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

const b = np.maximum(np.array([-1, 0, 1]), 0);
// array([0, 0, 1])  -- ReLU-like operation

minimum

Element-wise minimum of two arrays. Propagates NaN.
function minimum(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise minimum. If either element is NaN, the result is NaN.
import * as np from 'numpy-ts';

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

fmax

Element-wise maximum of two arrays, ignoring NaN. If one element is NaN, the other is returned.
function fmax(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise maximum, ignoring NaN.
import * as np from 'numpy-ts';

const a = np.fmax(np.array([1, NaN, 3]), np.array([NaN, 2, NaN]));
// array([1, 2, 3])  -- NaN ignored in each pair

fmin

Element-wise minimum of two arrays, ignoring NaN.
function fmin(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise minimum, ignoring NaN.
import * as np from 'numpy-ts';

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

nan_to_num

Replace NaN with zero and infinity with large finite numbers (or specified values).
function nan_to_num(
  x: ArrayLike,
  nan?: number,
  posinf?: number,
  neginf?: number
): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
nannumber0.0Value to replace NaN with.
posinfnumberundefinedValue to replace positive infinity. Defaults to a very large finite number.
neginfnumberundefinedValue to replace negative infinity. Defaults to a very large negative finite number.
Returns: NDArray — Array with NaN/Inf replaced.
import * as np from 'numpy-ts';

const a = np.nan_to_num(np.array([1, NaN, Infinity, -Infinity]));
// array([1, 0, 1.7977e+308, -1.7977e+308])

const b = np.nan_to_num(np.array([NaN, Infinity]), -1, 999);
// array([-1, 999])

interp

One-dimensional linear interpolation for monotonically increasing sample points.
function interp(
  x: ArrayLike,
  xp: ArrayLike,
  fp: ArrayLike,
  left?: number,
  right?: number
): NDArray
NameTypeDefaultDescription
xArrayLike-x-coordinates at which to evaluate the interpolation.
xpArrayLike-x-coordinates of the data points (must be increasing).
fpArrayLike-y-coordinates of the data points (same length as xp).
leftnumberfp[0]Value to return for x < xp[0].
rightnumberfp[fp.length - 1]Value to return for x > xp[-1].
Returns: NDArray — Interpolated values at each point in x.
import * as np from 'numpy-ts';

const xp = np.array([0, 1, 2, 3]);
const fp = np.array([0, 10, 20, 30]);
const x = np.array([0.5, 1.5, 2.5]);

const y = np.interp(x, xp, fp);
// array([5, 15, 25])

sinc

Return the normalized sinc function: sin(pi*x) / (pi*x). The sinc function is 1 at x = 0.
function sinc(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise sin(pi*x) / (pi*x).
import * as np from 'numpy-ts';

const a = np.sinc(np.array([0, 0.5, 1, 1.5, 2]));
// array([1, 0.6366..., 0, -0.2122..., 0])

i0

Modified Bessel function of the first kind, order 0.
function i0(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array (argument of the Bessel function).
Returns: NDArray — Element-wise modified Bessel function I_0(x).
import * as np from 'numpy-ts';

const a = np.i0(np.array([0, 1, 2]));
// array([1, 1.2661..., 2.2796...])

unwrap

Unwrap by changing deltas between values to their 2*pi complement. Useful for unwrapping radian phase data.
function unwrap(
  p: ArrayLike,
  discont?: number,
  axis?: number,
  period?: number
): NDArray
NameTypeDefaultDescription
pArrayLike-Input array of phase angles.
discontnumberMath.PIMaximum discontinuity between consecutive values. Jumps larger than this are wrapped.
axisnumber-1Axis along which to unwrap.
periodnumber2 * Math.PIWrapping period.
Returns: NDArray — Unwrapped array.
import * as np from 'numpy-ts';

// Phase angles with wrapping
const phase = np.array([0, 1, 2, 3, -3, -2, -1, 0]);
const unwrapped = np.unwrap(phase);
// Discontinuities are removed

real

Return the real part of each element. For real-valued arrays, returns a copy.
function real(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array (real or complex).
Returns: NDArray — The real part of each element.
import * as np from 'numpy-ts';
import { Complex } from 'numpy-ts';

const a = np.array([new Complex(1, 2), new Complex(3, 4)]);
const r = np.real(a);
// array([1, 3])

imag

Return the imaginary part of each element. For real-valued arrays, returns zeros.
function imag(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array (real or complex).
Returns: NDArray — The imaginary part of each element.
import * as np from 'numpy-ts';
import { Complex } from 'numpy-ts';

const a = np.array([new Complex(1, 2), new Complex(3, 4)]);
const i = np.imag(a);
// array([2, 4])

conj

Return the complex conjugate, element-wise. For each element a + bi, returns a - bi. Also available as the alias conjugate.
function conj(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array (real or complex).
Returns: NDArray — Complex conjugate of each element.
import * as np from 'numpy-ts';
import { Complex } from 'numpy-ts';

const a = np.array([new Complex(1, 2), new Complex(3, -4)]);
const c = np.conj(a);
// complex128 array: [(1-2j), (3+4j)]

// Alias
const c2 = np.conjugate(a);

angle

Return the angle (argument) of complex numbers.
function angle(x: ArrayLike, deg?: boolean): NDArray
NameTypeDefaultDescription
xArrayLike-Input array of complex numbers.
degbooleanfalseIf true, return angles in degrees instead of radians.
Returns: NDArray — The counterclockwise angle from the positive real axis, in radians (or degrees if deg is true).
import * as np from 'numpy-ts';
import { Complex } from 'numpy-ts';

const a = np.array([new Complex(1, 1), new Complex(0, 1), new Complex(-1, 0)]);
const angles = np.angle(a);
// array([0.7854..., 1.5708..., 3.1416...])  -- pi/4, pi/2, pi

const degrees = np.angle(a, true);
// array([45, 90, 180])