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 support for dtCMatrix&dsCMatrix #135

Merged
merged 8 commits into from Jun 18, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 63 additions & 14 deletions inst/include/RcppArmadilloAs.h
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"){
Copy link
Contributor

Choose a reason for hiding this comment

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

Stylistically... @eddelbuettel will complain that you did not put a space between ) and {.

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