Cyforanumera
npm

API reference

Universal functions (ufunc)

A universal function (ufunc) operates on arrays element by element. It supports broadcasting, type promotion and a set of standard options. Like NumPy's, numera's ufuncs are callable objects: np.add(a, b) computes, and np.add.reduce(a) reduces with the same operation.

JavaScript
np.add([1, 2, 3], 10);              // => [11, 12, 13]
np.sqrt([1, 4, 9]);                 // => [1, 2, 3]
np.add.reduce([1, 2, 3]).item();    // => 6

Broadcasting#

Inputs of different shapes are broadcast against each other using NumPy's rules: dimensions are compared from the last one, and each pair must be equal or contain a 1. Incompatible shapes raise BroadcastError.

JavaScript
np.add(np.ones([3, 1]), np.arange(4)).shape;  // => [3, 4]
np.multiply([[1], [2]], [10, 20]);            // => [[10, 20], [20, 40]]

Output type determination#

The result dtype comes from NumPy's promotion of the input dtypes. JS numbers are weak scalars (NEP 50), so they don't widen an array's dtype. See Data types.

JavaScript
np.add(np.array([1], { dtype: "int8" }), 1).dtype.name;     // => "int8"
np.add(np.array([1], { dtype: "int8" }), 1.5).dtype.name;   // => "float64"
np.divide([1, 2], 2).dtype.name;                            // => "float64"
np.add([1, 2], [3, 4], { dtype: "float32" }).dtype.name;    // => "float32"

Optional arguments#

Every ufunc accepts an options object as its last argument:

OptionNumPyMeaning
outout=write the result into this array and return it
wherewhere=boolean mask: compute only where true
dtypedtype=loop dtype: inputs are cast to it and the result has it
castingcasting=casting rule for inputs and out (default "same_kind")
orderorder=memory layout of a new result: "K" (default), "C", "F" or "A"
JavaScript
const o = np.zeros(3);
np.multiply([1, 2, 3], 2, { out: o }) === o;  // => true
o;                                            // => [2, 4, 6]

const w = np.full([3], -1.0, { dtype: "float64" });
np.sqrt([1, 4, 9], { out: w, where: [true, false, true] });
w;                                            // => [1, -1, 3]

Without out, positions where where is false are zero-filled. NumPy leaves them uninitialised. Writing a result into an out array of a lower kind is checked with casting:

JavaScript
let msg;
try { np.add(np.array([1.5]), 1, { out: np.zeros(1, { dtype: "int32" }) }); } catch (e) { msg = e.message; }
msg; // => "Cannot cast ufunc 'add' output from float64 to int32 with casting rule 'same_kind'"

Methods#

Binary ufuncs (two inputs, one output) have NumPy's five methods:

MethodNumPyMeaning
reduce(a, { axis, dtype, out, keepdims, initial, where })ufunc.reducecombine along an axis
accumulate(a, { axis, dtype, out })ufunc.accumulaterunning reduction
reduceat(a, indices, { axis, dtype, out })ufunc.reduceatreductions over slices
outer(a, b)ufunc.outerapply to every pair
at(a, indices, b)ufunc.atunbuffered in-place operation

Unary ufuncs have at(a, indices).

JavaScript
np.maximum.reduce([[1, 5], [4, 2]], { axis: 1 }); // => [5, 4]
np.add.accumulate([1, 2, 3]);                     // => [1, 3, 6]
np.add.reduceat([1, 2, 3, 4], [0, 2]);            // => [3, 7]
np.multiply.outer([1, 2, 3], [1, 10]);            // => [[1, 10], [2, 20], [3, 30]]

const c = np.array([1, 2, 3, 4]);
np.add.at(c, [0, 0, 1], 10);                      // repeated indices accumulate
c;                                                // => [21, 12, 3, 4]

Floating-point errors#

Division by zero, overflow, underflow and invalid operations are handled according to np.seterr / np.errstate. By default they emit a RuntimeWarning, and they can be ignored or raised instead. See Errors and floating-point.

JavaScript
np.errstate({ divide: "ignore" }, () => np.divide([1, -1], [0, 0]).toArray().map(String)); // => ["Infinity", "-Infinity"]

Available ufuncs#

numera 1.0.2 implements 106 of NumPy's ufuncs. The descriptions are NumPy's one-line summaries, and each NumPy name links to its page on numpy.org.

Math operations#

