What is MathJS ?

Math.js is an extensive math library for JavaScript and NodeJS. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices. Powerful and easy to use.

Weekly Downloads 284.285

Features

  • Supports numbers, big numbers, complex numbers, fractions, units, strings, arrays, and matrices.
  • Is compatible with JavaScript's built-in Math library.
  • Contains a flexible expression parser.
  • Does symbolic computation.
  • Comes with a large set of built-in functions and constants.
  • Can be used as a command line application as well.
  • Runs on any JavaScript engine.
  • Is easily extensible.
  • Open source.

Usage

Math.js can be used in both node.js and in the browser.

Install math.js using npm:

npm install mathjs

Note that when using mathjs in a TypeScript project, you will have to install type definition files too:

npm install @types/mathjs

Or Math.js can be downloaded or linked from various content delivery networks:

CDN Url
unpkg https://unpkg.com/mathjs@9.2.0/
cdnjs https://cdnjs.com/libraries/mathjs
jsDelivr https://www.jsdelivr.com/package/npm/mathjs
PageCDN https://pagecdn.com/lib/mathjs

Math.js web service

Math.js is available as a RESTful API. Evaluations can be done via GET or POST requests. The duration per evaluation of this free service is limited to 10 seconds, with a maximum of 10,000 requests per day.
See more on api.mathjs.org

Extensions

Here some notable extensions for mathjs:
Extension Description
mathsteps A step-by-step math solver library that is focused on pedagogy (how best to teach). The math problems it focuses on are pre-algebra and algebra problems involving simplifying expressions.
mathjs‑expression‑parser This custom build of mathjs contains just the expression parser and basic arithmetic functions for numbers. About four times as small as the full mathjs library.
mathjs-simple-integral Extends Math.js to be able to compute simple integrals.
math.diff.js Symbolic differentiation plugin for Math.js
postcss-math PostCSS plugin for making calculations with math.js

Browser support

Math.js works on any ES5 compatible JavaScript engine: node.js, Chrome, Firefox, Safari, Edge, and IE11.

Mathjs can be used similar to JavaScript's built-in Math library.
Besides that, math.js can evaluate expressions and supports chained operations.


import {
  atan2, chain, derivative, e, evaluate, log, pi, pow, round, sqrt
} from 'mathjs'

// functions and constants
round(e, 3)                    // 2.718
atan2(3, -3) / pi              // 0.75
log(10000, 10)                 // 4
sqrt(-4)                       // 2i
pow([[-1, 2], [3, 1]], 2)      // [[7, 0], [0, 7]]
derivative('x^2 + x', 'x')     // 2 * x + 1

// expressions
evaluate('12 / (2.3 + 0.7)')   // 4
evaluate('12.7 cm to inch')    // 5 inch
evaluate('sin(45 deg) ^ 2')    // 0.5
evaluate('9 / 3 + 2i')         // 3 + 2i
evaluate('det([-1, 2; 3, 1])') // -7

// chaining
chain(3)
    .add(4)
    .multiply(2)
    .done()  // 14

Mathjs Examples

Learn how to use mathjs by viewing and forking example apps that make use of mathjs on CodeSandbox.

Source code

The source code of math.js is available on GitHub: https://github.com/josdejong/mathjs.

License

Math.js is open source and licensed under the Apache 2.0 License

Coding Math

Coding Math is a video series aimed at demonstrating useful math principles using code. It also aims to help you learn the math to use to accomplish various tasks in your programs.

See more Episodes of Coding Math https://www.youtube.com


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *