rcpparmadillo Package in R
Last Updated :
28 Jun, 2023
A seamless interface between R programming and the superior C++ linear algebra library Armadillo is provided by the RcppArmadillo package. Since it provides an understandable syntax and quick algorithms for matrix and vector operations, Armadillo is a popular choice for scientific computing and machine learning workloads.
With the help of the RcppArmadillo package, R users may utilize Armadillo’s functionality without sacrificing the usefulness and simplicity of the R language. It provides a set of R functions that surround the Armadillo library to carry out matrix operations, solve linear problems, compute eigenvalues and eigenvectors, and carry out a number of other linear algebra activities.
Rcpp
With Rcpp, users may call C++ code from R and vice versa. Rcpp is a package that offers a number of C++ classes and methods. The Rcpp framework, which is developed on top of the R API, offers a simple interface for fusing C++ code with R.
Armadillo
Armadillo is a C++ package for scientific computing and linear algebra. It gives users a high-level interface for performing operations involving linear algebra, manipulating matrices, and other typical scientific computing activities. For calculations using linear algebra that are optimized, Armadillo is built on top of the BLAS and LAPACK libraries.
BLAS and LAPACK
Basic Linear Algebra Subprograms (BLAS) and Linear Algebra Package (LAPACK) are libraries of efficient linear algebra processing routines. They are built in Fortran and offer highly efficient versions of typical linear algebra operations including matrix multiplication, linear system solution, and eigenvalue and eigenvector computation.
Installation of Rcpparmadillo Package in R
Install the RcppArmadillo package in the R console to use the Armadillo C++ library by writing the following code:
install.packages("RcppArmadillo")
After installing we have to load the package into the R environment by writing:
library(Rcpp)
library(RcppArmadillo)
Functions in RcppArmadillo Package in R
The RcppArmadillo package in R programming provides a number of functions that are used to perform Armadillo’s functionality. A few of them are listed below:
|
Method |
Description |
1) |
armadillo_version() |
Tells the current version of the Armadillo |
2) |
armadillo_set_seed_random() |
Set Armadillo random number generator to a random number |
3) |
RcppArmadillo.package.skeleton() |
Creates a skeleton for a new package that uses RcppArmadillo |
4) |
armadillo_set_seed() |
Set Armadillo random number generator to a particular value |
5) |
fastLm() |
It is a bare-bone linear model-fitting function |
6) |
RcppArmadillo-package |
Integrates R with Armadillo |
Examples of Rcpparmadillo Package in R
Let us see a few examples of the Rcpparmadillo Package in R.
Example 1: Finding Armadillo’s version
R
library (Rcpp)
library (RcppArmadillo)
version <- armadillo_version ( TRUE )
print (version)
|
Output:
120001
Example 2: Adding the vectors
In this example, we will take two vectors and add them using Rcpparmadillo package in R and C++ programming,
1. Write C++ code
C++
#include <RcppArmadillo.h>
arma::vec add(arma::vec A, arma::vec B) {
return A + B;
}
|
Note: Mention the comments.
2. Complie C++ code
Once our C++ code is written, we will compile it using the sourceCpp() function in R.
Rcpp::sourceCpp("Specify the path")
R
Rcpp:: sourceCpp ( "C:/users/pgoya/Documents/GFG.cpp" )
|
3. Assign the values and call the function
R
A <- c (1,2,3,4)
B <- c (1,2,3,4)
C <- c ( add (A,B))
C
|
Output:
[1] 2 4 6 8
Example 3: Printing the sum from 1 to N
The sum of a number from 1 to N is defined as the addition of all positive integers from 1 to n. In the c++ file, we have written a simple program to calculate the sum of a number from 1 to N by using for loop.
C++
#include <RcppArmadillo.h>
int sum( int n) {
int sum = 0;
for ( int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
|
R
install.packages ( "RcppArmadillo" )
library (RcppArmadillo)
Rcpp:: sourceCpp ( "C:/users/pgoya/Documents/GFG.cpp" )
N <- 10
result <- sum (N)
paste ( "The sum is" , result)
|
Output:
[1] "The sum is 55"
Example 4: Factorial of a number N
The Factorial of a no. is defined as the multiplication of all positive integers smaller than or equal to n. In the C++ file, we have written a simple program to calculate the factorial of a number by using for loop.
C++
#include <RcppArmadillo.h>
int factorial( int n) {
int fact = 1;
for ( int i = 2; i <= n; i++) {
fact*= i;
}
return fact;
}
|
R
install.packages ( "RcppArmadillo" )
library (RcppArmadillo)
Rcpp:: sourceCpp ( "C:/users/pgoya/Documents/GFG.cpp" )
N <- 10
result <- factorial (N)
paste ( "The factorial of" ,N, "is" , result)
|
Output:
[1] "The factorial of 10 is 3628800"
Example 5: Determinant of a matrix
In this example, we will find the determinant of a matrix of 3*3 with values (3, 2, 6, -1, 7, 3, 2, 6, -1). det() is the inbuilt method in R to find out the determinant of a matrix.
C++
#include <RcppArmadillo.h>
double determinant(arma::mat X) {
return arma::det(X);
}
|
R
install.packages ( "RcppArmadillo" )
library (RcppArmadillo)
Rcpp:: sourceCpp ( "GFG.cpp" )
x <- matrix ( c (3, 2, 6, -1, 7, 3, 2, 6, -1), 3, 3)
result <- determinant (x)
paste ( "The determinant of is" , result)
|
Output:
[1] "The determinant of is -185"
Example 6: Generalizes Least Square
Let’s see an example to Generalize Least Squares using the Rcpparmadillo package.
C++
#include <RcppArmadillo.h>
arma::vec gls(arma::mat X, arma::vec y, arma::mat V) {
arma::mat XtVX = X.t() * V * X;
arma::vec XtVy = X.t() * V * y;
arma::vec res = arma::solve(XtVX, XtVy);
return res;
}
|
R
install.packages ( "RcppArmadillo" )
library (RcppArmadillo)
Rcpp:: sourceCpp ( "GFG.cpp" )
gls <- function (X, y, V) {
X <- as.matrix (X)
y <- as.numeric (y)
V <- as.matrix (V)
XtVX <- t (X) %*% V %*% X
XtVy <- t (X) %*% V %*% y
res <- solve (XtVX, XtVy)
return (res)
}
set.seed (123)
n <- 100
p <- 5
X <- matrix ( rnorm (n * p), ncol = p)
y <- X[,1] + X[,2] + rnorm (n)
V <- diag (n)
ans <- gls (X, y, V)
print (ans)
|
Output:
[,1]
[1,] 0.983587216
[2,] 1.147303338
[3,] 0.005854041
[4,] -0.082192665
[5,] 0.204912447
Share your thoughts in the comments
Please Login to comment...