Math.js is a powerful JavaScript library that extends the built-in Math capabilities with support for matrices, complex numbers, units, symbolic computation, and much more. Whether you're building a scientific calculator, data visualization tool, or need to perform mathematical operations in your web application, math.js provides the tools you need.
This notebook introduces the fundamental operations and features of math.js to get you started with mathematical computing in JavaScript.
$ npm install mathjs added 9 packages in 3s 30 packages are looking for funding run `npm fund` for details
Math.js library loaded successfully
Let's start with basic operations. Math.js provides functions for all standard math operations, offering more precision and features than JavaScript's built-in math operators.
Basic arithmetic operations: Addition: 5 Subtraction: 4 Multiplication: 22 Division: 5 Complex expression (2 + 3) * 4 / 2 = 10
Advanced operations: Square root of 16: 4 Power: 2^8 = 256 Exponential: e^2 = 7.38905609893065 Logarithm (base 10) of 1000: 3 Natural logarithm of 10: 2.302585092994046 Sine of 90° (in radians): 1 Cosine of 0°: 1 Tangent of 45° (in radians): 0.9999999999999999
One of math.js's strengths is its ability to work with various number types including fractions, complex numbers, and big numbers for arbitrary precision.
Working with fractions: Fraction 1: 0.(3) Fraction 2: 0.4 Addition: 0.7(3) Multiplication: 0.1(3) 0.75 as a fraction: 0.75
Working with complex numbers: Complex number 1: 3 + 4i Complex number 2: 1 - 2i Addition: 4 + 2i Multiplication: 11 - 2i Magnitude of 3 + 4i: 5 (3 + 4i) * (1 - 2i) = 11 - 2i
Math.js provides powerful tools for working with matrices and vectors, essential for linear algebra, machine learning, and data science applications.
Creating matrices and vectors: Matrix 1: [[1, 2, 3], [4, 5, 6]] Vector 1: [7, 8, 9] Matrix dimensions: 2 x 3 Is matrix? DenseMatrix
Matrix operations: Matrix 2: [[1, 2], [3, 4], [5, 6]] Matrix sum (1 + 3): [[2, 3, 4], [6, 7, 8]] Matrix product (1 x 2): [[22, 28], [49, 64]] Transposed matrix 1: [[1, 4], [2, 5], [3, 6]]
Vector operations and linear algebra: Vector sum: [5, 7, 9] Dot product: 32 Cross product: [-3, 6, -3] Determinant: -2 Solution to the system of equations: [[1], [2]]
Math.js has built-in support for units and unit conversions, making it great for scientific and engineering applications.
Working with units: Distance: 5 km Time: 1 hour Distance in miles: 3.1068559611866697 miles Time in minutes: 60 minutes Speed: 5 km / hour Speed in miles per hour: 3.1068559611866697 mi / h
More unit operations: 25 celsius = 77 fahrenheit 1 liter = 0.2641720523581484 gallons Area: 50 m^2 Area in square feet: 538.195520835486 sqft
Math.js supports symbolic math operations, allowing you to work with algebraic expressions and perform operations like differentiation and simplification.
Working with symbolic expressions: Expression: x ^ 2 + 2 * x + 1 When x = 2: 9 When x = -1: 0 Simplified expression: x ^ 2 + 2 * x * y + y ^ 2
Now let's look at some practical applications combining multiple math.js features.
Solving a quadratic equation (ax² + bx + c = 0): Roots of x² + 2x + 5 = 0: -1 + 2i, -1 - 2i Roots of x² - 5x + 6 = 0: 3, 2
Let's wrap up with some performance tips and best practices for working with math.js.
Selective imports: For better performance, import only the functions you need.
const { add, multiply } = require('mathjs');
Compile expressions: If you're evaluating the same expression multiple times with different values, use .compile()
for better performance.
Use appropriate number types:
Array vs. Matrix: Use regular arrays for simple operations and math.js matrices for more complex linear algebra operations.
Chain operations: Use method chaining where possible for cleaner code.
Reuse objects: Creating new objects for each calculation can be expensive; reuse when possible.
Error handling: Always implement proper error handling, as mathematical operations can fail in unexpected ways.
Performance comparison: Non-compiled evaluation: 1365.255 ms Compiled evaluation: 120.855 ms
In this notebook, we've explored the fundamentals of the math.js library:
Math.js provides an extensive set of tools for mathematical operations in JavaScript, making it suitable for a wide range of applications from simple calculations to complex scientific computing. Its ability to handle different number types, symbolic expressions, units, and matrices makes it one of the most versatile math libraries available for JavaScript.
For more information and advanced usage, check out the official documentation.