Matrices and Derivatives: Kronecker in the Realm of Matrices

By Behrooz Vedadian on Jan, 21st 2019math

NOTE: This article has been translated from Farsi using llama3-70b-8192 and groq

I stated in my previous post that when we consider derivatives with respect to matrices as derivatives with respect to vectorized matrices (using the same operator vec), we no longer need to deal with tensors, covectors and contravectors. Meanwhile, we need to bring the Kronecker product from that world to this world. The Kronecker product in tensor calculations creates a tensor with dimensions equal to the sum of the input tensors’ dimensions. For example, the Kronecker product of two vectors results in a matrix.

If we have two matrices A and B, their Kronecker product is defined as:

AB=[A11BA12BA1mBA21BA22BA2mBAn1BAn2BAnmB]

For example:

[3456][1001]=[3040030450600506]

This product has a beautiful property that is essential for matrix derivative calculations:

vec(AXB)=(BTA)vec(X)

For example, let’s calculate the derivative of AXB with respect to X:

AXBX=vec(AXB)vec(X)=(BTA)vec(X)vec(X)=(BTA)

Chain Rule and Product Rule

In the world of scalars, there was a set of general rule for derivatives that worked as a starting point for most of our calculations. These rules also apply to the world of matrices. One of these rules is the scalar product and another the sum rule, which I mentioned in the previous post.

Let’s see why these rules hold:

(αA+βB)Xij=vec(αA+βB)ivec(X)j=αvec(A)ivec(X)j+βvec(B)ivec(X)j

And so on.

Similarly, we can calculate the derivative of composite functions:

f(g(X))Xij=kvec(f(g(X)))ivec(g(X))kvec(g(X))kvec(X)j

And in general:

h(f(X),g(X))Xij=(h(f(X),g(X))f(X)f(X)X+h(f(X),g(X))g(X)g(X)X)ij

It seems that those who defined the matrix product rule have laid a solid foundation. Everything fits together nicely.

But what about the product rule for two functions?

f(X)g(X)X=?

If you’re tired of element-wise derivatives, you’re rightfully so; I’m tired too. So, let’s seek help from Kronecker. First, consider the function h(X,Y)=XY. The derivative of this function with respect to X and Y, using Kronecker’s relation, is:

vec(XYI)=(IX)vec(Y)XYY=(IX)vec(IXY)=(YTI)vec(X)XYX=(YTI)

Now, the derivative of h(f(X),g(X))=f(X)g(X) is:

f(X)g(X)X=(If(X))g(X)X+(g(X)TI)f(X)X

Note that the identity matrices related to f and g should not be assumed to have the same dimensions. The identity matrix I in the Kronecker product has dimensions equal to g(X), and vice versa.

There’s a small issue left to discuss, which is about the effect of transposition on the vec operator. For example, when calculating the derivative of AT with respect to A:

ATA=vec(AT)vec(A)

What is the value of vec(AT)? Does it have a relation with vec(A)? Of course, it does. Let’s consider a 3x3 matrix:

vec([123456789])=[147258369]vec([123456789]T)=[123456789]

The relation between these two matrices can be written as:

[100000000000100000000000100010000000000010000000000010001000000000001000000000001][123456789]=[147258369]

The matrices consisting of a 1 in each row, the permutation matrices, are used to reorder the rows of the vector (or matrix) that they multiply with. We call the matrix that transforms vec(A) to vec(AT) the permutation matrix T. But what about the derivative of the Kronecker product? Let’s forget about it for now. Just remember that the derivative of the Kronecker product results in something like:

ABA=(InTqmIp)(Imnvec(B))

This time, I wrote the dimensions of the identity matrices and T explicitly, as it’s crucial not to get them mixed up.

Well, that’s it for today. We’ll discuss functions like inverse and determinant of matrices in the next post and solve some problems using these tools in the final post.