# math.js — Extensive Math Library for JavaScript > A comprehensive math library for JavaScript and Node.js with expression parsing, units, matrices, and big numbers. ## Install Save as a script file and run: # math.js — Extensive Math Library for JavaScript ## Quick Use ```bash npm install mathjs ``` ```js import { evaluate, sqrt, matrix, multiply } from 'mathjs'; evaluate('12 * (2 + 4.1)'); // => 73.2 sqrt(-4).toString(); // => '2i' multiply(matrix([[1,2],[3,4]]), matrix([[5],[6]])); // => [[17],[39]] ``` ## Introduction math.js is a comprehensive mathematics library for JavaScript and Node.js. It provides a flexible expression parser, supports a wide range of data types including complex numbers, big numbers, fractions, units, and matrices, and offers hundreds of built-in functions. It serves as the go-to math library when native JavaScript arithmetic is not enough. ## What math.js Does - Parses and evaluates mathematical expressions from strings - Supports complex numbers, fractions, big numbers, and units - Provides matrix and linear algebra operations - Includes a symbolic computation engine for simplification and derivatives - Offers both a chained API and a functional programming style ## Architecture Overview math.js is built around a dependency injection system that allows tree-shaking unused functions. The expression parser tokenizes input into an AST, which is then compiled into an efficient evaluation function. Data types like BigNumber, Complex, Unit, and Matrix are first-class objects with operator overloading. The library supports custom function and constant registration, making it extensible for domain-specific use cases. ## Self-Hosting & Configuration - Install via npm: `npm install mathjs` - Import the full library or use `create()` with specific factories for smaller bundles - Configure precision for big numbers via `math.config({ number: 'BigNumber', precision: 64 })` - Register custom functions with `math.import({ myFunc: (x) => x * 2 })` - Use in the browser via bundlers or a CDN script tag ## Key Features - Expression parser with variable scope and function definitions - Arbitrary precision arithmetic via BigNumber support - Physical unit conversions (meters, joules, seconds, etc.) - Sparse and dense matrix types with decomposition algorithms - Chainable API for readable mathematical pipelines ## Comparison with Similar Tools - **decimal.js** — focused on arbitrary precision decimals; math.js wraps it and adds matrices, units, and parsing - **NumPy (Python)** — the Python equivalent; math.js brings similar capabilities to JavaScript - **algebra.js** — limited to polynomial algebra; math.js covers far more ground - **numbers.js** — smaller scope with basic stats and calculus; math.js is more comprehensive ## FAQ **Q: Can I use math.js for unit conversions?** A: Yes. Use `math.unit('5 inch').to('cm')` to convert between physical units. **Q: Does math.js support big number arithmetic?** A: Yes. Configure the library to use BigNumber as the default number type for arbitrary precision calculations. **Q: How do I evaluate user-supplied expressions safely?** A: Use a limited parser scope and avoid exposing dangerous functions. The expression parser does not execute arbitrary JavaScript. **Q: Can I tree-shake unused functions?** A: Yes. Use `create()` with only the factories you need to produce a smaller bundle. ## Sources - https://github.com/josdejong/mathjs - https://mathjs.org/docs/ --- Source: https://tokrepo.com/en/workflows/asset-fc0e56ce Author: Script Depot