【问题标题】:How to Determine N of an M x N CSR Sparse Matrix如何确定 M x N CSR 稀疏矩阵的 N
【发布时间】:2020-03-03 21:20:15
【问题描述】:

我有代表 M x N 矩阵的 CSR 稀疏矩阵数组:

  • 数据
  • 室内
  • 指数

我的问题是如何确定原始维度 N。我知道 indptr 有 M + 1 个条目,因此 M 很容易确定。但是我看不到如何确定N,除非矩阵是正方形(M = N)。

任何帮助将不胜感激。

亲切的问候

约翰

【问题讨论】:

  • 哪种语言?对于 Python scipy.sparse,矩阵对象有一个 shape 参数(就像 numpy 数组一样)。从这些输入创建矩阵时,没有指定形状,它只使用 indices 数组的最大值。

标签: sparse-matrix dimensions csr


【解决方案1】:

这在很大程度上取决于 CRS/CCS 的实施。正如您所注意到的,如果矩阵有M * N 条目,则只有在使用CRS 时才能直接提取行数M(或使用CCS 时直接提取列数N)。我将尝试使用 C++ 中的 Eigen SparseMatrix 实现来回答这个问题,因为您没有指定使用哪种语言。

要使用 Eigen 初始化稀疏矩阵,您必须首先定义一个三元组容器(其中包含系数的值、行和索引的信息):

std::vector<Eigen::Triplet<double>> coefficients;
buildProblem(coefficients); //fills the vector according to problem

然后你必须声明必须包含矩阵维数的稀疏矩阵:

Eigen::SparseMatrix<double> A(m,n);
A.setFromTriplets(coefficients.begin(), coefficients.end());

要检索维度,您可以使用以下方法:

A.rows();
A.cols();
A.size(); // returns rows*cols

A.innerSize(); // returns minor dimension with respect to the storage order, i.e., the number of rows for a column-major matrix
A.outerSize(); // returns major dimension with respect to the storage order, i.e., the number of columns for a column-major matrix

A.nonZeros(); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2021-08-21
    相关资源
    最近更新 更多