这段代码实现了一个压缩矩阵(稀疏矩阵)的基本操作,包括初始化、打印和转置。以下是代码的主要功能和结构:
1. 数据结构定义:
- Triple:表示矩阵中的一个非零元素,包含元素的行索引、列索引和元素值。
- CompressedMatrix:表示压缩矩阵,包含行数、列数、非零元素的数量和一个指向Triple数组的指针。
2. 函数功能:
- initCompressedMatrix:根据提供的数据初始化一个压缩矩阵。
- printCompressedMatrix:打印压缩矩阵中的所有非零元素及其位置。
- transposeCompressedMatrix:计算并返回压缩矩阵的转置。这个函数首先统计每列的非零元素数量,然后计算每列在转置矩阵中的起始位置,最后根据这些信息填充转置矩阵。
3. 测试函数:
- compressedMatrixTest:测试上述功能。首先创建一个具体的矩阵,然后初始化压缩矩阵,打印原始矩阵,进行转置,并打印转置后的矩阵。
4. 主函数:
- main:调用compressedMatrixTest函数来执行测试。
#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(sizeof(struct Triple)*paraElements);
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++) {
cout << "(" << paraPtr->elements[i].i << ", " << paraPtr->elements[i].j << "): " << paraPtr->elements[i].e << endl;
}
}
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];
cout << "tempOffsets[" << i << "] = " << tempOffsets[i] << endl;
}
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);
cout << "After initialization." << endl;
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
cout << "After transpose." << endl;
printCompressedMatrix(tempPtr2);
}
int main() {
compressedMatrixTest();
return 0;
}
下面是运行结果: