This cheatsheet provides a convenient reference for the most commonly used functions in math.js, a comprehensive mathematics library for JavaScript. Whether you're working on scientific computing, data analysis, engineering applications, or simply need to perform mathematical operations beyond what JavaScript natively offers, this reference will help you quickly find the right functions.
$ npm install mathjs added 9 packages in 3s 30 packages are looking for funding run `npm fund` for details
Math.js version: 14.3.1
Quick reference for basic arithmetic operations in math.js.
Basic arithmetic: Addition: math.add(2, 3) = 5 Subtraction: math.subtract(7, 3) = 4 Multiplication: math.multiply(4, 5) = 20 Division: math.divide(15, 3) = 5 Modulo: math.mod(8, 3) = 2 Power: math.pow(2, 8) = 256 Square root: math.sqrt(16) = 4 Cube root: math.cbrt(27) = 3 Absolute value: math.abs(-4.5) = 4.5
Rounding functions: Round: math.round(3.7) = 4 Floor: math.floor(3.7) = 3 Ceiling: math.ceil(3.2) = 4 Fix (toward zero): math.fix(-3.7) = -3 Round to 2 decimal places: math.round(3.14159, 2) = 3.14
Evaluating expressions: Simple expression: math.evaluate('2 + 3 * 4') = 14 With variables: math.evaluate('x^2 + y', {x: 3, y: 4}) = 13 With units: math.evaluate('5 cm + 2 inch') = 10.08 cm
Common trigonometric functions for angles in radians and degrees.
Trigonometric functions (radians): sin(π/2) = 1 cos(π) = -1 tan(π/4) = 0.9999999999999999 asin(1) = 1.5707963267948966 radians = 1.571 radians acos(0) = 1.5707963267948966 radians = 1.571 radians atan(1) = 0.7853981633974483 radians = 0.7854 radians
Trigonometric functions (degrees): sin(90°) = 1 cos(180°) = -1 tan(45°) = 0.9999999999999999 sin(90 deg) = 1 cos(180 deg) = -1 tan(45 deg) = 0.9999999999999999
Hyperbolic functions: sinh(1) = 1.1752011936438014 cosh(1) = 1.5430806348152437 tanh(1) = 0.7615941559557649 asinh(1) = 0.881373587019543 acosh(2) = 1.3169578969248166 atanh(0.5) = 0.5493061443340548
Quick reference for exponential and logarithmic operations.
Exponential and logarithmic functions: Exponential (e^x): math.exp(2) = 7.38905609893065 Natural logarithm: math.log(10) = 2.302585092994046 Base-10 logarithm: math.log10(100) = 2 Base-2 logarithm: math.log2(8) = 3 Logarithm base 3: math.log(27, 3) = 3.0000000000000004 Value of e = 2.718281828459045 Value of π = 3.141592653589793 Natural log of e = 1
Essential matrix and vector operations for linear algebra applications.
Creating matrices and vectors: Matrix 1: [[1, 2], [3, 4]] Vector 1: [5, 6] 3x3 Identity matrix: [[1, 0, 0], [0, 1, 0], [0, 0, 1]] 2x3 Zero matrix: [[0, 0, 0], [0, 0, 0]] 2x2 Ones matrix: [[1, 1], [1, 1]]
Basic matrix operations: A + B: [[6, 8], [10, 12]] A - B: [[-4, -4], [-4, -4]] A * B (matrix product): [[19, 22], [43, 50]] A .* B (element-wise): [[5, 12], [21, 32]] A^2: [[7, 10], [15, 22]] Transpose of A: [[1, 3], [2, 4]]
Advanced matrix operations: Determinant of M: 313 Inverse of M: [[0.12779552715654952, -0.04153354632587859, -0.012779552715654952], [-0.04153354632587859, 0.18849840255591055, -0.09584664536741215], [-0.012779552715654952, -0.09584664536741214, 0.2012779552715655]] Eigenvalues: [3.41, 6.53, 14.1] Solution to Ax = b: [[1], [2.5]]
Quick reference for statistical functions and probability.
Basic statistics functions: Data: [2,4,6,8,10,12] Mean: 7 Median: 7 Mode: 2,4,6,8,10,12 Standard deviation: 3.7416573867739413 Variance: 14 Min: 2 Max: 12 Sum: 42 Product: 46080
Percentiles and correlation: 25th percentile: 4.5 50th percentile (median): 7 75th percentile: 9.5 IQR: 5
Probability functions: C(7,3) = 35 P(7,3) = 210 5! = 120 Gamma(4.5) = 11.631728396567446 Random number: 0.5522813403776621 Random integer between 1 and 10: 2
Quick reference for unit handling and conversions.
Working with units: Length 1: 5 cm Length 2: 2 inch Mass: 35.5 kg Time: 45 min Temperature: 22 degC
Unit conversions: 5 cm = 50 mm 2 inch = 5.08 cm 35.5 kg = 78.26410307563154 lb 45 min = 2700 s 22 degC = 71.6 degF 10 m/s in km/h: 36 km / h 100 kg * 9.8 m/s^2 in Newtons: 980.0000000000001 N
Arithmetic with units: 5 cm + 2 inch = 10.08 cm 5 cm * 2 inch = 2540.0000000000005 mm^2 2540.0000000000005 mm^2 = 25.4 cm2 Speed: 100 km / 2 h = 50 km / h 50 km / h = 13.88888888888889 m / s
Quick reference for different number types in math.js: BigNumber, Fractions, and Complex numbers.
Working with BigNumber: Regular JS: 0.1 + 0.2 = 0.30000000000000004 BigNumber: 0.1 + 0.2 = 0.3 10^20 = 100000000000000000000
Working with fractions: Fraction 1: 0.(3) Fraction 2: 0.4 Fraction 3: 0.(285714) 0.(3) + 0.4 = 0.7(3) 0.(3) * 0.(285714) = 0.(095238) 0.4 / 0.(285714) = 1.4 0.75 as fraction: 0.75 0.33333 as fraction: 0.33333
Working with complex numbers: Complex 1: 2 + 3i Complex 2: 4 - 2i 2 + 3i + 4 - 2i = 6 + i 2 + 3i * 4 - 2i = 14 + 8i abs(2 + 3i) = 3.605551275463989 arg(2 + 3i) = 0.982793723247329 radians sqrt(-4) = 2i e^(i*π) = -1
Quick reference for parsing and evaluating mathematical expressions.
Expression evaluation: '2 + 3 * 4' = 14 'sqrt(16) + 2^3' = 12 'x^2 + y^2' with x=3, y=4 = 25 'x = x + 1' with x=3 = 4 After update, x = 4 Multi-line calculation result: 5,50,75,15
Parsing and compiling expressions: Expression: x ^ 2 + 2 * x + 1 Evaluating expression for different x values: x = 0, result = 1 x = 1, result = 4 x = 2, result = 9 x = 3, result = 16 x = 4, result = 25 x = 5, result = 36 Derivative: 2 * (x + 1) Simplified: x ^ 2 + 2 * x + 1
Summary of the most commonly used math.js functions.
Function | Description | Example |
---|---|---|
add(x, y) | Addition | math.add(2, 3) = 5 |
subtract(x, y) | Subtraction | math.subtract(5, 2) = 3 |
multiply(x, y) | Multiplication | math.multiply(2, 3) = 6 |
divide(x, y) | Division | math.divide(6, 2) = 3 |
pow(x, y) | Power | math.pow(2, 3) = 8 |
sqrt(x) | Square root | math.sqrt(9) = 3 |
abs(x) | Absolute value | math.abs(-5) = 5 |
Function | Description | Example |
---|---|---|
sin(x) | Sine | math.sin(math.pi/2) = 1 |
cos(x) | Cosine | math.cos(0) = 1 |
tan(x) | Tangent | math.tan(math.pi/4) = 1 |
asin(x) | Arc sine | math.asin(1) = π/2 |
acos(x) | Arc cosine | math.acos(1) = 0 |
atan(x) | Arc tangent | math.atan(1) = π/4 |
Function | Description | Example |
---|---|---|
mean(array) | Average | math.mean([1,2,3]) = 2 |
median(array) | Middle value | math.median([1,2,3]) = 2 |
std(array) | Standard deviation | math.std([2,4,6]) = 2 |
min(array) | Minimum value | math.min([4,2,7]) = 2 |
max(array) | Maximum value | math.max([4,2,7]) = 7 |
quantileSeq(array, prob) | Quantile | math.quantileSeq([1,2,3,4], 0.5) = 2.5 |
Function | Description | Example |
---|---|---|
matrix() | Create matrix | math.matrix([[1,2],[3,4]]) |
det(matrix) | Determinant | math.det([[1,2],[3,4]]) = -2 |
inv(matrix) | Inverse | math.inv([[1,2],[3,4]]) |
transpose(matrix) | Transpose | math.transpose([[1,2],[3,4]]) |
subset(matrix, index) | Extract subset | math.subset(matrix, math.index(0, 1)) |
Function | Description | Example |
---|---|---|
factorial(n) | Factorial | math.factorial(5) = 120 |
gamma(n) | Gamma function | math.gamma(5) = 24 |
log(x, [base]) | Logarithm | math.log(1000, 10) = 3 |
random([min, max]) | Random number | math.random(0, 10) |
distance(x, y) | Distance | math.distance([0,0], [3,4]) = 5 |
Here are some common patterns and tips when working with math.js:
Chain operations for cleaner code:
math.chain(3)
.add(4)
.multiply(2)
.done() // Returns 14
Selective importing for better performance:
const { add, multiply, sin } = require('mathjs');
Use compiled expressions for repeated evaluations:
const expr = math.compile('x^2 + y');
// Then use expr.evaluate({x: 3, y: 4}) multiple times
Format output with precision control:
math.format(math.pi, {precision: 4}) // Returns '3.142'
Use the right number type for your application:
Convert between types when needed:
math.number(math.bignumber('0.1')) // BigNumber to number
math.fraction(0.25) // Number to fraction
Handle expressions safely with try-catch:
try {
const result = math.evaluate(userInput);
} catch (error) {
console.log('Invalid expression');
}