numeraNumPyKindDescription
np.absabsunary · atCalculate the absolute value element-wise.
np.absoluteabsoluteunary · atCalculate the absolute value element-wise.
np.addaddbinary · reduce, accumulate, reduceat, outer, atAdd arguments element-wise.
np.cbrtcbrtunary · atReturn the cube-root of an array, element-wise.
np.conjconjunaryReturn the complex conjugate, element-wise.
np.conjugateconjugateunaryReturn the complex conjugate, element-wise.
np.dividedividebinary · reduce, accumulate, reduceat, outer, atDivide arguments element-wise.
np.divmoddivmodbinaryReturn element-wise quotient and remainder simultaneously.
np.expexpunary · atCalculate the exponential of all elements in the input array.
np.exp2exp2unary · atCalculate 2**p for all p in the input array.
np.expm1expm1unary · atCalculate exp(x) - 1 for all elements in the array.
np.fabsfabsunary · atCompute the absolute values element-wise.
np.floatPowerfloat_powerbinary · reduce, accumulate, reduceat, outer, atFirst array elements raised to powers from second array, element-wise.
np.floorDividefloor_dividebinary · reduce, accumulate, reduceat, outer, atReturn the largest integer smaller or equal to the division of the inputs. It is equivalent to the Python // operator and pairs with the Python % (remainder), function so that a = a % b + b * (a // b) up to roundoff.
np.fmodfmodbinary · reduce, accumulate, reduceat, outer, atReturns the element-wise remainder of division.
np.gcdgcdbinary · reduce, accumulate, reduceat, outer, atReturns the greatest common divisor of |x1| and |x2|
np.heavisideheavisidebinary · reduce, accumulate, reduceat, outer, atCompute the Heaviside step function.
np.lcmlcmbinary · reduce, accumulate, reduceat, outer, atReturns the lowest common multiple of |x1| and |x2|
np.loglogunary · atNatural logarithm, element-wise.
np.log10log10unary · atReturn the base 10 logarithm of the input array, element-wise.
np.log1plog1punary · atReturn the natural logarithm of one plus the input array, element-wise.
np.log2log2unary · atBase-2 logarithm of x.
np.logaddexplogaddexpbinary · reduce, accumulate, reduceat, outer, atLogarithm of the sum of exponentiations of the inputs.
np.logaddexp2logaddexp2binary · reduce, accumulate, reduceat, outer, atLogarithm of the sum of exponentiations of the inputs in base-2.
np.matmulmatmulbinaryMatrix product of two arrays.
np.matvecmatvecbinaryMatrix-vector dot product of two arrays.
np.modmodbinary · reduce, accumulate, reduceat, outer, atReturns the element-wise remainder of division.
np.multiplymultiplybinary · reduce, accumulate, reduceat, outer, atMultiply arguments element-wise.
np.negativenegativeunary · atNumerical negation, element-wise.
np.positivepositiveunary · atNumerical positive, element-wise.
np.powpowbinary · reduce, accumulate, reduceat, outer, atFirst array elements raised to powers from second array, element-wise.
np.powerpowerbinary · reduce, accumulate, reduceat, outer, atFirst array elements raised to powers from second array, element-wise.
np.reciprocalreciprocalunary · atReturn the reciprocal of the argument, element-wise.
np.remainderremainderbinary · reduce, accumulate, reduceat, outer, atReturns the element-wise remainder of division.
np.rintrintunary · atRound elements of the array to the nearest integer.
np.signsignunary · atReturns an element-wise indication of the sign of a number.
np.sqrtsqrtunary · atReturn the non-negative square-root of an array, element-wise.
np.squaresquareunary · atReturn the element-wise square of the input.
np.subtractsubtractbinary · reduce, accumulate, reduceat, outer, atSubtract arguments, element-wise.
np.trueDividetrue_dividebinary · reduce, accumulate, reduceat, outer, atDivide arguments element-wise.
np.vecdotvecdotbinaryVector dot product of two arrays.
np.vecmatvecmatbinaryVector-matrix dot product of two arrays.

Trigonometric functions#

numeraNumPyKindDescription
np.acosacosunary · atTrigonometric inverse cosine, element-wise.
np.acoshacoshunary · atInverse hyperbolic cosine, element-wise.
np.arccosarccosunary · atTrigonometric inverse cosine, element-wise.
np.arccosharccoshunary · atInverse hyperbolic cosine, element-wise.
np.arcsinarcsinunary · atInverse sine, element-wise.
np.arcsinharcsinhunary · atInverse hyperbolic sine, element-wise.
np.arctanarctanunary · atTrigonometric inverse tangent, element-wise.
np.arctan2arctan2binary · reduce, accumulate, reduceat, outer, atElement-wise arc tangent of x1/x2 choosing the quadrant correctly.
np.arctanharctanhunary · atInverse hyperbolic tangent, element-wise.
np.asinasinunary · atInverse sine, element-wise.
np.asinhasinhunary · atInverse hyperbolic sine, element-wise.
np.atanatanunary · atTrigonometric inverse tangent, element-wise.
np.atan2atan2binary · reduce, accumulate, reduceat, outer, atElement-wise arc tangent of x1/x2 choosing the quadrant correctly.
np.atanhatanhunary · atInverse hyperbolic tangent, element-wise.
np.coscosunary · atCosine element-wise.
np.coshcoshunary · atHyperbolic cosine, element-wise.
np.deg2raddeg2radunary · atConvert angles from degrees to radians.
np.degreesdegreesunary · atConvert angles from radians to degrees.
np.hypothypotbinary · reduce, accumulate, reduceat, outer, atGiven the "legs" of a right triangle, return its hypotenuse.
np.rad2degrad2degunary · atConvert angles from radians to degrees.
np.radiansradiansunary · atConvert angles from degrees to radians.
np.sinsinunary · atTrigonometric sine, element-wise.
np.sinhsinhunary · atHyperbolic sine, element-wise.
np.tantanunary · atCompute tangent element-wise.
np.tanhtanhunary · atHyperbolic tangent, element-wise.

