SIGN IN SIGN UP

Algorithms and Data Structures implemented in JavaScript for beginners, following best practices.

34083 0 0 JavaScript
2020-10-12 17:06:56 -05:00
// Wikipedia URL for General Matrix Multiplication Concepts: https://en.wikipedia.org/wiki/Matrix_multiplication
2020-10-12 17:23:23 -05:00
// 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.
2020-10-12 17:17:58 -05:00
// 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].
2020-10-12 16:35:28 -05:00
// MatrixCheck tests to see if all of the rows of the matrix inputted have similar size columns
2020-10-12 16:54:50 -05:00
const matrixCheck = (matrix) => {
let columnNumb
for (let index = 0; index < matrix.length; index++) {
if (index === 0) {
columnNumb = matrix[index].length
} else if (matrix[index].length !== columnNumb) {
// The columns in this array are not equal
2020-10-12 16:35:28 -05:00
} else {
2020-10-12 16:54:50 -05:00
return columnNumb
2020-10-12 16:35:28 -05:00
}
}
}
// 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 vice versa.
2020-10-12 16:54:50 -05:00
const twoMatricesCheck = (first, second) => {
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [
first.length,
second.length,
matrixCheck(first),
matrixCheck(second)
]
// These matrices do not have a common side
return (
firstRowLength === secondColLength && secondRowLength === firstColLength
)
2020-10-12 16:35:28 -05:00
}
// returns an empty array that has the same number of rows as the left matrix being multiplied.
2020-10-12 16:54:50 -05:00
// Uses Array.prototype.map() to loop over the first (or left) matrix and returns an empty array on each iteration.
const initiateEmptyArray = (first, second) => {
if (twoMatricesCheck(first, second)) {
const emptyArray = first.map(() => {
return ['']
2020-10-12 16:35:28 -05:00
})
2020-10-12 16:54:50 -05:00
return emptyArray
} else {
return false
2020-10-12 16:35:28 -05:00
}
}
// 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.
2020-10-12 16:54:50 -05:00
// 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.
// The dot product for each iteration is then saved to its respective index into `multMatrix`.
export const matrixMult = (firstArray, secondArray) => {
2020-10-12 16:54:50 -05:00
const multMatrix = initiateEmptyArray(firstArray, secondArray)
for (let rm = 0; rm < firstArray.length; rm++) {
const rowMult = []
for (let col = 0; col < firstArray[0].length; col++) {
rowMult.push(firstArray[rm][col])
2020-10-12 16:35:28 -05:00
}
2020-10-12 16:54:50 -05:00
for (let cm = 0; cm < firstArray.length; cm++) {
const colMult = []
for (let row = 0; row < secondArray.length; row++) {
colMult.push(secondArray[row][cm])
2020-10-12 16:35:28 -05:00
}
2020-10-12 16:54:50 -05:00
let newNumb = 0
for (let index = 0; index < rowMult.length; index++) {
newNumb += rowMult[index] * colMult[index]
2020-10-12 16:35:28 -05:00
}
2020-10-12 16:54:50 -05:00
multMatrix[rm][cm] = newNumb
2020-10-12 16:35:28 -05:00
}
}
2020-10-12 16:54:50 -05:00
return multMatrix
2020-10-12 16:35:28 -05:00
}
// const firstMatrix = [
// [1, 2],
// [3, 4]
// ]
2020-10-12 16:35:28 -05:00
// const secondMatrix = [
// [5, 6],
// [7, 8]
// ]
2020-10-12 16:35:28 -05:00
// matrixMult(firstMatrix, secondMatrix) // [ [ 19, 22 ], [ 43, 50 ] ]
2020-10-12 16:35:28 -05:00
// const thirdMatrix = [
// [-1, 4, 1],
// [7, -6, 2]
// ]
// const fourthMatrix = [
// [2, -2],
// [5, 3],
// [3, 2]
// ]
2020-10-12 16:35:28 -05:00
// matrixMult(thirdMatrix, fourthMatrix) // [ [ 21, 16 ], [ -10, -28 ] ]