Search This Blog

2024/04/09

Javascript :Matrix Multiplication

A matrix in arrangement of numbers into rows and columns.In JS a matrix can be
represented with arrays and multi dimensional arrays can be created by nesting
arrays. So in this problem we can use the terms matrices and arrays
interchangeably.

e.g. suppose we want to represent a 2 row 2 column matrix in javascript then it
will look like
let twoDimArr = [
[1,2],
[3, 4]
];

Matrix Multiplication:
Given two matrices say matrixA having m rows & n columns i.e. m x n matrix and
other matrix say matrixB having p rows & q columns i.e. p x q matrix their
multplication is possible only where number of columns of first matrix i.e n
here equals the number of rows of second matrix i.e. p.

How the addition is done

let matrixA =[
[ a[0][0], a[0][1] ],
[ a[1][0], a[1][1] ]
]

let matrixB =[
[ b[0][0], b[0][1] ],
[ b[1][0], b[1][1] ]
]

let multiMatrix =[
[ c[0][0], c[0][1] ],
[ c[1][0], c[1][1] ]
]

where
c[0][0] = (a[0][0] * b[0][0] )+ (a[0][1] * b[1][0])
c[0][1] = (a[0][0] * b[0][1] )+ (a[0][1] * b[1][1])

c[1][0] = (a[1][0] * b[0][0] )+ (a[1][1] * b[1][0])
c[1][1] = (a[1][0] * b[0][1] )+ (a[1][1] * b[1][1])

In General
c[i][j] = ( a[i][0] * b[0][j] ) + (a[i][1] * b[1][j])

Here both matrices we are doing sum are of size 2 x 2 the matrix after there
multiplication will be off size 2x2. w.r.t. our generic matrices matrixA (size m
x n) & matrixB (size p x q) if n = p then their muplication matrix will be of
size m x q.

In matrix of size say m x n either m & n can be same or different if same
matrix is called square matrix else rectangular matrix.

Here is Javascript Implementation:

//5 x 4 matrix
let matrixA = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
];

//4 x 6 matrix
let matrixB = [
[21, 22, 23, 24, 25, 26],
[27, 28, 29, 30, 31, 32],
[33, 34, 35, 36, 37, 38],
[39, 40, 41, 42, 43, 44],
];

let element = 0;

let result = [];
//loop 5 times
for (let i = 0; i < matrixA.length; i++) {
let row = matrixA[i];
//loops 6 time
let resultArrayIthRow=[]
for (let k = 0; k < matrixB[0].length; k++) {
let column = [];
//loop 4 times
let resultArrayIthRowKthColumnElement=0;
for (let j = 0; j < matrixB.length; j++) {
resultArrayIthRowKthColumnElement
= resultArrayIthRowKthColumnElement + row[j] * matrixB[j][k]
}
resultArrayIthRow.push(resultArrayIthRowKthColumnElement)
}
result.push(resultArrayIthRow)
}

console.log(result)

Output:
[
[ 330, 340, 350, 360, 370, 380 ],
[ 810, 836, 862, 888, 914, 940 ],
[ 1290, 1332, 1374, 1416, 1458, 1500 ],
[ 1770, 1828, 1886, 1944, 2002, 2060 ],
[ 2250, 2324, 2398, 2472, 2546, 2620 ]
]

Generic Matrix Multiplication function:

function matrixMultiplication(matrixA, matrixB) {
let matrixARowCount = matrixA.length; //5
let matrixAColumnCount = matrixA[0].length; //4
//size of matrixA is matrixARowCount x matrixAColumnCount i.e. 5 x 4

let matrixBRowCount = matrixB.length; //4
let matrixBColumnCount = matrixB[0].length; //6
//size of matrixB is matrixBRowCount x matrixBColumnCount i.e. 4 x 6

if (matrixAColumnCount == matrixBRowCount) {
//create matrixARowCount x matrixBColumnCount i.e. 5 x 6 matrix
let matrixC = Array(matrixARowCount)
.fill()
.map(() => Array(6).fill());

for (let i = 0; i < matrixC.length; i++) {
for (let j = 0; j < matrixC[0].length; j++) {
matrixC[i][j] = 0;
for (let k = 0; k < matrixB.length; k++) {
//4
matrixC[i][j] = matrixC[i][j] + matrixA[i][k] * matrixB[k][j];
}
}
}
return matrixC;
} else {
throw "Matrix Multiplication is not Possible for
given matrices as per their order";
}
}

let matrixA = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
];

//4 x 6 matrix
let matrixB = [
[21, 22, 23, 24, 25, 26],
[27, 28, 29, 30, 31, 32],
[33, 34, 35, 36, 37, 38],
[39, 40, 41, 42, 43, 44],
];

let matrixC = matrixMultiplication(matrixA,matrixB)
console.log("Result:", matrixC);

Output:
Result: [
[ 330, 340, 350, 360, 370, 380 ],
[ 810, 836, 862, 888, 914, 940 ],
[ 1290, 1332, 1374, 1416, 1458, 1500 ],
[ 1770, 1828, 1886, 1944, 2002, 2060 ],
[ 2250, 2324, 2398, 2472, 2546, 2620 ]
]

No comments:

Post a Comment