Statslabs::Matrix - The C++ Linear Algebra Library (in development)
The repository contains the Statslabs::Matrix C++ linear algebra library code. Statslabs::Matrix is the fundamental package of Statslabs for statistical computing in C++.
Overview
Statslabs::Matrix is the fundamental package of Statslabs for statistical computing in C++. The Statslabs.Matrix library code is based on the matrix design chapter in 'The C++ Programming Language (4th Edition)' and provides:
- A Matrix Template: Construction and Assignment; Subscripting and Slicing
- Matrix arithmetic operations: Scalar Operations; Additions; Multiplication
- Matrix Implementation: slice; MatrixSlice; MatrixRef; Matrix List Initialization; Matrix Access; Zero-Dimensional Matrix
- An interface to Intel(R) MKL BLAS operations which apply to the Matrix template
Prerequisites
CMake >= 3.6.0
Intel Math Kernel Library (Intel MKL)
Installation on Ubuntu / macOS
- Clone the repository.
git clone git@github.com:statslabs/matrix.git
- Configure the project.
To build docs/examples/tests, configure matrix with the following cmake options:
cd matrix mkdir build && cd build cmake ..
-DSLAB_INCLUDE_DOCS=ON -DSLAB_INCLUDE_EXAMPLES=ON -DSLAB_INCLUDE_TESTS=ON
- Compile and install the library.
make make install
Example program
examples/demo.cc
:
#include <iostream>
#include "slab/matrix.h"
using namespace std;
using namespace slab;
int main() {
mat A = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
mat B = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Element-wise addition
cout << "A + B = " << A + B << endl;
// Element-wise subtraction
cout << "A - B = " << A - B << endl;
// Element-wise multiplication
cout << "A * B = " << A * B << endl;
// Element-wise division
cout << "A / B = " << A / B << endl;
// Matrix multiplication
cout << "matmul(A, B) = " << matmul(A, B) << endl;
return 0;
}
Integration of Matrix in your own project
To make the project simple enough, we will create a CMake project for demo.cc
.
-
Make a project folder.
mkdir example && cd example
-
Create
demo.cc
andCMakeLists.txt
in the project folder where fileCMakeLists.txt
should look like:cmake_minimum_required(VERSION 3.0) project(example) set(CMAKE_CXX_STANDARD 11) add_executable(example demo.cc) find_package(Matrix REQUIRED) target_link_libraries(example Statslabs::Matrix)
-
Perform a out-of-source build.
mkdir build && cd build cmake .. make
-
Run the program.
./example