Linear algebra operations are essential for many applications including machine learning, computer graphics, physics simulations, and data analysis. Math.js offers powerful capabilities for working with matrices and vectors in JavaScript. This notebook provides a comprehensive guide to performing matrix and vector operations using the math.js library.
We'll explore everything from creating matrices and basic operations to advanced techniques like eigenvalue decomposition and solving linear systems.
$ npm install mathjs added 9 packages in 7s 30 packages are looking for funding run `npm fund` for details
Math.js version: 14.3.1
Let's start by exploring different ways to create matrices and vectors in math.js.
Creating matrices from arrays: 2x3 matrix: [[1, 2, 3], [4, 5, 6]] 3x3 matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Row vector: [[1, 2, 3, 4]] Column vector: [[1], [2], [3], [4]]
Creating special matrices: 3x3 identity matrix: [[1, 0, 0], [0, 1, 0], [0, 0, 1]] 2x4 zero matrix: [[0, 0, 0, 0], [0, 0, 0, 0]] 3x2 ones matrix: [[1, 1], [1, 1], [1, 1]] Diagonal matrix from vector [1, 2, 3]: 1,0,0,0,2,0,0,0,3 Diagonal extracted from 3x3 matrix: [1, 5, 9]
Other matrix creation methods: Matrix from range 1-9 reshaped to 3x3: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 2x3 random matrix (values between 0-1): 0.7886473348025803,0.24767441870923387,0.16474275628797758,0.5158762351334484,0.6405172826627917,0.6645898070793942 3x3 matrix filled with 5s: [[5, 5, 5], [5, 5, 5], [5, 5, 5]]
Now let's look at the fundamental matrix operations: addition, subtraction, and multiplication.
Matrix addition and subtraction: Matrix A: [[1, 2], [3, 4]] Matrix B: [[5, 6], [7, 8]] A + B: [[6, 8], [10, 12]] A - B: [[-4, -4], [-4, -4]] 2 * A: [[2, 4], [6, 8]]
Matrix multiplication: Matrix C (2x3): [[1, 2, 3], [4, 5, 6]] Matrix D (3x2): [[7, 8], [9, 10], [11, 12]] C * D (2x2): [[58, 64], [139, 154]] A .* B (element-wise): [[5, 12], [21, 32]] A^2: [[7, 10], [15, 22]]
Let's explore common matrix transformations such as transpose, inverse, and determinant calculations.
Matrix transpose: Matrix E (2x3): [[1, 2, 3], [4, 5, 6]] E transposed (3x2): [[1, 4], [2, 5], [3, 6]] Transpose of transpose (2x3): [[1, 2, 3], [4, 5, 6]]
Matrix inverse and determinant: Matrix F: [[4, 7], [2, 6]] Determinant of F: 10 Inverse of F: [[0.6, -0.7], [-0.2, 0.4]] F * F^(-1) (should be identity): [[0.9999999999999998, 4.440892098500626e-16], [-2.220446049250313e-16, 1.0000000000000004]] Singular matrix: [[1, 2], [2, 4]] Determinant: 0 Error computing inverse: Cannot calculate inverse, determinant is zero
Matrix trace and norm: Matrix G: [[3, 1, 4], [1, 5, 9], [2, 6, 5]] Trace of G: 13 Frobenius norm of G: 14.071247279470288 1-norm of G: 18 Infinity-norm of G: 15
Math.js provides several matrix decomposition methods that are useful for solving linear systems, eigenvalue problems, and other applications.
LU Decomposition: Matrix H: [[2, 1, 1], [4, -6, 0], [-2, 7, 2]] L (Lower triangular): [[1, 0, 0], [0.5, 1, 0], [-0.5, 1, 1]] U (Upper triangular): [[4, -6, 0], [0, 4, 1], [0, 0, 1]] P (Permutation vector): [ 1, 0, 2 ] L*U: [[4, -6, 0], [2, 1, 1], [-2, 7, 2]]
Math.js provides methods to solve systems of linear equations.
Solving linear equations: Coefficient matrix A: [[2, 1], [3, 2]] Constants vector b: [[5], [8]] Solution x: [[2.0000000000000004], [0.9999999999999993]] Verification (A*x): [[5], [8]] Solution using inverse: [[2], [1]]
Solving a larger system of equations: Matrix A: [[3, 2, -1], [2, -2, 4], [-1, 0.5, -1]] Vector b: [[1], [-2], [0]] Solution x: [[1], [-1.9999999999999996], [-1.9999999999999993]] A*x (should equal b): [[1.0000000000000002], [-1.9999999999999982], [-4.440892098500626e-16]] Attempting to solve an inconsistent system: Singular matrix: [[1, 2, 3], [2, 4, 6], [3, 6, 9]] Inconsistent b: [[1], [3], [3]] Error solving inconsistent system: Linear system cannot be solved since matrix is singular
Let's explore operations specific to vectors.
Vector operations: Vector v1: [ 1, 2, 3 ] Vector v2: [ 4, 5, 6 ] v1 + v2: [ 5, 7, 9 ] v1 - v2: [ -3, -3, -3 ] 2 * v1: [ 2, 4, 6 ] v1 · v2 = 32 |v1| = 3.7416573867739413 Unit vector of v1: [ 0.2672612419124244, 0.5345224838248488, 0.8017837257372732 ] Magnitude of unit vector: 1
Cross product: Vector a: [ 1, 0, 0 ] Vector b: [ 0, 1, 0 ] a × b: [ 0, 0, 1 ] b × a: [ 0, 0, -1 ] Area of parallelogram: 1 Angle between a and b: 1.5707963267948966 radians (90 degrees)
Let's explore some practical applications of matrix and vector operations.
Linear regression using matrices: Data points: (1, 2) (2, 3.9) (3, 6.1) (4, 7.8) (5, 9.9) Design matrix X: [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5]] Target vector y: [2, 3.9, 6.1, 7.8, 9.9] X^T * X: [[5, 15], [15, 55]] X^T * y: [29.700000000000003, 108.8] Beta coefficients [b, m]: [[0.030000000000001137], [1.9699999999999995]] Regression line: y = 1.9700x + 0.0300 Predictions using the regression model: x = 1.5, predicted y = 2.9850 x = 3.5, predicted y = 6.9250 x = 6, predicted y = 11.8500
2D transformations with matrices: Original point (x, y, 1): [[3], [2], [1]] Translation matrix: [[1, 0, 2], [0, 1, 3], [0, 0, 1]] Rotation matrix (45°): [[0.7071067811865476, -0.7071067811865475, 0], [0.7071067811865475, 0.7071067811865476, 0], [0, 0, 1]] Scaling matrix: [[2, 0, 0], [0, 0.5, 0], [0, 0, 1]] Translated point: [[5], [5], [1]] Rotated point: [[0.7071067811865479], [3.5355339059327378], [1]] Scaled point: [[6], [1], [1]] Combined transformation matrix: [[1.4142135623730951, -1.414213562373095, -1.4142135623730945], [0.35355339059327373, 0.3535533905932738, 1.7677669529663689], [0, 0, 1]] Point after all transformations: [[1.3322676295501878e-15], [3.5355339059327378], [1]]
Here are some tips for optimizing matrix operations in math.js.
Performance optimization tips: 1. For large matrices, consider using typed arrays 2. Configure math.js to use arrays instead of matrix objects when performance is critical: math.create({ matrix: 'Array' }) 3. For very large matrices, consider specialized libraries like numeric.js or GPGPU libraries 4. When possible, avoid creating intermediate matrices in calculations 5. Use specialized functions (like math.det) instead of general-purpose ones when available 6. For repeated calculations, reuse matrix objects rather than recreating them 7. Consider sparse matrices for large matrices with many zero elements: math.sparse([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) Dense vs Sparse representation: Dense matrix size: approximately 106 bytes Sparse matrix size: approximately 99 bytes
Here are some best practices and common pitfalls to avoid when working with matrices in math.js.
Choose the right matrix type
Matrix creation
identity()
, zeros()
, ones()
when appropriateMatrix access and modification
subset()
for efficient access and modificationError handling
Mixing matrix types
Matrix dimensions
Performance issues
Numerical stability
Example of a common pitfall: Dimensions mismatch Matrix 1 (2x2): [[1, 2], [3, 4]] Matrix 2 (2x3): [[5, 6, 7], [8, 9, 10]] Result: [[21, 24, 27], [47, 54, 61]]