Cyforanumera
npm

Topics

TypeScript

numera is written in TypeScript and its declarations ship in the package (dist/index.d.ts), so you don't need an @types package. Every function in the API reference has a typed signature, and the NumPy name index shows the declaration of every implemented name.

Setup#

numera is an ES module. Use a module resolution that understands package exports:

json
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ES2022",
    "strict": true
  }
}

Exported types#

Import the types with import type:

TypeScript
import np, { NDArray } from "@cyfora/numera";
import type { ArrayLike, DTypeLike, Shape, UfuncOptions, ReduceOptions } from "@cyfora/numera";

function normalize(x: ArrayLike, opts: ReduceOptions = {}): NDArray {
  const a = np.asarray(x);
  return np.divide(np.subtract(a, np.mean(a, opts)), np.std(a, opts));
}
TypeMeaning
NDArraythe array class (a value as well as a type, so instanceof NDArray works)
ArrayLikeNDArray or nested JS arrays of numbers, booleans, bigints or complex values
OperandArrayLike or a single complex value, as accepted by ufuncs
NestedArraythe nested JS array returned by toArray()
Shapereadonly number[]
DType, DTypeName, DTypeLikea dtype object, a canonical name such as "float32", or anything np.dtype() accepts
Casting"no" | "equiv" | "safe" | "same_kind" | "unsafe"
ComplexLikeComplex or { re, im }
IndexSpec, SliceTuplearguments of get, slice and set
UfuncOptions, UfuncReduceOptions, UfuncAccumulateOptions, UfuncReduceatOptionsufunc options (out, where, dtype, casting, order, …)
ReduceOptions, VarOptions, ArgReduceOptionsoptions for sum, var/std, argmax/argmin
ArrayOptions, CreationOptions, LinspaceOptions, EyeOptions, LikeOptionscreation options
SvdResult, QrResult, EigResult, LstsqResultnp.linalg results
FftOptions, FftNOptions, FftNormnp.fft options
Seed, Sizenp.random seeds and output sizes
ErrMode, ErrSettings, ErrStateseterr / errstate

Results are typed as NDArray#

The element type of an array is a runtime property (a.dtype), not part of the TypeScript type. Functions return NDArray, and conversions return the general types:

TypeScript
const a: NDArray = np.arange(6).reshape(2, 3);
const nested = a.toArray();          // NestedArray
const flat = a.toTypedArray();       // a typed array, or BigInt64Array for int64
const x: number = np.sum(a).item() as number;

Check a.dtype.name (or a.dtype.kind) at runtime when a value's element type matters.

Errors#

The error classes are exported as values, so you can narrow with instanceof:

TypeScript
import np, { ShapeError, LinAlgError } from "@cyfora/numera";

try {
  np.linalg.inv(m);
} catch (e) {
  if (e instanceof LinAlgError) { /* singular */ }
  else throw e;
}

See Errors and floating-point.