<div dir="ltr">Yup, that works. I actually forgot to increase -edgelist- by 1 (from "<span style="font-size:12.8px">adjmat_ans[edgelist] <- 1</span>" to "<span style="font-size:12.8px">adjmat_ans[edgelist + 1] <- 1"</span>) since it was indexing elements from 0, so what you got was OK. Anyway, I got the patched version and now everything works fine. So no problems from this end!<div><br></div><div>Best,</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>George G. Vega Yon<br>+1 (626) 381 8171<br><a href="http://cana.usc.edu/vegayon" target="_blank">http://cana.usc.edu/vegayon</a></div></div></div></div></div></div>
<br><div class="gmail_quote">On Mon, Oct 9, 2017 at 10:58 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hi George,<br>
<span class=""><br>
On 9 October 2017 at 10:46, George Vega Yon wrote:<br>
| Hi Dirk,<br>
|<br>
| It seems that something's going on with element modification of<br>
| arma::sp_mat objects. While in most of the cases I use batch insertion to<br>
<br>
</span>It looks like we got to the bottom reasonably quicky, but that start with<br>
some off-list emails by @fprive who also created a nice minimal example.<br>
<br>
I _think_ a one-line fix of adding sync() before wrap() builds its return<br>
object may do it.<br>
<br>
Can you take a look at what we have now at #178 in the RcppArmadillo repo?<br>
<br>
[ Never mind, you provided such a great example I did ... ]<br>
<br>
Turns out you identified the same issue as @fprive, and this should be fixed<br>
now.  Output from my box below.<br>
<br>
This gets us output, but from a quick glance it does not match what you get<br>
from Matrix.  Can you take a look at what I have below?<br>
<br>
Thanks so much for helping to find this!<br>
<br>
Dirk<br>
<br>
R> Rcpp::sourceCpp("/tmp/<wbr>rcppArma178p2.cpp")<br>
rcppArma178p2.cpp: In function ‘arma::sp_mat edgelist_to_adjmat_minimal(<wbr>Rcpp::IntegerMatrix, int)’:<br>
rcppArma178p2.cpp:10:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]<br>
<span class="">   for (unsigned int i = 0u; i < edgelist.nrow(); i++)<br>
</span>                             ~~^~~~~~~~~~~~~~~~~<br>
rcppArma178p2.cpp: In function ‘arma::sp_mat edgelist_to_adjmat_minimal2(<wbr>Rcpp::IntegerMatrix, int)’:<br>
rcppArma178p2.cpp:22:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]<br>
<span class="">   for (unsigned int i = 0u; i < edgelist.nrow(); i++)<br>
</span>                             ~~^~~~~~~~~~~~~~~~~<br>
rcppArma178p2.cpp: In function ‘arma::sp_mat edgelist_to_adjmat_minimal3(<wbr>Rcpp::IntegerMatrix, int)’:<br>
rcppArma178p2.cpp:35:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]<br>
<span class="">   for (unsigned int i = 0u; i < edgelist.nrow(); i++)<br>
</span>                             ~~^~~~~~~~~~~~~~~~~<br>
<br>
R> library(Matrix)<br>
<br>
R> # Creating a random edgelist with n vertices<br>
R> n <- 5<br>
<br>
R> set.seed(1)<br>
<br>
R> edgelist <- unique(matrix(<a href="http://sample.int" rel="noreferrer" target="_blank">sample.int</a>(n, 10, TRUE), ncol=2) - 1L)<br>
<br>
R> # Coercing to adjacency matrix<br>
R> edgelist_to_adjmat_minimal(<wbr>edgelist, n)<br>
<span class="">5 x 5 sparse Matrix of class "dgCMatrix"<br>
<br>
[1,] . . . . .<br>
</span>[2,] 1 . . . 1<br>
[3,] . . . 1 .<br>
<span class="">[4,] . . . . .<br>
</span>[5,] . . . 1 .<br>
<br>
R> edgelist_to_adjmat_minimal2(<wbr>edgelist, n)<br>
<span class="">5 x 5 sparse Matrix of class "dgCMatrix"<br>
<br>
[1,] . . . . .<br>
</span>[2,] 1 . . . 1<br>
[3,] . . . 1 .<br>
<span class="">[4,] . . . . .<br>
</span>[5,] . . . 1 .<br>
<br>
R> edgelist_to_adjmat_minimal3(<wbr>edgelist, n)<br>
<span class="">5 x 5 sparse Matrix of class "dgCMatrix"<br>
<br>
[1,] . . . . .<br>
</span>[2,] 1 . . . 1<br>
[3,] . . . 1 .<br>
<span class="">[4,] . . . . .<br>
</span>[5,] . . . 1 .<br>
<br>
R> # This is what we should get<br>
R> adjmat_ans <- matrix(0, ncol=n, nrow=n)<br>
<br>
R> adjmat_ans[edgelist] <- 1<br>
<br>
R> methods::as(adjmat_ans, "dgCMatrix")<br>
<span class="">5 x 5 sparse Matrix of class "dgCMatrix"<br>
<br>
[1,] . . . 1 .<br>
[2,] . . 1 . .<br>
[3,] . . . . .<br>
[4,] . . 1 . .<br>
[5,] . . . . .<br>
<br>
</span>R> sessionInfo()<br>
<span class="">R version 3.4.2 (2017-09-28)<br>
Platform: x86_64-pc-linux-gnu (64-bit)<br>
</span>Running under: Ubuntu 17.04<br>
<br>
Matrix products: default<br>
BLAS: /usr/lib/openblas-base/<wbr>libblas.so.3<br>
LAPACK: /usr/lib/<a href="http://libopenblasp-r0.2.19.so" rel="noreferrer" target="_blank">libopenblasp-r0.2.19.<wbr>so</a><br>
<br>
locale:<br>
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8<br>
 [8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C<br>
<span class=""><br>
attached base packages:<br>
[1] stats     graphics  grDevices utils     datasets  methods   base<br>
<br>
other attached packages:<br>
</span>[1] Matrix_1.2-11 dang_0.0.7<br>
<span class=""><br>
loaded via a namespace (and not attached):<br>
</span>[1] compiler_3.4.2            tools_3.4.2               RcppArmadillo_0.8.100.1.0 Rcpp_0.12.13              grid_3.4.2                lattice_0.20-35           fortunes_1.5-4<br>
R><br>
<div><div class="h5"><br>
<br>
<br>
<br>
| create arma::sp_mat objects, in some others I have no other way of doing so<br>
| but by looping through a list of elements to be added(modified). I've<br>
| created a minimal example in which I try to create an adjacency matrix (an<br>
| square matrix of size N) by filling its cells according to an edgelist (a<br>
| matrix with 2 columns providing coordinates). I ran the code using Valgrind<br>
| and no memory leaks were detected, but the code is not behaving as I would<br>
| expect. Here is the code:<br>
|<br>
| ---------------- edgelist_to_adjmat_minimal.cpp --------------------------<br>
|<br>
| #include <RcppArmadillo.h><br>
| using namespace Rcpp;<br>
|<br>
| // EXAMPLE 1: Element access with -at- and in-place addition<br>
| // [[Rcpp::export]]<br>
| arma::sp_mat edgelist_to_adjmat_minimal(<wbr>IntegerMatrix edgelist, int n) {<br>
|<br>
|   arma::sp_mat adjmat(n,n);<br>
|   for (unsigned int i = 0u; i < edgelist.nrow(); i++)<br>
|     <a href="http://adjmat.at" rel="noreferrer" target="_blank">adjmat.at</a>(<a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i,0u), <a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i, 1u)) += 1.0;<br>
|<br>
|   return adjmat;<br>
|<br>
| }<br>
|<br>
| // EXAMPLE 2: Element access with -at- and addition.<br>
| // [[Rcpp::export]]<br>
| arma::sp_mat edgelist_to_adjmat_minimal2(<wbr>IntegerMatrix edgelist, int n) {<br>
|<br>
|   arma::sp_mat adjmat(n,n);<br>
|   for (unsigned int i = 0u; i < edgelist.nrow(); i++)<br>
|     <a href="http://adjmat.at" rel="noreferrer" target="_blank">adjmat.at</a>(<a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i,0u), <a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i, 1u)) =<br>
|       <a href="http://adjmat.at" rel="noreferrer" target="_blank">adjmat.at</a>(<a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i,0u), <a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i, 1u)) + 1.0;<br>
|<br>
|   return adjmat;<br>
|<br>
| }<br>
|<br>
| // EXAMPLE 3: Element access with -()- and addition<br>
| // [[Rcpp::export]]<br>
| arma::sp_mat edgelist_to_adjmat_minimal3(<wbr>IntegerMatrix edgelist, int n) {<br>
|<br>
|   arma::sp_mat adjmat(n,n);<br>
|   for (unsigned int i = 0u; i < edgelist.nrow(); i++)<br>
|     adjmat(<a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i,0u), <a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i, 1u)) =<br>
|       adjmat(<a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i,0u), <a href="http://edgelist.at" rel="noreferrer" target="_blank">edgelist.at</a>(i, 1u)) + 1.0;<br>
|<br>
|   return adjmat;<br>
|<br>
| }<br>
|<br>
| /***R<br>
| library(Matrix)<br>
|<br>
| # Creating a random edgelist with n vertices<br>
| n <- 5<br>
| set.seed(1)<br>
| edgelist <- unique(matrix(<a href="http://sample.int" rel="noreferrer" target="_blank">sample.int</a>(n, 10, TRUE), ncol=2) - 1L)<br>
|<br>
| # Coercing to adjacency matrix<br>
| edgelist_to_adjmat_minimal(<wbr>edgelist, n)<br>
| edgelist_to_adjmat_minimal2(<wbr>edgelist, n)<br>
| edgelist_to_adjmat_minimal3(<wbr>edgelist, n)<br>
|<br>
| # This is what we should get<br>
| adjmat_ans <- matrix(0, ncol=n, nrow=n)<br>
| adjmat_ans[edgelist] <- 1<br>
| methods::as(adjmat_ans, "dgCMatrix")<br>
|<br>
| sessionInfo()<br>
| */<br>
|<br>
| ---------------- edgelist_to_adjmat_minimal.cpp --------------------------<br>
|<br>
|<br>
| And here is what I get<br>
|<br>
|<br>
| ----------------- begin of output ------------------------------<br>
| --------------<br>
| > Rcpp::sourceCpp("playground/<wbr>edgelist_to_adjmat_minimal.<wbr>cpp")<br>
|<br>
| > library(Matrix)<br>
|<br>
| > # Creating a random edgelist with n vertices<br>
| > n <- 5<br>
|<br>
| > set.seed(1)<br>
|<br>
| > edgelist <- unique(matrix(<a href="http://sample.int" rel="noreferrer" target="_blank">sample.int</a>(n, 10, TRUE), ncol=2) - 1L)<br>
|<br>
| > # Coercing to adjacency matrix<br>
| > edgelist_to_adjmat_minimal(<wbr>edgelist, n)<br>
| 5 x 5 sparse Matrix of class "dgCMatrix"<br>
|<br>
| [1,] . . . . .<br>
| [2,] . . . . .<br>
| [3,] . . . . .<br>
| [4,] . . . . .<br>
| [5,] . . . . .<br>
|<br>
| > edgelist_to_adjmat_minimal2(<wbr>edgelist, n)<br>
| 5 x 5 sparse Matrix of class "dgCMatrix"<br>
|<br>
| [1,] . . . . .<br>
| [2,] . . . . .<br>
| [3,] . . . . .<br>
| [4,] . . . . .<br>
| [5,] . . . . .<br>
|<br>
| > edgelist_to_adjmat_minimal3(<wbr>edgelist, n)<br>
| 5 x 5 sparse Matrix of class "dgCMatrix"<br>
|<br>
| [1,] . . . . .<br>
| [2,] . . . . .<br>
| [3,] . . . . .<br>
| [4,] . . . . .<br>
| [5,] . . . . .<br>
|<br>
| > # This is what we should get<br>
| > adjmat_ans <- matrix(0, ncol=n, nrow=n)<br>
|<br>
| > adjmat_ans[edgelist] <- 1<br>
|<br>
| > methods::as(adjmat_ans, "dgCMatrix")<br>
| 5 x 5 sparse Matrix of class "dgCMatrix"<br>
|<br>
| [1,] . . . 1 .<br>
| [2,] . . 1 . .<br>
| [3,] . . . . .<br>
| [4,] . . 1 . .<br>
| [5,] . . . . .<br>
|<br>
| > sessionInfo()<br>
| R version 3.4.2 (2017-09-28)<br>
| Platform: x86_64-pc-linux-gnu (64-bit)<br>
| Running under: Ubuntu 14.04.5 LTS<br>
|<br>
| Matrix products: default<br>
| BLAS: /usr/lib/libblas/libblas.so.3.<wbr>0<br>
| LAPACK: /usr/lib/lapack/liblapack.so.<wbr>3.0<br>
|<br>
| locale:<br>
|  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C<br>
|  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8<br>
|  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8<br>
|  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C<br>
|  [9] LC_ADDRESS=C               LC_TELEPHONE=C<br>
| [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C<br>
|<br>
| attached base packages:<br>
| [1] stats     graphics  grDevices utils     datasets  methods   base<br>
|<br>
| other attached packages:<br>
| [1] Matrix_1.2-11<br>
|<br>
| loaded via a namespace (and not attached):<br>
| [1] compiler_3.4.2            tools_3.4.2<br>
| [3] RcppArmadillo_0.8.100.1.0 Rcpp_0.12.13<br>
| [5] grid_3.4.2                lattice_0.20-35<br>
|<br>
| ------------------------- end of output ------------------------------<br>
| -----------<br>
|<br>
| I use three different examples thinking that perhaps some element accessing<br>
| method was no longer valid, or even the in-place addition was no longer<br>
| working.<br>
|<br>
| Anything looks wrong in what I've written? From what I've seen in my test<br>
| results, all of the errors that I've checked so far have to do with this.<br>
|<br>
| Best,<br>
|<br>
|<br>
| George G. Vega Yon<br>
</div></div>| <a href="tel:%2B1%20%28626%29%20381%208171" value="+16263818171">+1 (626) 381 8171</a> <(626)%20381-8171><br>
<div class="HOEnZb"><div class="h5">| <a href="http://cana.usc.edu/vegayon" rel="noreferrer" target="_blank">http://cana.usc.edu/vegayon</a><br>
|<br>
| On Sat, Oct 7, 2017 at 3:15 PM, Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br>
|<br>
| ><br>
| > RcppArmadillo 0.8.100.1.0 is available in the drat repo for the RcppCore<br>
| > org.<br>
| > See the README.md at <a href="https://github.com/RcppCore/drat" rel="noreferrer" target="_blank">https://github.com/RcppCore/<wbr>drat</a> which has this<br>
| > exammple:<br>
| ><br>
| >    # first add the repo<br>
| >    drat:::add("RcppCore")<br>
| >    # either install just one or more given packages<br>
| >    install.packages("<wbr>RcppArmadillo")<br>
| >    # or update already installed packages<br>
| >    update.packages()<br>
| ><br>
| > You can also add the repo URL by hand to options("repos"), or supply it to<br>
| > install.packages(), or ...  I happen to like drat. The files NEWS.Rd and<br>
| > ChangeLog have the goods, I make a fuller announcement if and when it makes<br>
| > it to CRAN.<br>
| ><br>
| > There are a rathre fair number of upstream changes in here -- making it the<br>
| > first Armadillo release by Conrad with an 8.* number.  It also has further<br>
| > sparse matrix improvements from our end thanks to Serguei and Binxiang.<br>
| ><br>
| > I had uploaded this to CRAN based on a reverse depends check ... where the<br>
| > previous version was still in the loadpath. Ooops. So I overlooked a few<br>
| > build or test errors for which I need a bit of help from the maintainers.<br>
| > In<br>
| > particular, packages<br>
| ><br>
| >    biglasso<br>
| >    bigstatsr<br>
| >    HSAR<br>
| >    netdiffuseR<br>
| >    repolr<br>
| ><br>
| > now come up as failing their tests.  I am BCCing the maintainers and kindly<br>
| > ask if they could take a look too --- I don't always have all suggested<br>
| > packages installed.<br>
| ><br>
| > In the case of HSAR, I found that a one life of code needs a changed<br>
| > imposed<br>
| > by Conrad as the sparse-to-dense conversion no longer works with with a<br>
| > copy. This patch covers it:<br>
| ><br>
| > diff -ru HSAR.orig/src/diagnostics.cpp HSAR/src/diagnostics.cpp<br>
| > --- HSAR.orig/src/diagnostics.cpp       2016-05-24 13:58:45.000000000 -0500<br>
| > +++ HSAR/src/diagnostics.cpp    2017-10-07 13:24:31.471883248 -0500<br>
| > @@ -10,7 +10,7 @@<br>
| ><br>
| >    sp_mat SW = I_sp+rho*W+pow(rho,2)*(W*W)+<wbr>pow(rho,3)*(W*W*W)+pow(rho,4)*<wbr>(<br>
| > W*W*W*W)+pow(rho,5)*(W*W*W*W*<wbr>W);<br>
| ><br>
| > -  vec d = SW.diag();<br>
| > +  vec d(SW.diag());<br>
| ><br>
| >    direct  = sum( d )/n * betas ;<br>
| >    total  = accu( SW )/n * betas ;<br>
| ><br>
| ><br>
| > For the other packages, I had less luck as the failures are generally in<br>
| > the<br>
| > tests and often require packages I have not installed (here at home, and<br>
| > for<br>
| > technical I don't currently have access to the machine where I usually run<br>
| > the tests).<br>
| ><br>
| > Sp I would be grateful if the maintainers of packages<br>
| ><br>
| >    biglasso, bigstatsr, netdiffuseR, repolr<br>
| ><br>
| > (or also any interested volunteers) could take a peek.   It seems of the<br>
| > now<br>
| > over 400 CRAN package using RcppArmadillo, all others are fine too -- and I<br>
| > suspect these four also need only small and simple changes.  Happy to<br>
| > discuss<br>
| > here.<br>
| ><br>
| ><br>
| > Cheers, Dirk<br>
| ><br>
| ><br>
| > --<br>
| > <a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
| ><br>
<br>
--<br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</div></div></blockquote></div><br></div>