Skip to content

Getting Started

Roman Poya edited this page Mar 14, 2020 · 8 revisions

Getting Started

Fastor is a tensor algebra library written in C++11 and beyond. Tensors are primarily multi-dimensional arrays. These include scalars, vectors, matrices, cubes, 4D arrays and so on. Although there is more to it than that, for the time being you can treat Fastor tensors just like arrays (such as numpy arrays or MATLAB/Julia matrices)

Installing Fastor

Fastor is header only library and does not require any installation. Just download the source code and include the Fastor header in your C++ source code

#include<Fastor/Fastor.h> 

and add the Fastor folder to your include path

-I/path/to/Fastor/

Your first example in Fastor

Create a C++ source file called first_example.cpp and the copy the following to it

#include <Fastor/Fastor.h>
using namespace Fastor;

int main() {

  Tensor<double> my_scalar = 2; // this is a scalar (double) with a value of 2
  // output this on the screen
  print("my scalar",my_scalar); // print is a built-in Fastor function for printing

  Tensor<double,3> my_vector = {1,2,3}; // this is a vector of 3 doubles with values {1,2,3}
  print("my vector",my_vector);
  Tensor<float,3,3> my_matrix = {{1,2,3},
                                 {4,5,6},
                                 {7,8,9}}; // this a 3x3 matrix of floats with values 1...9
  print("my_matrix",my_matrix);

  Tensor<int,2,2,2,2,2> array_5d; // this a 5D array of ints with dimension 2x2x2x2x2
  array_5d.iota(1); // fill the 5d_array with sequentially ascending numbers from 1 to 2x2x2x2x2=32
  print("my 5D array", array_5d);

  return 0;
}

Save and compile this file like

g++ first_example.cpp -o first_example.exe -I/path/to/Fastor/

and run it like

./first_example.exe

you should see the following on your screen

my scalar
2

my vector
[1]
[2]
[3]

my_matrix
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

my 5D array
[0,0,0,:,:]
[ 1,  2]
[ 3,  4]
[0,0,1,:,:]
[ 5,  6]
[ 7,  8]
[0,1,0,:,:]
[ 9, 10]
[11, 12]
[0,1,1,:,:]
[13, 14]
[15, 16]
[1,0,0,:,:]
[17, 18]
[19, 20]
[1,0,1,:,:]
[21, 22]
[23, 24]
[1,1,0,:,:]
[25, 26]
[27, 28]
[1,1,1,:,:]
[29, 30]
[31, 32]

As you can all Fastor tensors are row major like C-style arrays. Internally Fastor treats everything as row major contiguous. Other formats are supported and will be discussed in the later section.