Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd conversion & unit tests for pMatrix & ddiMatrix #140
Conversation
|
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); | |||
coatless
Jun 20, 2017
•
Contributor
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
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
binxiangni
Jun 20, 2017
Author
Contributor
I perhaps know what you mean. Let me see...
I perhaps know what you mean. Let me see...
thirdwing
Jun 20, 2017
Member
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?
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?
binxiangni
Jun 20, 2017
Author
Contributor
Now the bug for pMatrix has been fixed. I used the matrix James provided and passed the unit test locally.
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); |
coatless
Jun 21, 2017
•
Contributor
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.
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); |
coatless
Jun 21, 2017
Contributor
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
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
|
I think I can merge this now. Any objections? |
|
Sorry for not seeing this earlier. Everything looks to be in a better state. |
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?