Overview

Basic ideas

Seldon provides matrix and vector structures (for numerical computations) which are not part of the C++ standard library. Those structures are a lot more convenient than basic arrays (like float* vect = new float[5] or double mat[5][4]). They can be resized, displayed, copied, automatically destroyed such as the class vector of STL. Seldon vectors can be used along with operators with lazy evaluation. A debug mode is also present, such that the user can correct quickly any error coming from an index out of bounds for example. The use of Seldon is therefore easy.

Example 1 - basic example

#define SELDON_DEBUG_LEVEL_3

#include "SeldonLib.hxx"

using namespace Seldon;

int main()
{

  TRY;

  Matrix<double> A(3, 3);
  Vector<double> U, V;

  A.SetIdentity();
  A(0, 1) = -1.0;

  U.Reallocate(A.GetN());
  U.Fill();

  U.Print();

  V.Reallocate(A.GetM());
  Mlt(2.0, A, U, V);

  cout << V << endl;

  END;

  return 0;

}

The example above does the following:

  • A "debug level" is defined. It defines the amount of checks that Seldon will perform. A high debugging level will force Seldon to check a lot of things (e.g. indices validity), while a low debugging level will lead to less checks but to a faster code.

  • Seldon is included. Its classes and functions are in the namespace Seldon.

  • In the main function, the whole is included in a try block (macros TRY and END). This is not required, but it may catch Seldon exceptions.

  • A matrix of size 3 by 3 if declared.

  • Two vectors are defined. Their sizes are unknown.

  • The matrix A is set to the identity. Then its element at (0, 1) (first row, second column) is set to -1.0.

  • U is reallocated so that its length is the number of columns of A. It is simply filled with 0, 1 and 2. Then it is displayed thanks to the method Print.

  • The next operation is simply: 2.0 x A x U -> V. Notice that V is reallocated to have the right size; otherwise an exception would have been raised. If Blas was used, Mlt would call it.

  • The result V is displayed.

The output is:

0       1       2
-2      2       4


Example 2 - exceptions

#define SELDON_DEBUG_LEVEL_4

#include "SeldonLib.hxx"

using namespace Seldon;

int main()
{

  TRY;

  Matrix<double> A(3, 3);

  A.Zero();
  A(0, 3) = 2.0;

  END;

  cout << "The program should not reach this point..." << endl;

  return 0;

}

The example above does the following:

  • A high debugging level is defined. For example, the validity of indices is checked at every access.

  • Within a try block, a matrix A of size 3 by 3 is defined.

  • A is set to 0.

  • One then tries to set to 2.0 the element in the first row and the fourth column. Obviously the column index is out of range. An exception is raised.

  • The exception is caught, and the lines following the try block (i.e. after END) are executed.

The output is:

ERROR!
Index out of range in Matrix_Pointers::operator().
   Index along dimension #2 should be in [0, 2], but is equal to 3.