如何将sparsematrix.valueptr(),sparsematrix.outerindexptr()和spar

How to set SparseMatrix.valuePtr(), SparseMatrix.outerIndexPtr() and SparseMatrix.innerIndexPtr() for CSR Format?

本文关键字:sparsematrix outerindexptr spar valueptr      更新时间:2023-10-16

我已经有稀疏的矩阵数据以CSR格式,即:我已经有非零值的数据(以double[]的形式),行和列索引(都在非零值的int[])的形式。

我的问题是,如何将它们直接分配给特征库中的稀疏矩阵?我知道稀疏矩阵中的相关字段是valuePtrouterIndexPtrinnerIndexPtr,但我无法直接设置指针以下:

//the relevant SpMat fields (valuePtr,outerIndexPtr,innerIndexPtr) are not able to set
static SpMat CSRFormat2(double* nonZeroPtr, int* rowIndex, 
int* colIndex, int totDOF,  int nonZeroCount)  
{
    SpMat sparseMatrix = SpMat(totDOF,totDOF);
    double *nonZ=sparseMatrix.valuePtr();
    nonZ=nonZeroPtr;
    int *outerIndex = sparseMatrix.outerIndexPtr();
    outerIndex=rowIndex;
    int *innerIndex = sparseMatrix.innerIndexPtr();
    innerIndex = colIndex;
    sparseMatrix.reserve(nonZeroCount);
    return sparseMatrix;
}

我不想在非零值上迭代并再次设置所有内容。我认为那将是低效的。

如何设置SparseMatrix.valuePtr()SparseMatrix.outerIndexPtr()SparseMatrix.innerIndexPtr(),如果可以的话?

这是我没有真正测试过的黑客。但是,它确实复制了值:

SparseMatrix<double, whatever, indexType> m;
m.resize(rows, cols);
m.makeCompressed();
m.resizeNonZeros(nnz);
memcpy((void*)(m.valuePtr()), (void*)(valueSrc), sizeof(double) * nnz);
memcpy((void*)(m.outerIndexPtr()), (void*)(outerIndexPtrSrc), sizeof(indexType) * outSz);
memcpy((void*)(m.innerIndexPtr()), (void*)(innerIndexPtrSrc), sizeof(indexType) * nnz);
m.finalize();

如果您不想复制内存,则仅分配指针(sparseMatrix.valuePtr() = nonZeroPtr;)将稍后引起问题,因为矩阵认为它拥有内存并将其删除时将其删除。您可能应该使用std::swap

最后一个说明,Eigen::SparseMatrix的索引类型可能不为int,因此您可能需要在复制/交换之前处理。

感谢Ggael的评论,这就是我解决问题的方式:

      ///CSR format: nonZeroArray, rowIndex, colIndex
      SparseMatrix<double, Eigen::RowMajor> ConstructSparseMatrix(int rowCount, int colCount, int nonZeroCount, double *nonZeroArray, int *rowIndex, int *colIndex)
        {
            Map<SparseMatrix<double, Eigen::RowMajor>> spMap(rowCount, colCount, nonZeroCount,  rowIndex, colIndex, nonZeroArray, 0);
            SparseMatrix<double, Eigen::RowMajor> matrix= spMap.eval();
            matrix.reserve(nonZeroCount);
            return matrix;
        }