0

I have a problem when calculating the product of two sparse matrices. Here ist the program:

void RandomWalk::calculateAx(const SpMat &x, const SpMat &adj_mat1, const SpMat &adj_mat2, const double &lambda, SpMat &result)
{
    SpMat Y(adj_mat1.cols(), adj_mat2.rows());
    for (int k=0; k<x.outerSize(); ++k)
    {
        for (SpMat::InnerIterator it(x,k); it; ++it)
        {
            div_t divresult;
            divresult = div (it.row(),adj_mat1.rows());
            Y.insert(divresult.quot, divresult.rem) = it.value();
        }
    }
    SpMat tmp;
    tmp = adj_mat1 * Y; // <-- error in this line
    tmp = tmp * SpMat(adj_mat2.transpose());
    result.resize(adj_mat1.rows()*adj_mat2.rows(), 1);
    result.setZero();
    for (int k=0; k<tmp.outerSize(); ++k)
    {
        for (SpMat::InnerIterator it(tmp,k); it; ++it)
        {
            result.insert(it.col()*adj_mat1.rows()+it.row(), 0) = it.value();
        }
    }
    result = lambda * result;
    result = x - result;
}

x is a Matrix of size (k,1). adj_mat1 is a matrix of size nxn and adj_mat2 of size mxm. They both are symmetric. First I have to rescale x to a matrix Y of size (nxm) (by using the first n elements as the first column, the second n as the second column ans so on. After that the matrix adj_mat1*Y*adj_mat2^T has to be calculated. This result then has again to be vectorized by writing all the columns below each other into a vector.

I get a Segmentation fault at the multiplication of adj_mat1 with Y.

The problem only occurs if adj_mat1 and adj_mat 2 are of different sizes.

If you need any more information just ask.

Thank you in advance.

Alex

Solution:

The problem was the insertion of the values. I had to change the quot and the rem at the inset statement. Now it works

Y.insert(divresult.rem, divresult.quot) = it.value();
alex
  • 84
  • 7
  • 1
    You probably compiled your code with `-DNDEBUG`, otherwise such an error should have been explicitly caught by an assert. – ggael Jan 29 '14 at 19:57
  • Yes, you're right, after removing the flag I get a much more informative error message. I forgot to remove the flag after I compiled the code for the last release. Thank you – alex Jan 30 '14 at 07:07

0 Answers0