对于很多元素为零的稀疏矩阵,仅存储非零元素可使矩阵操作效率更高。现有许多种稀疏矩阵的存储方式,但是多数采用相同的基本技术,即存储矩阵所有的非零元素到一个线性数组中,并提供辅助数组来描述原数组中非零元素的位置。
以下是几种常见的稀疏矩阵存储格式:
1. Coordinate Format (COO)
这种存储方式的主要优点是灵活、简单。仅存储非零元素以及每个非零元素的坐标。
使用3个数组进行存储:values, rows, andcolumn
参数:矩阵中非零元素的数量 nnz,3个数组的长度均为nnz.
2. Diagonal Storage Format (DIA)
If the sparse matrix has diagonals containing only zero elements, then the diagonal storage format can be used to reduce the amount of information needed to locate the non-zero elements. This storage format is particularly useful in many applications where the matrix arises from a finite element or finite difference discretization.
The Intel MKL diagonal storage format is specified by two arrays:values anddistance, and two parameters:ndiag, which is the number of non-empty diagonals, andlval, which is the declared leading dimension in the calling (sub)programs.
A real or complex two-dimensional array is dimensioned aslval byndiag. Each column of it contains the non-zero elements of certain diagonal ofA. The key point of the storage is that each element invalues retains the row number of the original matrix. To achieve this diagonals in the lower triangular part of the matrix are padded from the top, and those in the upper triangular part are padded from the bottom. Note that the value ofdistance(i) is the number of elements to be padded for diagonali.
An integer array with dimension ndiag. Elementi of the arraydistance is the distance betweeni-diagonal and the main diagonal. The distance is positive if the diagonal is above the main diagonal, and negative if the diagonal is below the main diagonal. The main diagonal has a distance equal to zero.
3. Compressed Sparse Row Format (CSR)
The Intel MKL compressed sparse row (CSR) format is specified by four arrays: thevalues,columns,pointerB, andpointerE. The following table describes the arrays in terms of the values, row, and column positions of the non-zero elements in a sparse matrixA.
A real or complex array that contains the non-zero elements ofA. Values of the non-zero elements ofA are mapped into thevalues array using the row-major storage mapping described above.
Element i of the integer array columns is the number of the column inA that contains thei-th value in thevalues array.
Element j of this integer array gives the index of the element in thevalues array that is first non-zero element in a rowj ofA. Note that this index is equal topointerB(j) -pointerB(1)+1.
An integer array that contains row indices, such thatpointerE(j)-pointerB(1)is the index of the element in thevalues array that is last non-zero element in a row j of A.
4. Compressed Sparse Column Format (CSC)
The compressed sparse column format (CSC) is similar to the CSR format, but the columns are used instead the rows. In other words, the CSC format is identical to the CSR format for the transposed matrix. The CSR format is specified by four arrays: values, columns, pointerB, and pointerE. The following table describes the arrays in terms of the values, row, and column positions of the non-zero elements in a sparse matrixA.
A real or complex array that contains the non-zero elements ofA. Values of the non-zero elements ofA are mapped into thevalues array using the column-major storage mapping.
Element i of the integer array rows is the number of the row inA that contains thei-th value in thevalues array.
Element j of this integer array gives the index of the element in thevalues array that is first non-zero element in a columnj ofA. Note that this index is equal topointerB(j) -pointerB(1)+1.
An integer array that contains column indices, such thatpointerE(j)-pointerB(1)is the index of the element in thevalues array that is last non-zero element in a column j ofA.
5. Skyline Storage Format
The skyline storage format is important for the direct sparse solvers, and it is well suited for Cholesky or LU decomposition when no pivoting is required.
The skyline storage format accepted in Intel MKL can store only triangular matrix or triangular part of a matrix. This format is specified by two arrays:values andpointers. The following table describes these arrays:
A scalar array. For a lower triangular matrix it contains the set of elements from each row of the matrix starting from the first non-zero element to and including the diagonal element. For an upper triangular matrix it contains the set of elements from each column of the matrix starting with the first non-zero element down to and including the diagonal element. Encountered zero elements are included in the sets.
An integer array with dimension(m+1), where m is the number of rows for lower triangle (columns for the upper triangle).pointers(i) -pointers(1)+1gives the index of element invalues that is first non-zero element in row (column)i. The value ofpointers(m+1)is set tonnz+pointers(1), wherennz is the number of elements in the arrayvalues.
6. Block Compressed Sparse Row Format (BSR)
The Intel MKL block compressed sparse row (BSR) format for sparse matrices is specified by four arrays:values,columns,pointerB, andpointerE. The following table describes these arrays.
A real array that contains the elements of the non-zero blocks of a sparse matrix. The elements are stored block-by-block in row-major order. A non-zero block is the block that contains at least one non-zero element. All elements of non-zero blocks are stored, even if some of them is equal to zero. Within each non-zero block elements are stored in column-major order in the case of one-based indexing, and in row-major order in the case of the zero-based indexing.
Element i of the integer array columns is the number of the column in the block matrix that contains thei-th non-zero block.
Element j of this integer array gives the index of the element in thecolumns array that is first non-zero block in a rowj of the block matrix.
Element j of this integer array gives the index of the element in thecolumns array that contains the last non-zero block in a rowj of the block matrix plus 1.
7. ELLPACK (ELL)
8. Hybrid (HYB)
由ELL+COO两种格式结合而成。
选择稀疏矩阵存储格式的一些经验:
1. DIA和ELL格式在进行稀疏矩阵-矢量乘积(sparse matrix-vector products)时效率最高,所以它们是应用迭代法(如共轭梯度法)解稀疏线性系统最快的格式;
2. COO和CSR格式比起DIA和ELL来,更加灵活,易于操作;
3. ELL的优点是快速,而COO优点是灵活,二者结合后的HYB格式是一种不错的稀疏矩阵表示格式;
4. 根据Nathan Bell的工作,CSR格式在存储稀疏矩阵时非零元素平均使用的字节数(Bytes per Nonzero Entry)最为稳定(float类型约为8.5,double类型约为12.5),而DIA格式存储数据的非零元素平均使用的字节数与矩阵类型有较大关 系,适合于StructuredMesh结构的稀疏矩阵(float类型约为4.05,double类型约为8.10),对于Unstructured Mesh以及Random Matrix,DIA格式使用的字节数是CSR格式的十几倍;
5. 从我使用过的一些线性代数计算库来说,COO格式常用于从文件中进行稀疏矩阵的读写,如matrix market即采用COO格式,而CSR格式常用于读入数据后进行稀疏矩阵计算。
其他相关链接:
1. Intel MKL 库中使用的稀疏矩阵格式
http://software.intel.com/sites/products/documentation/hpc/mkl/mklman/GUID-9FCEB1C4-670D-4738-81D2-F378013412B0.htm
2. Sparse Matrix Representations & Iterative Solvers, Lesson 1 by Nathan Bell
http://www.bu.edu/pasi/files/2011/01/NathanBell1-10-1000.pdf
欢迎来到我的CSDN博客:http://blog.csdn.net/anshan1984/
相关推荐
1. Re:随机采样方法整理与讲解 2. Re:稀疏矩阵存储格式总结+存储效 3. Re:稀疏矩阵存储格式总结+存储效 4. Re:机器学习降维算法一:PCA
主要介绍了Python 稀疏矩阵-sparse 存储和转换的相关资料,需要的朋友可以参考下
稀疏矩阵的人脸检测 基于sparse的人脸检测paper
稀疏矩阵节点优化 matlab 程序 !!!!!!!
c语言中的稀疏矩阵问题以及代码 希望大家喜欢
最快的稀疏矩阵乘法运算,英文版 Let A and B two n £ n matrices over a ring R (e.g., the reals or the integers) each containing at most m non-zero elements. We present a new algorithm that multiplies A...
Batched Sparse Matrix Multiplication for Accelerating Graph Convolutional Networks 对图卷积网络进行加速的批量稀疏矩阵乘法 作者的ppt的pdf版本
稀疏矩阵 稀疏矩阵库。 安装 $ npm i ml-sparse-matrix 用法 import { SparseMatrix } from "ml-sparse-matrix" ; const matrix1 = new SparseMatrix ( [ [ 1 , 2 ] , [ 3 , 4 ] , ] ) ; const matrix2 = new ...
通过两个for循环将全矩阵转换为按行三数组存储模式,时间复杂度为$O(n^2)$,运行结果见下图,转换结果的正确性可以通过后续运算过程体现出来 三数组转换为全存储 通过两个for循环,其中一个for循环对行遍历,然后第二...
如果在矩阵中,多数的元素并没有资料,称此矩阵为稀疏矩阵(sparse matrix),由于矩阵在程式中常使用二维阵列表示,二维阵列的大小与使用的记忆体空间成正比,如果多数的元素没有资料,则会造成记忆体空间的浪费,...
SparseMatrix.java
Source Code for 2009 Supercomputing Paper Implementing Sparse Matrix-Vector Multiplication on Throughput-Oriented Processors
在压缩存储稀疏矩阵的时候我们只存储极少数的有效数据。我们在这里使用三元组存储每一个有效数据,三元组按原矩阵中的位置,以行优先级先后次序依次存放。下面我们来看一下代码实现。 #include #include #include ...
C++经典学习代码 (初学,高手均适合)工具是VC++ 2005专业版 揭示C++技术内幕经典学习代码! 有注释的,但是您需要时间学习的,一分耕耘一分收获!不管您是C++初学者还是高手均适合 这些都是经典中的...
With sparse matrix computation
稀疏矩阵的存储压缩 稀疏矩阵 (Sparse Matrix) 用三元组表表示的稀疏矩阵及其转置 稀疏矩阵转置算法思想 设矩阵列数为 Cols对矩阵三元组表扫描Cols 次第 k 次检测列号为 k 的项
大气排放源清单处理模型 Sparse Matrix Operator Kernel Emissions 大气污染源源清单处理模型手册
稀疏矩阵 我的实现CSR-压缩稀疏行和CSIR-压缩稀疏(下三角)行稀疏矩阵格式。 参考 M. Yu。Balandin,E。P. Shurina“解决大型SLAE的方法”
算法主要是关于稀疏矩阵的表示比较适合初学者学习应用 sparse_representation\omp\1-grey.bmp .....................\...\1.bmp .....................\...\2.jpg .....................\...\DWT.m ...............