0

The following function should extract the row indices, column indices and non-zeros of a sparse matrix of type T.

template<typename T>
tuple<uvec,uvec,Col<T>> find(SpMat<T> A)
{
    SpMat<T>::const_iterator start = A.begin();
    SpMat<T>::const_iterator end   = A.end();

    auto n = std::distance(start,end);
    uvec row_ind(n);
    uvec col_ind(n);
    Col<T> nz_vals(n);

    int i = 0;
    for(SpMat<T>::const_iterator it = start; it != end; ++it)
    {
        row_ind[i] = it.row();
        col_ind[i] = it.col();
        nz_vals[i] = (*it);
       i++;
    }
    return make_tuple(row_ind, col_ind, nz_vals);
}

Unfortunately it doesn't work. On the other hand if I replace the typeparameter by double instead, it works

typedef double T;
tuple<uvec,uvec,Col<T>> find(SpMat<T> A)
{
    SpMat<T>::const_iterator start = A.begin();
    SpMat<T>::const_iterator end   = A.end();

    auto n = std::distance(start,end);
    uvec row_ind(n);
    uvec col_ind(n);
    Col<T> nz_vals(n);

    int i = 0;
    for(SpMat<T>::const_iterator it = start; it != end; ++it)
    {
        row_ind[i] = it.row();
        col_ind[i] = it.col();
        nz_vals[i] = (*it);
       i++;
    }
    return make_tuple(row_ind, col_ind, nz_vals);
}

Can anyone explain this behavior?

René Hiemstra
  • 117
  • 1
  • 4

0 Answers0