Numeric.js for Numerical Analysis and Linear Algebra in JavaScript

Numeric.js is a JavaScript library that provides a range of numerical computing and linear algebra/matrix computing functionalities.

Table of Contents

Numeric.js is an old but well built library that provides a convenient and efficient way to perform numerical computations in JavaScript. It aims to bridge the gap between JavaScript and numerical computing, enabling developers to work with complex mathematical operations directly in the browser or in Node.js applications. The library is well-documented, actively maintained, and has a large user community, making it a reliable choice for numerical computing tasks in JavaScript.

Key Features of Numeric.js:

  1. Matrix Operations: Numeric.js allows you to perform essential matrix operations like addition, subtraction, multiplication, and inversion. It supports both dense and sparse matrices, enabling efficient handling of large datasets.

  2. Linear Algebra: The library provides functions for solving linear systems of equations, computing eigenvalues and eigenvectors, performing singular value decomposition (SVD), and solving least-squares problems. These operations are essential for many mathematical and scientific computations.

  3. Interpolation: Numeric.js supports various interpolation methods, including linear, polynomial, and spline interpolation. Interpolation is useful for estimating values between known data points, smoothing data, and generating continuous functions from discrete data.

  4. Numerical Optimization: Numeric.js includes optimization algorithms such as Newton’s method, conjugate gradient, and simulated annealing. These algorithms allow you to find the minimum or maximum of a function, which is valuable for parameter estimation, curve fitting, and optimization problems.

  5. Statistics: The library provides statistical functions for computing mean, variance, standard deviation, correlation, covariance, and more. These functions are helpful in analyzing data and extracting meaningful insights.

  6. Special Functions: Numeric.js offers a range of special mathematical functions, including trigonometric functions, exponential and logarithmic functions, gamma function, Bessel functions, and more. These functions are commonly used in scientific and engineering applications.

  7. Utilities: Numeric.js provides various utility functions for array manipulation, rounding, formatting numbers, random number generation, and other common numerical operations.

Examples of Numeric.js #

Here are some of the key functionalities offered by numeric.js along with examples:

Numeric Computations: Numeric.js provides various mathematical functions, including trigonometric, exponential, logarithmic, and special functions.

const x = 2;
const sinValue = numeric.sin(x);
const expValue = numeric.exp(x);
const gammaValue = numeric.gamma(x);
// sinValue = 0.909
// expValue = 7.389
// gammaValue = 1

For numeric functions in numeric.js, x can be a scalar number or an array.

Matrix Operations and Linear Algebra: Numeric.js allows you to perform various matrix operations, such as matrix addition, subtraction, multiplication, and inversion.

const A = [[1, 2], [3, 4]];
const B = [[5, 6], [7, 8]];

const C = numeric.add(A, B);
// C = [[6, 8], [10, 12]]

const D = numeric.dot(A, B);
// D = [[19, 22], [43, 50]]

const E = numeric.inv(A);
// E = [[-2, 1], [1.5, -0.5]]

You can solve linear systems of equations using the numeric.solve() function.

const A = [[2, 3], [4, -1]];
const b = [7, 1];

const x = numeric.solve(A, b);
// x = [2, -1]

Eigenvalues and Eigenvectors: Numeric.js allows you to compute eigenvalues and eigenvectors of a matrix using the numeric.eig() function

const A = [[2, -1], [4, 3]];

const { lambda, E } = numeric.eig(A);
// lambda = [4, 1]
// E = [[0.707, 0.447], [0.707, -0.894]]

Singular Value Decomposition (SVD): Numeric.js provides SVD functionality through the numeric.svd() function.

const A = [[1, 2], [3, 4], [5, 6]];

const { U, S, V } = numeric.svd(A);
// U = [[-0.229, 0.883], [-0.524, 0.240], [-0.819, -0.402]]
// S = [9.525, 0.514]
// V = [[-0.619, -0.785], [-0.785, 0.619]]

Cubic Spline Interpolation Interpolation: Numeric.js supports spline interpolation, using the numeric.spline() function. Cubic spline interpolation is a commonly used method for smooth interpolation between data points.

// Data points
const x = [0, 1, 2, 3, 4];
const y = [1, 4, 7, 12, 21];

// Create a cubic spline function
const spline = numeric.spline(x, y);

// Evaluate the spline at a specific point
const interpolatedValue = spline.at(2.5);
console.log(interpolatedValue);