finishing touches on matrixMult.js

This commit is contained in:
Pete Looney
2020-10-12 16:54:50 -05:00
parent faafa34b5c
commit b4e3f2e6d6

View File

@ -1,26 +1,26 @@
// MatrixCheck tests to see if all of the rows of the matrix inputted have similar size columns
const matrixCheck = (matrix) => {
let columnNumb;
let columnNumb
for (let index = 0; index < matrix.length; index++) {
if (index == 0){
columnNumb = matrix[index].length;
} else if (matrix[index].length != columnNumb){
if (index === 0) {
columnNumb = matrix[index].length
} else if (matrix[index].length !== columnNumb) {
console.log('The columns in this array are not equal')
} else {
return columnNumb;
return columnNumb
}
}
}
// 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.
const twoMatricesCheck = (first, second) => {
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)];
if (firstRowLength != secondColLength || secondRowLength != firstColLength){
console.log('These matrices do not have a common side');
return false;
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)]
if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) {
console.log('These matrices do not have a common side')
return false
} else {
return true;
return true
}
}
@ -29,11 +29,11 @@ const twoMatricesCheck = (first, second)=>{
const initiateEmptyArray = (first, second) => {
if (twoMatricesCheck(first, second)) {
const emptyArray = first.map(() => {
return [''];
return ['']
})
return emptyArray;
return emptyArray
} else {
return false;
return false
}
}
@ -41,47 +41,47 @@ const initiateEmptyArray = (first, second)=>{
// 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`.
const matrixMult = (firstArray, secondArray) => {
let multMatrix = initiateEmptyArray(firstArray, secondArray);
const multMatrix = initiateEmptyArray(firstArray, secondArray)
for (let rm = 0; rm < firstArray.length; rm++) {
rowMult = [];
const rowMult = []
for (let col = 0; col < firstArray[0].length; col++) {
rowMult.push(firstArray[rm][col]);
rowMult.push(firstArray[rm][col])
}
for (let cm = 0; cm < firstArray.length; cm++) {
colMult = [];
const colMult = []
for (let row = 0; row < secondArray.length; row++) {
colMult.push(secondArray[row][cm]);
colMult.push(secondArray[row][cm])
}
let newNumb = 0;
let newNumb = 0
for (let index = 0; index < rowMult.length; index++) {
newNumb += rowMult[index] * colMult[index];
newNumb += rowMult[index] * colMult[index]
}
multMatrix[rm][cm] = newNumb;
multMatrix[rm][cm] = newNumb
}
}
return multMatrix;
return multMatrix
}
const firstMatrix = [
[1, 2],
[3, 4]
];
]
const secondMatrix = [
[5, 6],
[7, 8]
];
]
console.log(matrixMult(firstMatrix, secondMatrix)); // [ [ 19, 22 ], [ 43, 50 ] ]
console.log(matrixMult(firstMatrix, secondMatrix)) // [ [ 19, 22 ], [ 43, 50 ] ]
const thirdMatrix = [
[-1, 4, 1],
[7, -6, 2],
];
[7, -6, 2]
]
const fourthMatrix = [
[2, -2],
[5, 3],
[3, 2],
];
[3, 2]
]
console.log(matrixMult(thirdMatrix, fourthMatrix)); // [ [ 21, 16 ], [ -10, -28 ] ]
console.log(matrixMult(thirdMatrix, fourthMatrix)) // [ [ 21, 16 ], [ -10, -28 ] ]