Skip to main content
All arithmetic functions operate element-wise. Binary functions accept an NDArray or a scalar as the second argument and broadcast as needed.

add

Add arguments element-wise.
function add(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise sum of the inputs.
import * as np from 'numpy-ts';

const a = np.array([1, 2, 3]);
const b = np.add(a, np.array([10, 20, 30]));
// array([11, 22, 33])

const c = np.add(a, 100);
// array([101, 102, 103])

subtract

Subtract arguments element-wise.
function subtract(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise difference x1 - x2.
import * as np from 'numpy-ts';

const a = np.subtract(np.array([10, 20, 30]), np.array([1, 2, 3]));
// array([9, 18, 27])

multiply

Multiply arguments element-wise.
function multiply(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise product.
import * as np from 'numpy-ts';

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

divide

Divide arguments element-wise (true division).
function divide(x: ArrayLike, divisor: ArrayLike | number): NDArray
NameTypeDefaultDescription
xArrayLike-Dividend array.
divisorArrayLike | number-Divisor array or scalar.
Returns: NDArray — Element-wise quotient x / divisor.
import * as np from 'numpy-ts';

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

true_divide

Alias for divide. Returns true (floating-point) division.
function true_divide(x: ArrayLike, divisor: ArrayLike | number): NDArray

floor_divide

Element-wise floor division. Returns the largest integer less than or equal to the division.
function floor_divide(x: ArrayLike, divisor: ArrayLike | number): NDArray
NameTypeDefaultDescription
xArrayLike-Dividend array.
divisorArrayLike | number-Divisor array or scalar.
Returns: NDArray — Element-wise floor of x / divisor.
import * as np from 'numpy-ts';

const a = np.floor_divide(np.array([7, 8, 9]), 3);
// array([2, 2, 3])

mod

Element-wise modulo (remainder of floor division). Equivalent to remainder.
function mod(x: ArrayLike, divisor: ArrayLike | number): NDArray
NameTypeDefaultDescription
xArrayLike-Dividend array.
divisorArrayLike | number-Divisor array or scalar.
Returns: NDArray — Element-wise remainder.
import * as np from 'numpy-ts';

const a = np.mod(np.array([7, 8, 9]), 3);
// array([1, 2, 0])

remainder

Element-wise remainder of division. Equivalent to mod.
function remainder(x: ArrayLike, y: ArrayLike | number): NDArray
NameTypeDefaultDescription
xArrayLike-Dividend array.
yArrayLike | number-Divisor.
Returns: NDArray — Element-wise remainder of x1 / x2.

fmod

Element-wise remainder using C-library fmod. The result has the same sign as the dividend.
function fmod(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-Dividend.
x2ArrayLike | number-Divisor.
Returns: NDArray — Element-wise C-style remainder.
import * as np from 'numpy-ts';

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

divmod

Return element-wise quotient and remainder simultaneously.
function divmod(x: ArrayLike, y: ArrayLike | number): [NDArray, NDArray]
NameTypeDefaultDescription
xArrayLike-Dividend.
yArrayLike | number-Divisor.
Returns: [NDArray, NDArray] — Tuple of [quotient, remainder].
import * as np from 'numpy-ts';

const [q, r] = np.divmod(np.array([7, 8, 9]), 3);
// q = array([2, 2, 3])
// r = array([1, 2, 0])

power

Element-wise exponentiation.
function power(x: ArrayLike, exponent: ArrayLike | number): NDArray
NameTypeDefaultDescription
xArrayLike-Base array.
exponentArrayLike | number-Exponent array or scalar.
Returns: NDArray — Element-wise x1 ** x2.
import * as np from 'numpy-ts';

const a = np.power(np.array([2, 3, 4]), 2);
// array([4, 9, 16])

pow

Alias for power.
function pow(x: ArrayLike, exponent: ArrayLike | number): NDArray

float_power

Element-wise power, always returning float64 results.
function float_power(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-Base array.
x2ArrayLike | number-Exponent.
Returns: NDArray — Element-wise x1 ** x2 as float64.

sqrt

Return the non-negative square root of each element.
function sqrt(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise square root.
import * as np from 'numpy-ts';

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

square

Return the element-wise square.
function square(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise x * x.
import * as np from 'numpy-ts';

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

cbrt

Return the cube root of each element.
function cbrt(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise cube root.
import * as np from 'numpy-ts';

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

absolute

Compute the absolute value element-wise.
function absolute(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise absolute value.
import * as np from 'numpy-ts';

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

abs

Alias for absolute.
function abs(x: ArrayLike): NDArray

fabs

Compute the absolute value element-wise (float version). Identical to absolute for real-valued arrays.
function fabs(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise absolute value as float.

sign

Return the element-wise sign: -1 for negative, 0 for zero, 1 for positive.
function sign(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise sign indication.
import * as np from 'numpy-ts';

const a = np.sign(np.array([-5, 0, 3]));
// array([-1, 0, 1])

negative

Numerical negative, element-wise.
function negative(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise -x.
import * as np from 'numpy-ts';

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

positive

Numerical positive, element-wise. Returns a copy.
function positive(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Copy of the input array.

reciprocal

Return the reciprocal (1/x) of each element.
function reciprocal(x: ArrayLike): NDArray
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: NDArray — Element-wise 1 / x.
import * as np from 'numpy-ts';

const a = np.reciprocal(np.array([1, 2, 4, 10]));
// array([1, 0.5, 0.25, 0.1])

heaviside

Compute the Heaviside step function. Returns 0 where x1 < 0, x2 where x1 == 0, and 1 where x1 > 0.
function heaviside(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-Input values.
x2ArrayLike | number-Value to use where x1 == 0.
Returns: NDArray — Element-wise Heaviside step function.
import * as np from 'numpy-ts';

const a = np.heaviside(np.array([-1, 0, 1]), 0.5);
// array([0, 0.5, 1])

gcd

Return the greatest common divisor of |x1| and |x2|, element-wise.
function gcd(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise GCD.
import * as np from 'numpy-ts';

const a = np.gcd(np.array([12, 18, 24]), 6);
// array([6, 6, 6])

lcm

Return the least common multiple of |x1| and |x2|, element-wise.
function lcm(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-First input array.
x2ArrayLike | number-Second input array or scalar.
Returns: NDArray — Element-wise LCM.
import * as np from 'numpy-ts';

const a = np.lcm(np.array([4, 6]), np.array([6, 8]));
// array([12, 24])

frexp

Decompose each element into mantissa and exponent, such that x = mantissa * 2**exponent.
function frexp(x: ArrayLike): [NDArray, NDArray]
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: [NDArray, NDArray] — Tuple of [mantissa, exponent] arrays.
import * as np from 'numpy-ts';

const [m, e] = np.frexp(np.array([1, 2, 4, 8]));
// m = array([0.5, 0.5, 0.5, 0.5])
// e = array([1, 2, 3, 4])

ldexp

Compute x1 * 2**x2, element-wise. The inverse of frexp.
function ldexp(x1: ArrayLike, x2: ArrayLike | number): NDArray
NameTypeDefaultDescription
x1ArrayLike-Mantissa array.
x2ArrayLike | number-Exponent array or scalar.
Returns: NDArray — Element-wise x1 * 2**x2.
import * as np from 'numpy-ts';

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

modf

Return the fractional and integer parts of each element.
function modf(x: ArrayLike): [NDArray, NDArray]
NameTypeDefaultDescription
xArrayLike-Input array.
Returns: [NDArray, NDArray] — Tuple of [fractional_part, integer_part].
import * as np from 'numpy-ts';

const [frac, intg] = np.modf(np.array([1.5, 2.7, -3.2]));
// frac = array([0.5, 0.7, -0.2])
// intg = array([1, 2, -3])