Bit-twiddling functions#

numeraNumPyKindDescription
np.bitwiseAndbitwise_andbinary · reduce, accumulate, reduceat, outer, atCompute the bit-wise AND of two arrays element-wise.
np.bitwiseCountbitwise_countunary · atComputes the number of 1-bits in the absolute value of x. Analogous to the builtin int.bit_count or popcount in C++.
np.bitwiseInvertbitwise_invertunary · atCompute bit-wise inversion, or bit-wise NOT, element-wise.
np.bitwiseLeftShiftbitwise_left_shiftbinary · reduce, accumulate, reduceat, outer, atShift the bits of an integer to the left.
np.bitwiseNotbitwise_notunary · atCompute bit-wise inversion, or bit-wise NOT, element-wise.
np.bitwiseOrbitwise_orbinary · reduce, accumulate, reduceat, outer, atCompute the bit-wise OR of two arrays element-wise.
np.bitwiseRightShiftbitwise_right_shiftbinary · reduce, accumulate, reduceat, outer, atShift the bits of an integer to the right.
np.bitwiseXorbitwise_xorbinary · reduce, accumulate, reduceat, outer, atCompute the bit-wise XOR of two arrays element-wise.
np.invertinvertunary · atCompute bit-wise inversion, or bit-wise NOT, element-wise.
np.leftShiftleft_shiftbinary · reduce, accumulate, reduceat, outer, atShift the bits of an integer to the left.
np.rightShiftright_shiftbinary · reduce, accumulate, reduceat, outer, atShift the bits of an integer to the right.

Comparison functions#

numeraNumPyKindDescription
np.equalequalbinary · reduce, accumulate, reduceat, outer, atReturn (x1 == x2) element-wise.
np.fmaxfmaxbinary · reduce, accumulate, reduceat, outer, atElement-wise maximum of array elements.
np.fminfminbinary · reduce, accumulate, reduceat, outer, atElement-wise minimum of array elements.
np.greatergreaterbinary · reduce, accumulate, reduceat, outer, atReturn the truth value of (x1 > x2) element-wise.
np.greaterEqualgreater_equalbinary · reduce, accumulate, reduceat, outer, atReturn the truth value of (x1 >= x2) element-wise.
np.lesslessbinary · reduce, accumulate, reduceat, outer, atReturn the truth value of (x1 < x2) element-wise.
np.lessEqualless_equalbinary · reduce, accumulate, reduceat, outer, atReturn the truth value of (x1 <= x2) element-wise.
np.logicalAndlogical_andbinary · reduce, accumulate, reduceat, outer, atCompute the truth value of x1 AND x2 element-wise.
np.logicalNotlogical_notunary · atCompute the truth value of NOT x element-wise.
np.logicalOrlogical_orbinary · reduce, accumulate, reduceat, outer, atCompute the truth value of x1 OR x2 element-wise.
np.logicalXorlogical_xorbinary · reduce, accumulate, reduceat, outer, atCompute the truth value of x1 XOR x2, element-wise.
np.maximummaximumbinary · reduce, accumulate, reduceat, outer, atElement-wise maximum of array elements.
np.minimumminimumbinary · reduce, accumulate, reduceat, outer, atElement-wise minimum of array elements.
np.notEqualnot_equalbinary · reduce, accumulate, reduceat, outer, atReturn (x1 != x2) element-wise.

Floating functions#

numeraNumPyKindDescription
np.ceilceilunary · atReturn the ceiling of the input, element-wise.
np.copysigncopysignbinary · reduce, accumulate, reduceat, outer, atChange the sign of x1 to that of x2, element-wise.
np.floorfloorunary · atReturn the floor of the input, element-wise.
np.frexpfrexpbinaryDecompose the elements of x into mantissa and twos exponent.
np.isfiniteisfiniteunary · atTest element-wise for finiteness (not infinity and not Not a Number).
np.isinfisinfunary · atTest element-wise for positive or negative infinity.
np.isnanisnanunary · atTest element-wise for NaN and return result as a boolean array.
np.isnatisnatunary · atTest element-wise for NaT (not a time) and return result as a boolean array.
np.ldexpldexpbinary · reduce, accumulate, reduceat, outer, atReturns x1 * 2**x2, element-wise.
np.modfmodfbinaryReturn the fractional and integral parts of an array, element-wise.
np.nextafternextafterbinary · reduce, accumulate, reduceat, outer, atReturn the next floating-point value after x1 towards x2, element-wise.
np.signbitsignbitunary · atReturns element-wise True where signbit is set (less than zero).
np.spacingspacingunary · atReturn the distance between x and the nearest adjacent number.
np.trunctruncunary · atReturn the truncated value of the input, element-wise.