Open In App

rcpparmadillo Package in R

Last Updated : 28 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)
 
# Get the version of Armadillo
version <- armadillo_version(TRUE)
 
# Print the version
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>
 
// [[Rcpp::depends(RcppArmadillo)]]
 
// [[Rcpp::export]]
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




# Compiling and loading the C++ file using sourceCpp() function
Rcpp::sourceCpp("C:/users/pgoya/Documents/GFG.cpp")


3. Assign the values and call the function

R




# Initializing the vectors
A <- c(1,2,3,4)
B <- c(1,2,3,4)
 
# Calling the add() function from the C++ code
C <- c(add(A,B))
 
# Printing the vector C
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++ Code

C++




#include <RcppArmadillo.h>
 
// [[Rcpp::depends(RcppArmadillo)]]
 
// [[Rcpp::export]]
 
int sum(int n) {
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += i;
  }
  return sum;
}


  • R code

R




# Install the RcppArmadillo package to use Armadillo C++ library in R
install.packages("RcppArmadillo")
 
#loading the package
library(RcppArmadillo)
 
# Compiling and loading the C++ file using sourceCpp() function
Rcpp::sourceCpp("C:/users/pgoya/Documents/GFG.cpp")
 
# Initializing the value N
N <- 10
 
# Calling the sum() function from the C++ code
result <- sum(N)
 
# Printing the result
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++ code

C++




#include <RcppArmadillo.h>
 
// [[Rcpp::depends(RcppArmadillo)]]
 
// [[Rcpp::export]]
 
int factorial(int n) {
  int fact = 1;
  for (int i = 2; i <= n; i++) {
    fact*= i;
  }
  return fact;
}


  • R code

R




# Install the RcppArmadillo package to use Armadillo C++ library in R
install.packages("RcppArmadillo")
 
#loading the package
library(RcppArmadillo)
 
# Compiling and loading the C++ file using sourceCpp() function
Rcpp::sourceCpp("C:/users/pgoya/Documents/GFG.cpp")
 
# Initializing the value N
N <- 10
 
# Calling the sum() function from the C++ code
result <- factorial(N)
 
# Printing the result
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++ code

C++




#include <RcppArmadillo.h>
 
// [[Rcpp::depends(RcppArmadillo)]]
 
// [[Rcpp::export]]
 
double determinant(arma::mat X) {
  return arma::det(X);
}


  • R code

R




# Install the RcppArmadillo package to use Armadillo C++ library in R
install.packages("RcppArmadillo")
 
#loading the package
library(RcppArmadillo)
 
# Compiling and loading the C++ file using sourceCpp() function
Rcpp::sourceCpp("GFG.cpp")
 
# Initializing the matrix
x <- matrix(c(3, 2, 6, -1, 7, 3, 2, 6, -1), 3, 3)
 
# Calling the determinant() function from the C++ code
result <- determinant(x)
 
# Printing the result
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++ code

C++




#include <RcppArmadillo.h>
 
// [[Rcpp::depends(RcppArmadillo)]]
 
// [[Rcpp::export]]
 
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 code

R




# Install the RcppArmadillo package to use Armadillo C++ library in R
install.packages("RcppArmadillo")
 
#loading the package
library(RcppArmadillo)
 
# Compiling and loading the C++ file using sourceCpp() function
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)
}
 
 
# Test the gls function
# raw data
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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads