[英]Sparse matrix compressed on rows in C++
I have to implement the CSR matrix data structure in C++ using 3 dynamic arrays (indexing starts at 0) and I've got stuck.我必须使用 3 个动态数组(索引从 0 开始)在 C++ 中实现 CSR 矩阵数据结构,但我被卡住了。 So I have to implement 2 functions:
所以我必须实现2个功能:
1) modify(int i, int j, TElem e)
- modifies the value of (i,j) to e or adds if (if it does not exist) or deletes it if e is null. 1)
modify(int i, int j, TElem e)
- 将 (i,j) 的值修改为 e 或添加 if(如果不存在)或在 e 为空时删除它。
2) element(int i, int j) const
- returns the value found on (i,j) 2)
element(int i, int j) const
- 返回在 (i,j) 上找到的值
I wanted to test my code in the next way:我想以下一种方式测试我的代码:
Matrix m(4,4);矩阵 m(4,4); m.print();
m.print(); It will print:
它会打印:
Lines: 0 0 0 0 0行:0 0 0 0 0
Columns:列:
Values:价值观:
(And this is fine) (这很好)
Now if I want to modify: m.modify(1,1,5);现在如果我想修改: m.modify(1,1,5); //The element (1,1) will be set to 5
//元素(1,1)将被设置为5
The output of m.print(); m.print() 的输出; will be:
将会:
Lines: 0 1 1 1 1行数:0 1 1 1 1
Columns: 1列:1
Values: 5 (which again is fine)值:5(这也很好)
And now if I want to print m.element(1, 1) it will return 0 and m.element(0, 1) will return 5.现在如果我想打印 m.element(1, 1) 它将返回 0 而 m.element(0, 1) 将返回 5。
This is my implementation of element(int i, int j) :这是我对 element(int i, int j) 的实现:
int currCol;
for (int pos = this->lines[i]; pos < this->lines[i+1]; pos++) {
currCol = this->columns[pos];
if (currCol == j)
return this->values[pos];
else if (currCol > j)
break;
}
return NULL_TELEM;
The constructor looks like this:构造函数如下所示:
Matrix::Matrix(int nrLines, int nrCols) {
if (nrLines <= 0 || nrCols <= 0)
throw exception();
this->nr_lines = nrLines;
this->nr_columns = nrCols;
this->values = new TElem[100];
this->values_capacity = 1;
this->values_size = 0;
this->lines = new int[nrLines + 1];
this->columns = new TElem[100];
this->columns_capacity = 1;
this->columns_size = 0;
for (int i = 0; i <= nrLines; i++)
this->lines[i] = NULL_TELEM;
}
This is the "modify" method:这是“修改”方法:
TElem Matrix::modify(int i, int j, TElem e) {
if (i < 0 || j < 0 || i >= this->nr_lines || j >= nr_columns)
throw exception();
int pos = this->lines[i];
int currCol = 0;
for (; pos < this->lines[i + 1]; i++) {
currCol = this->columns[pos];
if (currCol >= j)
break;
}
if (currCol != j) {
if (!(e == 0))
add(pos, i, j, e);
}
else if (e == 0)
remove(pos, i);
else
this->values[pos] = e;
return NULL_TELEM;
}
And this is the inserting method:这是插入方法:
void Matrix::add(int index, int line, int column, TElem value)
{
this->columns_size++;
this->values_size++;
for (int i = this->columns_size; i >= index + 1; i--) {
this->columns[i] = this->columns[i - 1];
this->values[i] = this->values[i - 1];
}
this->columns[index] = column;
this->values[index] = value;
for (int i = line; i <= this->nr_lines; i++) //changed to i = line + 1;
this->lines[i]++;
}
Can somebody help me, please?有人可以帮我吗? I can't figure out why this happens and I really need to finish this implementation these days.
我不知道为什么会发生这种情况,这些天我真的需要完成这个实现。
It just can't pass the next test.它只是无法通过下一个测试。 And if I want to print the elements i have (4,0)=0 (4,1)=0 ... (4,8)=0 and (4,9)=3.
如果我想打印元素,我有 (4,0)=0 (4,1)=0 ... (4,8)=0 和 (4,9)=3。 Now this looks pretty weird why it happens.
现在这看起来很奇怪为什么会发生。
void testModify() {
cout << "Test modify" << endl;
Matrix m(10, 10);
for (int j = 0; j < m.nrColumns(); j++)
m.modify(4, j, 3);
for (int i = 0; i < m.nrLines(); i++)
for (int j = 0; j < m.nrColumns(); j++)
if (i == 4)
assert(m.element(i, j) == 3);
//cout << i << " " << j << ":" << m.element(i, j)<<'\n';
else
assert(m.element(i, j) == NULL_TELEM);
}
When you call modify(1, 1, 5)
with an empty matrix (all zeros), that results in a call to add(0, 1, 1, 5)
.当您使用空矩阵(全为零)调用
modify(1, 1, 5)
时,会导致调用add(0, 1, 1, 5)
。 That increments columns_size
and values_size
(both to 1
), the for loop body will not execute, you update columns[0]
to 1
and values[0]
to 5
, then increment all the lines
values starting at element lines[1]
, setting them all to 1 ( lines[0]
will still be 0
).这会增加
columns_size
和values_size
(都为1
),for 循环体将不会执行,您将columns[0]
更新为1
并将values[0]
为5
,然后从元素lines[1]
开始增加所有lines
值,设置它们全部为 1( lines[0]
仍将是0
)。 But lines[1]
should indicate the element we just added, so it should be 0
, since the value is found using columns[0]
.但是
lines[1]
应该表示我们刚刚添加的元素,所以它应该是0
,因为该值是使用columns[0]
。
The for loop at the end of add
should start at element line + 1
. add
末尾的 for 循环应该从元素line + 1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.