Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2017-06-17 Binxiang Ni <binxiangni@gmail.com>

* inst/include/RcppArmadilloAs.h: Add conversion for dtCMatrix & dsCMatrix
* inst/unitTests/runit.sparse.R: Add unit tests for the conversion for
dtCMatrix & dsCMatrix

2017-06-04 Dirk Eddelbuettel <edd@debian.org>

* configure.ac: Whitespace changes
Expand Down
77 changes: 63 additions & 14 deletions inst/include/RcppArmadilloAs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ namespace traits {
private:
MATRIX mat ;
};


// 14 June 2017
Copy link
Member

Choose a reason for hiding this comment

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

I think we don't have time in other place, right?

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 just followed the style of the source code. There are also dates there

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't matter. Let's focus on the unit test.

// Add support for dgCMatrix, dtCMatrix and dsCMatrix
template <typename T>
class Exporter< arma::SpMat<T> > {
public:
Expand All @@ -87,23 +89,70 @@ namespace traits {
const int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype;

IntegerVector dims = mat.slot("Dim");
IntegerVector i = mat.slot("i") ;
IntegerVector p = mat.slot("p") ;
Vector<RTYPE> x = mat.slot("x") ;

// Creating an empty SpMat
arma::SpMat<T> res((unsigned) dims[0], (unsigned) dims[1]);
int nrow = dims[0];
int ncol = dims[1];

// Making space for the elements
res.mem_resize((unsigned) x.size());
// Creating an empty SpMat
arma::SpMat<T> res(static_cast<unsigned>(nrow), static_cast<unsigned>(ncol));

// Copying elements
std::copy(i.begin(), i.end(), arma::access::rwp(res.row_indices));
std::copy(p.begin(), p.end(), arma::access::rwp(res.col_ptrs));
std::copy(x.begin(), x.end(), arma::access::rwp(res.values));
// Get the type of sparse matrix
std::string type = Rcpp::as<std::string>(mat.slot("class"));
Copy link
Member

@thirdwing thirdwing Jun 15, 2017

Choose a reason for hiding this comment

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

Maybe we can print some message when converting the matrix type?

Like "Converting dgCMatrix into Armadillo type".

What is your opinion? @coatless

if (type == "dgCMatrix") {
IntegerVector i = mat.slot("i");
IntegerVector p = mat.slot("p");
Vector<RTYPE> x = mat.slot("x");

// Making space for the elements
res.mem_resize(static_cast<unsigned>(x.size()));

// Copying elements
std::copy(i.begin(), i.end(), arma::access::rwp(res.row_indices));
std::copy(p.begin(), p.end(), arma::access::rwp(res.col_ptrs));
std::copy(x.begin(), x.end(), arma::access::rwp(res.values));
}
else if (type == "dtCMatrix") {
// The following 3 lines might be duplicate, but when the type == dgT or dgR, we have to include the lines inside the conditional statements rather than outside.
IntegerVector i = mat.slot("i");
IntegerVector p = mat.slot("p");
Vector<RTYPE> x = mat.slot("x");
std::string diag = Rcpp::as<std::string>(mat.slot("diag"));

// Making space for the elements
res.mem_resize(static_cast<unsigned>(x.size()));

// Copying elements
std::copy(i.begin(), i.end(), arma::access::rwp(res.row_indices));
std::copy(p.begin(), p.end(), arma::access::rwp(res.col_ptrs));
std::copy(x.begin(), x.end(), arma::access::rwp(res.values));

if (diag == "U") {
res.diag().ones();
}
}
else if (type == "dsCMatrix") {
// The following 3 lines might be duplicate, but when the type == dgT or dgR, we have to include the lines inside the conditional statements rather than outside.
IntegerVector i = mat.slot("i");
IntegerVector p = mat.slot("p");
Vector<RTYPE> x = mat.slot("x");
std::string uplo = Rcpp::as<std::string>(mat.slot("uplo"));

// Making space for the elements
res.mem_resize(static_cast<unsigned>(x.size()));

// Copying elements
std::copy(i.begin(), i.end(), arma::access::rwp(res.row_indices));
std::copy(p.begin(), p.end(), arma::access::rwp(res.col_ptrs));
std::copy(x.begin(), x.end(), arma::access::rwp(res.values));

if (uplo == "U") {
res = symmatu(res);
} else {
res = symmatl(res);
}
}

// Setting the sentinel
arma::access::rw(res.col_ptrs[(unsigned) dims[1] + 1]) =
arma::access::rw(res.col_ptrs[static_cast<unsigned>(ncol + 1)]) =
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for the static_cast usage.

std::numeric_limits<arma::uword>::max();

return res;
Expand Down
37 changes: 36 additions & 1 deletion inst/unitTests/runit.sparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,40 @@ if (.runThisTest) {
l <- list(SM, SM)
checkEquals(l, sparseList(l), msg="sparseList")
}


test.dtc2dgc <- function() {
mtxt <- c("0 0 0 3",
"0 0 7 0",
"0 0 0 0",
"0 0 0 0")
M <- as.matrix(read.table(textConnection(mtxt)))
dimnames(M) <- NULL
dtc <- Matrix(M, sparse=TRUE)
dgc <- as(dtc, "dgCMatrix")

checkEquals(dgc, asSpMat(dtc), msg="asSpMat")

dtc@diag <- "U"
dgc <- as(dtc, "dgCMatrix")
checkEquals(dgc, asSpMat(dtc), msg="asSpMat")
}

test.dsc2dgc <- function() {
mtxt <- c("10 0 1 0 3",
"0 10 0 1 0",
"1 0 10 0 1",
"0 1 0 10 0",
"3 0 1 0 10")
M <- as.matrix(read.table(textConnection(mtxt)))
dimnames(M) <- NULL
dsc <- Matrix(M, sparse=TRUE)
dgc <- as(dsc, "dgCMatrix")

checkEquals(dgc, asSpMat(dsc), msg="asSpMat")

dsc <- t(dsc)
dgc <- as(dsc, "dgCMatrix")

checkEquals(dgc, asSpMat(dsc), msg="asSpMat")
}
}