<div dir="ltr"><div><div><div>Thanks for your explanation, Dirk! Indeed, I looked at Armadillo documentation, but it was not clear what was the kind of return.<br><br></div>Regarding the linking error, I had created the package using Rcpp.package.skeleton (because I didn't know I was going to use RcppArmadillo), and then I made the necessary changes according to documentation (<a href="http://romainfrancois.blog.free.fr/index.php?post/2010/05/19/RcppArmadillo-0.2.1">http://romainfrancois.blog.free.fr/index.php?post/2010/05/19/RcppArmadillo-0.2.1</a>). So I created the package again using "RcppArmadillo.package.skeleton" and everything worked fine!<br>
<br>Actually, what "RcppArmadillo.package.skeleton" creates is slightly different from what is described on documentation (maybe it is system dependent):<br><br></div>- Documentation<br><br># Makevars<br>PKG_LIBS = $(shell $(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()" ) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)<br>
<br># Makevars.win<br>PKG_LIBS = $(shell Rscript.exe -e "Rcpp:::LdFlags()") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)<br><br>- Documentation<br><br># Makevars<br>PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)<br>
<br></div><div># Makevars.win<br>PKG_LIBS = $(shell $(R_HOME)/bin/Rscript.exe -e "Rcpp:::LdFlags()") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)<br></div><div><br><br></div><div>So, it seems that the R libraries was not being imported during the package compilation; problem solved!<br>
<br></div>Regards,<br><br>Joćo Daniel<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/3/11 Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
<br>
On 11 March 2013 at 22:42, Joćo Daniel Nunes Duarte wrote:<br>
| Hello,<br>
|<br>
| I'm trying to calculate principal component using 'princomp' function from<br>
| RcppArmadillo. Here's the cpp code:<br>
|<br>
| #include <RcppArmadillo.h><br>
|<br>
| RcppExport SEXP pca(SEXP mats) { <br>
| try {<br>
| <br>
| Rcpp::NumericMatrix matr(mats);<br>
| int n = matr.nrow(), k = matr.ncol(); <br>
|<br>
| arma::mat mat(matr.begin(), n, k, false);<br>
| arma::colvec pca;<br>
| <br>
| pca = arma::princomp(mat);<br>
| <br>
| return Rcpp::wrap(mat);<br>
| <br>
| } catch(...) {<br>
| ::Rf_error("c++ error");<br>
| }<br>
| }<br>
|<br>
| However, when I "R CMD check" the package, I get the following error:<br>
|<br>
| ** testing if installed package can be loaded<br>
| Error in dyn.load(file, DLLpath = DLLpath, ...) :<br>
| unable to load shared object '/home/tecto/cpp/Rcpp/amora.Rcheck/amora/libs/<br>
| amora.so':<br>
| /home/tecto/cpp/Rcpp/amora.Rcheck/amora/libs/amora.so: undefined symbol:<br>
| dgesvd_<br>
| Error: loading failed<br>
| Execution halted<br>
|<br>
| I've read the Armadillo documentation for that function (http://<br>
| <a href="http://arma.sourceforge.net/docs.html#princomp" target="_blank">arma.sourceforge.net/docs.html#princomp</a>), however it was not clear to me how to<br>
| use it correctly.<br>
|<br>
| Does my code have any mistake?<br>
<br>
</div></div>Yes, several in fact. princomp() returns a matrix, not a vector. You were<br>
trying to return mat when you probably meant to return pca.<br>
<br>
As for your linking error: should not happen. On all systems, RcppArmadillo<br>
should get these BLAS functions from R by linking to R. Unless your R is<br>
built in a weird way.<br>
<br>
Here is what I get in the slightly corrected (and rewritten to use<br>
sourceCpp() instead) example below:<br>
<br>
<br>
R> sourceCpp("/tmp/joao.cpp")<br>
<br>
R> M <- matrix(1:9, 3, 3)<br>
<br>
R> localpca(M)<br>
1.0000 4.0000 7.0000<br>
2.0000 5.0000 8.0000<br>
3.0000 6.0000 9.0000<br>
<br>
0.5774 0.8165 0<br>
0.5774 -0.4082 -0.7071<br>
0.5774 -0.4082 0.7071<br>
<br>
[,1] [,2] [,3]<br>
[1,] 0.57735 0.816497 0.000000<br>
[2,] 0.57735 -0.408248 -0.707107<br>
[3,] 0.57735 -0.408248 0.707107<br>
R><br>
<br>
<br>
The program follows:<br>
<br>
-----------------------------------------------------------------------------<br>
<br>
#include <RcppArmadillo.h><br>
<br>
// [[Rcpp::depends(RcppArmadillo)]]<br>
<br>
// [[Rcpp::export]]<br>
Rcpp::NumericMatrix localpca(Rcpp::NumericMatrix matr) {<br>
<div class="im"> int n = matr.nrow(), k = matr.ncol();<br>
arma::mat mat(matr.begin(), n, k, false);<br>
</div> Rcpp::Rcout << mat << std::endl;<br>
arma::mat pca = arma::princomp(mat);<br>
Rcpp::Rcout << pca << std::endl;<br>
return Rcpp::wrap(pca);<br>
}<br>
<br>
/*** R<br>
M <- matrix(1:9, 3, 3)<br>
localpca(M)<br>
*/<br>
<br>
-----------------------------------------------------------------------------<br>
<br>
And for what it is worth, R returns the same (modulo ordering):<br>
<br>
R> prcomp(M)<br>
Standard deviations:<br>
[1] 1.73205 0.00000 0.00000<br>
<br>
Rotation:<br>
PC1 PC2 PC3<br>
[1,] 0.57735 0.000000 0.816497<br>
[2,] 0.57735 -0.707107 -0.408248<br>
[3,] 0.57735 0.707107 -0.408248<br>
<br>
<br>
<br>
Hth, Dirk<br>
<br>
|<br>
| Cheers,<br>
|<br>
| Joćo Daniel<br>
|<br>
| ----------------------------------------------------------------------<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<span class="HOEnZb"><font color="#888888">--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div>