压缩矩阵是一个特殊的矩阵表示,其中只存储非零元素及其位置。以下是该函数的简要过程描述。
计算每列的非零元素数量:
遍历原始矩阵的每一个非零元素,并增加对应列在 tempColumnCounts
数组中的计数
计算每列的偏移量:
tempOffsets
数组用于记录每列在转置矩阵中的起始位置。第一列的起始位置是0。对于每一列(从第二列开始),其起始位置是前一列的起始位置加上前一列的非零元素数量。
遍历并填入数据:
遍历原始矩阵的每一个非零元素。
确定该元素在转置矩阵中的列位置。
更新 tempOffsets
数组以反映该列已有一个元素被放置。
#include<iostream>
using namespace std;
typedef int elem;
//定义压缩矩阵
typedef struct Triple {
int i;
int j;
elem e;
} Triple, * TriplePtr;
typedef struct CompressedMatrix {
int rows, columns, numElements;
Triple* elements;
} CompressedMatrix, * CompressedMatrixPtr;
//初始化
CompressedMatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData) {
int i;
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraRows;
resultPtr->columns = paraColumns;
resultPtr->numElements = paraElements;
resultPtr->elements = (TriplePtr)malloc(paraElements * sizeof(struct Triple));
for (i = 0; i < paraElements; i++) {
resultPtr->elements[i].i = paraData[i][0];
resultPtr->elements[i].j = paraData[i][1];
resultPtr->elements[i].e = paraData[i][2];
}
return resultPtr;
}
//输出矩阵
void printCompressedMatrix(CompressedMatrixPtr paraPtr) {
int i;
for (i = 0; i < paraPtr->numElements; i++) {
printf("(%d, %d): %d\r\n", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}
}
//矩阵转置
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr) {
//分配空间
int i, tempColumn, tempPosition;
int* tempColumnCounts = (int*)malloc(paraPtr->columns * sizeof(int));
int* tempOffsets = (int*)malloc(paraPtr->columns * sizeof(int));
for (i = 0; i < paraPtr->columns; i++) {
tempColumnCounts[i] = 0;
}
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraPtr->columns;
resultPtr->columns = paraPtr->rows;
resultPtr->numElements = paraPtr->numElements;
resultPtr->elements = (TriplePtr)malloc(paraPtr->numElements * sizeof(struct Triple));
//遍历一次
for (i = 0; i < paraPtr->numElements; i++) {
tempColumnCounts[paraPtr->elements[i].j]++;
}
tempOffsets[0] = 0;
for (i = 1; i < paraPtr->columns; i++) {
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
}
//遍历填入数据
for (i = 0; i < paraPtr->numElements; i++) {
tempColumn = paraPtr->elements[i].j;
tempPosition = tempOffsets[tempColumn];
resultPtr->elements[tempPosition].i = paraPtr->elements[i].j;
resultPtr->elements[tempPosition].j = paraPtr->elements[i].i;
resultPtr->elements[tempPosition].e = paraPtr->elements[i].e;
tempOffsets[tempColumn]++;
}
return resultPtr;
}
void compressedMatrixTest() {
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for (i = 0; i < tempElements; i++) {
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}
int tempMatrix2[4][3] = { {0, 0, 2}, {0, 2, 3}, {2, 0, 5}, {2, 1, 6} };
for (i = 0; i < tempElements; i++) {
for (j = 0; j < 3; j++) {
tempMatrix1[i][j] = tempMatrix2[i][j];
}
}
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.\r\n");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.\r\n");
printCompressedMatrix(tempPtr2);
}
int main() {
compressedMatrixTest();
return 0;
}
end