Machine learning research and just about many fields of engineering utilize matlab for prototyping and development. Matlab costs quite a good amount of money and when your applications needs production quality, you are more likely to move away from matlab.
The core functionality of matlab that makes it a good, easy and quick prototyping platform is its easy to use matrix and linear algebraic operations. Beyond prototyping, speed of execution and production standards require your code to be implemented in C,C++,C#, java or one of the numerous new languages that pop up each morning. In this post we will see the implementation of matrix operations in C++ (the ones you thought was only possible in matlab) with the least programming effort.
BLAS:
BLAS (Basic Linear Algebra Subprograms) is a set of highly optimized library for vector and matrix operations. BLAS forms the backbone for numerous libraries that are built to facilitate ‘Matlab like ‘ vector operations. BLAS is written in fortran.
LAPACK:
While BLAS contains optimized routines for most fundamental operations, more sophisticated functions like matrix decompositions, factorizations etc are grouped into a library called LAPACK (Linear Algebra PACKage). The fundamental operations in LAPACK routines are optimized since they use the BLAS package. LAPACK is fortran aswell.
ATLAS:
ATLAS is a package that helps to optimize build variables so as to optimize LAPACK further. Apart from the mentioned packages, there are numerous variants of such libraries that claim to outperform one another.
The question that naturally arises is the usability of these packages. The good news is that you can integrate these libraries in C++ using a package called Armadillo. Armadillo by itself has most of the basic vector and matrix operations. Armadillo is a C++ linear algebra library which offers most operations on vectors and matrix in a stand alone manner. In order to access the more complicated operations like matrix factorizations that are described well in LAPACK, Armadillo offers nicer interface to call those functions from your C++ code.
To give a quick overview:
Just for the sake of code comparison between matlab and C++ using armadillo (offcourse including LAPACK and BLAS installed on a machine) we will write a small matrix multiplication code here. We will create a random matrix of dimensions 5×5 and multiply with itself and print the results.
MATLAB:
A = rand(5,5);
A = A*A;
A
C++ code:
mat A = rand(5, 5);
A = A*A;
A.print(“A = “);
Now that does not sound all that different , does it? Armadillo thus provides a nice set of classes that can perform most of the function that are a part of the core MATLAB software. I am sure MATLAB is a great tool when it comes to quick prototyping and visualization but I am sure these opensource packages are giving the companies a run for their money. We will continue the art of converting your Matlab code to C++ and the details about using Armadillo, LAPACK and BLAS for that purpose in future on this blog.