编写一个程序代码,利用C语言采用压缩矩阵的三阶上三角矩阵实现矩阵的加法和乘法
时间: 2024-05-28 12:15:21 浏览: 12
好的,以下是用C语言编写压缩矩阵的三阶上三角矩阵实现矩阵的加法和乘法的程序代码:
//压缩矩阵的结构体定义
struct SparseMatrix {
int *val;
int *rowPtr;
int *colInd;
int rowCount;
int colCount;
int nonzeroCount;
};
//矩阵加法
void matrixPlus(struct SparseMatrix *A, struct SparseMatrix *B, struct SparseMatrix *C) {
//保证矩阵A和矩阵B的行数和列数相等
if (A->rowCount != B->rowCount || A->colCount != B->colCount) {
printf("Error: the rows and columns of matrix A and matrix B must be equal.\n");
return;
}
int i, j, k;
int pA, pB = 0, pC = 0;
//遍历上三角元素
for (i = 0; i < A->rowCount; i++) {
pA = A->rowPtr[i];
for (j = i; j < A->colCount; j++) {
//矩阵A和矩阵B中都有非零元素
if (A->colInd[pA] == j && B->colInd[pB] == j) {
C->colInd[pC] = j;
C->val[pC] = A->val[pA] + B->val[pB];
pA++, pB++, pC++;
}
//矩阵A中有非零元素,矩阵B中没有
else if (A->colInd[pA] == j && B->colInd[pB] != j) {
C->colInd[pC] = j;
C->val[pC] = A->val[pA];
pA++, pC++;
}
//矩阵B中有非零元素,矩阵A中没有
else if (A->colInd[pA] != j && B->colInd[pB] == j) {
C->colInd[pC] = j;
C->val[pC] = B->val[pB];
pB++, pC++;
}
}
//补充矩阵A中的未遍历到的0元素
while (pA < A->rowPtr[i+1]) {
C->colInd[pC] = A->colInd[pA];
C->val[pC] = A->val[pA];
pA++, pC++;
}
//补充矩阵B中的未遍历到的0元素
while (pB < B->rowPtr[i+1]) {
C->colInd[pC] = B->colInd[pB];
C->val[pC] = B->val[pB];
pB++, pC++;
}
C->rowPtr[i+1] = pC;
}
C->nonzeroCount = pC;
}
//矩阵乘法
void matrixMultiply(struct SparseMatrix *A, struct SparseMatrix *B, struct SparseMatrix *C) {
//保证矩阵A的列数等于矩阵B的行数
if (A->colCount != B->rowCount) {
printf("Error: the columns of matrix A must be equal to the rows of matrix B.\n");
return;
}
int i, j, k;
int pA, pB;
int sum;
//遍历矩阵A的每一行
for (i = 0; i < A->rowCount; i++) {
pA = A->rowPtr[i];
//遍历矩阵B的每一列
for (j = 0; j < B->colCount; j++) {
sum = 0;
pB = B->rowPtr[j];
//对于矩阵A中第i行中非零元素,其列编号为k,矩阵B中第k列中非零元素,其行编号为j,将每一项累加
for (k = i; k < A->colCount && A->colInd[pA] <= j; k++) {
if (A->colInd[pA] == j) {
sum += A->val[pA] * B->val[pB];
pA++, pB++;
} else {
pA++;
}
}
//将所有累加得到的值放入矩阵C的(i,j)位置中
if (sum != 0) {
C->colInd[C->nonzeroCount] = j;
C->val[C->nonzeroCount] = sum;
C->nonzeroCount++;
}
}
C->rowPtr[i+1] = C->nonzeroCount;
}
}
注意:上面的程序代码是针对压缩矩阵的三阶上三角矩阵的,可能并不适用于其他类型的矩阵。另外,在实际应用中,还需要考虑输入输出、内存分配等问题,需要根据具体情况进行优化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)