gpt4 book ai didi

c - 稀疏矩阵的分块压缩行存储格式

转载 作者:太空宇宙 更新时间:2023-11-04 04:42:25 24 4
gpt4 key购买 nike

我正在编写一个程序,将稀疏矩阵转换为分块压缩行存储 BCRS .

我知道如何获取Rowptr、Colind(虽然不在代码中)和A_f。

代码:

    p = 0;
for (i = 0; i < m; i++) {
row_ptr[++p] = (row_ptr[p - 1] + count_blocks(A, i, n));
for (j = 0; j < n; j++) {
if (A[i][j]) {
A_f[k] = A[i][j];
k++;
}
}
}

我在上面使用的函数返回一行中的 block 数:

int count_blocks(int matrix[100][100], int row_idx, int n) {
int i;
int block_count = 0;

for (i = 0; i < n - 1; i++) {
if (matrix[row_idx][i]) {
if (!matrix[row_idx][i + 1])
block_count++;
else if (matrix[row_idx][i + 1] && i == n - 2)
block_count++;
}
else if (!matrix[row_idx][i] && i == n - 2) {
if (matrix[row_idx][i + 1])
block_count++;
break;
}
}
return block_count;

但我在收购 Nzptr 时遇到了困难。我该如何计算?

代码有点多余,我知道。

谢谢:)

最佳答案

这是一个计算 nzptr 给定大小为 5x5 的矩阵的函数。它返回 nzptr 中的项目数。它还使用 C 样式索引计算索引。

int computeNzptr(double matrix[5][5], int nzptr[])
{
int isNewBlock = 1;
int nzIndex = 0;
int nonZeroCount = 0;
int i = 0;
int j = 0;
for ( i = 0; i < 5; ++i )
{
for ( j = 0; j < 5; ++j )
{
if ( matrix[i][j] != 0.0 )
{
++nonZeroCount;
if ( isNewBlock )
{
isNewBlock = 0;
nzptr[nzIndex++] = nonZeroCount-1;
}
}
else
{
isNewBlock = 1;
}
}
}
return nzIndex;
}

附言

在引用图片中,描述和答案不匹配。当你有一个 5x5 矩阵时,它与发布图像中的矩阵相同,

double matrix[5][5] =
{
{5.0, 1.0, 7.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 2.0, 3.0},
{0.0, 2.0, 4.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 3.0, 0.0},
{0.0, 6.0, 0.0, 0.0, 3.0}
};

根据描述输出应该是:

0 3 4 6 8 10 11

这与引用中的输出不同。

我假设引用文档中的描述是正确的,输出是不正确的。

关于c - 稀疏矩阵的分块压缩行存储格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24977648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com