CALCULATORCASTLE

Matrix Calculator

Perform matrix operations: addition, multiplication, inverse, and determinant.

About

Matrix Calculator

A matrix, in a mathematical context, is a rectangular array of numbers, symbols or expressions arranged in rows and columns. Matrices turn up across physics, computer graphics, probability, statistics, calculus and numerical analysis, usually because they are the compact way to write a whole system of relationships at once.

The dimensions of a matrix A are written m × n, meaning A has m rows and n columns. An individual value is called an element, and a variable with two subscripts identifies it by position: given ai,j, the element a1,3 is the value in the first row and the third column.

Matrix operations such as addition, multiplication and subtraction resemble ordinary arithmetic, but they differ in ways that matter and they carry constraints on size. Below are the operations this calculator performs.

Matrix addition

Addition can only be performed on matrices of the same size, so both must be m × n. You can add two 3 × 3, 1 × 2 or 5 × 4 matrices, but not a 2 × 3 to a 3 × 2, or a 4 × 4 to a 3 × 3. The rows and columns have to match exactly.

When the sizes agree, addition works element by element: each pair in the same position is added and the result placed in the same position of a new matrix C.

A = 1234;B = 5678

Here a1,1 = 1, a1,2 = 2, b1,1 = 5 and so on, so adding the corresponding values gives:

a1,1 + b1,1 = 1 + 5 = 6 = c1,1
a1,2 + b1,2 = 2 + 6 = 8 = c1,2
a2,1 + b2,1 = 3 + 7 = 10 = c2,1
a2,2 + b2,2 = 4 + 8 = 12 = c2,2

Thus matrix C is:

C = 681012

Matrix subtraction

Subtraction works the same way, with values subtracted rather than added, and the same size requirement applies. Using the same two matrices:

a1,1b1,1 = 1 − 5 = −4 = c1,1
a1,2b1,2 = 2 − 6 = −4 = c1,2
a2,1b2,1 = 3 − 7 = −4 = c2,1
a2,2b2,2 = 4 − 8 = −4 = c2,2

which gives:

C = −4−4−4−4

Scalar multiplication

A matrix can be multiplied by a single number, called a scalar, by multiplying every element by it. Given the matrix A above and c = 5:

5 × 1234 = 5101520

Nothing about the shape changes. One consequence worth knowing: scaling an n × n matrix by c multiplies its determinant by c to the power n, not by c, because every one of the n rows has been scaled.

Matrix multiplication

Multiplying two matrices is more involved than multiplying by a scalar. The number of columns in the first matrix must match the number of rows in the second. You can multiply a 2 × 3 by a 3 × 4, but not a 2 × 3 by a 4 × 3.

When the sizes work, matrices are multiplied using the dot product: multiply the elements of a row of the first matrix by the corresponding elements of a column of the second, and sum the results into a single value. The dot product only works on sequences of equal length, which is exactly why the inner dimensions must match.

That value becomes the element in the corresponding row and column of the new matrix C. The dot product of row 1 of A with column 1 of B gives c1,1, row 1 with column 2 gives c1,2, and so on:

a1,1×b1,1 + a1,2×b2,1 + a1,3×b3,1 = c1,1

The resulting matrix has the same number of rows as the first matrix and the same number of columns as the second. Multiplying a 2 × 3 by a 3 × 4 therefore gives a 2 × 4:

121341 × 561178111111 = 202344445188

with each element coming from its own dot product:

c1,1 = 1×5 + 2×7 + 1×1 = 20
c1,2 = 1×6 + 2×8 + 1×1 = 23
c1,3 = 1×1 + 2×1 + 1×1 = 4
c1,4 = 1×1 + 2×1 + 1×1 = 4
c2,1 = 3×5 + 4×7 + 1×1 = 44
c2,2 = 3×6 + 4×8 + 1×1 = 51
c2,3 = 3×1 + 4×1 + 1×1 = 8
c2,4 = 3×1 + 4×1 + 1×1 = 8

Note that A × B does not generally equal B × A. Matrix multiplication is not commutative, and in fact just because A can be multiplied by B does not mean B can be multiplied by A: the sizes may not even permit it. It is associative, so (AB)C equals A(BC), which is what allows a chain of transformations to be combined into one matrix ahead of time.

