forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.js
More file actions
Latest commit
95 lines (84 loc) · 3.3 KB
/
Copy pathMatrixMultiplication.js
File metadata and controls
95 lines (84 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Wikipedia URL for General Matrix Multiplication Concepts: https://en.wikipedia.org/wiki/Matrix_multiplication
// 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.
// 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].
// MatrixCheck tests to see if all of the rows of the matrix inputted have similar size columns
constmatrixCheck=(matrix)=>{
letcolumnNumb
for(letindex=0;index<matrix.length;index++){
if(index===0){
columnNumb=matrix[index].length
}elseif(matrix[index].length!==columnNumb){
// The columns in this array are not equal
}else{
returncolumnNumb
}
}
}
// 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.
consttwoMatricesCheck=(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
)
}
// returns an empty array that has the same number of rows as the left matrix being multiplied.
// Uses Array.prototype.map() to loop over the first (or left) matrix and returns an empty array on each iteration.
constinitiateEmptyArray=(first,second)=>{
if(twoMatricesCheck(first,second)){
constemptyArray=first.map(()=>{
return['']
})
returnemptyArray
}else{
returnfalse
}
}
// 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.
// 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`.
exportconstmatrixMult=(firstArray,secondArray)=>{
constmultMatrix=initiateEmptyArray(firstArray,secondArray)
for(letrm=0;rm<firstArray.length;rm++){
constrowMult=[]
for(letcol=0;col<firstArray[0].length;col++){
rowMult.push(firstArray[rm][col])
}
for(letcm=0;cm<firstArray.length;cm++){
constcolMult=[]
for(letrow=0;row<secondArray.length;row++){
colMult.push(secondArray[row][cm])
}
letnewNumb=0
for(letindex=0;index<rowMult.length;index++){
newNumb+=rowMult[index]*colMult[index]
}
multMatrix[rm][cm]=newNumb
}
}
returnmultMatrix
}
// const firstMatrix = [
// [1, 2],
// [3, 4]
// ]
// const secondMatrix = [
// [5, 6],
// [7, 8]
// ]
// matrixMult(firstMatrix, secondMatrix) // [ [ 19, 22 ], [ 43, 50 ] ]
// const thirdMatrix = [
// [-1, 4, 1],
// [7, -6, 2]
// ]
// const fourthMatrix = [
// [2, -2],
// [5, 3],
// [3, 2]
// ]
// matrixMult(thirdMatrix, fourthMatrix) // [ [ 21, 16 ], [ -10, -28 ] ]