Math.js is a powerful mathematics library for JavaScript that provides advanced mathematical capabilities beyond the native Math object. In this comprehensive guide, we'll explore the advanced features of math.js including matrix manipulations, symbolic computing, numerical methods, and practical applications.
Whether you're working on scientific computing, engineering calculations, or data analysis, this deep dive will help you leverage the full power of math.js in your projects.
$ npm install mathjs up to date in 295ms 30 packages are looking for funding run `npm fund` for details
Math.js version: 14.3.1 Library loaded successfully
Math.js provides powerful matrix operations that are essential for linear algebra, data analysis, and scientific computing. Let's explore some advanced matrix capabilities.
Creating different types of matrices: Matrix from arrays: [[1, 2], [3, 4]] Matrix from range and reshape: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 3x3 identity matrix: [[1, 0, 0], [0, 1, 0], [0, 0, 1]] Zeros matrix: [[0, 0, 0], [0, 0, 0]] Ones matrix: [[1, 1, 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]] A transpose: [[1, 3], [2, 4]]
Advanced matrix operations: Matrix M: [[9, 3, 2], [3, 8, 4], [2, 4, 7]] 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]] M * M^-1 (should be identity): [[0.9999999999999999, 5.551115123125783e-17, 0], [0, 1, 0], [0, 1.1102230246251565e-16, 1]] Eigenvalues: [3.41007767316509, 6.526744108201979, 14.06317821863293]
One of math.js's powerful features is symbolic mathematics - working with expressions and equations algebraically rather than just numerically.
Working with symbolic expressions: Expression: x ^ 2 + 2 * x + 3 Expression evaluated at x=4: 27 Original: x * y + x * z Simplified: x * (y + z) Original: sin(x) ^ 2 + cos(x) ^ 2 Simplified: sin(x) ^ 2 + cos(x) ^ 2
Symbolic differentiation: Original expression: x ^ 3 + 2 * x ^ 2 - 5 * x + 3 Derivative: 3 * x ^ 2 + 4 * x - 5 Derivative at x=2: 15 Expression: x ^ 2 * y + 3 * x * y ^ 2 ∂f/∂x: 2 * y * x + 3 * y ^ 2 ∂f/∂y: x ^ 2 + 6 * x * y ∂²f/∂x²: 2 * y
Math.js supports complex numbers, advanced operations, and different number formats for precise calculations.
Advanced complex number operations: z1 = 3 + 4i z2 = 1 - 2i z1 + z2 = 4 + 2i z1 * z2 = 11 - 2i z1 / z2 = -1 + 2i |z1| (magnitude): 5 arg(z1) (phase in radians): 0.9272952180016122 arg(z1) (phase in degrees): 53.13010235415598° Polar form of z1: 5 ∠ 0.9272952180016122 rad Complex from polar (5 ∠ π/4): 3.5355339059327378 + 3.5355339059327373i e^(i*π) = -1 ln(z1) = 1.6094379124341003 + 0.9272952180016122i
Complex analysis functions: sin(z1) = 3.853738037919377 - 27.016813258003932i cos(z1) = -27.034945603074224 - 3.851153334811777i tan(z1) = -0.00018734620462947842 + 0.9993559873814732i z1^2 = -6.999999999999997 + 24i sqrt(z1) = 2 + i (2+3i)^(1+i) = -0.8636068988831277 + 1.0368893969147763i Complex matrix: [[1 + i, 2 - i], [2i, 3]] Determinant of complex matrix: 1 - i Inverse of complex matrix: [[1.5 + 1.5i, -1.5 - 0.5i], [1 - i, i]]
Math.js provides various numerical methods for solving equations and optimization problems.
Solving equations numerically: Equation: x^3 - 6x^2 + 11x - 6 = 0 Root 1: 1 (f(root1) = 0) Root 2: 2.999999999970896 (f(root2) = -5.820766091346741e-11) Root 3: 4 (f(root3) = 6) Factored form check: (x-1)(x-2)(x-3) = x^3 - 6x^2 + 11x - 6
Numerical Integration: ∫ sin(x) dx from 0 to π ≈ 2.0000000000010805 (exact: 2.0) ∫ x^2 dx from 0 to 1 ≈ 0.33333333333333315 (exact: 1/3 ≈ 0.3333) ∫ e^(-x^2) dx from -2 to 2 ≈ 1.7641627815227583
Solving systems of linear equations: System of equations: 2x + y = 5 3x - 2y = 4 Solution (using inverse): [1.9999999999999998, 1] Solution (using LU decomposition): [[2], [1.0000000000000002]] Verification (A * x = b): [5, 3.999999999999999] Underdetermined system (more variables than equations): Equations: x + 2y + 3z = 7 4x + 5y + 6z = 8 Particular solution (using pseudoinverse): [-3.0555555555555642, 0.11111111111110683, 3.2777777777777772]
Math.js allows you to extend its functionality with custom functions and transformations.
Let's explore some practical applications of math.js in real-world problems.
Application: Linear Regression Slope (m): 1.4999999999999998 Intercept (b): 0.4400000000000013 R-squared: 0.9968102073365231 Predictions for new x values: x = 2.5, predicted y = 4.190000000000001 x = 6, predicted y = 9.44
Application: Statistical analysis Sample statistics (n=1000): Mean: 49.848103126421364 Median: 49.92090757521336 Standard Deviation: 9.579327666021364 Range: [16.081342666086776, 79.81270439924904] Percentiles: 25th percentile: 43.47979603687525 50th percentile: 49.92090757521336 75th percentile: 56.1182468610788 90th percentile: 62.220846622312045 95th percentile: 65.42574749322438
When working with math.js in production or with large datasets, performance optimization becomes important.
Performance optimization with custom configuration: Created minimal math instance with only basic functions Result of 2+3: 5 Benchmark: Full vs. minimal library for simple operations: Full library: 315.68 ms for 100000 iterations Minimal library: 246.87 ms for 100000 iterations Performance ratio: 1.28x
Using compiled expressions for better performance: Regular evaluation: 158.79 ms Compiled evaluation: 13.94 ms Speedup: 11.39x Recommendations: - Use compile() when evaluating the same expression multiple times - Use evaluate() for one-time expressions - Create custom math.js instances for specific applications - Consider native JavaScript for very simple operations
Let's review some best practices and common issues when working with math.js.
Use the right number type for your application:
Optimize for performance:
Error handling:
Memory management:
Mixing number types: Calculations involving different types (like BigNumber and regular numbers) can give unexpected results
Equality checks: Math.js objects often need special equality functions rather than ===
Performance expectations: Some operations (like symbolic computation) can be much slower than numerical equivalents
Matrix indexing: Math.js uses 0-based indexing, which can differ from mathematical conventions
Operator precedence: Math.js follows standard precedence rules which might differ from some other systems
Example of a common pitfall: Mixing number types Different representations of 0.1: Regular JavaScript: 0.1 BigNumber: 0.1 Fraction: 0.1 Direct addition: 0.2
In this deep dive, we've explored the advanced capabilities of math.js including:
Math.js provides a comprehensive mathematical toolkit for JavaScript developers, enabling sophisticated numerical and symbolic computation directly in JavaScript environments. Whether you're building scientific applications, data analysis tools, or financial calculators, math.js offers the advanced mathematical capabilities you need.
For more information, check out the official math.js documentation and GitHub repository.