如何为 CSR 格式设置 SparseMatrix.valuePtr()、SparseMatrix.outerIndexPtr() 和 SparseMatrix.innerIndexPtr()?
How to set SparseMatrix.valuePtr(), SparseMatrix.outerIndexPtr() and SparseMatrix.innerIndexPtr() for CSR Format?
我已经在 CSR format 中有我的稀疏矩阵数据,即:我已经有非零值的数据(以 double[]
的形式),行和列索引(都在int[]
) 的非零值的形式。
我的问题是,如何将它们直接分配给特征库中的稀疏矩阵?我知道稀疏矩阵中的相关字段是valuePtr
、outerIndexPtr
和innerIndexPtr
,但是我不能直接按照下面的方式设置指针:
//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()
?
这是一个我还没有真正测试过(最近)的 hack。它确实复制值,但是:
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
,因此您可能需要在 copying/swapping.
之前处理它
感谢,我是这样解决问题的:
///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 format 中有我的稀疏矩阵数据,即:我已经有非零值的数据(以 double[]
的形式),行和列索引(都在int[]
) 的非零值的形式。
我的问题是,如何将它们直接分配给特征库中的稀疏矩阵?我知道稀疏矩阵中的相关字段是valuePtr
、outerIndexPtr
和innerIndexPtr
,但是我不能直接按照下面的方式设置指针:
//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()
?
这是一个我还没有真正测试过(最近)的 hack。它确实复制值,但是:
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
,因此您可能需要在 copying/swapping.
感谢
///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;
}