Numeric JavaScript
Numeric JavaScript is a legacy library for performing numerical analysis within JavaScript environments, including matrix operations, solving linear systems, finding eigenvalues, and optimization problems. Despite its capabilities, the package's last stable version (1.2.6) was published in December 2012, indicating it is no longer actively maintained. Its core differentiators were performing complex calculations directly in the browser, reducing server load. Due to its age, it lacks modern JavaScript features, performance optimizations, and official TypeScript support, making it generally unsuitable for new projects. Developers seeking numerical analysis in contemporary JavaScript should consider actively maintained alternatives like `math.js` or `ndarray`.
Common errors
-
TypeError: numeric.dot is not a function
cause The 'numeric' object was not correctly imported or is not available in the current scope, or the `numeric.js` script failed to load in the browser.fixEnsure `const numeric = require('numeric');` is at the top of your Node.js file, or that `<script src="numeric.js"></script>` is loaded before your code in a browser. Verify the file path is correct. -
ReferenceError: numeric is not defined
cause The 'numeric' library was not successfully loaded or imported before being used.fixFor Node.js, ensure `require('numeric')` is used to import the module. For browser usage, confirm the `<script>` tag pointing to `numeric.js` is correctly placed and loaded, and the `numeric` global object is available. Check for typos in variable names. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES Module `import` syntax with the CommonJS-only `numeric` package in an environment not configured for ESM or without a bundler to transpile.fixReplace `import numeric from 'numeric';` with `const numeric = require('numeric');`. If you must use ESM syntax, ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`) and your bundler can handle CJS interoperability.
Warnings
- breaking The 'numeric' package is effectively abandoned, with its last update over a decade ago. It lacks active maintenance, bug fixes, security patches, or support for modern JavaScript features and environments.
- gotcha As an older package, 'numeric' is built using CommonJS modules. Attempting to import it directly using ES module syntax (`import ... from 'numeric'`) in modern ESM-only environments will result in runtime errors.
- gotcha JavaScript's `Number` type uses 64-bit floating-point representation (IEEE 754), which inherently leads to precision issues for certain decimal arithmetic (e.g., `0.1 + 0.2 !== 0.3`). This is critical in numerical analysis where exactness is often required.
- gotcha The JavaScript `Number` type has limitations for representing large integers reliably (up to `Number.MAX_SAFE_INTEGER`). Calculations exceeding this range can lead to silent data corruption or incorrect results.
- gotcha The library does not provide official TypeScript type definitions. Integrating 'numeric' into a TypeScript project will either require manually writing declaration files (`.d.ts`) or relying on potentially outdated community-maintained `@types/numeric` packages.
Install
-
npm install numeric -
yarn add numeric -
pnpm add numeric
Imports
- numeric
import numeric from 'numeric';
const numeric = require('numeric'); - numeric (browser global)
<script src="path/to/numeric.js"></script> // 'numeric' object is now globally available
- Type definitions
// No official types are shipped. Consider community-maintained @types/numeric or manual declaration.
Quickstart
const numeric = require('numeric');
// 1. Matrix Multiplication (dot product)
const A = [[1, 2], [3, 4]];
const B = [[5, 6], [7, 8]];
const C = numeric.dot(A, B);
console.log('Matrix C (A * B):', C); // Expected: [[19, 22], [43, 50]]
// 2. Identity Matrix
const I = numeric.identity(3);
console.log('3x3 Identity Matrix:', I);
// 3. Solving a Linear System (Ax = b)
// A = [[2, 1], [1, 3]], b = [4, 7]
const solve_A = [[2, 1], [1, 3]];
const solve_b = [4, 7];
const x = numeric.solve(solve_A, solve_b);
console.log('Solution x for Ax=b:', x); // Expected: [1, 2]
// 4. Eigenvalues (for a symmetric matrix)
const eigen_matrix = [[2, 0], [0, 3]];
const eigenvalues = numeric.eig(eigen_matrix);
// The real parts of the eigenvalues (lambda.x) and eigenvectors (E.x)
console.log('Eigenvalues (real parts):', eigenvalues.lambda.x); // Expected: [2, 3]
console.log('Eigenvectors (real parts):', eigenvalues.E.x);