Skip to content

Commit b4e3f2e

Browse files
committed
finishing touches on matrixMult.js
1 parent faafa34 commit b4e3f2e

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

Maths/matrixMult.js

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
11

22
// 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){
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) {
99
console.log('The columns in this array are not equal')
1010
} else {
11-
return columnNumb;
11+
return columnNumb
1212
}
1313
}
1414
}
1515

1616
// 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-
}
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+
}
2525
}
2626

2727
// 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 [''];
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 ['']
3333
})
34-
return emptyArray;
35-
}else{
36-
return false;
34+
return emptyArray
35+
} else {
36+
return false
3737
}
3838
}
3939

4040
// 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]);
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+
const multMatrix = initiateEmptyArray(firstArray, secondArray)
45+
for (let rm = 0; rm < firstArray.length; rm++) {
46+
const rowMult = []
47+
for (let col = 0; col < firstArray[0].length; col++) {
48+
rowMult.push(firstArray[rm][col])
4949
}
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]);
50+
for (let cm = 0; cm < firstArray.length; cm++) {
51+
const colMult = []
52+
for (let row = 0; row < secondArray.length; row++) {
53+
colMult.push(secondArray[row][cm])
5454
}
55-
let newNumb = 0;
56-
for (let index = 0; index < rowMult.length; index++){
57-
newNumb += rowMult[index] * colMult[index];
55+
let newNumb = 0
56+
for (let index = 0; index < rowMult.length; index++) {
57+
newNumb += rowMult[index] * colMult[index]
5858
}
59-
multMatrix[rm][cm] = newNumb;
59+
multMatrix[rm][cm] = newNumb
6060
}
6161
}
62-
return multMatrix;
62+
return multMatrix
6363
}
6464

6565
const firstMatrix = [
6666
[1, 2],
6767
[3, 4]
68-
];
68+
]
6969

7070
const secondMatrix = [
7171
[5, 6],
7272
[7, 8]
73-
];
73+
]
7474

75-
console.log(matrixMult(firstMatrix, secondMatrix)); // [ [ 19, 22 ], [ 43, 50 ] ]
75+
console.log(matrixMult(firstMatrix, secondMatrix)) // [ [ 19, 22 ], [ 43, 50 ] ]
7676

7777
const thirdMatrix = [
7878
[-1, 4, 1],
79-
[7, -6, 2],
80-
];
79+
[7, -6, 2]
80+
]
8181
const fourthMatrix = [
8282
[2, -2],
8383
[5, 3],
84-
[3, 2],
85-
];
84+
[3, 2]
85+
]
8686

87-
console.log(matrixMult(thirdMatrix, fourthMatrix)); // [ [ 21, 16 ], [ -10, -28 ] ]
87+
console.log(matrixMult(thirdMatrix, fourthMatrix)) // [ [ 21, 16 ], [ -10, -28 ] ]

0 commit comments

Comments
 (0)