<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Cambria, sans-serif;"><div><div><div>Soren and Doug,</div><div><br></div><div>Thanks for the info about the sparse matrices.  I'll give it a go and seen what happens.  If it works out, then perhaps I'll be able to provide a nice RcppGallery example.</div><div><br></div><div>Joshua</div><div><font face="Cambria">- - <br>
Joshua French, Ph.D.<br>
Assistant Professor<br>
Department of Mathematical and Statistical Sciences<br>
University of Colorado Denver<br>
Joshua.French@ucdenver.edu<br>
http://math.ucdenver.edu/~jfrench/<br>
Ph:  303-556-6253  Fax:  303-556-8550</font></div></div></div><div><br></div><span id="OLK_SRC_BODY_SECTION"><div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt"><span style="font-weight:bold">From: </span> Søren Højsgaard <<a href="mailto:sorenh@math.aau.dk">sorenh@math.aau.dk</a>><br><span style="font-weight:bold">Date: </span> Friday, January 31, 2014 at 4:22 PM<br><span style="font-weight:bold">To: </span> Douglas Bates <<a href="mailto:bates@stat.wisc.edu">bates@stat.wisc.edu</a>>, Joshua French <<a href="mailto:joshua.french@ucdenver.edu">joshua.french@ucdenver.edu</a>><br><span style="font-weight:bold">Cc: </span> "<a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a>" <<a href="mailto:rcpp-devel@r-forge.wu-wien.ac.at">rcpp-devel@r-forge.wu-wien.ac.at</a>><br><span style="font-weight:bold">Subject: </span> RE: [Rcpp-devel] Sparse matrix operations<br></div><div><br></div><div xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><meta name="Generator" content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Cambria;
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.hoenzb
        {mso-style-name:hoenzb;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Courier New";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:3.0cm 2.0cm 3.0cm 2.0cm;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--><div lang="DA" link="blue" vlink="purple"><div class="WordSection1"><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);">I’ll just add that I have used the sparse matrices in RcppEigen for representing graphs as adjacency matrices, as well as for implementing graph algorithms
 (with some help from Doug Bates; thanks Doug!). At the time when I implemented that I don’t think sparse matrices were available in RcppArmadillo, so I can’t tell which is easier.
<o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);">A nice feature of the sparse matrices in RcppEigen is that there are iterators for those classes such that one can iterate over a sparse matrix efficiently
 by only visiting the “non-zero elements”; this is used in the gRbase package.<o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);">I find the documentation of Armadillo somewhat easier to read than that of Eigen (perhaps less abstract), but it is well worth spending the time digging
 into the Eigen documentation. <o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);">All the best<o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);">Søren<o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size: 11pt; font-family: 'Courier New'; color: rgb(31, 73, 125);"><o:p> </o:p></span></p><p class="MsoNormal"><b><span lang="EN-US" style="font-size: 10pt; font-family: Tahoma, sans-serif;">From:</span></b><span lang="EN-US" style="font-size: 10pt; font-family: Tahoma, sans-serif;"> <a href="mailto:rcpp-devel-bounces@lists.r-forge.r-project.org">rcpp-devel-bounces@lists.r-forge.r-project.org</a> [<a href="mailto:rcpp-devel-bounces@lists.r-forge.r-project.org">mailto:rcpp-devel-bounces@lists.r-forge.r-project.org</a>]
