deepseek-v3-0324· DeepSeek LMArena Human Preference 140K
Explain matrix multiplication with JavaScript examples
- User
Explain matrix multiplication with JavaScript examples
- Assistant
Matrix Multiplication in JavaScript
Matrix multiplication is a fundamental operation in linear algebra where two matrices are multiplied to produce a new matrix. Here's how it works and how to implement it in JavaScript.
Rules of Matrix Multiplication
For two matrices A (m×n) and B (p×q) to be multiplied:
- The number of columns in A must equal the number of rows in B (n = p)
- The resulting matrix will have dimensions m×q
The element at position (i,j) in the product matrix is computed as the dot product of the i-th row of A and the j-th column of B.
JavaScript Implementation
Basic Implementation
function matrixMultiply(a, b) { // Check if matrices can be multiplied if (a[0].length !== b.length) { throw new Error('Incompatible matrix dimensions'); } // Create result matrix with dimensions a.length x b[0].length const result = new Array(a.length); for (let i = 0; i < result.length; i++) { result[i] = new Array(b[0].length).fill(0); } // Perform multiplication for (let i = 0; i < a.length; i++) { for (let j = 0; j < b[0].length; j++) { for (let k = 0; k < b.length; k++) { result[i][j] += a[i][k] * b[k][j]; } } } return result; } // Example usage const matrixA = [ [1, 2], [3, 4] ]; const matrixB = [ [5, 6], [7, 8] ]; const product = matrixMultiply(matrixA, matrixB); console.log(product); // Output: [[19, 22], [43, 50]]Optimized Implementation (for larger matrices)
function optimizedMatrixMultiply(a, b) { const aRows = a.length; const aCols = a[0].length; const bCols = b[0].length; if (aCols !== b.length) { throw new Error('Incompatible matrix dimensions'); } const result = new Array(aRows); for (let i = 0; i < aRows; i++) { result[i] = new Array(bCols).fill(0); } // Transpose matrix B for better cache performance const bTransposed = new Array(b[0].length); for (let i = 0; i < b[0].length; i++) { bTransposed[i] = new Array(b.length); for (let j = 0; j < b.length; j++) { bTransposed[i][j] = b[j][i]; } } // Multiply with transposed matrix for (let i = 0; i < aRows; i++) { for (let j = 0; j < bCols; j++) { let sum = 0; for (let k = 0; k < aCols; k++) { sum += a[i][k] * bTransposed[j][k]; } result[i][j] = sum; } } return result; } // Example with 3x3 matrices const matrixC = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; const matrixD = [ [9, 8, 7], [6, 5, 4], [3, 2, 1] ]; console.log(optimizedMatrixMultiply(matrixC, matrixD)); // Output: [[30, 24, 18], [84, 69, 54], [138, 114, 90]]Using ES6 Features
const matrixMultiplyES6 = (a, b) => { if (a[0].length !== b.length) { throw new Error('Incompatible matrix dimensions'); } return a.map((row, i) => b[0].map((_, j) => row.reduce((sum, val, k) => sum + val * b[k][j], 0) ) ); }; // Example with non-square matrices const matrixE = [ [1, 2, 3], [4, 5, 6] ]; const matrixF = [ [7, 8], [9, 10], [11, 12] ]; console.log(matrixMultiplyES6(matrixE, matrixF)); // Output: [[58, 64], [139, 154]]Key Points
- Matrix multiplication is not commutative (A×B ≠ B×A in general)
- The identity matrix I has 1s on the diagonal and 0s elsewhere, and A×I = A
- Matrix multiplication is associative: (A×B)×C = A×(B×C)
- The time complexity is O(n³) for n×n matrices (without optimization)
These implementations cover the basics of matrix multiplication in JavaScript. For production use with very large matrices, you might want to consider specialized libraries like math.js or numeric.js.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.