## Sjming

from today to future, I will try my best to study and practice

# Sparse Matrix's Transpose

## Sparse Matrix

Usually, suppose that there is m$m$ rows and n$n$ cols, and t$t$ non-zero element in a matrix. Make δ=t/(mn)$\delta = t/(m*n)$, δ$\delta$ is called sparse factor. If δ0.05$\delta \le 0.05$, we could call the matrix a sparse matrix.

## Transpose Algorithm

• The data is ordered by row.

• According to matrix’s diagonal, making half of number of all element( munu$mu * nu$) tranpose is a easy way to realize matrix’s transpose.

Time Complexity: O(munu)$O(mu * nu)$

There are two methods.
• First

Requirement:
The result must orderly by col. If don’t require the result is orderly, We could do this in time O(nu)$O(nu)$.

Description:
From first row, find the elem whose row-num(i$i$) is this row, and transpose to col.

// M is source Matrix, T is target Matrix
// mu is row-num, nu is col-num, tu is non-zero-num
// .i is row, .j is col, .e is elem

Status TranposeMatrix(Matrix M, Matrix &T) {
T.mu = M.nu, T.nu = m.Mu, T.tu = M.tu;
if (T.mu) {
q = 1;
for(col = 1; col <= M.nu; ++col)
for(p = 1; p <= M.tu; ++p)
if(M.data[p].j == col) {
T.data[q].i = M.data[p].i;
T.data[q].j = M.data[p].j;
T.data[q].e = M.data[p].e;
++q;
}
}
return OK;
}

Obviously, there is nutu$nu * tu$ times operation.

Time Complexity: O(nutu)$O(nu * tu)$

If tumu$tu \ge mu$, this method is bad.

• second

Name: Fast Transpose

Description:
If we could determine every col of result serial number, we could make every data right location.

Known:
cpot[1] = 1;
cpot[col] = cpot[col- 1] + num[col- 1];

Because the source data is ordered by row, we could make every col a point to mark this col’s the data sequence.

Status FastTransposeMatrix(Matrix M, Matrix &T) {
T.mu = M.nu; T.nu = M.mu; T.tu = M.tu;
if(T.tu) {
for(col = 1; col <= M.nu; ++col) num[col] = 0;
for(t = 1; t <= M.tu; ++t) ++num[M.data[t].j];
cpot[i] = 1;

for (col = 2; col <= M.nu; ++col) cpot[col] = cpot[col - 1] + num [col - 1];
for (p= 1; p<= M.tu; ++p) {
col = M.data[p].j;
q = cpot[col];
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
++ cpot[col];
}
}
}

Obviously, there is nu+tu$nu + tu$ times operation.

Time Complexity: O(nu+tu)$O(nu + tu)$

In worst situation, tu=munu$tu = mu * nu$, this time is O(munu)$O(mu * nu)$, it still works well.

#### Sparse Matrix

2009-07-21 01:15:00

#### [leetcode] 311. Sparse Matrix Multiplication 解题报告

2016-03-13 04:19:12

#### scipy.sparse学习

2017-06-30 08:39:22

#### c语言实现矩阵的三元组表示 + 矩阵的转置

2010-12-30 12:11:00

#### 稀疏矩阵乘法(三元数组存储）

2011-10-13 19:48:32

#### 实训C++语言设计——稀疏矩阵SparseMatrix

2008-02-17 22:37:00

#### Matrix Transpose

2013-04-07 18:38:03

#### matlab-sparse函数和full函数-sparse matrix和full matrix

2013-04-23 18:06:52

#### python 中 sparse matrix的应用和基本操作

2018-05-08 23:14:16

#### Inverse transpose matrix

2011-08-21 18:16:03