SciPy,scipy安裝
http://blog.csdn.net/pipisorry/article/details/41762945
一、Sparse Matrix Storage Formats
對於很多元素為零的疏鬆陣列,僅儲存非零元素可使矩陣操作效率更高。
現有許多種疏鬆陣列的儲存方式,但是多數採用相同的基本技術,即儲存矩陣所有的非零元素到一個線性數組中,並提供輔助數組來描述原數組中非零元素的位置。
1. Coordinate Format (COO)
這種儲存方式的主要優點是靈活、簡單。僅儲存非零元素以及每個非零元素的座標。
使用3個數組進行儲存:values, rows, andcolumn
-
values: 實數或複數資料,包括矩陣中的非零元素, 順序任意。
-
-
rows: 資料所處的行。
-
-
columns: 資料所處的列.
參數:矩陣中非零元素的數量 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.
-
values
-
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.
-
distance
-
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.
-
values
-
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.
-
columns
-
Element i of the integer array columns is the number of the column inA that contains thei-th value in thevalues array.
-
pointerB
-
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 .
-
pointerE
-
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.
-
values
-
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.
-
rows
-
Element i of the integer array rows is the number of the row inA that contains thei-th value in thevalues array.
-
pointerB
-
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 .
-
pointerE
-
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:
-
values
-
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.
-
pointers
-
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)+1 gives 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.
-
values
-
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.
-
columns
-
Element i of the integer array columns is the number of the column in the block matrix that contains thei-th non-zero block.
-
pointerB
-
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.
-
pointerE
-
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格式常用於讀入資料後進行疏鬆陣列計算。
【Sparse Matrix Representations & Iterative Solvers, Lesson 1 by Nathan Bell】
【稀疏線性系統 Sparse Linear Systems】
【Intel MKL 庫中使用的疏鬆陣列格式】
三、sparse matrix不同的儲存形式在sparse模組中對應如下:bsr_matrix(arg1[, shape, dtype,copy, blocksize]) Block Sparse Row matrixcoo_matrix(arg1[, shape, dtype,copy]) A sparse matrix in COOrdinate format.csc_matrix(arg1[, shape, dtype,copy]) Compressed Sparse Column matrixcsr_matrix(arg1[, shape, dtype,copy]) Compressed Sparse Row matrixdia_matrix(arg1[, shape, dtype,copy]) Sparse matrix with DIAgonal storagedok_matrix(arg1[, shape, dtype,copy]) Dictionary Of Keys based sparse matrix.lil_matrix(arg1[, shape, dtype,copy]) Row-based linked list sparse matrix
四、sparse matrix相關操作
將普通的非疏鬆陣列變為相應儲存形式的疏鬆陣列
以coo_matrix為例:1 A =coo_matrix([[1,2],[3,4]])2 按照相應儲存形式的要求構建矩陣:>>>row = np.array([0,0,1,3,1,0,0])>>>col = np.array([0,2,1,3,1,0,0])>>>data = np.array([1,1,1,1,1,1,1])>>>coo_matrix((data, (row,col)), shape=(4,4)).todense()matrix([[3, 0, 1,0], [0, 2, 0,0], [0, 0, 0,0], [0, 0, 0,1]])將疏鬆陣列橫向或者縱向合并>>>from scipy.sparse import coo_matrix, vstack>>>A = coo_matrix([[1,2],[3,4]])>>>B = coo_matrix([[5,6]])>>>vstack( [A,B] ).todense()matrix([[1, 2], [3,4], [5,6]])如果A和B的資料形式不一樣,不能合并。一個矩陣中的資料格式必須是相同的。
diags函數可以建立稀疏的對角矩陣
對於大多數(似乎只處了coo之外)疏鬆陣列的儲存格式,都可以進行slice操作,比如對於csc,csr。也可以進行arithmeticoperations,矩陣的加減乘除,速度很快。取矩陣的指定列數,比如取矩陣的第1,3,8列:matrix[:,[0,2,7]]???
sparce矩陣的讀取:可以像常規矩陣一樣通過下標讀取。也可以通過getrow(i),gecol(i)讀取特定的列或者特定的行,以及nonzero()讀取非零元素的位置。
sparse模組的官方document:http://docs.scipy.org/doc/scipy/reference/sparse.html
from:
http://blog.csdn.net/pipisorry/article/details/41762945
ref:
http://blog.sina.com.cn/s/blog_6a90ae320101aavg.html