Vector2

Definition

Seldon::Vector2 is a structure that acts like a vector of full vectors. The inner vectors can be of any dimension, so that this structure is more flexible than a matrix.

Vector2 is a template class: Vector2<T, Allocator0, Allocator1>. T is the numerical type of the inner vectors. Allocator0 is the allocator for the inner vectors. It has the same default value as for vectors and matrices: SELDON_DEFAULT_ALLOCATOR. Allocator1 is the allocator for the vector of vectors. It is recommended to choose NewAlloc or, for more efficient in reallocations, MallocObject: these allocators can manage an array of inner vectors. The default allocator is MallocObject, in which case Allocator1 is the same as MallocObject<Vector<T, VectFull, Allocator0> >.

Declaration

  
Vector2<double> V;

This defines an empty vector (of vectors).

To define a Vector2 with 5 empty inner vectors:

  
Vector2<double> V(5);

To define a Vector2 with 3 inner vectors of size 2, 3 and 7:

  
Vector<int> length(3);
length(0) = 2;
length(1) = 3;
length(2) = 7;
Vector2<double> V(length);

Use of Vector2

Seldon::Vector2 comes with the following methods:

The other methods are described in the reference documentation.

An Example

doc/example/vector2.cpp
#define SELDON_DEBUG_LEVEL_4
#include "Seldon.hxx"
using namespace Seldon;

#include "vector/Vector2.cxx"

int main()
{

  TRY;

  Vector<int> length(3);
  length(0) = 2;
  length(1) = 3;
  length(2) = 7;
  Vector2<double> V(length);

  // Fills all inner vectors with 2.
  V.Fill(2.);
  // Access to the second inner vector, to fill it with 5.
  V(1).Fill(5.);

  V.Print();
  cout << "First element of the second inner vector: " << V(1, 0) << endl;
  // Note that V(1)(0) would have returned the same element.

  Vector<double> inner_vector(4);
  inner_vector.Fill();
  // Appends a new inner vector.
  V.PushBack(inner_vector);
  V.Print();

  cout << "After setting to -10 the second element of the last inner vector:"
       << endl;
  V(3, 1) = -10.;
  V.Print();

  END;

  return 0;

}

Output:

Vector 0: 2     2       
Vector 1: 5     5       5       
Vector 2: 2     2       2       2       2       2       2       
First element of the second inner vector: 5
Vector 0: 2     2       
Vector 1: 5     5       5       
Vector 2: 2     2       2       2       2       2       2       
Vector 3: 0     1       2       3       
After setting to -10 the second element of the last inner vector:
Vector 0: 2     2       
Vector 1: 5     5       5       
Vector 2: 2     2       2       2       2       2       2       
Vector 3: 0     -10     2       3