Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:42:52+00:00 2026-05-31T03:42:52+00:00

I was using Eigen matrix framework, and the SparseVector library. I was running into

  • 0

I was using Eigen matrix framework, and the SparseVector library. I was running into performance issues, and I all need from it is Sparse vector dot product. So I rolled my own SparseMatrix implemenation, hoping it’d somehow be a bit faster:

A bit of sample code:

#include <map>
using namespace std ;

struct SparseMatrix
{
  map<int, Vector> vals ;

  Vector dot( SparseMatrix& o )
  {
    SparseMatrix *LS, *RS ;

    // iterate over the smaller of the 2 
    if( vals.size() < o.vals.size() )
    {
      // walk vals
      LS = this ;
      RS = &o ;
    }
    else
    {
      LS = &o ;
      RS = this ;
    }

    // walk LS
    Vector sum = 0 ;

    for( map<int,Vector>::iterator LSIter = LS->vals.begin() ; LSIter != LS->vals.end() ; ++LSIter )
    {
      const int& key = LSIter->first ;

      // use the key, see if RS has a similar entry.
      map<int,Vector>::iterator RSIter = RS->vals.find( key );
      if( RSIter != RS->vals.end() )
        sum += RSIter->second * LSIter->second ;
    }

    return sum ;
  }
} ;

So the dot product of 2 vectors, say that had entries like:

+---------------+
| vec 1         |
| index   value |
|   2      18   |
|   7       4   |
|  18      33   |
+---------------+

+---------------+
| vec 2         |
| index   value |
|   2       1   |
|  15      87   |
|  21      92   |
+---------------+

The dot product is then 18.

So, as you can see I used std::map to do element look up, to see if an element from one vector is in another vector.

Since I’m using integer indexing and 1d arrays only, is there a way I can make the lookup faster? My sparse matrix multiply is still a bottleneck (performance of my code is only marginally faster than Eigen)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:42:53+00:00Added an answer on May 31, 2026 at 3:42 am

    Have a vector of pairs: index -> value, sorted by index. Then iterate both vectors at once. If you have the same index in two, multiply values, add this to result and go to the next pair in both vectors. Else increment iteration index on the vector where the first element of pair is smaller.

    Assuming you don’t modify data a lot, of course.

    I.e. in pseudocode:

    for i = 0, j = 0; i < vec1.size && j < vec2.size:
        if vec1[i].first == vec2[j].first:
            result += vec1[i++].second * vec2[j++].second
        elif vec1[i].first < vec2[j].first:
            i++
        else
            j++
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making use of Eigen library which promises vectorization of matrix operations. I don't
I am currently working on a C++ sparse matrix/math/iterative solver library, for a simulation
When I create a matrix using Eigen, like this: Eigen::MatrixXd M(3,3); M<< 1.0, 4.0,
Could you tell me if using a matrix library results in a faster run-time
Has anybody successfully imported the eigen library using the ndk? I was thinking about
I am getting a wrong eigen-vector (also checked by running multiple times to be
Using C#, I need a class called User that has a username, password, active
So, I'm finding the Eigen package crashes when I try to declare a matrix
Is there a way to implement using eigen something like (pseudocode): A = BooleanExpr(X)
in my project I'm making use of Eigen C++ library for linear algebra and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.