- 对M的列进行转置
先对列进行循环,再对矩阵非零元进行,列相等时,则存入转置T
- 快速转置
引入变量num[col]、cpot[col],表示每列非零元的个数以及第一个非零元的位置
取出非零元的列和第一个的位置,存入T的(cpot[col],j),cpot[col]++
读取下一个非零元
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#define OK 1
#define ERROR 0
#define maxsize 10000
typedef int datatype;
typedef int status;
typedef struct
{
int i, j;
datatype e;
}Triple;
typedef struct
{
Triple data[maxsize];
int m, n, t;
}TSMatrix;
status tranmatrix(TSMatrix M, TSMatrix* T) {
T->m = M.n, T->n = M.m, T->t = M.t;
if (T->t) {
int q = 1;
for(int col=1;col<M.n;col++)
for (int p = 1; p < M.t; p++) {
if (M.data[p].j == 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;
q++;
}
}
}
return OK;
}
status fastmatrix(TSMatrix M, TSMatrix* T) {
T->m = M.n, T->n = M.m, T->t = M.t;
int num[20];
int cpot[20];
if (T->t) {
for (int col = 1; col < M.n; col++)
num[col] = 0;
for (int t = 1; t < M.t; t++)
num[M.data[t].j]++;
cpot[1] = 1;
for (int col = 2; col < M.n; col++)
cpot[col] = cpot[col - 1] + num[col-1];
for (int p = 1; p <= M.t; p++) {
int col = M.data[p].j;
int 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]++;
}
}
return OK;
}