General transpose and fast transpose algorithm for sparse matrices in general, for coefficient matrices, we use triples to store them. Thatis to store the triples of all non-0 elements of the matrix in a sequential table:
Note the premise of a transpose: the order table is ordered, priority, column second.
I. GENERAL TRANSPOSE
This algorithm is simple and easy to think about:
algorithm idea:
theM.dataScan from beginning to finish: ?when the first scan isM.dataThe column number is1The ternary group is assigned a value ofT.datain ?when the second scan isM.dataThe column number is2The ternary group is assigned a value ofT.datain ?and so on, until youM.dataall triples are assigned a value ofT.datain
Code
The transpose algorithm void Transpose2 (Tsmatrix M, Tsmatrix t) {//uses the ternary table storage representation to find the transpose matrix T t.rows=m.rows of the sparse matrix M ; T.size=m.size;; Q=1; Q is the current ternary group in t.data[] storage location (subscript) for (col=1; col<=m.cols; ++col) for (p=1; p<=m.size; ++P) if (M.data.get (p). j= =col) { t.data.get (q). I =m.data.get (P). J; T.data.get (q). J=m.data.get (P). I; T.data.get (q). Value=m.data.get (P). value; ++q;} }
second, fast transpose
This algorithm is often more difficult to understand.
algorithm idea:
?Transpose in m in ternary order, transpose results into the appropriate position in T.
?KeyvPredetermine the first non-0-dollar position in T for each column in Mvto determine these positions, the number of non-0 elements in each column of M should be evaluated before the transpose
?implementation: Set two arraysnum[],cpos[]
vNum[col]: StorageMthe firstColcolumn not 0-dollar numbervCpos[col]: StorageMthe firstColcolumn of the first non-0-yuan ternary group inT.datain the location
?Cpos[col]method of calculation:vcpos[1]=1vCpos[col]=cpos[col-1]+num[col-1](2<=col <=m.nu)
Code:
Sparse matrix Fast transpose algorithm (c language description) void transpose (Tsmatrix M, Tsmatrix &t) {//Using ternary sequential table storage representation, to find the transpose matrix T T.M=M.N of sparse matrix M; T.N=M.M; T.tu=m.tu; Assigns the number of rows of M to the number of columns of T, assigns the number of columns of M to the number of rows of T, and the total number of non-0 elements is also assigned past for (col=1; col<=m.nu; ++col) num[col]=0; Initialization assumes that each column of the original ternary group is 0 for (t=1; t<=m.tu; ++t) ++NUM[M.DATA[T].J]; Enumerates each non-0 element, M.DATA[T].J for its corresponding column cpot[1]=1; The ordinal number for the first non-0-dollar column in Col. T.data for (col=2; col<=m.t; ++col)//<span style= "Color:black;" ><span style= "Color:black;" ><span style= "Color:black;" > Storage </span><span style= "Color:black;" >m</span><span style= "Color:black;" > </span><span style= "color:black;" >col</span><span style= "Color:black;" > First non-0 yuan ternary group in </span><span style= "color:black;" >t.data</span><span style= "Color:black;" > Location </span></span></span> cpot[col]=cpot[col-1]+num[col-1]; for (p=1; p<m.tu;++P) {//Enumerate all triples col=m.data[p].j; Assigns the number of columns of the first triples to Col Q=cpot[col]; /* Remove the value of Cpot[col] in the cpot array, note that it is important here: because in the sequential table, all the row elements are sorted in order, so when we traverse to the ternary group, it must be the smallest when compared to the triples that have the same column elements behind it. So should be placed in the front, so we take this value, will be Cpot[col] value of +1, you can search in the following traversal of the value adjacent to it, just subscript has +1, directly added up can * */t.data[q].i=m.data[p ].J; T.DATA[Q].J=M.DATA[P].I; t.data[q].value=m.data[p].value;//assignment Operation ++cpot[col]; Subscript Record +1}}
General transpose and fast transpose algorithm for sparse matrices