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

conservativeResize() for SparseVector?

Tags: None
(comma "," separated)
kfrgaricus
Registered Member
Posts
3
Karma
0
There isn't a conservativeResize() for SparseVector. I need to resize while leaving old values untouched. How do I do this for a SparseVector?
Or if copying is the only way to do it, how can i copy a smaller SparseVector to a bigger SparseVector?

In any case, I could always copy each element but there could be a better (faster) way. Thanks.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You can extend SparseVector via EIGEN_SPARSEVECTOR_PLUGIN (see http://eigen.tuxfamily.org/dox/TopicCus ... Eigen.html), and send us a patch to be included in next releases.
kfrgaricus
Registered Member
Posts
3
Karma
0
Thank you. I extended SparseVector as you suggested.
I think this code works good enough.

Code: Select all
void conservativeResize(Index newSize)
{
   m_size = newSize;
   m_data.reserve(newSize);
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
that's a bit more complicated:
1 - if newSize<m_size, then coefficients above newSize have to be removed.
2 - you should not call m_data.reserve(newSize); as this allocate the amount of memory equivalent to a dense vector.
kfrgaricus
Registered Member
Posts
3
Karma
0
Thanks for pointing that out. I got confused with nonZero(m_data) size and row size.

From the code, I guess the philosophy is to allocate memory on-demand, so no resizing m_data when newSize>m_size.
(though resizeNonZeros can be called after)
I tried some quick tests and i think this attempt seems to work.

Code: Select all
void conservativeResize(Index newSize)
{
   if (newSize < m_size)
   {
      Index i = 0;
      while (m_data.index(i)<=newSize-1)
      {
         i++;
      }
      m_data.resize(i);
      m_data.squeeze();
   }
   m_size = newSize;

}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This version is still broken ;) Anyway, here it is:

https://bitbucket.org/eigen/eigen/commits/528c88972ed4/
Summary: Add SparseVector::conservativeResize() method.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft