Matrix Arithmetic Calculator
Perform Addition, Subtraction, and Multiplication on 3×3 Matrices
Select Operation:
Addition (A + B)
Subtraction (A – B)
Multiplication (A × B)
Calculate
Reset
Understanding Matrix Arithmetic
Matrix arithmetic forms the backbone of linear algebra, a branch of mathematics used extensively in engineering, physics, computer science, and economics. Unlike standard scalar arithmetic, matrices follow specific rules for combining values based on their positions within a grid.
1. Matrix Addition and Subtraction
To add or subtract two matrices, they must have the same dimensions (number of rows and columns). In this calculator, we use 3×3 matrices. The operation is performed element-wise :
(A + B)ij = Aij + Bij
(A – B)ij = Aij – Bij
Example: If A11 is 5 and B11 is 3, the resulting R11 for addition is 8.
2. Matrix Multiplication (The Dot Product)
Matrix multiplication is more complex. It is not element-wise. To find the value of a specific cell in the result matrix (Row i , Column j ), you must take the dot product of Row i from Matrix A and Column j from Matrix B.
Result[i][j] = (A[i][1] * B[1][j]) + (A[i][2] * B[2][j]) + (A[i][3] * B[3][j])
Important Note: Unlike regular multiplication, matrix multiplication is generally not commutative (A × B is not the same as B × A).
Practical Applications
Computer Graphics: Rotating, scaling, and moving 3D objects in video games involves multiplying matrices.
Statistics: Multivariate analysis and linear regression often use matrices to solve for multiple variables simultaneously.
Robotics: Calculating the movement of robotic arms requires coordinate transformations handled through matrix arithmetic.
function calculateMatrix() {
var a = [
[parseFloat(document.getElementById('a11').value) || 0, parseFloat(document.getElementById('a12').value) || 0, parseFloat(document.getElementById('a13').value) || 0],
[parseFloat(document.getElementById('a21').value) || 0, parseFloat(document.getElementById('a22').value) || 0, parseFloat(document.getElementById('a23').value) || 0],
[parseFloat(document.getElementById('a31').value) || 0, parseFloat(document.getElementById('a32').value) || 0, parseFloat(document.getElementById('a33').value) || 0]
];
var b = [
[parseFloat(document.getElementById('b11').value) || 0, parseFloat(document.getElementById('b12').value) || 0, parseFloat(document.getElementById('b13').value) || 0],
[parseFloat(document.getElementById('b21').value) || 0, parseFloat(document.getElementById('b22').value) || 0, parseFloat(document.getElementById('b23').value) || 0],
[parseFloat(document.getElementById('b31').value) || 0, parseFloat(document.getElementById('b32').value) || 0, parseFloat(document.getElementById('b33').value) || 0]
];
var op = document.getElementById('operation').value;
var result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
if (op === 'add') {
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
} else if (op === 'subtract') {
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
result[i][j] = a[i][j] – b[i][j];
}
}
} else if (op === 'multiply') {
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var sum = 0;
for (var k = 0; k < 3; k++) {
sum += a[i][k] * b[k][j];
}
result[i][j] = sum;
}
}
}
displayResult(result);
}
function displayResult(res) {
var container = document.getElementById('result-container');
var grid = document.getElementById('result-grid');
grid.innerHTML = '';
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var cell = document.createElement('div');
cell.style.padding = '10px';
cell.style.background = '#fff';
cell.style.border = '2px solid #27ae60';
cell.style.textAlign = 'center';
cell.style.fontWeight = 'bold';
cell.style.borderRadius = '4px';
// Rounding to 4 decimal places for cleanliness
var val = res[i][j];
cell.innerText = Math.round(val * 10000) / 10000;
grid.appendChild(cell);
}
}
container.style.display = 'block';
container.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
function clearMatrices() {
var inputs = document.querySelectorAll('#matrix-calculator-wrapper input[type="number"]');
for (var i = 0; i < inputs.length; i++) {
inputs[i].value = 0;
}
document.getElementById('result-container').style.display = 'none';
}