《科学计算科学计算 (13).pdf》由会员分享,可在线阅读,更多相关《科学计算科学计算 (13).pdf(17页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、2 2.5 5 SparseSparse MatricesMatricesStorage forms of matrixSparse storage formsApplication example of sparse matrix1 1S Storage torage Forms of Matrix Forms of Matrix Full storage form:store all elements of the matrix by columns.Sparse storage form:only store the nonzero elements and their position
2、s,row numbersand column numbers.Note that when sparse storage is used,the storage order of matrix elements does notchange,and they are also stored in the order of columns.A100005002007=sparse storage form of Matrix A:(1,1),1(3,1),2(2,2),5(3,4),7When the size of matrix is large,It can save a lot of s
3、torage space to use sparsestorage form.2 2GGenerate enerate S Sparse parse S Storagetorage(1)Transformation between full storage and sparse storageA=sparse(S):The matrix S is transformed into the matrix Aof sparse storage.S=full(A):The matrix Ais transformed into the matrix S of full storage form.A=
4、sparse(eye(5)A=(1,1)1(2,2)1(3,3)1(4,4)1(5,5)1 B=full(A)B=1 0 0 0 00 1 0 0 00 0 1 0 00 0 0 1 00 0 0 0 1 whosName Size Bytes Class AttributesA 5x5 128 double sparse B 5x5 200 double(2)Create sparse storage matrix directlyOther call forms of the sparse function:sparse(m,n):Generates an mn sparse matrix
5、 where all elements are zero.sparse(u,v,S):Where u,v and S are 3 equal-length vectors.S is the non-zero elementof the sparse storage matrix to be built,and u(i)and v(i)are the row and columnsubscripts of S(i),respectively.A=sparse(1,2,2,2,1,4,4,5,-7)A=(2,1)5(1,2)4(2,4)-7 B=full(A)B=0 4 0 05 0 0 -7Us
6、e the spconvert function to directly create a sparse storage matrix,the syntax is:B=spconvert(A)Among them,A is an m3 or m4 matrix,and each row represents a non-zero element,andm is the number of non-zero elements.A(i,1)represents the row of the i-th non-zero element.A(i,2)represents the column of t
7、he i-th non-zero element.A(i,3)represents the real part of the i-th non-zero element value.A(i,4)represents the imaginary part of the i-th non-zero element value.If all elements of the matrix are real numbers,the fourth column is not required.A=2,2,1;2,1,-1;2,4,3A=2 2 12 1 -12 4 3 B=spconvert(A)B=(2
8、,1)-1(2,2)1(2,4)3(3)Sparse storage of banded sparse matrixThere are two basic types of sparse matrix:sparse matrix with irregularstructure and sparse matrix with regular structure.Banded sparse matrix is a very typical sparse matrix with regular structure,which refers to a matrix where all non-zero
9、elements are concentrated on thediagonal.B,d=spdiags(A):extracts all non-zero diagonal elements from the banded sparse matrixA and assign them to the matrix B and the vector d which is composed of the positions ofnonzero diagonals.A=spdiags(B,d,m,n):generates a sparse storage matrix A of banded spar
10、se matrix,where m and n are respectively the row number and column number of the banded sparsematrix,the i-th column of matrix B is the i-th non-zero diagonal of the original bandedsparse matrix,the vector d is the position of all nonzero diagonals of the original bandedsparse matrix.A=11,0,0,12,0,0
11、;0,21,0,0,22,0;0,0,31,0,0,32;41,0,0,42,0,0;0,51,0,0,52,0A=11 0 0 12 0 00 21 0 0 22 00 0 31 0 0 3241 0 0 42 0 00 51 0 0 52 0 B,d=spdiags(A)B=0 11 120 21 220 31 3241 42 051 52 0d=-303 A=spdiags(B,d,5,6)A=(1,1)11(4,1)41(2,2)21(5,2)51(3,3)31(1,4)12(4,4)42(2,5)22(5,5)52(3,6)32By using the matrix B compos
12、ed of non-zero diagonal elements of the banded sparsematrix and the vector D composed of diagonal position,a sparse storage matrix A isgenerated after the command is executed.Use the spdiags function to generate the sparse storage Aof the banded sparse matrix:A=spdiags(B,d,m,n)Where m and n are the
13、numbers of rows and columns of the original banded matrix.B is the rpmatrix,where r=min(m,n),p is the number of all non-zero diagonals of the original bandedmatrix,the i-th column of the matrix B is the i-th non-zero diagonal of the original banded matrix.The method of getting the value is:if the nu
14、mber of elements on the non-zero diagonal isequal to r,then all elements are taken;if the number of elements on the non-zero diagonal isless than r,then zero elements should be used to replenish r elements.The principle of zero padding is:if mn(number of rows number of columns),then whend0(above the
15、 main diagonal),add 0 in the back;When m n(number of rows number of columns),then 0 is added at theback when d0.(4)Sparse storage of identity matrixThe function speye(m,n)returns an mn sparse storage identity matrix.speye(3)ans=(1,1)1(2,2)1(3,3)1xxxxx12345231411642621103215=3.3.Application Application E Example of xample of S Sparse parse MMatrixatrixSolve tridiagonal systems of equations.kf1=1;1;2;1;0;k0=2;4;6;6;1;k1=0;3;1;4;2;B=kf1,k0,k1;d=-1;0;1;A=spdiags(B,d,5,5);f=0;3;2;1;5;x=Afx=-0.16670.11112.7222-3.61118.6111
限制150内