Power of a matrix

Raising a matrix to a power means multiplying it by itself that many times, so a "power of 2" for matrix A means A². Exponents behave as they normally do, except that matrix multiplication rules apply, which means only square matrices can be raised to a power. A non-square matrix cannot be multiplied by itself, because its inner dimensions will not agree.

A2 = 13212
= 1321 × 1321
= 7647

As with ordinary exponents, A³ is A × A × A, A⁴ is A × A × A × A, and so on. A⁰ is defined as the identity matrix, for the same reason any number to the power zero is 1.

Transpose of a matrix

The transpose, usually written with a superscript T, flips a matrix over its main diagonal. Row and column indices swap, so aij in A becomes aji in AT, and an m × n matrix becomes n × m.

A = 1321
AT = 1231
B = 202344445188
BT = 204423514848

Two properties make it more than a formatting operation. Transposing twice returns the original, and the transpose of a product reverses the order: (AB)T = BTAT. A matrix equal to its own transpose is called symmetric, which is a common and very convenient property in statistics, where covariance matrices are always symmetric.

Determinant of a matrix

The determinant is a single value computed from the elements of a square matrix. It is used throughout linear algebra and calculus, including for computing an inverse and for solving systems of linear equations. Several formulas exist; the Leibniz and Laplace formulas are the two most commonly used.

For a 2 × 2 matrix the Leibniz formula is simple arithmetic:

|A| = abcd = adbc

Taking the determinant is normally written with vertical bars around the matrix. For the matrix with rows 2, 4 and 6, 8, the determinant is 2 × 8 − 4 × 6 = −8.

For a 3 × 3 matrix, the Laplace formula expands along the first row, multiplying each element by the determinant of the 2 × 2 left after deleting that element's row and column:

|A| = abcdefghi
= aefhibdfgi + cdegh
= a(eifh) − b(difg) + c(dheg)

which simplifies to the Leibniz formula for a 3 × 3: |A| = aei + bfg + cdh − ceg − bdi − afh.

A 4 × 4 and higher works the same way. Cycling through each element of the first row reduces the problem to a set of smaller determinants, with the signs alternating + − + − across the row. The 4 × 4 reduces to 3 × 3s, which reduce to 2 × 2s. As you can see, this gets tedious very quickly, though it is a method that works for any n × n matrix once the pattern is understood.

It is also unusable in practice beyond small sizes. Cofactor expansion costs on the order of n factorial operations, so a 20 × 20 determinant would need more arithmetic than any computer could finish. Real software uses Gaussian elimination instead, reducing the matrix to triangular form and multiplying the diagonal, which costs roughly n³ operations. This calculator uses elimination for the answer and shows the cofactor expansion for the working, because that is the version worth understanding.

The determinant has a geometric meaning that makes it much less arbitrary. It is the factor by which the matrix scales area in two dimensions or volume in three. A determinant of 2 doubles areas; a negative determinant means the transformation also flips orientation, turning a shape into its mirror image; and a determinant of exactly 0 means the transformation squashes space into a lower dimension, collapsing a square into a line segment. That last case is precisely why a matrix with a zero determinant has no inverse: once dimensions have been collapsed, no operation can restore them.

Inverse of a matrix

The inverse of A is written A−1, and it is the matrix satisfying A × A−1 = A−1 × A = I, where I is the identity matrix.

The identity matrix is a square matrix with 1 across its diagonal and 0 everywhere else. It is the matrix equivalent of the number 1: just as any number multiplied by 1 is unchanged, any matrix multiplied by an identity matrix of the right size is unchanged.

1001;100010001;1000010000100001

To invert a 2 × 2 matrix, swap the diagonal elements, negate the other two, and divide by the determinant:

A−1 = abcd−1
= 1det(A)dbca
= 1adbcdbca

For the matrix with rows 2, 4 and 3, 7, the determinant is 2 × 7 − 4 × 3 = 2, so the inverse is:

A = 2437
A−1 = 12×7 − 4×37−4−32
= 127−4−32
= 3.5−2−1.51

Multiplying either way round returns the identity matrix, which is the check worth doing whenever an inverse matters.

The inverse of a 3 × 3 is more tedious. It is the transpose of the cofactor matrix, called the adjugate, divided by the determinant. Beyond 3 × 3 the cofactor route becomes impractical and Gauss-Jordan elimination takes over: write the matrix beside an identity matrix of the same size, apply row operations until the left half becomes the identity, and the right half has become the inverse. That is the method this calculator uses.

One piece of practical advice that textbooks often skip: if you are solving a system of equations Ax = b, do not compute A−1 and multiply. Solving directly by elimination is faster and numerically more accurate. The inverse is a useful object to think with and rarely the right thing to actually compute.

How matrices are used

Computer graphics is the most visible application. Every rotation, scale, reflection and translation of a 3D model is a matrix, and because multiplication is associative a whole chain of them can be collapsed into a single matrix applied once per vertex. That is why graphics hardware is built around fast matrix arithmetic.

Systems of linear equations are the classical use: a system with n unknowns becomes the single equation Ax = b. Markov chains use a transition matrix whose powers give the state of a system after several steps, which is the idea behind the original PageRank algorithm. Statistics uses matrices for covariance and least-squares regression, and machine learning is largely matrix multiplication carried out at scale.

The word matrix in this sense was coined by James Joseph Sylvester in 1850, and Arthur Cayley set out matrix algebra as a subject in his memoir of 1858. Multiplication itself has kept mathematicians busy since: the schoolbook method costs n³ operations, Volker Strassen showed in 1969 that n2.807 was possible, and the current theoretical record sits near n2.371, though practical libraries stay close to the simple version.

Using this calculator

Set the size of each matrix with the row and column steppers, up to 8 × 8, then type the values. The buttons on each panel operate on that matrix alone: transpose, power, determinant, inverse and multiplication by a scalar. The row of buttons between the panels combines them: A + B, A − B, AB, and a swap that exchanges the two.

The result appears alongside the inputs with its size and, where the result is square, its determinant. The working underneath shows the arithmetic behind that particular operation, whether that is element-by-element sums, the dot products behind a product, or the cofactor expansion behind a determinant. Copy to A and Copy to B feed a result back into either panel, which is how you chain several operations together.

Common questions

Frequently asked questions

Addition and subtraction need both matrices to have exactly the same number of rows and columns, because each element is combined with the one in the same position. A 2 × 3 and a 3 × 2 contain the same six numbers, but there is no matching position for each, so the operation is undefined rather than merely awkward.

The number of columns in the first matrix must equal the number of rows in the second. A 2 × 3 times a 3 × 4 works and gives a 2 × 4: the outer dimensions survive and the inner ones cancel. A 2 × 3 times a 4 × 3 does not work at all.

Almost never. Matrix multiplication is not commutative, and swapping the order is often not even legal: if A is 2 × 3 and B is 3 × 4 then AB exists but BA does not. Where both exist and the matrices are square, the two products are still usually different, which is why the order of transformations in graphics matters.

It is the factor by which the matrix scales area in 2D or volume in 3D. A determinant of 3 triples areas, a negative one flips orientation like a mirror, and 0 means the transformation flattens space into a lower dimension. That last case is exactly why a zero determinant means no inverse exists.

Its determinant is zero, which makes it singular. Geometrically the transformation collapses space, so information is lost and nothing can undo it. This happens whenever one row or column is a multiple or a combination of the others. Only square matrices can be inverted at all.

By Gauss-Jordan elimination: the matrix is written beside an identity matrix of the same size and row operations are applied until the left half becomes the identity, at which point the right half has become the inverse. The cofactor and adjugate method taught for 3 × 3 matrices is correct but far too slow to use beyond small sizes.

It is the matrix equivalent of the number 1: multiplying any matrix by an identity matrix of the right size leaves it unchanged. It is square, with 1s down the diagonal and 0s elsewhere, and it defines what an inverse means, since a matrix times its inverse is the identity.

No. A power means repeated multiplication by itself, and a non-square matrix cannot be multiplied by itself because the inner dimensions do not match. A 2 × 3 times a 2 × 3 is undefined. Only square matrices have powers, and a power of 0 gives the identity matrix.