Skip to content

Commit 0bfd1c2

Browse files
authored
Merge pull request #466 from plooney81/master
Added MatrixMultiplication Algorithm
2 parents 89df465 + 45b35f4 commit 0bfd1c2

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

Maths/MatrixMultiplication.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 ] ]

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@babel/plugin-transform-runtime": "^7.11.5",
1414
"@babel/preset-env": "^7.11.5",
1515
"jsdom": "^16.3.0",
16+
"node": "^14.13.1",
1617
"node-fetch": "2.6.1"
1718
},
1819
"standard": {

0 commit comments

Comments
 (0)