繁体   English   中英

从给定的稀疏矩阵中提取对角矩阵

[英]Extract diagonal matrix from a given sparse matrix

我想使用RcppEigen仅提取稀疏矩阵的对角线作为稀疏矩阵。 看起来很容易-在下面可以找到我的尝试,没有一个能达到我想要的结果。 请注意,您尝试5无法编译且无法正常工作。 这是我使用的一些资源。 RCPP画廊KDE论坛并在同一职位KDE论坛(2) , 本征稀疏教程SO 感觉像我接近了……也许没有……我会让专家们决定。

// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
#include <Eigen/SparseCore>

// [[Rcpp::export]]
Eigen::SparseMatrix<double> diag_mat1(Eigen::Map<Eigen::SparseMatrix<double> > &X){
  // cannot access diagonal of mapped sparse matrix
  const int n(X.rows());
  Eigen::VectorXd dii(n);
  for (int i = 0; i < n; ++i) {
     dii[i] = X.coeff(i,i);
  }
  Eigen::SparseMatrix<double> ans(dii.asDiagonal());
  return ans;
}

// [[Rcpp::export]]
Eigen::SparseMatrix<double> diag_mat2(Eigen::SparseMatrix<double> &X){
  Eigen::SparseVector<double> dii(X.diagonal().sparseView());
  Eigen::SparseMatrix<double> ans(dii);
  return ans;
}

// [[Rcpp::export]]
Eigen::SparseMatrix<double> diag_mat3(Eigen::SparseMatrix<double> &X){
  Eigen::VectorXd dii(X.diagonal());
  Eigen::SparseMatrix<double> ans(dii.asDiagonal());
  ans.pruned(); //hoping this helps
  return ans;
}

// [[Rcpp::export]]
Eigen::SparseMatrix<double> diag_mat4(Eigen::SparseMatrix<double> &X){
  Eigen::SparseMatrix<double> ans(X.diagonal().asDiagonal());
  return ans;
}

// [[Rcpp::export]]
Eigen::SparseMatrix<double> diag_mat5(Eigen::SparseMatrix<double> &X){
  struct keep_diag {
    inline bool operator() (const int& row, const int& col, const double&) const
    { return row==col; }
  };
  Eigen::SparseMatrix<double> ans(X.prune(keep_diag()));
  return ans;
}


/***R
library(Matrix)
set.seed(42)
nc <- nr <- 5
m  <- rsparsematrix(nr, nc, nnz = 10)
diag_mat1(m)
diag_mat2(m)
diag_mat3(m)
diag_mat4(m)

*/

编辑:添加了每次尝试给出的结果;

> diag_mat1(m)
5 x 5 sparse Matrix of class "dgCMatrix"

[1,] 0  .     . . .  
[2,] . -0.095 . . .  
[3,] .  .     0 . .  
[4,] .  .     . 2 .  
[5,] .  .     . . 1.5
> diag_mat2(m)
5 x 1 sparse Matrix of class "dgCMatrix"

[1,]  .    
[2,] -0.095
[3,]  .    
[4,]  2.000
[5,]  1.500
> diag_mat3(m)
5 x 5 sparse Matrix of class "dgCMatrix"

[1,] 0  .     . . .  
[2,] . -0.095 . . .  
[3,] .  .     0 . .  
[4,] .  .     . 2 .  
[5,] .  .     . . 1.5
> diag_mat4(m)
5 x 5 sparse Matrix of class "dgCMatrix"

[1,] 0  .     . . .  
[2,] . -0.095 . . .  
[3,] .  .     0 . .  
[4,] .  .     . 2 .  
[5,] .  .     . . 1.5

EDIT2:添加了所需的输出;

5 x 5 sparse Matrix of class "dgCMatrix"               
[1,] .  .     . . .  
[2,] . -0.095 . . .  
[3,] .  .     . . .  
[4,] .  .     . 2 .  
[5,] .  .     . . 1.5

灵感得益于ALEH 答案 ;

Eigen::SparseMatrix<double> diag_mat6(Eigen::Map<Eigen::SparseMatrix<double> > &X){
  const int n(X.rows());
  Eigen::SparseMatrix<double> dii(n, n);
  for (int i = 0; i < n; ++i) {
    if (X.coeff(i,i) != 0.0 ) dii.insert(i, i) = X.coeff(i,i);
  }
  dii.makeCompressed();
  return dii;
}

我更喜欢RcppArmadillo,因为它的行为通常比RcppEigen更像R。

对于您的问题,使用RcppArmadillo ,您可以执行以下操作:

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::sp_mat extractDiag(const arma::sp_mat& x) {

  int n = x.n_rows;
  arma::sp_mat res(n, n);

  for (int i = 0; i < n; i++)
    res(i, i) = x(i, i);

  return res;
}

如@mtall所建议,您可以简单地使用:

// [[Rcpp::export]]
arma::sp_mat extractDiag3(const arma::sp_mat& x) {
  return arma::diagmat(x);
}

如果您真的想在Eigen中执行此操作,请从文档中获得:

// [[Rcpp::export]]
Eigen::SparseMatrix<double> extractDiag2(Eigen::Map<Eigen::SparseMatrix<double> > &X){

  int n = X.rows();
  Eigen::SparseMatrix<double> res(n, n);
  double d;

  typedef Eigen::Triplet<double> T;
  std::vector<T> tripletList;
  tripletList.reserve(n);
  for (int i = 0; i < n; i++) {
    d = X.coeff(i, i);
    if (d != 0) tripletList.push_back(T(i, i, d));
  }
  res.setFromTriplets(tripletList.begin(), tripletList.end());

  return res;
}

我认为您只需要跳过对角线中的零个元素:

 for (int i = 0; i < n; ++i) {
     if (X.coeff(i,i) != 0.0)
        dii[i] = X.coeff(i,i);
     }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM