# Matrix Multiplication

### Matrix Multiplication

The product of two matrices is defined *only* when the number of columns of the first matrix is the same as the number of rows of the second; in other words, it is only possible to multiply m x n and n x p size matrices. The reason for this becomes clear upon defining the product:

![](/files/-LeYoFQeCxEZRutA0drm)

![](/files/-LeYoZE7EU3IKJp_yu3Q)

![](/files/-LeYohmJIFQJ3yQmWU_9)

Another way to see is this:&#x20;

![](/files/-Lese3MiJ_QE8RNvt3Jl)

and&#x20;

![](/files/-LeseBXMNMvZAvO3pzXp)

Another way to look at multiplication through columns

![](/files/-LesoAgY4a774qoqxhrD)

4th way to multiply

![](/files/-LespXeX7f8xQN3m-aOX)

Also, the **multiplication of matrices need not be commutative**. Therefore AB =/= BA generally.&#x20;

When AB = BA then AB are said to commute. This is the case of the identity matrix.&#x20;

### Elimination

Say you have this matrix you want to solve:&#x20;

![](/files/-LevgpAxcKtEHm7M51_5)

Find the matrix you need to multiply it by to solve it. Or to get U (upper triangle which allows you to easily solve the system.&#x20;

Step 1 : You want a 0 in position (2, 1) and (3,1) - but in this example there is already a 0 in (3,1).&#x20;

![](/files/-Levgf0R-K7sWgIzgEz9)

Step 2: You want a 0 in position (3,2). So in this example you subtract 2 x row 2 from row 3 to get the 0.&#x20;

![](/files/-LeshfEOdQgueXa3Uh5H)

So now, z=5, and you can easily solve the rest.&#x20;

You can change the order in which you do multiplications with matrix multiplication.&#x20;

![](/files/-LesibEtxs5HNa3-i9kr)

Let's finish the topic of elimination.‌

Ex: 2 x 2 elimnation. Let A be a matrix as below where I can do elimination, but no pivots. I want to get from A to U to solve A. But then I want to know how is A related to U where there is a matrix L where A = LU. How do you get there?‌

First, to solve A and get U (that is then easy to solve), I multiply by my elementary matrix at the position 2, 1 (E(2,1)) because that is how I get a 0 in position 2,1. Therefore we get:​‌

![](blob:https://app.gitbook.com/7052d423-ab6d-4f74-b352-2810fe93919b)

Then to get A = L U: you need to multiply E, (2,1) by the inverse which becomes L (-4 becomes 4).​‌

![](blob:https://app.gitbook.com/65ca6b0f-36f5-4124-a81a-1508453df632)

Where L is the Lower triangle, and U is the Upper triangle.<br>

Now let's try to do this is a 3x3 matrix. What are the steps to producing elimination?&#x20;

![](/files/-LevkGMB4CUMdukH9FlO)

Now, suppose we want all the E's on the right hand side of the  equation. We get:&#x20;

![](/files/-Levkcxa2uUZiJjkHdN5)

So L is the product of inverses. Why do  we do this? Because when you multiply the non inverses, you don't get a good matrix. The multipliers go directly into L (see the number 10 in the matrix below). However, when you multiply the inverses, you get a clean L with no 0 in position 3,1. See:&#x20;

![](/files/-LevmeVEJGXniYNwn0e1)

### Cost of the operations&#x20;

Say you have a matrix nxn where n=100. How many operations will we have to do with elimination? Turns out that the operations for A and b (Ax  = b) there will be:&#x20;

![](/files/-LevviSSk8gEPCa3e1IE)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://julienbeaulieu.gitbook.io/wiki/sciences/math/linear-algebra/operations/matrix-multiplication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
