Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen: Efficient Kronecker Product

Tags:

c++

math

eigen

I am using Eigen for a project I am working on, where runtime performance is absolutely crucial (needs to meet real-time constraints).

So far, Eigen gives me reasonably good performance. However, I need to evaluate a Kronecker product. I am using Eigen's unsupported KroneckerProduct module, but I am thinking it is suboptimal for my needs.

The two matrices I am computing the Kronecker product with are of fixed size (known at compile time), and structure. One matrix is square and diagonal, let's assume it is an Identity matrix. The other is a small, square matrix. In code, like this:

MatrixXf I = MatrixXf::Identity(4,4);
MatrixXf X = MatrixXf::Random(8,8);
MatrixXf P = kroneckerProduct(I,X);

Since I is diagonal, I am guessing that we can make this faster since we only need to evaluate 4 matrix by scalar multiplications in order to compute all of the elements (since many will be zero).

What is the fastest and most efficient way to do this with Eigen?

like image 479
NOP Avatar asked Aug 08 '16 22:08

NOP


1 Answers

In Eigen 3.3 beta there is now (unsupported) support for sparse Kronecker products. That being said, if performance is critical, I would not yet recommend moving to 3.3 beta. Additionally, if you know that I is a diagonal matrix, you would probably get better performance writing your own. Plus, if the size is known at compile time (and not too large), you can replace MatrixXf with Matrix4f (fixed size, will be allocated on the stack, not the heap). So roll it all together and you get:

Matrix4f I4 = Matrix4f::Identity();
MatrixXf P2(I4.rows() * X.rows(), I4.cols() * X.cols());
P2.setZero();

for (int i = 0; i < I4.RowsAtCompileTime; i++)
{
    P2.block(i*X.rows(), i*X.cols(), X.rows(), X.cols()) = I4(i, i) * X;
}
like image 112
Avi Ginsburg Avatar answered Sep 21 '22 12:09

Avi Ginsburg