This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Fast matrix multiplication with only select columns

Tags: None
(comma "," separated)
aruiz
Registered Member
Posts
2
Karma
0
I have a matrix X and a vector of its column indices. I want to multiply another vector with a matrix composed of only those columns of X listed in the vector of indices. Currently I am using

Code: Select all
for (int i = 0; i < indices.size(); ++i)
    res(i) = q.dot(X.col(indices(i)));


to do this. Is there a faster way?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
if res is quite large, then better copy the set of X.col(indices(i)) columns to a temporary matrix and then perform a matrix-matrix product. (Sorry, the handling of indexed columns/rows is still missing)
aruiz
Registered Member
Posts
2
Karma
0
ggael wrote:if res is quite large, then better copy the set of X.col(indices(i)) columns to a temporary matrix and then perform a matrix-matrix product. (Sorry, the handling of indexed columns/rows is still missing)


Thanks for the reply! Unfortunately X can be quite large so copying the columns to a temporary matrix would be slower. Is there no other way?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Internally, Eigen's matrix-vector product works per block of 4 columns, so you can write a little helper function working per block of 4:

int b=4;
MatrixXd tmp(X.rows(),b);
for(int j=0;j<(X.cols()+b-1)/b; ++j) {
int c = std::max(b,X.cols()-j);
// copy the c columns into tmp
res += q.transpose() * tmp.leftCols(c);
}


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]