Multiply Matrices Calculator

Matrix Multiplication Calculator

Use this calculator to multiply two matrices. Enter the dimensions for Matrix A and Matrix B, then input the individual elements. The calculator will perform the multiplication and display the resulting product matrix.

Matrix A Dimensions

Matrix B Dimensions

Matrix A Elements

Matrix B Elements

Resulting Matrix (A × B)

.calculator-container { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 800px; margin: 20px auto; padding: 25px; border-radius: 10px; background-color: #f9f9f9; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); border: 1px solid #e0e0e0; } .calculator-container h2, .calculator-container h3 { color: #333; text-align: center; margin-bottom: 20px; } .calculator-container p { color: #555; line-height: 1.6; margin-bottom: 20px; text-align: justify; } .calc-input-group { background-color: #ffffff; padding: 15px; border-radius: 8px; border: 1px solid #e9e9e9; margin-bottom: 15px; display: flex; flex-wrap: wrap; align-items: center; gap: 10px; } .calc-input-group label { flex: 1 1 150px; color: #444; font-weight: bold; margin-right: 10px; } .calc-input-group input[type="number"] { flex: 2 1 100px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; font-size: 1rem; box-sizing: border-box; } .calculator-container button { display: block; width: fit-content; margin: 15px auto; padding: 12px 25px; background-color: #007bff; color: white; border: none; border-radius: 5px; font-size: 1rem; cursor: pointer; transition: background-color 0.3s ease, transform 0.2s ease; } .calculator-container button:hover { background-color: #0056b3; transform: translateY(-1px); } .calculator-container button:active { transform: translateY(0); } #matrixAInputs, #matrixBInputs { background-color: #ffffff; padding: 15px; border-radius: 8px; border: 1px solid #e9e9e9; margin-bottom: 15px; } #matrixAInputs h3, #matrixBInputs h3 { margin-top: 0; margin-bottom: 15px; text-align: left; color: #333; } .matrix-grid { display: grid; gap: 5px; margin-top: 10px; } .matrix-grid input { width: 60px; /* Adjust width as needed */ padding: 8px; border: 1px solid #ddd; border-radius: 4px; font-size: 0.9rem; text-align: center; } #resultMatrix { background-color: #eaf7ff; padding: 20px; border-radius: 8px; border: 1px solid #cce0ff; margin-top: 20px; text-align: center; } #resultMatrix h3 { color: #0056b3; margin-top: 0; margin-bottom: 15px; } .result-matrix-display { display: inline-block; border: 1px solid #a0c0e0; padding: 10px; border-radius: 5px; background-color: #ffffff; font-family: 'Courier New', Courier, monospace; font-size: 1.1rem; line-height: 1.8; text-align: left; } .result-matrix-row { white-space: nowrap; } .result-matrix-element { display: inline-block; width: 50px; /* Fixed width for alignment */ text-align: right; padding: 0 5px; } #matrixInputError, #calculationError { text-align: center; font-weight: bold; margin-top: 10px; } @media (max-width: 600px) { .calc-input-group label, .calc-input-group input { flex: 1 1 100%; } .matrix-grid input { width: 100%; } .result-matrix-element { width: auto; padding: 0 2px; } } function clearMatrices() { document.getElementById('matrixAInputs').innerHTML = '

Matrix A Elements

'; document.getElementById('matrixBInputs').innerHTML = '

Matrix B Elements

'; document.getElementById('resultMatrix').innerHTML = '

Resulting Matrix (A × B)

'; document.getElementById('matrixInputError').innerHTML = "; document.getElementById('calculationError').innerHTML = "; } function generateMatrixInputs() { 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); var matrixInputErrorDiv = document.getElementById('matrixInputError'); matrixInputErrorDiv.innerHTML = "; document.getElementById('resultMatrix').innerHTML = '

Resulting Matrix (A × B)

'; document.getElementById('calculationError').innerHTML = "; if (isNaN(rowsA) || isNaN(colsA) || isNaN(rowsB) || isNaN(colsB) || rowsA < 1 || colsA < 1 || rowsB < 1 || colsB < 1) { matrixInputErrorDiv.innerHTML = 'Please enter valid positive numbers for all matrix dimensions.'; return; } if (colsA !== rowsB) { matrixInputErrorDiv.innerHTML = 'Error: For matrix multiplication, the number of columns in Matrix A must equal the number of rows in Matrix B.'; return; } var matrixAInputsDiv = document.getElementById('matrixAInputs'); var matrixBInputsDiv = document.getElementById('matrixBInputs'); matrixAInputsDiv.innerHTML = '

Matrix A Elements

'; matrixBInputsDiv.innerHTML = '

Matrix B Elements

'; var gridA = document.createElement('div'); gridA.className = 'matrix-grid'; gridA.style.gridTemplateColumns = 'repeat(' + colsA + ', auto)'; for (var i = 0; i < rowsA; i++) { for (var j = 0; j < colsA; j++) { var input = document.createElement('input'); input.type = 'number'; input.id = 'a_' + i + '_' + j; input.value = '0'; // Default value gridA.appendChild(input); } } matrixAInputsDiv.appendChild(gridA); var gridB = document.createElement('div'); gridB.className = 'matrix-grid'; gridB.style.gridTemplateColumns = 'repeat(' + colsB + ', auto)'; for (var i = 0; i < rowsB; i++) { for (var j = 0; j < colsB; j++) { var input = document.createElement('input'); input.type = 'number'; input.id = 'b_' + i + '_' + j; input.value = '0'; // Default value gridB.appendChild(input); } } matrixBInputsDiv.appendChild(gridB); } function calculateMatrixProduct() { 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); var calculationErrorDiv = document.getElementById('calculationError'); calculationErrorDiv.innerHTML = ''; document.getElementById('resultMatrix').innerHTML = '

Resulting Matrix (A × B)

'; if (isNaN(rowsA) || isNaN(colsA) || isNaN(rowsB) || isNaN(colsB) || rowsA < 1 || colsA < 1 || rowsB < 1 || colsB < 1) { calculationErrorDiv.innerHTML = 'Please set valid matrix dimensions first.'; return; } if (colsA !== rowsB) { calculationErrorDiv.innerHTML = 'Error: The number of columns in Matrix A must equal the number of rows in Matrix B to perform multiplication. Please adjust dimensions and regenerate inputs.'; return; } var matrixA = []; var matrixB = []; var isValid = true; // Populate Matrix A for (var i = 0; i < rowsA; i++) { matrixA[i] = []; for (var j = 0; j < colsA; j++) { var inputElement = document.getElementById('a_' + i + '_' + j); if (!inputElement) { calculationErrorDiv.innerHTML = 'Matrix A input fields not generated. Please click "Set Dimensions" first.'; return; } var val = parseFloat(inputElement.value); if (isNaN(val)) { isValid = false; break; } matrixA[i][j] = val; } if (!isValid) break; } // Populate Matrix B if (isValid) { for (var i = 0; i < rowsB; i++) { matrixB[i] = []; for (var j = 0; j < colsB; j++) { var inputElement = document.getElementById('b_' + i + '_' + j); if (!inputElement) { calculationErrorDiv.innerHTML = 'Matrix B input fields not generated. Please click "Set Dimensions" first.'; return; } var val = parseFloat(inputElement.value); if (isNaN(val)) { isValid = false; break; } matrixB[i][j] = val; } if (!isValid) break; } } if (!isValid) { calculationErrorDiv.innerHTML = 'Please ensure all matrix elements are valid numbers.'; return; } // Perform Matrix Multiplication var resultMatrix = []; for (var i = 0; i < rowsA; i++) { resultMatrix[i] = []; for (var j = 0; j < colsB; j++) { var sum = 0; for (var k = 0; k < colsA; k++) { // colsA is equal to rowsB sum += matrixA[i][k] * matrixB[k][j]; } resultMatrix[i][j] = sum; } } // Display Result var resultDiv = document.getElementById('resultMatrix'); var resultHtml = '

Resulting Matrix (A × B)

'; for (var i = 0; i < rowsA; i++) { resultHtml += '
[ '; for (var j = 0; j < colsB; j++) { resultHtml += '' + resultMatrix[i][j].toFixed(2) + ''; // Display with 2 decimal places if (j < colsB – 1) { resultHtml += ' '; } } resultHtml += ' ]
'; } resultHtml += '
'; resultDiv.innerHTML = resultHtml; } // Initial generation on page load window.onload = function() { generateMatrixInputs(); };

Understanding Matrix Multiplication

Matrix multiplication is a fundamental operation in linear algebra with wide-ranging applications in mathematics, physics, engineering, computer graphics, and data science. Unlike scalar multiplication (where each element is simply multiplied by a number), matrix multiplication involves a more complex process that combines rows and columns of the input matrices.

What is a Matrix?

A matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. For example, a 2×3 matrix has 2 rows and 3 columns:

    A = [ a₁₁ a₁₂ a₁₃ ]
        [ a₂₁ a₂₂ a₂₃ ]
    

The Rules of Matrix Multiplication

For two matrices, A and B, to be multiplied to form a product matrix C (i.e., C = A × B), a crucial condition must be met:

  1. Compatibility: The number of columns in the first matrix (A) must be equal to the number of rows in the second matrix (B). If A is an m × n matrix (m rows, n columns) and B is an n × p matrix (n rows, p columns), then their product C will be an m × p matrix.
  2. Resulting Dimensions: The resulting matrix C will have the same number of rows as matrix A and the same number of columns as matrix B.

If the compatibility condition is not met, matrix multiplication is undefined.

How to Calculate the Product

Each element cᵢⱼ in the product matrix C is calculated by taking the dot product of the i-th row of matrix A and the j-th column of matrix B. This means you multiply corresponding elements from the row and column and then sum those products.

Let's say we have:

    A = [ a₁₁ a₁₂ ]   (2x2 matrix)
        [ a₂₁ a₂₂ ]

    B = [ b₁₁ b₁₂ ]   (2x2 matrix)
        [ b₂₁ b₂₂ ]
    

The product C = A × B will be a 2×2 matrix:

    C = [ c₁₁ c₁₂ ]
        [ c₂₁ c₂₂ ]
    

Where:

  • c₁₁ = (a₁₁ × b₁₁) + (a₁₂ × b₂₁)
  • c₁₂ = (a₁₁ × b₁₂) + (a₁₂ × b₂₂)
  • c₂₁ = (a₂₁ × b₁₁) + (a₂₂ × b₂₁)
  • c₂₂ = (a₂₁ × b₁₂) + (a₂₂ × b₂₂)

Example Calculation

Let's multiply the following matrices:

    A = [ 1 2 ]
        [ 3 4 ]

    B = [ 5 6 ]
        [ 7 8 ]
    

Here, A is 2×2 and B is 2×2. Since the columns of A (2) equal the rows of B (2), multiplication is possible, and the result will be a 2×2 matrix.

  • c₁₁ = (1 × 5) + (2 × 7) = 5 + 14 = 19
  • c₁₂ = (1 × 6) + (2 × 8) = 6 + 16 = 22
  • c₂₁ = (3 × 5) + (4 × 7) = 15 + 28 = 43
  • c₂₂ = (3 × 6) + (4 × 8) = 18 + 32 = 50

So, the product matrix C is:

    C = [ 19 22 ]
        [ 43 50 ]
    

How to Use the Calculator

  1. Enter Dimensions: Input the number of rows and columns for Matrix A and Matrix B in the respective fields.
  2. Generate Input Fields: Click the "Set Dimensions & Generate Input Fields" button. The calculator will check for compatibility and create input boxes for each element of both matrices. If dimensions are incompatible, an error message will appear.
  3. Input Elements: Fill in the numerical values for each element in Matrix A and Matrix B.
  4. Calculate: Click the "Calculate Matrix Product" button. The resulting product matrix (A × B) will be displayed below.

This calculator simplifies the process of matrix multiplication, allowing you to quickly verify your calculations or explore different matrix products without manual computation.

Leave a Reply

Your email address will not be published. Required fields are marked *