Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in creating a unit diagonal sparse matrix #149

Closed
binxiangni opened this issue Jul 23, 2017 · 4 comments
Closed

Error in creating a unit diagonal sparse matrix #149

binxiangni opened this issue Jul 23, 2017 · 4 comments

Comments

@binxiangni
Copy link
Contributor

binxiangni commented Jul 23, 2017

Error occurred when creating a unit diagonal sparse matrix whose size is the same as another sparse matrix's. The error message is:

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

using namespace Rcpp;
using namespace arma;

//[[Rcpp::export]]
sp_mat speye_(sp_mat X) {
  // Create a unit diagonal sparse matrix whose size is the same as X's.  
  return speye(size(X)); 
}

/*** R
library(Matrix)
dtT <- new("dtTMatrix", x= c(3,7), i= 0:1, j=3:2, Dim= as.integer(c(4,4)))
speye_(dtT)

dgT <- new("dgTMatrix",
          i = as.integer(c(1,1,0,3,3)),
          j = as.integer(c(2,2,4,0,0)), x=10*1:5, Dim=4:5)
speye_(dgT)

# Output
# > speye_(dtT)
# 4 x 4 sparse Matrix of class "dgCMatrix"
#            
# [1,] 1 . . .
# [2,] . 1 . .
# [3,] . . 1 .
# [4,] . . . 1
# 
# > speye_(dgT)
# 4 x 5 sparse Matrix of class "dgCMatrix"
# Error in validObject(x) : 
#  invalid class “dgCMatrix” object: slot p must be non-decreasing

*/

@thirdwing
Copy link
Member

thirdwing commented Jul 23, 2017

Actually, this has nothing to do with your code in this GSOC. This is a corner case we didn't think about.

Another smaller example to reproduce this error:

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
//
using namespace Rcpp;
using namespace arma;

//[[Rcpp::export]]
sp_mat test() {
  return speye(4, 5);
}

/*
> Rcpp::sourceCpp("test.cpp")
> library(Matrix)
> test()
4 x 5 sparse Matrix of class "dgCMatrix"
Error in validObject(x) : 
  invalid class “dgCMatrix” object: slot p must be non-decreasing
*/

@binxiangni
Copy link
Contributor Author

binxiangni commented Jul 23, 2017

But after loading the package "Matrix", your error message disappeared while mine came out.

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
//
using namespace Rcpp;
using namespace arma;

//[[Rcpp::export]]
sp_mat test() {
  return speye(4, 5);
}

/*
> Rcpp::sourceCpp("test.cpp")
> test()
Error in getClass("dgCMatrix") : “dgCMatrix” is not a defined class 
> library(Matrix)
> test()
Error in validObject(x) : 
  invalid class “dgCMatrix” object: slot p must be non-decreasing
*/

Your example might prove there might not be a problem in as<>(), but the problem might exist in wrap().

@thirdwing
Copy link
Member

I have updated my error message. The problem is in wrap.

@thirdwing
Copy link
Member

I think we met the error because the p slot in dgCMatrix from Matrix pkg is slightly different from the col_ptrs in arma.

thirdwing pushed a commit to thirdwing/RcppArmadillo that referenced this issue Jul 23, 2017
thirdwing pushed a commit to thirdwing/RcppArmadillo that referenced this issue Jul 24, 2017
thirdwing pushed a commit to thirdwing/RcppArmadillo that referenced this issue Jul 24, 2017
thirdwing pushed a commit to thirdwing/RcppArmadillo that referenced this issue Jul 24, 2017
thirdwing pushed a commit to thirdwing/RcppArmadillo that referenced this issue Aug 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants