<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/using-armadillo-with-superlu/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://gallery.rcpp.org//articles/armadillo-with-superlu/"> Rcpp Gallery</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://arma.sourceforge.net/">Armadillo</a> is very versatile C++ library for linear algebra, brough<br>
to R via the <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.armadillo.html">RcppArmadillo</a> package. It<br>
has proven to be very useful and popular, and is (as of February 2017) used by well over 300 CRAN<br>
packages as indicated by the reverse depends / linking-to on its<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=RcppArmadillo">CRAN page</a>. Several earlier posts here on the<br>
Rcpp Gallery also demonstrate different usage patterns.</p>
<p><a target="_blank" rel="nofollow" href="http://arma.sourceforge.net/">Armadillo</a> has a core focus on dense matrices, but continues to<br>
expand its capabilities for sparse matrices. Basic operation are supported directly via the<br>
templated header files, along with calls back into the default (dense) LAPACK and BLAS libraries we<br>
can access easily as R uses them.</p>
<p><a target="_blank" rel="nofollow" href="http://arma.sourceforge.net/">Armadillo</a> also supports dedicated sparse solvers via the<br>
<a target="_blank" rel="nofollow" href="http://crd-legacy.lbl.gov/~xiaoye/SuperLU/">SuperLU</a> package. <em>However</em>, this requires access to<br>
the <a target="_blank" rel="nofollow" href="http://crd-legacy.lbl.gov/~xiaoye/SuperLU/">SuperLU</a> library. Many Linux distributions ship<br>
it, see e.g. the <a target="_blank" rel="nofollow" href="https://packages.debian.org/source/sid/armadillo">Debian package page</a> and the<br>
<a target="_blank" rel="nofollow" href="http://packages.ubuntu.com/source/yakkety/superlu">Ubuntu package page</a>; there is also a<br>
<a target="_blank" rel="nofollow" href="https://github.com/Homebrew/homebrew-science/blob/master/superlu.rb">homebrew recipe</a> for OS X /<br>
macOS (or other systems using <code>brew</code>). As of this writing, the version in the current Ubuntu<br>
release is behind the version Debian. But it is the 5.2.* version that is in Debian that is also<br>
required with current Armadillo versions 7.700.* so we prepared ‘backports’ via<br>
<a target="_blank" rel="nofollow" href="https://launchpad.net/~edd/+archive/ubuntu/misc/+packages">Dirk’s PPA repo</a>.)</p>
<p>Recently, a <a target="_blank" rel="nofollow" href="https://github.com/RcppCore/RcppArmadillo/issues/120">GitHub issue ticket</a> asked how to<br>
use <a target="_blank" rel="nofollow" href="http://crd-legacy.lbl.gov/~xiaoye/SuperLU/">SuperLU</a> along with<br>
<a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.armadillo.html">RcppArmadillo</a>. This post essentially<br>
repeats the main answer, which was spread out over multiple posts, in a single article.</p>
<p>In a nutshell, two things need to happen:</p>
<ol>
<li>
<p>One needs to define the required variable <code>ARMA_USE_SUPERLU</code> which has to be done before the<br>
Armadillo headers are included. One possibility (shown below) is a <code>#define</code> statement right in the<br>
code file.</p>
</li>
<li>
<p>One needs to tell the linker to use the <a target="_blank" rel="nofollow" href="http://crd-legacy.lbl.gov/~xiaoye/SuperLU/">SuperLU</a><br>
library. This step is of course not perfectly portable, and does require that the library be<br>
installed. (A genuinely portable solution would either need to test for presence of SuperLU, or include its<br>
sources. Both aspects are beyond the scope of this post._</p>
</li>
</ol>
<figure>
<pre><code data-lang="r"><span>Sys.setenv</span><span>(</span><span>&quot;PKG_LIBS&quot;</span><span>=</span><span>&quot;-lsuperlu&quot;</span><span>)</span></code></pre>
</figure>
<p>Now that R knows about this library, we can present the code requiring it:</p>
<figure>
<pre><code data-lang="cpp"><span>// Important: this definition ensures Armadillo enables SuperLU
</span><span>#define ARMA_USE_SUPERLU 1
</span>
<span>#include &lt;RcppArmadillo.h>
</span><span>// [[Rcpp::depends(RcppArmadillo)]]
</span>
<span>using</span> <span>namespace</span> <span>arma</span><span>;</span>

<span>// [[Rcpp::export]]
</span><span>void</span> <span>superLuDemo</span><span>()</span> <span>{</span>
    <span>sp_mat</span> <span>A</span> <span>=</span> <span>sprandu</span><span>&lt;</span><span>sp_mat</span><span>></span><span>(</span><span>1000</span><span>,</span> <span>1000</span><span>,</span> <span>0.1</span><span>);</span>

    <span>vec</span> <span>b</span> <span>=</span> <span>randu</span><span>&lt;</span><span>vec</span><span>></span><span>(</span><span>1000</span><span>);</span>
    <span>mat</span> <span>B</span> <span>=</span> <span>randu</span><span>&lt;</span><span>mat</span><span>></span><span>(</span><span>1000</span><span>,</span> <span>5</span><span>);</span>

    <span>vec</span> <span>x</span> <span>=</span> <span>spsolve</span><span>(</span><span>A</span><span>,</span> <span>b</span><span>);</span>  <span>// solve one system
</span>    <span>mat</span> <span>X</span> <span>=</span> <span>spsolve</span><span>(</span><span>A</span><span>,</span> <span>B</span><span>);</span>  <span>// solve several systems
</span>
    <span>bool</span> <span>status</span> <span>=</span> <span>spsolve</span><span>(</span><span>x</span><span>,</span> <span>A</span><span>,</span> <span>b</span><span>);</span>  <span>// use default solver
</span>    <span>if</span> <span>(</span><span>status</span> <span>==</span> <span>false</span><span>)</span>  <span>{</span> <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;no solution&quot;</span> <span>&lt;&lt;</span> <span>endl</span><span>;</span> <span>}</span>

    <span>spsolve</span><span>(</span><span>x</span><span>,</span> <span>A</span><span>,</span> <span>b</span><span>,</span> <span>&quot;lapack&quot;</span><span>);</span>   <span>// use LAPACK solver
</span>    <span>spsolve</span><span>(</span><span>x</span><span>,</span> <span>A</span><span>,</span> <span>b</span><span>,</span> <span>&quot;superlu&quot;</span><span>);</span>  <span>// use SuperLU solver
</span>
    <span>Rcpp</span><span>::</span><span>Rcout</span> <span>&lt;&lt;</span> <span>&quot;Done.</span><span>\n</span><span>&quot;</span><span>;</span>
<span>}</span></code></pre>
</figure>
<p>This code snippet defines a function <code>superLuDemo()</code> which we can call from R:</p>
<figure>
<pre><code data-lang="r"><span>superLuDemo</span><span>()</span></code></pre>
</figure>
<pre>
Done.
</pre>
<p>As the data generated here is random, we did not bother printing the (dense) result vector of<br>
length 1000.</p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/rcpp-gallery/~4/iQW5siOyV8Q"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://gallery.rcpp.org//articles/armadillo-with-superlu/"> Rcpp Gallery</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Q3Px49wajMY"></a></div>