Skip to content

Commit 3d5927a

Browse files
committed
added matrixMult.js
1 parent 89df465 commit 3d5927a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Maths/matrixMult.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
// MatrixCheck tests to see if all of the rows of the matrix inputted have similar size columns
3+
const matrixCheck = (matrix)=>{
4+
let columnNumb;
5+
for (let index = 0; index < matrix.length; index++){
6+
if (index == 0){
7+
columnNumb = matrix[index].length;
8+
} else if (matrix[index].length != columnNumb){
9+
console.log('The columns in this array are not equal')
10+
} else {
11+
return columnNumb;
12+
}
13+
}
14+
}
15+
16+
// 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.
17+
const twoMatricesCheck = (first, second)=>{
18+
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)];
19+
if (firstRowLength != secondColLength || secondRowLength != firstColLength){
20+
console.log('These matrices do not have a common side');
21+
return false;
22+
} else {
23+
return true;
24+
}
25+
}
26+
27+
// returns an empty array that has the same number of rows as the left matrix being multiplied.
28+
//Uses Array.prototype.map() to loop over the first (or left) matrix and returns an empty array on each iteration.
29+
const initiateEmptyArray = (first, second)=>{
30+
if (twoMatricesCheck(first, second)){
31+
const emptyArray = first.map(()=>{
32+
return [''];
33+
})
34+
return emptyArray;
35+
}else{
36+
return false;
37+
}
38+
}
39+
40+
// 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.
41+
// 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.
42+
// The dot product for each iteration is then saved to its respective index into `multMatrix`.
43+
const matrixMult = (firstArray, secondArray)=>{
44+
let multMatrix = initiateEmptyArray(firstArray, secondArray);
45+
for (let rm = 0; rm < firstArray.length; rm++){
46+
rowMult = [];
47+
for (let col = 0; col < firstArray[0].length; col++){
48+
rowMult.push(firstArray[rm][col]);
49+
}
50+
for (let cm = 0; cm < firstArray.length; cm++){
51+
colMult = [];
52+
for (let row = 0; row < secondArray.length; row++){
53+
colMult.push(secondArray[row][cm]);
54+
}
55+
let newNumb = 0;
56+
for (let index = 0; index < rowMult.length; index++){
57+
newNumb += rowMult[index] * colMult[index];
58+
}
59+
multMatrix[rm][cm] = newNumb;
60+
}
61+
}
62+
return multMatrix;
63+
}
64+
65+
const firstMatrix = [
66+
[1, 2],
67+
[3, 4]
68+
];
69+
70+
const secondMatrix = [
71+
[5, 6],
72+
[7, 8]
73+
];
74+
75+
console.log(matrixMult(firstMatrix, secondMatrix)); // [ [ 19, 22 ], [ 43, 50 ] ]
76+
77+
const thirdMatrix = [
78+
[-1, 4, 1],
79+
[7, -6, 2],
80+
];
81+
const fourthMatrix = [
82+
[2, -2],
83+
[5, 3],
84+
[3, 2],
85+
];
86+
87+
console.log(matrixMult(thirdMatrix, fourthMatrix)); // [ [ 21, 16 ], [ -10, -28 ] ]

0 commit comments

Comments
 (0)