API reference
Comparison, logic and bitwise
Element-wise comparisons and logical operations return bool arrays. They are ufuncs with NumPy broadcasting and the .reduce/.accumulate/.outer/.at methods.
| Name | Summary |
|---|---|
np.equal | Element-wise ==, !=, <, <=, >, >= with a bool result. |
np.logicalAnd | Element-wise truth-value AND, OR, XOR and NOT with a bool result (zero, false and 0+0j are false; NaN is true). |
np.all | Whether every (all) or some (any) element is truthy, over all axes by default. |
np.isnan | Element-wise NaN, infinity and finiteness tests with a bool result. |
np.isposinf | Element-wise test for +Infinity / -Infinity. |
np.isscalar | True for JS numbers, booleans, bigints, strings and complex scalars. |
np.bitwiseAnd | Element-wise &, |, ^ and ~ on integer and bool arrays (float input raises DTypeError). |
np.leftShift | Element-wise a << b and arithmetic a >> b on integers. |
np.bitwiseCount | Number of 1 bits in the absolute value of each integer (or bool), as uint8. |
np.isclose | Element-wise |a - b| <= atol + rtol * |b| (with b finite), or a == b. |
np.arrayEqual | arrayEqual returns true if both inputs have the same shape and equal elements (equalNan lets NaNs in the same places match). |
np.packbits | Packs the elements of a bool or integer array into the bits of a uint8 array (any nonzero value is a 1 bit). |
np.unpackbits | Expands each element of a uint8 array into 8 bits (uint8 values 0 and 1). |
np.equal
#np.equal(a, b, options?) · np.notEqual · np.less · np.lessEqual · np.greater · np.greaterEqual
Element-wise ==, !=, <, <=, >, >= with a bool result. Complex values compare lexicographically (real part first), NaN compares false (notEqual gives true). JS integers outside an integer array's range compare by exact value.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar. [options]UfuncOptionsout,where,casting,order;dtypeaccepts only"bool"(the output type).
Returns
NDArray (bool)
Example
np.equal([1, 2, 3], 2); // => [false, true, false]
np.notEqual([1, NaN], [1, NaN]); // => [false, true]
np.less([[1], [3]], [2, 4]); // => [[true, true], [false, true]]
np.lessEqual([1, 2], 1); // => [true, false]
np.greater(np.array([1], { dtype: "int8" }), -1000); // => [true]
np.greaterEqual([1, 2], 2); // => [false, true]TypeScript declaration
np.equal(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.notEqual(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.less(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.lessEqual(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.greater(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.greaterEqual(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArraynp.logicalAnd
#np.logicalAnd(a, b, options?) · np.logicalOr · np.logicalXor · np.logicalNot(a, options?)
Element-wise truth-value AND, OR, XOR and NOT with a bool result (zero, false and 0+0j are false; NaN is true). .reduce uses NumPy's identities (logicalAnd true, the others false).
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar. [options]UfuncOptionsout,where,casting,order;dtypeaccepts only"bool"(the output type).
Returns
NDArray (bool)
Example
np.logicalAnd([1, 0, 2], [1, 1, 0]); // => [true, false, false]
np.logicalOr([0, 0, 2], [0, 1, 0]); // => [false, true, true]
np.logicalXor([1, 1], [0, 1]); // => [true, false]
np.logicalNot([0, 1.5, NaN]); // => [true, false, false]
np.logicalXor.reduce([1, 1, 1]); // => trueTypeScript declaration
np.logicalAnd(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.logicalOr(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.logicalXor(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.logicalNot(a: ArrayLike, opts?: UfuncOptions | undefined): NDArraynp.all
#np.all(a, { axis?, keepdims?, where?, out? }) · np.any · a.all() · a.any()
Whether every (all) or some (any) element is truthy, over all axes by default. Empty input gives true for all and false for any. Also available as NDArray methods.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | number[] | null- Axis or axes to reduce. Omitted or
nullreduces all axes. [options.keepdims]boolean- Keep reduced axes with length 1.
[options.where]ArrayLike- Bool mask of the elements to include.
[options.out]NDArray- Output array (the result is cast to its dtype).
Returns
NDArray (bool)
Example
np.all([[1, 0], [1, 1]]); // => false
np.all([[1, 0], [1, 1]], { axis: 0 }); // => [true, false]
np.any([[0, 0], [0, 1]], { axis: 1 }); // => [false, true]
np.all([1, 0], { where: [true, false] }); // => true
np.array([0, 2]).any(); // => trueTypeScript declaration
np.all(a: ArrayLike, opts?: AllAnyOptions | undefined): NDArray
np.any(a: ArrayLike, opts?: AllAnyOptions | undefined): NDArray
a.all(opts?: AllAnyOptions | undefined): NDArray
a.any(opts?: AllAnyOptions | undefined): NDArraynp.isnan
#np.isnan(a, options?) · np.isinf · np.isfinite · np.isnat
Element-wise NaN, infinity and finiteness tests with a bool result. Integers are never NaN or infinite. A complex value is NaN or infinite if either part is, and finite only if both parts are. isnat needs a datetime dtype, which numera does not have yet, so it always raises DTypeError (as NumPy does for non-datetime input).
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options]UfuncOptionsout,where,casting,order;dtypeaccepts only"bool"(the output type).
Returns
NDArray (bool)
Example
np.isnan([1, NaN, Infinity]); // => [false, true, false]
np.isinf([1, NaN, -Infinity]); // => [false, false, true]
np.isfinite([1, NaN, Infinity]); // => [true, false, false]
np.isnan(np.array([np.complex(1, NaN)])); // => [true]TypeScript declaration
np.isnan(a: ArrayLike, opts?: UfuncOptions | undefined): NDArray
np.isinf(a: ArrayLike, opts?: UfuncOptions | undefined): NDArray
np.isfinite(a: ArrayLike, opts?: UfuncOptions | undefined): NDArray
np.isnat(a: ArrayLike, opts?: UfuncOptions | undefined): NDArraynp.isposinf
#np.isposinf(a, { out? }) · np.isneginf(a, { out? })
Element-wise test for +Infinity / -Infinity. Complex input raises DTypeError.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.out]NDArray- Output array.
Returns
NDArray (bool)
Example
np.isposinf([-Infinity, Infinity, 1]); // => [false, true, false]
np.isneginf([-Infinity, Infinity, 1]); // => [true, false, false]TypeScript declaration
np.isposinf(x: ArrayLike, opts?: { out?: NDArray | null | undefined; } | undefined): NDArray
np.isneginf(x: ArrayLike, opts?: { out?: NDArray | null | undefined; } | undefined): NDArraynp.isscalar
#np.isscalar(x)
True for JS numbers, booleans, bigints, strings and complex scalars. False for any NDArray (also 0-d) and for lists.
Parameters
xunknown- Any value.
Returns
boolean
Example
np.isscalar(3.5); // => true
np.isscalar(np.array(3.5)); // => false
np.isscalar([1]); // => falseTypeScript declaration
np.isscalar(x: unknown): booleannp.bitwiseAnd
#np.bitwiseAnd(a, b, options?) · np.bitwiseOr · np.bitwiseXor · np.invert(a, options?)
Element-wise &, |, ^ and ~ on integer and bool arrays (float input raises DTypeError). invert of bool is logical NOT. np.bitwiseNot and np.bitwiseInvert are the same ufunc as np.invert. bitwiseAnd.reduce of an empty array gives all ones.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar. [options]UfuncOptionsout,where,dtype,casting,order.
Returns
NDArray
Example
np.bitwiseAnd([12, 10], [10, 6]); // => [8, 2]
np.bitwiseOr([12, 10], 1); // => [13, 11]
np.bitwiseXor([12, 10], [10, 6]); // => [6, 12]
np.invert(np.array([5], { dtype: "uint8" })); // => [250]
np.bitwiseNot([true, false]); // => [false, true]
np.bitwiseInvert([0]); // => [-1]TypeScript declaration
np.bitwiseAnd(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.bitwiseOr(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.bitwiseXor(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.invert(a: ArrayLike, opts?: UfuncOptions | undefined): NDArraynp.leftShift
#np.leftShift(a, b, options?) · np.rightShift · np.bitwiseLeftShift · np.bitwiseRightShift
Element-wise a << b and arithmetic a >> b on integers. A shift count that is negative or at least the bit width gives 0 (-1 when right-shifting a negative value). bitwiseLeftShift/bitwiseRightShift are the same ufuncs.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar. [options]UfuncOptionsout,where,dtype,casting,order.
Returns
NDArray
Example
np.leftShift(1, [1, 2, 3]); // => [2, 4, 8]
np.rightShift([-8, 8], 1); // => [-4, 4]
np.bitwiseLeftShift(np.array([1], { dtype: "int8" }), 9); // => [0]
np.bitwiseRightShift([16], 2); // => [4]TypeScript declaration
np.leftShift(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.rightShift(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.bitwiseLeftShift(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArray
np.bitwiseRightShift(a: Operand, b: Operand, opts?: UfuncOptions | undefined): NDArraynp.bitwiseCount
#np.bitwiseCount(a, options?)
Number of 1 bits in the absolute value of each integer (or bool), as uint8.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options]UfuncOptionsout,where,dtype(uint8only),casting,order.
Returns
NDArray (uint8)
Example
np.bitwiseCount([0, 7, -1, 255]); // => [0, 3, 1, 8]TypeScript declaration
np.bitwiseCount(a: ArrayLike, opts?: UfuncOptions | undefined): NDArraynp.isclose
#np.isclose(a, b, { rtol?, atol?, equalNan? }) · np.allclose(a, b, options?)
Element-wise |a - b| <= atol + rtol * |b| (with b finite), or a == b. The test is not symmetric in a and b. Integers are compared as float64. equalNan treats NaN in both inputs as equal. allclose returns a JS boolean: true if isclose holds everywhere. A non-finite rtol/atol is reported through the np.seterr invalid mode.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar. [options.rtol]number- Relative tolerance (default
1e-5). [options.atol]number- Absolute tolerance (default
1e-8). [options.equalNan]boolean- Treat NaNs in the same place as equal (default
false).
Returns
NDArray (bool) · boolean (allclose)
Example
np.isclose([1e10, 1e-7], [1.00001e10, 1e-8]); // => [true, false]
np.isclose([1, NaN], [1, NaN], { equalNan: true }); // => [true, true]
np.allclose([1e10, 1e-8], [1.00001e10, 1e-9]); // => trueTypeScript declaration
np.isclose(a: CloseOperand, b: CloseOperand, opts?: IscloseOptions | undefined): NDArray
np.allclose(a: CloseOperand, b: CloseOperand, opts?: IscloseOptions | undefined): booleannp.arrayEqual
#np.arrayEqual(a1, a2, { equalNan? }) · np.arrayEquiv(a1, a2)
arrayEqual returns true if both inputs have the same shape and equal elements (equalNan lets NaNs in the same places match). arrayEquiv only needs the shapes to broadcast. Both return a JS boolean and give false for inputs that cannot be converted.
Parameters
a1ArrayLike- An
NDArray, nested JS array or scalar. a2ArrayLike- An
NDArray, nested JS array or scalar. [options.equalNan]boolean- Treat NaNs in the same place as equal (default
false).
Returns
boolean
Example
np.arrayEqual([1, 2], [1, 2]); // => true
np.arrayEqual([1, 2], [1, 2, 3]); // => false
np.arrayEqual([1, NaN], [1, NaN], { equalNan: true }); // => true
np.arrayEquiv([1, 2], [[1, 2], [1, 2]]); // => trueTypeScript declaration
np.arrayEqual(a1: ArrayLike, a2: ArrayLike, opts?: { equalNan?: boolean | undefined; } | undefined): boolean
np.arrayEquiv(a1: ArrayLike, a2: ArrayLike): booleannp.packbits
#np.packbits(a, { axis?, bitorder? })
Packs the elements of a bool or integer array into the bits of a uint8 array (any nonzero value is a 1 bit). Without axis the flattened array is packed. The last byte is padded with zero bits.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | null- Axis to pack along (default: flatten).
[options.bitorder]"big" | "little"- Bit order inside each byte (default
"big").
Returns
NDArray (uint8)
Example
np.packbits([1, 0, 1, 1, 0, 0, 0, 0, 1]); // => [176, 128]
np.packbits([[1, 1], [0, 1]], { axis: 1 }); // => [[192], [64]]
np.packbits([1, 0, 1], { bitorder: "little" }); // => [5]TypeScript declaration
np.packbits(a: ArrayLike, opts?: PackbitsOptions | undefined): NDArraynp.unpackbits
#np.unpackbits(a, { axis?, count?, bitorder? })
Expands each element of a uint8 array into 8 bits (uint8 values 0 and 1). Without axis the flattened array is unpacked. count keeps that many bits along the axis (padding with zeros past the end); a negative count drops bits from the end.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.axis]number | null- Axis to unpack along (default: flatten).
[options.count]number | null- Number of bits to keep.
[options.bitorder]"big" | "little"- Bit order inside each byte (default
"big").
Returns
NDArray (uint8)
Example
const b = np.array([5], { dtype: "uint8" });
np.unpackbits(b); // => [0, 0, 0, 0, 0, 1, 0, 1]
np.unpackbits(b, { count: -5 }); // => [0, 0, 0]
np.unpackbits(b, { bitorder: "little", count: 3 }); // => [1, 0, 1]TypeScript declaration
np.unpackbits(a: ArrayLike, opts?: UnpackbitsOptions | undefined): NDArray