Skip to content

Armadillo

AI & Machine LearningMachine LearningC++

What it is

Armadillo is a high-quality C++ linear algebra library that provides efficient matrix and vector operations, while offering a syntax similar to MATLAB. It is designed for both speed and ease of use, making it popular in scientific computing, machine learning, and signal processing.

Armadillo supports dense and sparse matrices, vectors, linear algebra, statistics, eigendecompositions, and integration with LAPACK/BLAS backends. Its MATLAB-like syntax makes it accessible to researchers migrating to C++.

Installation

sudo apt install libarmadillo-dev

Getting started

The smallest useful thing you can do with it, and what each part means.

Matrix multiplication
#include <armadillo>
#include <iostream>

int main() {
    arma::mat A = {{1, 2}, {3, 4}};
    arma::mat B = {{5, 6}, {7, 8}};
    arma::mat C = A * B;

    C.print("Result of A*B:");
    return 0;
}
Performs matrix multiplication using Armadillo with a concise MATLAB-like syntax.

Advanced usage

Where the library earns its place over a simpler alternative.

Solving linear systems
arma::vec x = arma::solve(A, b);
Solves the system of linear equations Ax = b efficiently.
Eigen decomposition
arma::vec eigval;
arma::mat eigvec;
arma::eig_sym(eigval, eigvec, A);
Computes eigenvalues and eigenvectors of a symmetric matrix.
Singular value decomposition (SVD)
arma::mat U, V;
arma::vec s;
arma::svd(U, s, V, A);
Computes the SVD decomposition of a matrix.
Using sparse matrices
arma::sp_mat S(1000, 1000);
S(0,0) = 1.5;
S(100,200) = 2.3;
Efficiently handles large sparse matrices with minimal memory overhead.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Runtime error: matrix singular
Occurs when trying to solve a system with a singular matrix. Ensure the matrix is invertible or use pseudo-inverse.
Linker errors with LAPACK/BLAS
Ensure you link with `-llapack -lblas` or have OpenBLAS/Intel MKL installed.
Slow performance
Install optimized BLAS/LAPACK backends instead of relying on the default implementation.

Best practices

  • Use Armadillo’s high-level syntax for readability, but enable optimized BLAS/LAPACK backends for performance.
  • Use `arma::sp_mat` for sparse matrices to save memory in large datasets.
  • Call `.t()` for transpose instead of manually looping.
  • Avoid unnecessary copies by using Armadillo’s lazy evaluation (`.eval()` when needed).
  • Prefer built-in solvers over manually implementing algorithms.

Background

Why it exists, and what it was reacting to.

Armadillo was created to bridge the gap between high-performance C++ libraries (like BLAS and LAPACK) and user-friendly numerical software like MATLAB. It provides a natural, concise API for expressing mathematical operations while relying on optimized backends (e.g., Intel MKL, OpenBLAS, LAPACK) for performance. It has become a standard tool in research, academia, and industries requiring numerical simulations and machine learning.