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

Add conversion & unit tests for pMatrix & ddiMatrix #140

Merged
merged 1 commit into from
Jun 23, 2017
Merged

Add conversion & unit tests for pMatrix & ddiMatrix #140

merged 1 commit into from
Jun 23, 2017

Conversation

binxiangni
Copy link
Contributor

In order to solve #17 and #114 . Now the conversion for all the numeric sparse matrices is done. @thirdwing @eddelbuettel When doing unit tests, I found some bugs in the package Matrix. But there is only a read-only repo on the GitHub so that I cannot open a pull request. Do you have any idea where I can contribute?

@eddelbuettel
Copy link
Member

I think Matrix lives here as an SVN repo on R-Forge. That is consistent with its DESCRIPTION file.

@@ -403,14 +403,61 @@ namespace traits {
res = symmatl(res);
}
}
else if (type == "pMatrix") {
IntegerVector i(ncol);
IntegerVector p(ncol+1);
Copy link
Contributor

@coatless coatless Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this one...

Consider:

(pm1 <- as(as.integer(c(2,3,1)), "pMatrix"))
# 3 x 3 sparse Matrix of class "pMatrix"
# 
# [1,] . | .
# [2,] . . |
# [3,] | . .
str(pm1)
# Formal class 'pMatrix' [package "Matrix"] with 4 slots
# ..@ perm    : int [1:3] 2 3 1
# ..@ Dim     : int [1:2] 3 3
# ..@ Dimnames:List of 2
# .. ..$ : NULL
# .. ..$ : NULL
# ..@ factors : list()

Built off of: https://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/pMatrix-class.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I perhaps know what you mean. Let me see...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not sure about converting permutation matrix into a numeric matrix.

Maybe we can discuss this in another issue or PR. How about removing this for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the bug for pMatrix has been fixed. I used the matrix James provided and passed the unit test locally.


// Calculate i
for(std::map<int, int>::iterator tmp = colrow.begin(); tmp != colrow.end(); tmp++){
i.push_back(tmp -> second);
Copy link
Contributor

@coatless coatless Jun 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .push_back is problematic on IntegerVector because Rcpp objects are proxy models that serve as shallow wrappers around R objects. Thus, each time you add an element, there is a significant overhead associated with copying the existing vector to a new vector. It would be much better in this case to directly use std::vector<int> and then cast at the end to IntegerVector.

std::copy(x.begin(), x.end(), arma::access::rwp(res.values));
}
else if (type == "ddiMatrix") {
IntegerVector i(ncol);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to be tweaked slightly as it does not take into account N or U classes of the ddiMatrix.

e.g.

(d_U <- Diagonal(n = 3))
# 3 x 3 diagonal matrix of class "ddiMatrix"
#     [,1] [,2] [,3]
# [1,]    1    .    .
# [2,]    .    1    .
# [3,]    .    .    1
str(d_U)
# Formal class 'ddiMatrix' [package "Matrix"] with 4 slots
#  ..@ diag    : chr "U"
#  ..@ Dim     : int [1:2] 3 3
#  ..@ Dimnames:List of 2
#  .. ..$ : NULL
#  .. ..$ : NULL
#  ..@ x       : num(0) 
(d_N <- Diagonal(x = c(10,1)))
# 2 x 2 diagonal matrix of class "ddiMatrix"
#      [,1] [,2]
# [1,]   10    .
# [2,]    .    1
str(d_N)
# Formal class 'ddiMatrix' [package "Matrix"] with 4 slots
#  ..@ diag    : chr "N"
#  ..@ Dim     : int [1:2] 2 2
#  ..@ Dimnames:List of 2
#  .. ..$ : NULL
#  .. ..$ : NULL
#  ..@ x       : num [1:2] 10 1

@eddelbuettel
Copy link
Member

I think I can merge this now. Any objections?

@coatless
Copy link
Contributor

Sorry for not seeing this earlier.

Everything looks to be in a better state.

@eddelbuettel eddelbuettel merged commit 31ce005 into RcppCore:master Jun 23, 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

Successfully merging this pull request may close these issues.

None yet

4 participants