JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Perform Matrix Multiplication
Photo Credit to CodeToFun
π Introduction
Matrix multiplication is a fundamental operation in linear algebra and computer science. It involves multiplying two matrices to produce a third matrix.
In this tutorial, we will explore a javascript program that performs matrix multiplication.
We'll break down the logic step by step and provide a sample implementation.
π Example
Let's dive into the javascript code that performs matrix multiplication.
// Function to perform matrix multiplication
function multiplyMatrices(firstMatrix, secondMatrix) {
const result = [];
const m = firstMatrix.length; // Number of rows in the first matrix
const n = firstMatrix[0].length; // Number of columns in the first matrix
const p = secondMatrix[0].length; // Number of columns in the second matrix
// Initialize the result matrix with zeros
for (let i = 0; i < m; ++i) {
result[i] = Array(p).fill(0);
}
// Perform matrix multiplication
for (let i = 0; i < m; ++i) {
for (let j = 0; j < p; ++j) {
for (let k = 0; k < n; ++k) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
return result;
}
// Function to display a matrix
function displayMatrix(matrix) {
for (let i = 0; i < matrix.length; ++i) {
console.log(matrix[i].join('\t'));
}
}
// Sample input matrices (3x3)
const firstMatrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
const secondMatrix = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
];
// Call the function to perform matrix multiplication
const resultMatrix = multiplyMatrices(firstMatrix, secondMatrix);
// Display the matrices
console.log("First Matrix:");
displayMatrix(firstMatrix);
console.log("\nSecond Matrix:");
displayMatrix(secondMatrix);
console.log("\nResult Matrix:");
displayMatrix(resultMatrix);
π» Testing the Program
Feel free to replace the sample matrices with your own 3x3 matrices in the main function to test the program with different input.
First Matrix: 1 2 3 4 5 6 7 8 9 Second Matrix: 9 8 7 6 5 4 3 2 1 Result Matrix: 30 24 18 84 69 54 138 114 90
Run the program to see the result of the matrix multiplication.
π§ How the Program Works
- The program defines a function multiplyMatrices that takes two matrices (firstMatrix and secondMatrix) and computes their product, returning the result.
- It initializes the result matrix with zeros and uses three nested loops to perform the matrix multiplication.
- The displayMatrix function is used to display the contents of a matrix.
- Sample input matrices are provided, and the program calls the multiplication function.
- The program then displays the original matrices and the result matrix.
π§ Understanding the Concept of Matrix Multiplication
Matrix multiplication is defined as follows: given two matrices A (of dimensions m x n) and B (of dimensions n x p), the product matrix C (of dimensions m x p) is obtained by multiplying each element of a row in matrix A by the corresponding element of a column in matrix B and summing up the results.
π Conclusion
Matrix multiplication is a powerful operation used in various fields, including graphics, physics simulations, and machine learning.
Understanding the logic behind matrix multiplication and implementing it in javascript provides a foundational understanding of linear algebra in computer science.
Feel free to experiment with different matrix sizes and values to further explore the capabilities of this program. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Perform Matrix Multiplication), please comment here. I will help you immediately.