Files
JavaScript/Maths/MatrixMultiplication.js
Rak Laptudirm 072523d594 merge: Fix spellings (#821)
* chore: remove codespell from ci

* feat: add codespell workflow

* fix: codespell workflow

* fix: ignore spellings in directory

* chore: fix spellings

./Dynamic-Programming/KadaneAlgo.js:2: contiguos ==> contiguous
./Dynamic-Programming/KadaneAlgo.js:14: posible ==> possible

* chore: fix spelling

./Dynamic-Programming/SieveOfEratosthenes.js:4: upto ==> up to

* chore: fix spellings

./Dynamic-Programming/MaxNonAdjacentSum.js:22: Exmaple ==> Example

* chore: fix spelling

./Project-Euler/test/Problem010.test.js:4: upto ==> up to
./Project-Euler/test/Problem010.test.js:8: upto ==> up to
./Project-Euler/test/Problem010.test.js:12: upto ==> up to

* chore: fix spelling

./String/AlphaNumericPalindrome.js:10: recieves ==> receives
./String/AlphaNumericPalindrome.js:10: sting ==> string
./String/AlphaNumericPalindrome.js:46: varaible ==> variable

* chore: fix spelling

./String/DiceCoefficient.js:3: stings ==> strings

* chore: fix spelling

./String/test/DiceCoefficient.test.js:9: atleast ==> at least

* chore: fix spelling

./String/test/MaxWord.test.js:8: ba ==> be

* chore: ignore `PermutateString.test.js`

* chore: fix spelling

./String/test/CheckVowels.test.js:62: occurances ==> occurrences

* chore: ignore `SubsequenceRecursive.js`

* chore: fix spelling

./Conversions/TemperatureConversion.js:2: arguement ==> argument

* chore: fix spelling

./Conversions/RailwayTimeConversion.js:7: Formate ==> Format
./Conversions/RailwayTimeConversion.js:8: Formate ==> Format

* chore: remove Linear Algebra

The deleted directory hosted a package which are not accepted by this repository.

* Auto-update DIRECTORY.md

* chore: fix spelling

* chore: fix spellings

* merge: Created composite Simpson's integration method. Tests included. (#819)

* Created composite Simpson's integration method.Tests included

* Minor corrections

* Auto-update DIRECTORY.md

* Styled with standard.js

* chore: remove blank line

* chore: remove blank line

Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com>

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: remove codespell from ci

* feat: add codespell workflow

* fix: codespell workflow

* fix: ignore spellings in directory

* chore: fix spellings

./Dynamic-Programming/KadaneAlgo.js:2: contiguos ==> contiguous
./Dynamic-Programming/KadaneAlgo.js:14: posible ==> possible

* chore: fix spelling

./Dynamic-Programming/SieveOfEratosthenes.js:4: upto ==> up to

* chore: fix spellings

./Dynamic-Programming/MaxNonAdjacentSum.js:22: Exmaple ==> Example

* chore: fix spelling

./Project-Euler/test/Problem010.test.js:4: upto ==> up to
./Project-Euler/test/Problem010.test.js:8: upto ==> up to
./Project-Euler/test/Problem010.test.js:12: upto ==> up to

* chore: fix spelling

./String/AlphaNumericPalindrome.js:10: recieves ==> receives
./String/AlphaNumericPalindrome.js:10: sting ==> string
./String/AlphaNumericPalindrome.js:46: varaible ==> variable

* chore: fix spelling

./String/DiceCoefficient.js:3: stings ==> strings

* chore: fix spelling

./String/test/DiceCoefficient.test.js:9: atleast ==> at least

* chore: fix spelling

./String/test/MaxWord.test.js:8: ba ==> be

* chore: ignore `PermutateString.test.js`

* chore: fix spelling

./String/test/CheckVowels.test.js:62: occurances ==> occurrences

* chore: ignore `SubsequenceRecursive.js`

* chore: fix spelling

./Conversions/TemperatureConversion.js:2: arguement ==> argument

* chore: fix spelling

./Conversions/RailwayTimeConversion.js:7: Formate ==> Format
./Conversions/RailwayTimeConversion.js:8: Formate ==> Format

* chore: remove Linear Algebra

The deleted directory hosted a package which are not accepted by this repository.

* Auto-update DIRECTORY.md

* chore: fix spelling

* chore: fix spellings

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: fix spelling

* chore: no need to check filenames

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: ggkogkou <76820848+ggkogkou@users.noreply.github.com>
Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr>
2021-10-28 15:37:43 +05:30

92 lines
3.3 KiB
JavaScript

// 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
const matrixCheck = (matrix) => {
let columnNumb
for (let index = 0; index < matrix.length; index++) {
if (index === 0) {
columnNumb = matrix[index].length
} else if (matrix[index].length !== columnNumb) {
// The columns in this array are not equal
} else {
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 vice versa.
const twoMatricesCheck = (first, second) => {
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)]
if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) {
// These matrices do not have a common side
return false
} else {
return true
}
}
// 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.
const initiateEmptyArray = (first, second) => {
if (twoMatricesCheck(first, second)) {
const emptyArray = first.map(() => {
return ['']
})
return emptyArray
} else {
return false
}
}
// 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`.
export const matrixMult = (firstArray, secondArray) => {
const multMatrix = initiateEmptyArray(firstArray, secondArray)
for (let rm = 0; rm < firstArray.length; rm++) {
const rowMult = []
for (let col = 0; col < firstArray[0].length; col++) {
rowMult.push(firstArray[rm][col])
}
for (let cm = 0; cm < firstArray.length; cm++) {
const colMult = []
for (let row = 0; row < secondArray.length; row++) {
colMult.push(secondArray[row][cm])
}
let newNumb = 0
for (let index = 0; index < rowMult.length; index++) {
newNumb += rowMult[index] * colMult[index]
}
multMatrix[rm][cm] = newNumb
}
}
return multMatrix
}
// 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 ] ]