What is the best way to create a sparse array in C++?

后端 未结 11 2135
心在旅途
心在旅途 2020-11-29 01:45

I am working on a project that requires the manipulation of enormous matrices, specifically pyramidal summation for a copula calculation.

In short, I need to keep

相关标签:
11条回答
  • 2020-11-29 01:48

    Small detail in the index comparison. You need to do a lexicographical compare, otherwise:

    a= (1, 2, 1); b= (2, 1, 2);
    (a<b) == (b<a) is true, but b!=a
    

    Edit: So the comparison should probably be:

    return lhs.x<rhs.x
        ? true 
        : lhs.x==rhs.x 
            ? lhs.y<rhs.y 
                ? true 
                : lhs.y==rhs.y
                    ? lhs.z<rhs.z
                    : false
            : false
    
    0 讨论(0)
  • 2020-11-29 01:50

    Eigen is a C++ linear algebra library that has an implementation of a sparse matrix. It even supports matrix operations and solvers (LU factorization etc) that are optimized for sparse matrices.

    0 讨论(0)
  • 2020-11-29 01:52

    Complete list of solutions can be found in the wikipedia. For convenience, I have quoted relevant sections as follows.

    https://en.wikipedia.org/wiki/Sparse_matrix#Dictionary_of_keys_.28DOK.29

    Dictionary of keys (DOK)

    DOK consists of a dictionary that maps (row, column)-pairs to the value of the elements. Elements that are missing from the dictionary are taken to be zero. The format is good for incrementally constructing a sparse matrix in random order, but poor for iterating over non-zero values in lexicographical order. One typically constructs a matrix in this format and then converts to another more efficient format for processing.[1]

    List of lists (LIL)

    LIL stores one list per row, with each entry containing the column index and the value. Typically, these entries are kept sorted by column index for faster lookup. This is another format good for incremental matrix construction.[2]

    Coordinate list (COO)

    COO stores a list of (row, column, value) tuples. Ideally, the entries are sorted (by row index, then column index) to improve random access times. This is another format which is good for incremental matrix construction.[3]

    Compressed sparse row (CSR, CRS or Yale format)

    The compressed sparse row (CSR) or compressed row storage (CRS) format represents a matrix M by three (one-dimensional) arrays, that respectively contain nonzero values, the extents of rows, and column indices. It is similar to COO, but compresses the row indices, hence the name. This format allows fast row access and matrix-vector multiplications (Mx).

    0 讨论(0)
  • 2020-11-29 01:52

    I would suggest doing something like:

    typedef std::tuple<int, int, int> coord_t;
    typedef boost::hash<coord_t> coord_hash_t;
    typedef std::unordered_map<coord_hash_t, int, c_hash_t> sparse_array_t;
    
    sparse_array_t the_data;
    the_data[ { x, y, z } ] = 1; /* list-initialization is cool */
    
    for( const auto& element : the_data ) {
        int xx, yy, zz, val;
        std::tie( std::tie( xx, yy, zz ), val ) = element;
        /* ... */
    }
    

    To help keep your data sparse, you might want to write a subclass of unorderd_map, whose iterators automatically skip over (and erase) any items with a value of 0.

    0 讨论(0)
  • 2020-11-29 01:55

    Since only values with [a][b][c]...[w][x][y][z] are of consequence, we only store the indice themselves, not the value 1 which is just about everywhere - always the same + no way to hash it. Noting that the curse of dimensionality is present, suggest go with some established tool NIST or Boost, at least read the sources for that to circumvent needless blunder.

    If the work needs to capture the temporal dependence distributions and parametric tendencies of unknown data sets, then a Map or B-Tree with uni-valued root is probably not practical. We can store only the indice themselves, hashed if ordering ( sensibility for presentation ) can subordinate to reduction of time domain at run-time, for all 1 values. Since non-zero values other than one are few, an obvious candidate for those is whatever data-structure you can find readily and understand. If the data set is truly vast-universe sized I suggest some sort of sliding window that manages file / disk / persistent-io yourself, moving portions of the data into scope as need be. ( writing code that you can understand ) If you are under commitment to provide actual solution to a working group, failure to do so leaves you at the mercy of consumer grade operating systems that have the sole goal of taking your lunch away from you.

    0 讨论(0)
  • 2020-11-29 01:59

    Boost has a templated implementation of BLAS called uBLAS that contains a sparse matrix.

    https://www.boost.org/doc/libs/release/libs/numeric/ublas/doc/index.htm

    0 讨论(0)
提交回复
热议问题