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
Changes from 1 commit
214a686
409c2f0
c74a0b8
04faf58
ba58bd2
ecbb0d0
f5d2f74
457c6c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,9 @@ namespace traits { | |
private: | ||
MATRIX mat ; | ||
}; | ||
|
||
|
||
// 14 June 2017 | ||
// Add support for dgCMatrix, dtCMatrix and dsCMatrix | ||
template <typename T> | ||
class Exporter< arma::SpMat<T> > { | ||
public: | ||
|
@@ -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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stylistically... @eddelbuettel will complain that you did not put a space between |
||
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)]) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for the |
||
std::numeric_limits<arma::uword>::max(); | ||
|
||
return res; | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.