一、压缩矩阵转置的概念
压缩矩阵的转置就是将压缩矩阵的行和列交换,但矩阵压缩后我们不能再按原来的方式用n*m的遍历方式进行转置操作,实际的操作就是将转置前的每一列的非零元素记录下来,然后再计算对应的转置后的压缩矩阵每一行对应的首元素的首个相对地址,然后将转置前的第i列赋值给转置后的第i行
二、代码步骤
1、定义矩阵结构体
2、初始化矩阵
3、打印矩阵
4、矩阵转置
5、测试代码
6、程序入口
7、运行结果
三、代码功能
1、定义矩阵结构体
typedef struct Triple
{
int i;
int j;
elem e;
} Triple, *TriplePtr;
typedef struct CompressedMatrix
{
int rows,columns,numElements;
Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;
2、初始化矩阵
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];
}//Of for i
return resultPtr;
}
3、打印矩阵
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
int i;
for(i = 0; i < paraPtr->numElements; i ++)
{
printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}
4、矩阵转置
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
//Step 1. Allocate space.
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;
}//Of for i
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. One scan to calculate offsets.
for(i = 0; i < paraPtr->numElements; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}//Of for i
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]);
}//Of for i
//Step 3. Another scan to fill data.
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]++;
}//Of for i
return resultPtr;
}
5、测试代码
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}
6、程序入口
int main()
{
compressedMatrixTest();
return 1;
}
7、运行结果
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
四、整体代码
#include
#include
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];
}//Of for i
return resultPtr;
}// Of initCompressedMatrix
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
int i;
for(i = 0; i < paraPtr->numElements; i ++)
{
printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}// Of printCompressedMatrix
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
//Step 1. Allocate space.
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;
}//Of for i
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. One scan to calculate offsets.
for(i = 0; i < paraPtr->numElements; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}//Of for i
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]);
}//Of for i
//Step 3. Another scan to fill data.
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]++;
}//Of for i
return resultPtr;
}//Of transposeCompressedMatrix
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}// Of main
int main()
{
compressedMatrixTest();
return 1;
}
typedef struct Triple
{
int i;
int j;
elem e;
} Triple, *TriplePtr;
typedef struct CompressedMatrix
{
int rows,columns,numElements;
Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;
2、初始化矩阵
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];
}//Of for i
return resultPtr;
}
3、打印矩阵
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
int i;
for(i = 0; i < paraPtr->numElements; i ++)
{
printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}
4、矩阵转置
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
//Step 1. Allocate space.
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;
}//Of for i
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. One scan to calculate offsets.
for(i = 0; i < paraPtr->numElements; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}//Of for i
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]);
}//Of for i
//Step 3. Another scan to fill data.
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]++;
}//Of for i
return resultPtr;
}
5、测试代码
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}
6、程序入口
int main()
{
compressedMatrixTest();
return 1;
}
7、运行结果
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
四、整体代码
#include
#include
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];
}//Of for i
return resultPtr;
}// Of initCompressedMatrix
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
int i;
for(i = 0; i < paraPtr->numElements; i ++)
{
printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}// Of printCompressedMatrix
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
//Step 1. Allocate space.
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;
}//Of for i
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. One scan to calculate offsets.
for(i = 0; i < paraPtr->numElements; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}//Of for i
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]);
}//Of for i
//Step 3. Another scan to fill data.
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]++;
}//Of for i
return resultPtr;
}//Of transposeCompressedMatrix
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}// Of main
int main()
{
compressedMatrixTest();
return 1;
}
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
int i;
for(i = 0; i < paraPtr->numElements; i ++)
{
printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}
4、矩阵转置
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
//Step 1. Allocate space.
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;
}//Of for i
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. One scan to calculate offsets.
for(i = 0; i < paraPtr->numElements; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}//Of for i
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]);
}//Of for i
//Step 3. Another scan to fill data.
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]++;
}//Of for i
return resultPtr;
}
5、测试代码
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}
6、程序入口
int main()
{
compressedMatrixTest();
return 1;
}
7、运行结果
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
四、整体代码
#include
#include
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];
}//Of for i
return resultPtr;
}// Of initCompressedMatrix
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
int i;
for(i = 0; i < paraPtr->numElements; i ++)
{
printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}// Of printCompressedMatrix
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
//Step 1. Allocate space.
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;
}//Of for i
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. One scan to calculate offsets.
for(i = 0; i < paraPtr->numElements; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}//Of for i
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]);
}//Of for i
//Step 3. Another scan to fill data.
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]++;
}//Of for i
return resultPtr;
}//Of transposeCompressedMatrix
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}// Of main
int main()
{
compressedMatrixTest();
return 1;
}
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}
6、程序入口
int main()
{
compressedMatrixTest();
return 1;
}
7、运行结果
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
四、整体代码
#include
#include
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];
}//Of for i
return resultPtr;
}// Of initCompressedMatrix
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
int i;
for(i = 0; i < paraPtr->numElements; i ++)
{
printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
}//Of for i
}// Of printCompressedMatrix
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
//Step 1. Allocate space.
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;
}//Of for i
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. One scan to calculate offsets.
for(i = 0; i < paraPtr->numElements; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}//Of for i
tempOffsets[0] = 0;
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]);
}//Of for i
//Step 3. Another scan to fill data.
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]++;
}//Of for i
return resultPtr;
}//Of transposeCompressedMatrix
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//Construct the first sample matrix.
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i ++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}//Of for i
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];
}//Of for j
}//Of for i
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("After initialization.rn");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("After transpose.rn");
printCompressedMatrix(tempPtr2);
}// Of main
int main()
{
compressedMatrixTest();
return 1;
}
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四、整体代码
#include#include 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]; }//Of for i return resultPtr; }// Of initCompressedMatrix void printCompressedMatrix(CompressedMatrixPtr paraPtr) { int i; for(i = 0; i < paraPtr->numElements; i ++) { printf("(%d, %d): %drn", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e); }//Of for i }// Of printCompressedMatrix CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr) { //Step 1. Allocate space. 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; }//Of for i 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. One scan to calculate offsets. for(i = 0; i < paraPtr->numElements; i ++) { tempColumnCounts[paraPtr->elements[i].j] ++; }//Of for i tempOffsets[0] = 0; for(i = 1; i < paraPtr->columns; i ++) { tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1]; printf("tempOffsets[%d] = %d rn", i, tempOffsets[i]); }//Of for i //Step 3. Another scan to fill data. 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]++; }//Of for i return resultPtr; }//Of transposeCompressedMatrix void compressedMatrixTest() { CompressedMatrixPtr tempPtr1, tempPtr2; int i, j, tempElements; //Construct the first sample matrix. tempElements = 4; int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*)); for(i = 0; i < tempElements; i ++) { tempMatrix1[i] = (int*)malloc(3 * sizeof(int)); }//Of for i 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]; }//Of for j }//Of for i tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1); printf("After initialization.rn"); printCompressedMatrix(tempPtr1); tempPtr2 = transposeCompressedMatrix(tempPtr1); printf("After transpose.rn"); printCompressedMatrix(tempPtr2); }// Of main int main() { compressedMatrixTest(); return 1; }



