<div dir="ltr">First, this is not a good way of constructing a matrix to test the Cholesky decomposition because the result is rank-deficient. It is a 2 by 2 matrix of rank 1.  In fact it is the very definition of a rank-1 matrix - a column vector multiplied by its transpose.<div><br></div><div>I am surprised at the result because I can't think of a way in computer arithmetic that the off-diagonals would be different.  They should both be </div><div><br></div><div>prod(x)</div><div><br></div><div>The dput function will print the values out to full precision so try</div><div><br></div><div>dput(prod(x))</div><div>dput(x %*% t(x))</div><div><br></div><div>to see the full precision results.</div><div><br></div><div>Thirdly, R has two functions, crossprod and tcrossprod, to compute X'X and XX', respectively.  They compute one triangle of the result and copy it into the other, so the result is guaranteed to be symmetric. Try</div><div><br></div><div>dput(tcrossprod(x))</div><div><br></div><div>Finally, what are you going to do with the Cholesky decomposition?  If you are doing this simply to get the decomposition, compiling C++ code, even with all the help provided by Rcpp, is doing things the hard way. Just use the chol function in R or the cholesky decomposition in the Matrix package if you want more control.</div><br><div class="gmail_quote"><div dir="ltr">On Wed, Jul 27, 2016 at 7:40 AM Dirk Eddelbuettel <<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Sorry for the delay in responding.  Mail delivery was stuck at R-Forge for a<br>
few days it seems.<br>
<br>
On 21 July 2016 at 10:46, Jendoubi, Takoua wrote:<br>
| Dear all,<br>
|<br>
| I am using RcppArmadillo to deal with some matrix computations. Specifically I<br>
| need to cholesky factorization of some symmetric matrices.<br>
|<br>
| I am generating random vectors using Rcpp and using them to construct symmetric<br>
| matrices.<br>
|<br>
| I have an error stating that my matrix is not symmetric although it definitely<br>
| should be. Here is the example I am working with (in R):<br>
|<br>
| >x<br>
| [1] -1.6683320 -0.8597148<br>
| >x%*%t(x)<br>
|          [,1]      [,2]<br>
| [1,] 2.783332 1.4342896<br>
| [2,] 1.434290 0.7391095<br>
|<br>
| Apparently, it is a rounding-off error. Is there any way to ensure that x%*%t<br>
| (x) gives an exactly symmetric matrix to use for cholesky factorization?<br>
<br>
I don't know of an automatic way.  R surely has no native type for symmetric<br>
matrices (at least not at the SEXP level as it really only has vectors with<br>
dim attributes).  I would probably start with some helper functions at the R<br>
or C++ level.  Some may have better tricks...<br>
<br>
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" target="_blank">edd@debian.org</a><br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div></div>