<b>On Behalf Of </b>Douglas Bates<br><b>Sent:</b> 31. januar 2014 21:45<br><b>To:</b> French, Joshua<br><b>Cc:</b> <a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a><br><b>Subject:</b> Re: [Rcpp-devel] Sparse matrix operations<o:p></o:p></span></p><p class="MsoNormal"><o:p> </o:p></p><div><div><div><p class="MsoNormal">On Fri, Jan 31, 2014 at 1:14 PM, French, Joshua <<a href="mailto:JOSHUA.FRENCH@ucdenver.edu" target="_blank">JOSHUA.FRENCH@ucdenver.edu</a>> wrote:<o:p></o:p></p><div><div><div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;">Hello everyone,<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;"><o:p> </o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;">For those of you who have used the sparse matrix capabilities of Armadillo/RcppArmadillo, how seamless are the operations in moving between sparse and dense matrices?  I know
 there are some tricks for getting sparseMatrix objects into SpMat objects (Dirk has a nice Rcpp Gallery post on this), but is it just as easy when it comes to doing Linear Algebra? My google ninja skills didn't seem to produce any helpful information.<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;"><o:p> </o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;">E.g., if A is a dense matrix and B is a sparse matrix in Armadillo, it is pretty much seamless to do calculations like A + B, A*B, solve(B, A), etc.?  <o:p></o:p></span></p></div></div></div></div><div><p class="MsoNormal"><o:p> </o:p></p></div><div><p class="MsoNormal">I'm not that familiar with the sparse matrix capabilities of Armadillo but I do know those of Eigen fairly well.  Operations on sparse matrices require more care and thought than do those on dense matrices.  It is possible to use "one size
 fits all" algorithms but they can be slow or inaccurate compared to more specialized approaches to operations like solve(B,A).  For dense matrices you could use an LU factorization or a QR factorization on a symmetric B and never notice the difference relative
 to using a Cholesky factorization.  With sparse matrices you could notice the difference.<o:p></o:p></p></div><div><p class="MsoNormal"><o:p> </o:p></p></div><div><p class="MsoNormal">Operations like A + B would not be terribly meaningful because the result would be dense if A is dense so you may as well extract a dense version of B and use that.  A * B is definitely available in Eigen and I imagine also available with
 Armadillo.  A general solve(B, A) could be written using a sparse LU but that may not be an effective way of solving such a system.  Most sparse matrix operations have a symbolic phase and a numeric phase.  In the symbolic phase the problem is analyzed to
 determine the positions of the nonzeros in the result and possibly auxiliary information such as a fill-reducing permutation.  Often if you need to solve several systems of the same form but with different numerical values you can save and update the factorization
 rather than starting from scratch each time.<o:p></o:p></p></div><div><p class="MsoNormal"><o:p> </o:p></p></div><div><p class="MsoNormal">This is why I prefer Eigen which has classes representing factorizations in addition to the one-shot methods.<o:p></o:p></p></div><blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm"><div><div><div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;"><o:p> </o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;">I'm trying to decide whether it would be worth it to convert my code from R using sparseMatrix objects (Matrix package) to a C++ equivalent using RcppArmadillo.  On the same topic,
 would it be easier/more difficult if I used RcppEigen instead?<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;"><o:p> </o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;">Thanks,<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;"><o:p> </o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif;">Joshua<o:p></o:p></span></p></div><div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Calibri, sans-serif; color: rgb(136, 136, 136);">-- <o:p></o:p></span></p></div><div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif; color: rgb(136, 136, 136);">Joshua French, Ph.D.<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif; color: rgb(136, 136, 136);">Assistant Professor<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif; color: rgb(136, 136, 136);">Department of Mathematical and Statistical Sciences<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif; color: rgb(136, 136, 136);">University of Colorado Denver<o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif; color: rgb(136, 136, 136);"><a href="mailto:Joshua.French@ucdenver.edu" target="_blank">Joshua.French@ucdenver.edu</a><o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif; color: rgb(136, 136, 136);"><a href="http://math.ucdenver.edu/~jfrench/" target="_blank">http://math.ucdenver.edu/~jfrench/</a><o:p></o:p></span></p></div><div><p class="MsoNormal"><span style="font-size: 10.5pt; font-family: Cambria, serif; color: rgb(136, 136, 136);">Ph:  <a href="tel:303-315-1709" target="_blank">303-315-1709</a>  Fax:  <a href="tel:303-315-1701" target="_blank">303-315-1701</a><o:p></o:p></span></p></div></div></div></div></div></div><p class="MsoNormal"><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><o:p></o:p></p></blockquote></div><p class="MsoNormal"><o:p> </o:p></p></div></div></div></div></div></span></body></html>