如何将sparsematrix.valueptr(),sparsematrix.outerindexptr()和spar
How to set SparseMatrix.valuePtr(), SparseMatrix.outerIndexPtr() and SparseMatrix.innerIndexPtr() for CSR Format?
我已经有稀疏的矩阵数据以CSR格式,即:我已经有非零值的数据(以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()
,如果可以的话?
这是我没有真正测试过的黑客。但是,它确实复制了值:
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;
}
相关文章:
- 对于eigen sparsematrix,InnerIndExptr()和exout indExptr()准确表示什么
- 将eigen :: sparsematrix转换为cusparse,反之亦然
- Converting Eigen::SparseMatrix<double> to deal.ii ::Sp
- 如何将sparsematrix.valueptr(),sparsematrix.outerindexptr()和spar
- 是否有任何窗口函数可以使用域名获取所有OU(组织单位)名称
- 在Eigen::SparseMatrix的转置处的std::bad_alloc
- 如何在预先存在的3个标准压缩行/列数组上包装Eigen::SparseMatrix
- 对于SparseMatrix实现,我们能做的最好的事情是什么?
- 使用C++获取本地计算机所属的OU(组织单位)名称
- 从Eigen::SparseMatrix中提取块/ROI而不进行复制
- 来自Linux Mint 17.3 repo的Eigen3,SparseMatrix未命名
- LDAP 查询 - ADSI 使用 c++ 筛选 OU 中的所有计算机