Matrix Calculator
:root {
–primary-blue: #004a99;
–success-green: #28a745;
–light-background: #f8f9fa;
–border-color: #dee2e6;
–text-color: #212529;
–input-bg: #ffffff;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(–light-background);
color: var(–text-color);
line-height: 1.6;
margin: 0;
padding: 20px;
}
.matrix-calc-container {
max-width: 800px;
margin: 30px auto;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
padding: 30px;
}
h1, h2 {
color: var(–primary-blue);
text-align: center;
margin-bottom: 25px;
}
.input-section, .result-section {
margin-bottom: 30px;
padding: 25px;
border: 1px solid var(–border-color);
border-radius: 5px;
background-color: var(–light-background);
}
.input-group {
margin-bottom: 20px;
display: flex;
flex-direction: column;
gap: 8px;
}
.input-group label {
font-weight: 600;
color: var(–primary-blue);
display: block;
}
.input-group input[type="text"],
.input-group textarea {
width: 100%;
padding: 10px;
border: 1px solid var(–border-color);
border-radius: 4px;
box-sizing: border-box;
font-size: 1rem;
background-color: var(–input-bg);
transition: border-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
}
.input-group input[type="text"]:focus,
.input-group textarea:focus {
border-color: var(–primary-blue);
box-shadow: 0 0 0 0.2rem rgba(0, 74, 153, 0.25);
outline: none;
}
.matrix-input-description {
font-size: 0.9em;
color: #6c757d;
margin-top: 5px;
}
button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: var(–primary-blue);
color: white;
border: none;
border-radius: 5px;
font-size: 1.1rem;
font-weight: 600;
cursor: pointer;
transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
text-transform: uppercase;
letter-spacing: 0.5px;
}
button:hover {
background-color: #003b7a;
transform: translateY(-2px);
}
button:active {
transform: translateY(0);
}
.result-section h2 {
color: var(–success-green);
margin-bottom: 15px;
}
#result {
background-color: var(–success-green);
color: white;
padding: 20px;
border-radius: 5px;
font-size: 1.5rem;
font-weight: bold;
text-align: center;
box-shadow: 0 2px 8px rgba(40, 167, 69, 0.4);
white-space: pre-wrap; /* To display matrix structure */
word-break: break-all; /* Break long lines if needed */
}
#error-message {
color: red;
text-align: center;
margin-top: 15px;
font-weight: bold;
}
.article-section {
margin-top: 40px;
padding: 30px;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
.article-section h2 {
text-align: left;
margin-bottom: 20px;
color: var(–primary-blue);
}
.article-section p, .article-section ul, .article-section li {
margin-bottom: 15px;
color: #333;
}
.article-section h3 {
color: var(–primary-blue);
margin-top: 20px;
margin-bottom: 10px;
}
code {
background-color: var(–light-background);
padding: 2px 5px;
border-radius: 3px;
font-family: 'Courier New', Courier, monospace;
}
@media (max-width: 768px) {
.matrix-calc-container {
margin: 15px;
padding: 20px;
}
button {
font-size: 1rem;
padding: 10px 15px;
}
#result {
font-size: 1.2rem;
}
}
Matrix Calculator
Result
Enter matrices and select an operation.
Understanding Matrices and Matrix Operations
Matrices are fundamental mathematical objects that consist of a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. They are widely used in various fields, including linear algebra, physics, engineering, computer graphics, economics, and statistics, to represent systems of linear equations, transformations, and data sets.
What is a Matrix?
A matrix is typically denoted by a capital letter and its elements are enclosed in square brackets or parentheses. For example, a matrix 'A' can be represented as:
A = [[a11, a12, ..., a1n],
[a21, a22, ..., a2n],
...,
[am1, am2, ..., amn]]
Here, 'm' represents the number of rows and 'n' represents the number of columns. The element in the i-th row and j-th column is denoted by 'aij'. A matrix with 'm' rows and 'n' columns is said to have the dimensions m x n.
Common Matrix Operations
This calculator supports several fundamental matrix operations:
-
Matrix Addition/Subtraction: Two matrices can be added or subtracted if and only if they have the same dimensions (i.e., the same number of rows and columns). The resulting matrix has the same dimensions, and each element is the sum or difference of the corresponding elements from the original matrices. For matrices A and B of size m x n, the element at position (i, j) in the result C is given by
cij = aij ± bij.
-
Matrix Multiplication: Matrix multiplication is more complex. For two matrices A (m x n) and B (p x q) to be multiplied (A * B), the number of columns in A ('n') must equal the number of rows in B ('p'). The resulting matrix C will have dimensions m x q. The element at position (i, j) in C, denoted as
cij, is calculated by taking the dot product of the i-th row of A and the j-th column of B:
cij = sum(aik * bkj for k from 1 to n)
This operation is crucial for solving systems of linear equations and performing transformations in geometry.
-
Matrix Transpose: The transpose of a matrix A, denoted as AT, is obtained by interchanging its rows and columns. If A is an m x n matrix, its transpose AT will be an n x m matrix. The element at position (i, j) in AT is the element at position (j, i) in A (i.e.,
(A^T)ij = Aji). Transposition is a fundamental operation used in various linear algebra concepts like finding inverse matrices and solving linear systems.
Use Cases
Matrices and their operations are indispensable in numerous applications:
- Computer Graphics: Used for transformations like translation, scaling, and rotation of 2D and 3D objects.
- Engineering: Solving systems of differential equations, structural analysis, circuit analysis.
- Data Science & Machine Learning: Representing datasets, performing principal component analysis (PCA), training neural networks.
- Economics: Modeling input-output relationships, solving linear programming problems.
- Physics: Quantum mechanics, classical mechanics, solving complex physical systems.
This calculator provides a practical tool to perform these essential matrix computations, aiding students, researchers, and professionals in understanding and applying linear algebra concepts.
function parseMatrix(matrixString) {
var rows = matrixString.trim().split(';');
var matrix = [];
var numCols = -1;
for (var i = 0; i 0) {
var row = [];
for (var j = 0; j < rowElements.length; j++) {
var num = parseFloat(rowElements[j]);
if (isNaN(num)) {
throw new Error("Invalid number '" + rowElements[j] + "' found in Matrix input.");
}
row.push(num);
}
if (numCols === -1) {
numCols = row.length;
} else if (row.length !== numCols) {
throw new Error("Matrices must have consistent column counts in each row.");
}
matrix.push(row);
}
}
// Handle case where input is empty or only contains whitespace/semicolons
if (matrix.length === 0 && matrixString.trim() !== "") {
throw new Error("Matrix input is not properly formatted or is empty.");
}
return matrix;
}
function formatMatrix(matrix) {
if (!matrix || matrix.length === 0) return "[]";
var output = "";
for (var i = 0; i < matrix.length; i++) {
output += "[" + matrix[i].join(", ") + "]\n";
}
return output.trim();
}
function addMatrices(matrixA, matrixB) {
if (matrixA.length !== matrixB.length || matrixA[0].length !== matrixB[0].length) {
throw new Error("Matrices must have the same dimensions for addition.");
}
var rows = matrixA.length;
var cols = matrixA[0].length;
var result = [];
for (var i = 0; i < rows; i++) {
result[i] = [];
for (var j = 0; j < cols; j++) {
result[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
return result;
}
function subtractMatrices(matrixA, matrixB) {
if (matrixA.length !== matrixB.length || matrixA[0].length !== matrixB[0].length) {
throw new Error("Matrices must have the same dimensions for subtraction.");
}
var rows = matrixA.length;
var cols = matrixA[0].length;
var result = [];
for (var i = 0; i < rows; i++) {
result[i] = [];
for (var j = 0; j < cols; j++) {
result[i][j] = matrixA[i][j] – matrixB[i][j];
}
}
return result;
}
function multiplyMatrices(matrixA, matrixB) {
var rowsA = matrixA.length;
var colsA = matrixA[0].length;
var rowsB = matrixB.length;
var colsB = matrixB[0].length;
if (colsA !== rowsB) {
throw new Error("Number of columns in Matrix A must equal number of rows in Matrix B for multiplication.");
}
var result = [];
for (var i = 0; i < rowsA; i++) {
result[i] = [];
for (var j = 0; j < colsB; j++) {
var sum = 0;
for (var k = 0; k < colsA; k++) {
sum += matrixA[i][k] * matrixB[k][j];
}
result[i][j] = sum;
}
}
return result;
}
function transposeMatrix(matrix) {
var rows = matrix.length;
var cols = matrix[0].length;
var result = [];
for (var j = 0; j < cols; j++) {
result[j] = [];
for (var i = 0; i < rows; i++) {
result[j][i] = matrix[i][j];
}
}
return result;
}
function calculateMatrix() {
var matrixAStr = document.getElementById("matrixA").value;
var matrixBStr = document.getElementById("matrixB").value;
var operation = document.getElementById("operation").value;
var resultDiv = document.getElementById("result");
var errorDiv = document.getElementById("error-message");
resultDiv.textContent = ""; // Clear previous result
errorDiv.textContent = ""; // Clear previous error
try {
var matrixA = parseMatrix(matrixAStr);
var matrixB = null;
if (operation !== "transposeA" && operation !== "transposeB") {
matrixB = parseMatrix(matrixBStr);
}
var finalResult = null;
if (operation === "add") {
finalResult = addMatrices(matrixA, matrixB);
} else if (operation === "subtract") {
finalResult = subtractMatrices(matrixA, matrixB);
} else if (operation === "multiply") {
finalResult = multiplyMatrices(matrixA, matrixB);
} else if (operation === "transposeA") {
if (matrixA.length === 0 || matrixA[0].length === 0) throw new Error("Matrix A is empty.");
finalResult = transposeMatrix(matrixA);
} else if (operation === "transposeB") {
if (matrixB === null) throw new Error("Matrix B is required for Transpose of B.");
if (matrixB.length === 0 || matrixB[0].length === 0) throw new Error("Matrix B is empty.");
finalResult = transposeMatrix(matrixB);
}
if (finalResult !== null) {
resultDiv.textContent = formatMatrix(finalResult);
} else {
errorDiv.textContent = "An unexpected error occurred.";
}
} catch (e) {
errorDiv.textContent = "Error: " + e.message;
}
}