Yesterday while I was running system update on my home desktop, I noticed the armadillo package has been upgraded. I was pretty sure I never requested to have it installed on this desktop. The package name, though, looks very interesting.

Upon looking it up, I found out that Armadillo is a C++ library for linear algebra. Coincidentally, I’ve been reviewing C++ during this end of year holiday. So it’s like a very good time to try this library out.

Another Google search for “Amardillo tutorial” lead me this article Getting started with Armadillo a C++ Linear Algebra Library on Windows, Mac and Linux.

Here is the code taken from the above article:

#include <iostream>
#include <armadillo>

int main() {
    // Initialize the random generator
    arma::arma_rng::set_seed_random();

    // Create a 4x4 random matrix and print it on the screen
    arma::Mat<double> A = arma::randu(4,4);
    std::cout << "A:\n" << A << "\n";

    // Multiply A with its transpose
    std::cout << "A * A.t() =\n";
    std::cout << A * A.t() << std::endl;

    // Access/Modify rows and columns from the array
    A.row(0) = A.row(1) + A.row(3);
    A.col(3).zeros();
    std::cout << "add rows 1 and 3, store result in row 0, also fill 4th column with zero" << std::endl;
    std::cout << "A:\n" << A << std::endl;

    // Create a new diagonal matrix using the main diagonal of A:
    arma::Mat<double> B = arma::diagmat(A);
    std::cout << "B:\n" << B << std::endl;

    // Save matrices A and B: (need -lhdf5)
    A.save("A_mat.txt", arma::arma_ascii);
    B.save("B_mat.txt", arma::arma_ascii);

    return 0;
}

To compile the above code on a Fedora (29) machine, I also need the following devel packages:

  • armadillo-devel.x86_64 : Development headers and documentation for the Armadillo C++ library
  • lapack-devel.x86_64 : LAPACK development libraries
  • hdf5-devel.x86_64 : HDF5 development files
  • openblas-devel.x86_64 : Development headers and libraries for OpenBLAS

With the source code saved as armadillo.cpp, it can be compiled as the following:

❯❯❯ clang++ armadillo.cpp -o armadillo -DARMA_DONT_USE_WRAPPER -lopenblas -llapack -lhdf5

The compilation should have produced a binary file called armadillo. Here is a sample output when it’s run:

❯❯❯ ./armadillo
A:
   0.3471   0.6340   0.8044   0.3969
   0.4508   0.1420   0.9976   0.4360
   0.0361   0.0905   0.0687   0.6907
   0.3746   0.5387   0.2674   0.5731

A * A.t() =
   1.3270   1.2221   0.3993   0.9141
   1.2221   1.4088   0.3989   0.7620
   0.3993   0.3989   0.4913   0.4764
   0.9141   0.7620   0.4764   0.8304

add rows 1 and 3, store result in row 0, also fill 4th column with zero
A:
   0.8254   0.6807   1.2650        0
   0.4508   0.1420   0.9976        0
   0.0361   0.0905   0.0687        0
   0.3746   0.5387   0.2674        0

B:
   0.8254        0        0        0
        0   0.1420        0        0
        0        0   0.0687        0
        0        0        0        0

Though I don’t really have a need to use linear algebra for my project in anytime soon, I still think it’s cool, and thanks to the author of the Getting started article.

If you’re using Ubuntu, Windows, or even OSX and would like to try out Armadillo library, you should click on the Getting started link. The article includes how to setup the library, explains what this library is for in more details.