|
| 1 | +// Wikipedia URL for General Matrix Multiplication Concepts: https://en.wikipedia.org/wiki/Matrix_multiplication |
| 2 | + |
| 3 | +// This algorithm has multiple functions that ultimately check if the inputs are actually matrices and if two Matrices (that can be different sizes) can be multiplied together. |
| 4 | +// matrices that are of the same size [2x2]x[2x2], and the second is the multiplication of two matrices that are not the same size [2x3]x[3x2]. |
| 5 | + |
| 6 | +// MatrixCheck tests to see if all of the rows of the matrix inputted have similar size columns |
| 7 | +const matrixCheck = (matrix) => { |
| 8 | + let columnNumb |
| 9 | + for (let index = 0; index < matrix.length; index++) { |
| 10 | + if (index === 0) { |
| 11 | + columnNumb = matrix[index].length |
| 12 | + } else if (matrix[index].length !== columnNumb) { |
| 13 | + console.log('The columns in this array are not equal') |
| 14 | + } else { |
| 15 | + return columnNumb |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// tests to see if the matrices have a like side, i.e. the row length on the first matrix matches the column length on the second matrix, or vise versa. |
| 21 | +const twoMatricesCheck = (first, second) => { |
| 22 | + const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)] |
| 23 | + if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) { |
| 24 | + console.log('These matrices do not have a common side') |
| 25 | + return false |
| 26 | + } else { |
| 27 | + return true |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// returns an empty array that has the same number of rows as the left matrix being multiplied. |
| 32 | +// Uses Array.prototype.map() to loop over the first (or left) matrix and returns an empty array on each iteration. |
| 33 | +const initiateEmptyArray = (first, second) => { |
| 34 | + if (twoMatricesCheck(first, second)) { |
| 35 | + const emptyArray = first.map(() => { |
| 36 | + return [''] |
| 37 | + }) |
| 38 | + return emptyArray |
| 39 | + } else { |
| 40 | + return false |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Finally, `matrixMult` uses `Array.prototype.push()`, multiple layers of nested `for` loops, the addition assignment `+=` operator and multiplication operator `*` to perform the dot product between two matrices of differing sizes. |
| 45 | +// Dot product, takes the row of the first matrix and multiplies it by the column of the second matrix, the `twoMatricesCheck` tested to see if they were the same size already. |
| 46 | +// The dot product for each iteration is then saved to its respective index into `multMatrix`. |
| 47 | +const matrixMult = (firstArray, secondArray) => { |
| 48 | + const multMatrix = initiateEmptyArray(firstArray, secondArray) |
| 49 | + for (let rm = 0; rm < firstArray.length; rm++) { |
| 50 | + const rowMult = [] |
| 51 | + for (let col = 0; col < firstArray[0].length; col++) { |
| 52 | + rowMult.push(firstArray[rm][col]) |
| 53 | + } |
| 54 | + for (let cm = 0; cm < firstArray.length; cm++) { |
| 55 | + const colMult = [] |
| 56 | + for (let row = 0; row < secondArray.length; row++) { |
| 57 | + colMult.push(secondArray[row][cm]) |
| 58 | + } |
| 59 | + let newNumb = 0 |
| 60 | + for (let index = 0; index < rowMult.length; index++) { |
| 61 | + newNumb += rowMult[index] * colMult[index] |
| 62 | + } |
| 63 | + multMatrix[rm][cm] = newNumb |
| 64 | + } |
| 65 | + } |
| 66 | + return multMatrix |
| 67 | +} |
| 68 | + |
| 69 | +const firstMatrix = [ |
| 70 | + [1, 2], |
| 71 | + [3, 4] |
| 72 | +] |
| 73 | + |
| 74 | +const secondMatrix = [ |
| 75 | + [5, 6], |
| 76 | + [7, 8] |
| 77 | +] |
| 78 | + |
| 79 | +console.log(matrixMult(firstMatrix, secondMatrix)) // [ [ 19, 22 ], [ 43, 50 ] ] |
| 80 | + |
| 81 | +const thirdMatrix = [ |
| 82 | + [-1, 4, 1], |
| 83 | + [7, -6, 2] |
| 84 | +] |
| 85 | +const fourthMatrix = [ |
| 86 | + [2, -2], |
| 87 | + [5, 3], |
| 88 | + [3, 2] |
| 89 | +] |
| 90 | + |
| 91 | +console.log(matrixMult(thirdMatrix, fourthMatrix)) // [ [ 21, 16 ], [ -10, -28 ] ] |
0 commit comments