本文介绍了如何为CSR格式设置SparseMatrix.valuePtr(),SparseMatrix.outerIndexPtr()和SparseMatrix.innerIndexPtr()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I already have my sparse matrix data in CSR format, ie: I already have data for non zero values ( in the form of double[]), the row and the column index ( both in the form of int[]) of the non zero values.

我的问题是,如何将它们直接分配给特征库中的稀疏矩阵?我知道稀疏矩阵中的相关字段是 valuePtr outerIndexPtr innerIndexPtr ,但是我不能直接按照以下方式设置指针:

My problem is, how can I assign them directly to Sparse Matrix in eigen library? I know that the relevant fields in Sparse Matrix are valuePtr, outerIndexPtr and innerIndexPtr, but I can't set the pointer directly as per below:

//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;

}

我不想遍历非零值并重新设置所有内容。我认为那将是低效的。

I don't want to iterate over the non zero values and set everything again. That would be inefficient, I think.

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

How to set SparseMatrix.valuePtr(), SparseMatrix.outerIndexPtr() and SparseMatrix.innerIndexPtr(), if this is possible at all?

推荐答案

感谢,这就是我解决问题的方法:

Thanks to the comment from ggael, this is how I solve the problem:

      ///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;
        }

这篇关于如何为CSR格式设置SparseMatrix.valuePtr(),SparseMatrix.outerIndexPtr()和SparseMatrix.innerIndexPtr()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 17:09