压缩矩阵的转置
压缩矩阵
只存储矩阵中的非 0 元素,稀疏矩阵非 0 元素的存储需同时存储该元素所在矩阵中的行标和列标。
例:
转置
将原矩阵行列互换得到新矩阵,即aij=bji.
结构体创建
typedef int elem;
typedef struct Triple{
int i;
int j;
elem e;
}*TriplePtr;
typedef struct CompressedMatrix{
int rows;
int columns;
int numElements;
Triple* elements;
}*CompressedMatrixPtr;
初始化
CompressedMatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData){
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(int 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){
for(int 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){
//Step 1. 分配空间
int tempColumn, tempPosition;
int *tempColumnCounts=(int*)malloc(paraPtr->columns * sizeof(int));
int *tempOffsets=(int*)malloc(paraPtr->columns * sizeof(int));
for(int 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));
//Step 2. 一次扫描计算开端数
for(int i=0;i<paraPtr->numElements;i++) {
tempColumnCounts[paraPtr->elements[i].j]++;
}
tempOffsets[0] = 0;
for(int i=1;i<paraPtr->columns;i++){
tempOffsets[i] = tempOffsets[i-1] + tempColumnCounts[i-1];
printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
}
//Step 3. 再次扫描填充数据
for(int 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 test(){
CompressedMatrixPtr tempPtr1, tempPtr2;
int tempElements=4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(int 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(int i=0;i<tempElements;i++){
for(int 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);
}
完整代码
#include <stdio.h>
#include <malloc.h>
typedef int elem;
typedef struct Triple{
int i;
int j;
elem e;
}*TriplePtr;
typedef struct CompressedMatrix{
int rows;
int columns;
int numElements;
Triple* elements;
}*CompressedMatrixPtr;
//初始化
CompressedMatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData){
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(int 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){
for(int 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){
//Step 1. 分配空间
int tempColumn, tempPosition;
int *tempColumnCounts=(int*)malloc(paraPtr->columns * sizeof(int));
int *tempOffsets=(int*)malloc(paraPtr->columns * sizeof(int));
for(int 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));
//Step 2. 一次扫描计算开端数
for(int i=0;i<paraPtr->numElements;i++) {
tempColumnCounts[paraPtr->elements[i].j]++;
}
tempOffsets[0] = 0;
for(int i=1;i<paraPtr->columns;i++){
tempOffsets[i] = tempOffsets[i-1] + tempColumnCounts[i-1];
printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
}
//Step 3. 再次扫描填充数据
for(int 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 test(){
CompressedMatrixPtr tempPtr1, tempPtr2;
int tempElements=4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(int 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(int i=0;i<tempElements;i++){
for(int 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(){
test();
}
运行结果
After initialization.
(0, 0): 2
(0, 2): 3
(2, 0): 5
(2, 1): 6
tempOffsets[1] = 2
tempOffsets[2] = 3
After transpose.
(0, 0): 2
(0, 2): 5
(1, 2): 6
(2, 0): 3