When should I be using `sparse`?

前端 未结 3 2024
渐次进展
渐次进展 2021-01-04 03:10

I\'ve been looking through Matlab\'s sparse documentation trying to find whether there are any guidelines for when it makes sense to use a sparse representation rather than

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 03:50

    I am not an expert in using sparse matrices, however Mathworks does have some documentation pertaining to the operation and computation efficiency.

    Their computation complexity description:

    The computational complexity of sparse operations is proportional to nnz, the number of nonzero elements in the matrix. Computational complexity also depends linearly on the row size m and column size n of the matrix, but is independent of the product m*n, the total number of zero and nonzero elements.

    The complexity of fairly complicated operations, such as the solution of sparse linear equations, involves factors like ordering and fill-in, which are discussed in the previous section. In general, however, the computer time required for a sparse matrix operation is proportional to the number of arithmetic operations on nonzero quantities.

    Without boring you with the algorithmic details, another answer suggests you shouldn't bother with sparse for an array that is only 25% non-zeros. They offer some code for you to test on. See their post for details.

    A = sprand(2000,2000,0.25);
    tic,B = A*A;toc
    Elapsed time is 1.771668 seconds.
    
    Af = full(A);
    tic,B = Af*Af;toc
    Elapsed time is 0.499045 seconds.
    

提交回复
热议问题