Enter dimensions and elements for Matrix A and Matrix B, then click "Multiply Matrices".
Understanding Matrix Multiplication
Matrix multiplication is a fundamental operation in linear algebra. It's used extensively in fields like computer graphics, physics, engineering, economics, and data science. For two matrices, A and B, to be multiplied (in that order, A * B), the number of columns in matrix A must be equal to the number of rows in matrix B. If matrix A has dimensions m x n and matrix B has dimensions n x p, the resulting matrix C will have dimensions m x p.
The Calculation Process
Each element in the resulting matrix C, denoted as Cij, is calculated by taking the dot product of the i-th row of matrix A and the j-th column of matrix B. The dot product is the sum of the products of corresponding elements.
Specifically, for Cij:
Cij = ∑k=1n (Aik * Bkj)
Where:
i is the row index of the resulting matrix C.
j is the column index of the resulting matrix C.
k is the index that iterates through the columns of A and rows of B.
Aik is the element in the i-th row and k-th column of matrix A.
Bkj is the element in the k-th row and j-th column of matrix B.
Compatibility Requirement
A critical rule for matrix multiplication is that the inner dimensions must match. If Matrix A is of size m x n, Matrix B must be of size n x p. If this condition is not met, multiplication is not possible.
Use Cases
Computer Graphics: Transforming 3D models, applying rotations, translations, and scaling.
Machine Learning: Training neural networks involves extensive matrix operations.
Image Processing: Applying filters and transformations to images.
Solving Systems of Linear Equations: Representing and solving complex mathematical problems.
Economics and Finance: Modeling economic systems and financial portfolios.
Example:
Let Matrix A be:
[[1, 2, 3],
[4, 5, 6]] (2x3 matrix)
Let Matrix B be:
[[7, 8],
[9, 10],
[11, 12]] (3x2 matrix)
The resulting matrix C will be 2×2. Let's calculate C11:
Continuing this process for all elements, the resulting matrix C is:
[[58, 64],
[139, 154]] (2x2 matrix)
var matrixA = [];
var matrixB = [];
var resultMatrix = [];
function createMatrixInputs(matrixId, rows, cols) {
var container = document.getElementById(matrixId + 'Container');
container.innerHTML = "; // Clear previous inputs
var matrixData = [];
for (var i = 0; i < rows; i++) {
var rowData = [];
for (var j = 0; j < cols; j++) {
var input = document.createElement('input');
input.type = 'number';
input.id = matrixId + 'Element' + i + '_' + j;
input.value = i + '' + j; // Default value for easier testing
input.placeholder = '0';
input.addEventListener('input', function() {
// Update matrix data on input
var ids = this.id.split('Element');
var row = parseInt(ids[1].split('_')[0]);
var col = parseInt(ids[1].split('_')[1]);
var matrixName = ids[0];
var value = parseFloat(this.value);
if (isNaN(value)) value = 0; // Default to 0 if not a valid number
if (matrixName === 'A') {
matrixA[row][col] = value;
} else {
matrixB[row][col] = value;
}
});
container.appendChild(input);
rowData.push(0); // Initialize with 0
}
matrixData.push(rowData);
}
if (matrixId === 'A') {
matrixA = matrixData;
} else {
matrixB = matrixData;
}
}
function updateMatrixDimensions(matrixId) {
var rows = parseInt(document.getElementById('rows' + matrixId).value);
var cols = parseInt(document.getElementById('cols' + matrixId).value);
if (isNaN(rows) || rows < 1) rows = 1;
if (isNaN(cols) || cols < 1) cols = 1;
document.getElementById('rows' + matrixId).value = rows;
document.getElementById('cols' + matrixId).value = cols;
createMatrixInputs(matrixId, rows, cols);
// Immediately populate matrix data with current input values after creating inputs
populateMatrixDataFromInputs(matrixId, rows, cols);
}
function populateMatrixDataFromInputs(matrixId, rows, cols) {
var currentMatrix = (matrixId === 'A') ? matrixA : matrixB;
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
var inputElement = document.getElementById(matrixId + 'Element' + i + '_' + j);
var value = parseFloat(inputElement.value);
if (isNaN(value)) {
value = 0; // Ensure default value if input is invalid
inputElement.value = '0';
}
currentMatrix[i][j] = value;
}
}
}
function getMatrixValues(matrixId, rows, cols) {
var matrix = [];
for (var i = 0; i < rows; i++) {
var row = [];
for (var j = 0; j < cols; j++) {
var input = document.getElementById(matrixId + 'Element' + i + '_' + j);
var value = parseFloat(input.value);
if (isNaN(value)) {
return null; // Indicate error if any value is not a number
}
row.push(value);
}
matrix.push(row);
}
return matrix;
}
function calculateMatrixMultiply() {
var rowsA = parseInt(document.getElementById('rowsA').value);
var colsA = parseInt(document.getElementById('colsA').value);
var rowsB = parseInt(document.getElementById('rowsB').value);
var colsB = parseInt(document.getElementById('colsB').value);
// Validate dimensions
if (isNaN(rowsA) || isNaN(colsA) || isNaN(rowsB) || isNaN(colsB) || rowsA < 1 || colsA < 1 || rowsB < 1 || colsB < 1) {
document.getElementById('resultOutput').innerHTML = 'Please enter valid positive dimensions for both matrices.';
return;
}
// Ensure matrixA and matrixB are updated with current input values before multiplication
populateMatrixDataFromInputs('A', rowsA, colsA);
populateMatrixDataFromInputs('B', rowsB, colsB);
// Check compatibility
if (colsA !== rowsB) {
document.getElementById('resultOutput').innerHTML = 'Error: The number of columns in Matrix A must equal the number of rows in Matrix B for multiplication.';
return;
}
var resultMatrixContainer = document.getElementById('resultOutput');
resultMatrixContainer.innerHTML = ''; // Clear previous results
var resultRows = rowsA;
var resultCols = colsB;
resultMatrix = [];
for (var i = 0; i < resultRows; i++) {
var newRow = [];
for (var j = 0; j < resultCols; j++) {
var sum = 0;
for (var k = 0; k < colsA; k++) { // colsA is same as rowsB
var valA = matrixA[i][k];
var valB = matrixB[k][j];
// Ensure values are numbers before multiplying
if (typeof valA !== 'number' || typeof valB !== 'number') {
resultMatrixContainer.innerHTML = 'Error: Invalid element found in matrices. Please ensure all elements are numbers.';
return;
}
sum += valA * valB;
}
newRow.push(sum);
}
resultMatrix.push(newRow);
}
// Display the result matrix
var resultDisplay = '
';
resultDisplay += '
Resultant Matrix (C = A * B)
';
resultDisplay += '
';
resultDisplay += '
';
for (var i = 0; i < resultRows; i++) {
resultDisplay += '