# Diagonal of product of matrices, expression with inner product

I have two matrices $$\mathbf{A}$$ and $$\mathbf{B}$$. They are full and not square, though their dimensions implies that their product is : $$\dim (\mathbf{A}) = (n,p) \quad \text{and} \quad \dim (\mathbf{B}) = (p,n) \implies \dim (\mathbf{A}\mathbf{B})=(n,n)$$

Now, I am interested in calculating the diagonal terms of this product, but without having to calculate all the other terms in the process (this will be in a code where $$n$$ and $$p$$ are very large). I wrote it on a simple example and ended up with : $$diag(\mathbf{A}\mathbf{B}) = \sum Col\left(\mathbf{A} \cdot \mathbf{B}^\mathsf{T} \right)$$

with $$\cdot$$ the inner product between the two matrices, and $$\sum Col$$ the sum on the columns of the matrix. Sorry if my formalism is bad, I wrote it in a coding style, so please feel free to correct me.

I tested it on my code and it matches, but :

• How could I demonstrate that properly?
• Is there a theorem or a property for that? I failed to find it on the internet...

The $$i$$th term on the diagonal is the scalar product of the $$i$$th row of $$A$$ with the transpose of the $$i$$th column of $$B$$. Is that what you need?

• Thanks for your answer! Could be, would you have a reference for that? I could dig in to tell you if this is what I seek. I did not even find a wikipedia page for the "diag" operator ^^" – jeannej Sep 11 at 22:23
• This is basically just the definition of the matrix product en.wikipedia.org/wiki/Matrix_multiplication – S. Dolan Sep 11 at 22:26
• Ok I already stated that $diag(\mathbf{A}\mathbf{B})=\left[\sum_{k=1}^p a_{ik} b_{ki} \right]_{i=1..n}$ (hope the formalism is ok here). I also have $\mathbf{A}\cdot\mathbf{B} = \left[ a_{ik} b_{ik} \right]_{i=1..n \, , \, k=1..p}$, but I fail to see how to properly link the twos (like said I had to write it down... well I didn't say I was good at maths ^^) – jeannej Sep 11 at 22:35
• Your diag(AB) result is fine. However A.B. does **not ** give you this. – S. Dolan Sep 11 at 22:43
• Yes but you need to add elements like $a_{12}b_{21}$, not $a_{21}b_{21}$ – S. Dolan Sep 11 at 22:56

With the help of @S. Dolan, I finally got the demonstration I was searching for. So in case others need it, here it is step by step.

Let $$\mathbf{A}$$ and $$\mathbf{B}$$ two matrices such as:

$$\dim (\mathbf{A}) = (n,p) \quad \text{and} \quad \dim (\mathbf{B}) = (p,n)$$

As their dimensions match, their multiplication gives:

$$\mathbf{A} \mathbf{B}= \sum_{k=1}^p a_{ik}b_{kj} \quad \forall i\in[1,n] \text{ and } \forall j\in[1,n]$$

were $$a$$ and $$b$$ are the elements of $$\mathbf{A}$$ and $$\mathbf{B}$$. Taking only the diagonal terms of this product, hence the terms for which $$i=j$$, yields:

$$diag\left(\mathbf{A} \mathbf{B}\right)= \sum_{k=1}^p a_{ik}b_{ki} \quad \forall i\in[1,n]$$

Besides, their dot/inner product $$\mathbf{A} \cdot \mathbf{B}^\mathsf{T}$$ is the term-by-term product: $$\mathbf{A} \cdot \mathbf{B}^\mathsf{T} = a_{ik} b_{ki} \quad \forall i\in[1,n] \text{ and } \forall k\in [1,p]$$

So that the sum on its columns (with index $$k$$) is: $$\sum Col \left(\mathbf{A} \cdot \mathbf{B}^\mathsf{T}\right) = \sum_{k=1}^p a_{ik} b_{ki} \quad \forall i\in[1,n]$$

It can then be concluded that: $$diag\left(\mathbf{A} \mathbf{B}\right)= \sum Col \left(\mathbf{A} \cdot \mathbf{B}^\mathsf{T}\right)$$

QED