Vector3

Definition

Seldon::Vector3 is a structure that acts like a vector of vector of full vectors. The inner vectors can be of any dimension, so that this structure is more flexible than a 3-D array.

Vector3 is a template class: Vector3<T, Allocator0, Allocator1, Allocator2>. T is the numerical type of the inner vectors. Allocator0 is the allocators for the inner vectors. Allocator1 is the allocator for intermediate vectors. Allocator0 has the same default value as for vectors and matrices: SeldonDefaultAllocator<VectFull, T>. Allocator2 is the allocator for the vector of vectors of vectors. It is recommended to select the default allocator, which is MallocObject: these allocators can manage efficiently an array of inner vectors.

Declaration

#include "Seldon.hxx"

// you need to include Vector2 and Vector3 files
// if you include SeldonLib.hxx, these files are already included
#include "vector/Vector2.cxx"
#include "vector/Vector3.cxx"

Vector3<double> V;

This defines an empty vector (of vectors). If you include SeldonLib.hxx, the files Vector2.cxx and Vector3.cxx are already included:

#include "SeldonLib.hxx"

using namespace Seldon;

Vector3<double> V;

To define a Vector3 with 5 empty intermediate vectors:

Vector3<double> V(5);

To define a Vector3 with 3 intermediate vectors of size 2, 3 and 7:

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

To define a Vector3 with 2 intermediate vectors of size 2 (inner vectors of size 3 and 5) and 3 (inner vectors of size 7, 4 and 6):

Vector<Vector<int> > length(2);
length(0).Reallocate(2);
length(0)(0) = 3; length(0)(1) = 5;
length(1).Reallocate(3);
length(1)(0) = 7; length(1)(1) = 4; length(1)(2) = 6;
Vector3<double> V(length);

Use of Vector3

Seldon::Vector3 comes with the following methods:

  • Reallocate(int M), Reallocate(int i, int n) and Reallocate(int i, int j, int p) which allow to reallocate the vector of vectors, the i-th intermediate vector or the (i, j) inner vector, respectively.

  • GetLength() which returns the number of intermediate vectors.

  • GetLength(int i) which returns the number of inner vectors of intermediate vector i.

  • GetLength(int i, int j) which returns the length of an inner vector.

  • operator()(int i) which returns the i-th intermediate vector.

  • operator()(int i, int j) which returns the (i, j) inner vector.

  • operator()(int i, int j, int k) which returns the k-th element of the (i, j) inner vector.

Methods of Vector3 :

The other methods are described in the table below.

Vector constructors
Vector operators
GetLength returns the size of inner vectors or outer vectors
GetSize returns the size of inner vector or outer vectors
GetMemorySize returns the memory used by the object in bytes
GetNelement returns the total number of elements stored
GetShape returns the number of elements contained in an inner vector
Reallocate modifies the size of outer vectors or inner vectors
Flatten copies the elements into a simple vector
PushBack appends outer or inner vectors at the end
Clear removes all elements of an inner or outer vector
Fill fills outer or inner vectors with the same value
GetVector returns outer or inner vectors as reference
Print displays the object
Write writes the object in an output stream
Read reads the object from an output stream


An Example

doc/example/vector3.cpp
#define SELDON_DEBUG_LEVEL_4
#include "SeldonLib.hxx"
using namespace Seldon;
int main()
{
TRY;
Vector<Vector<int> > length(2);
length(0).Reallocate(2);
length(0)(0) = 4;
length(0)(1) = 5;
length(1).Reallocate(3);
length(1)(0) = 7;
length(1)(1) = 2;
length(1)(2) = 3;
Vector3<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.);
// Access to an inner vector at lowest level, and fill it
V(0, 1).Fill(3.0);
V.Print();
cout << "First vector 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 to second vector.
V.PushBack(1, inner_vector);
V.Print();
cout << "After setting to -10 the second vector of the last inner vector:"
<< endl;
V(1, 1) = -10.;
V.Print();
END;
return 0;
}

Output:

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

Vector3 constructors

Syntax :

  Vector3();
  Vector3(int);
  Vector3(Vector<int>);
  Vector3(Vector<Vector<int> >); 

Example :

// default constructor -> empty vector of vectors
Vector3<double> V;
cout << "Number of elements "<< V.GetNelement() << endl; // should return 0 

// then you can use Reallocate to fill the structure
V.Reallocate(2); // 2 intermediate vectors
V.Reallocate(0, 3); // first intermediate vector contains 3 inner vectors
V.Reallocate(0, 0, 2); V.Reallocate(0, 1, 5); V.Reallocate(0, 2, 4); // size of inner vectors

V.Reallocate(1, 4); // second intermediate vector contains 4 inner vectors
V.Reallocate(1, 0, 3); V.Reallocate(1, 1, 6); V.Reallocate(1, 2, 2); V.Reallocate(1, 3, 5);
// V.GetNelement() should return 2+5+4 + 3+6+2+5 = 27 

// constructor specifying only the number of intermediate vectors
Vector3<double> U(2);
// then each inner vector can be initialized with Reallocate
U.Reallocate(0, 3); U.Reallocate(0, 0, 2); // etc

// constructor specifying the size of intermediate vectors
Vector<int> shape(2);
shape(0) = 3; 
shape(1) = 4;
Vector3<double> X(shape);
// then each inner vector can be initialized with Reallocate
U.Reallocate(0, 0, 2); U.Reallocate(0, 1, 5); U.Reallocate(0, 2, 4);// etc

// constructor specifying the size of all the inner vectors
Vector<Vector<int> > shap(3);
shap(0).Reallocate(3);
shap(0)(0) = 2; shap(0)(1) = 5;  shap(0)(2) = 4;
shap(1).Reallocate(4);
shap(1)(0) = 3; shap(1)(1) = 6; shap(1)(2) = 2; shap(1)(3) = 5;
Vector3<double> W(shap);

Related topics :

Reallocate
GetNelement

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Vector3 operators

Syntax :

  T& operator(int i, int j, int k);
  Vector<T>& operator(int i, int j);
  Vector<Vector<T> >& operator(int i);

You can use the operator() to modify an inner vector so that class Vector3 can be used exactly in the same way as an object Vector<Vector<Vector<T> > >. You can use the notation V(i, j, k) as for 3-D arrays.

Example :

// declaration of a vector with 3 intermediate vectors
Vector3<double> U(3);
// then each inner vector can be accessed directly with the operator()
U(0).Reallocate(4); // first intermediate vector contains 4 inner vectors
U(1).Reallocate(5); // second intermediate vector contains 5 inner vectors
U(2).Reallocate(3); // third intermediate vector contains 3 inner vectors

// allocation of inner vectors of first intermediate vector
U(0, 0).Reallocate(2); U(0, 1).Reallocate(6); U(0, 2).Reallocate(5); U(0, 3).Reallocate(3);

// allocation of inner vectors of second intermediate vector
U(1, 0).Reallocate(7); U(1, 1).Reallocate(8); U(1, 3).Reallocate(6);
U(1, 3).Reallocate(3); U(1, 4).Reallocate(4);

// allocation of inner vectors of third intermediate vector
U(2, 0).Reallocate(5); U(2, 1).Reallocate(9); U(2, 2).Reallocate(7);

// you can also use operator (i, j, k) to access elements of U
U(0, 2, 1) = 2.5;
U(2, 1, 5) = -0.8;

Related topics :

Reallocate
GetVector

Location :

Class Vector3
Vector3.hxx Vector3.cxx

GetLength, GetSize

Syntax :

  int GetLength()
  int GetSize()
  int GetLength(int i)
  int GetSize(int i)
  int GetLength(int i, int j)
  int GetSize(int i, int j)

This method returns the number of inner vectors if no argument is provided, and the size of the inner vector i if i is given as argument.

Example :

// declaration of a vector with 3 intermediate vectors
Vector3<double> U(3);
// U.GetSize() should return 3
cout << "Number of intermediate vectors " << U.GetSize() << endl;

U(0).Reallocate(2);
U(1).Reallocate(4);
U(2).Reallocate(3);

// size of intermediate vector 2 ?
cout << "Size of third intermediate vector " << U.GetSize(2) << endl;

U(0, 0).Reallocate(3); U(0, 1).Reallocate(5);
U(1, 0).Reallocate(4); U(1, 1).Reallocate(6); U(1, 2).Reallocate(7);  U(1, 3).Reallocate(5); 
U(2, 0).Reallocate(3); U(2, 1).Reallocate(4); U(2, 2).Reallocate(8);

// size of inner vector (2, 1) ?
cout << "Size of inner vector (2,1) " << U.GetSize(2, 1) << endl;

Related topics :

GetShape
GetNelement

Location :

Class Vector3
Vector3.hxx Vector3.cxx

GetMemorySize

Syntax :

  size_t GetMemorySize();

This method returns the memory (in bytes) used to store the object.

Example :

// declaration of a vector with 3 intermediate vectors
Vector3<double> U(3);

U(0).Reallocate(2);
U(1).Reallocate(4);
U(2).Reallocate(6);

// memory needed to store all these elements : 
cout << "Number of bytes to store U = " << U.GetMemorySize() << endl;

Location :

Class Vector3
Vector3.hxx Vector3.cxx

GetNelement

Syntax :

  int GetNelement()
  int GetNelement(int beg, int end)
  int GetNelement(int beg0, int end0, int beg1, int end1)

This method returns the number of elements stored in all the object, that is the sum of the sizes of inner vectors.

Example :

Vector3<double> V;
V.Reallocate(3); // 3 intermediate vectors
V.Reallocate(0, 3); // first intermediate vector contains 3 inner vectors
V.Reallocate(1, 4); // second intermediate vector contains 4 inner vectors
V.Reallocate(2, 5); // third intermediate vector contains 5 inner vectors
V.Reallocate(0, 0, 4); V.Reallocate(0, 1, 5); V.Reallocate(0, 2, 6);
V.Reallocate(1, 0, 3); V.Reallocate(1, 1, 7); V.Reallocate(1, 2, 4); V.Reallocate(1, 3, 5);
V.Reallocate(2, 0, 5); V.Reallocate(2, 1, 6); V.Reallocate(2, 2, 8); V.Reallocate(2, 2, 6); V.Reallocate(2, 2, 7);
// V.GetNelement() should return 4+5+6 + 3+7+4+5 + 5+6+8+7 = 60 
cout << "Number of doubles stored in V " << U.GetNelement() << endl;

// if you wish to know the number of elements for a subset of intermediate vectors :
int beg = 1; int end = 3;  ( beg <= i < end)
cout << "Number of inner vectors from second inner vector until third inner vector " << V.GetNelement(beg, end) << endl;

// if you wish to know the number of elements for a subset of inner vectors :
int beg0 = 1; int end0 = 3; int beg1 = 0; int end1 = 2; ( beg0 <= i < end0  and beg1 <= j < end1)
cout << "Number of inner vectors from (1,0) until (3,2) " << V.GetNelement(beg0, end0, beg1, end1) << endl;

Related topics :

GetShape
GetSize

Location :

Class Vector3
Vector3.hxx Vector3.cxx

GetShape

Syntax :

  Vector<int> GetShape()
  void GetShape(Vector<int>& );

This method returns the shape of the vector, i.e. the size of each intermediate vector.

Example :

Vector3<double> V;
V.Reallocate(5); // 5 intermediate vectors
V.Reallocate(0, 4); // first intermediate vector contains 4 inner vectors
V.Reallocate(1, 5); // second intermediate vector contains 5 inner vectors
V.Reallocate(2, 3); // third intermediate vector contains 3 inner vectors
V.Reallocate(3, 7); // fourth intermediate vector contains 7 inner vectors
V.Reallocate(4, 6); // fifth intermediate vector contains 6 inner vectors

// retrieving the size of all intermediate vectors
Vector<int> shape;
V.GetShape(shape); // equivalent to shape = V.GetShape();
// shape should contain [4, 5, 3, 7, 6]

Related topics :

Reallocate
GetSize

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Reallocate

Syntax :

  void Reallocate(int n)
  void Reallocate(int i, int n)
  void Reallocate(int i, int j, int n)

This method sets the number of inner vectors or the size of each inner vector.

Example :

Vector3<double> V;
V.Reallocate(5); // 5 inner vectors
V.Reallocate(0, 4); // first intermediate vector contains 4 inner vectors
V.Reallocate(1, 5); // second intermediate vector contains 5 inner vectors
V.Reallocate(2, 3); // third intermediate vector contains 3 inner vectors
V.Reallocate(3, 7); // fourth intermediate vector contains 7 inner vectors
V.Reallocate(4, 6); // fifth intermediate vector contains 6 inner vectors
V.Reallocate(1, 3, 10); // inner vector (1, 3) contains 10 elements
V.Reallocate(2, 4, 8); // inner vector (2, 4) contains 8 elements

Related topics :

GetShape
GetSize

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Flatten

Syntax :

  Vector<T> Flatten();
  void Flatten(Vector<T>& data);
  void Flatten(int beg, int end, Vector<T>& data);
  void Flatten(int beg0, int end0, int beg1, int end1, Vector<T>& data);

This method puts all the elements contained in the structure into a simple vector. You can also flatten only a subset of inner vectors by specifying extremities beg, end.

Example :

Vector3<double> V;
// structure with two intermediate vectors 
V.Reallocate(2);
// V(0) = ((x(0,0), x(0,1), ..., x(0, n0)), (x(1,0),x(1,1), ..., x(1,n1)), ... )
V.Reallocate(0, 3);
V.Reallocate(0, 0, 2); V.Reallocate(0, 1, 4); V.Reallocate(0, 2, 3);

// V(1) = ((y(0,0), y(0,1), ..., y(0, m0)), (y(1,0),y(1,1), ..., y(1,m1)), ... )
V.Reallocate(1, 4);
V.Reallocate(1, 0, 5); V.Reallocate(1, 1, 3); V.Reallocate(1, 2, 4); V.Reallocate(1, 3, 6);

// then you can obtain a simple vector U = (x(0,0),x(0,1), ..., x(1, n1) ..., y(0,0), y(0, 1) ... y(1, m1))
Vector<double> vec;
V.Flatten(vec);

// if you want to flatten only a subset of intermediate vectors
V.Flatten(1, 4, vec);

// or a subset of inner vectors
V.Flatten(1, 4, 0, 3, vec);

Related topics :

Reallocate
GetSize

Location :

Class Vector3
Vector3.hxx Vector3.cxx

PushBack

Syntax :

  void PushBack(int i, int j, const T& x)
  void PushBack(int i, const Vector<T>& x)
  void PushBack(const Vector<Vector<T> >& x)
  void PushBack(const Vector<Vector<Vector<T> > >& x)
  void PushBack(const Vector3<T>& x)

This method can be used to insert an element at the end of an inner vector, or append an inner vector at the end, or several inner vectors.

Example :

Vector3<double> V;
// structure with two intermediate vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(0, 0, 2); V.Reallocate(0, 1, 4); V.Reallocate(0, 2, 3);
V(0, 0, 0) = 2.2; V(0, 0, 1) = 1.0; V(0, 1, 0) = -3.5; V(0, 1, 1) = 2.7;
V(0, 1, 2) = 5.2; V(0, 1, 3) = -0.9; V(0, 2, 0) = 2.1; V(0, 2, 1) = 2.4; V(0, 2, 2) = 1.9;

V.Reallocate(1, 2);
V.Reallocate(1, 0, 5); V.Reallocate(1, 1, 3);
V(1, 0, 0) = 0.8; V(1, 0, 1) = 2.4; // etc

// then you can append an element at the end of inner vector (0, 2), for instance :
V.PushBack(0, 2, 2.431);

// add a third inner vector to second intermediate vector
Vector<double> vec(4);
vec.Fill();
V.PushBack(1, vec);

// add a third intermediate vector
Vector<Vector<double> > U(2);
U(0).Reallocate(4);
U(1).Reallocate(7);
V.PushBack(U);

// or add more intermediate vectors
Vector<Vector<Vector<double> > > W(2);
W(0).Reallocate(3); W(0)(0).Reallocate(4); W(0)(1).Reallocate(5); W(0)(2).Reallocate(6);
W(1).Reallocate(2); W(1)(0).Reallocate(7); W(1)(1).Reallocate(3);
V.PushBack(W);

// or use Vector3 
Vector3<double> Uc(2);
Uc.Reallocate(0, 3); // etc
Uc.Reallocate(1, 2); // etc
V.PushBack(Uc);

Related topics :

Reallocate
GetSize

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Clear

Syntax :

  void Clear()
  void Clear(int i);
  void Clear(int i, int j);

This method clears a single inner/intermediate vector or all the structure.

Example :

Vector3<double> V;
// structure with two intermediate vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(0, 0, 2); V.Reallocate(0, 1, 4); V.Reallocate(0, 2, 3);
V(0, 0, 0) = 2.2; V(0, 0, 1) = 1.0; V(0, 1, 0) = -3.5; V(0, 1, 1) = 2.7;
V(0, 1, 2) = 5.2; V(0, 1, 3) = -0.9; V(0, 2, 0) = 2.1; V(0, 2, 1) = 2.4; V(0, 2, 2) = 1.9;

V.Reallocate(1, 2);
V.Reallocate(1, 0, 5); V.Reallocate(1, 1, 3);
V(1, 0, 0) = 0.8; V(1, 0, 1) = 2.4; // etc

// you can clear inner vector (1, 1);
V.Clear(1, 1);

// you can clear first intermediate vector
V.Clear(0);

// and all the structure
V.Clear();

Related topics :

Reallocate
GetSize

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Fill

Syntax :

  void Fill(const T& x);

This method sets all the elements to the same value

Example :

Vector3<double> V;
// structure with two intermediate vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(0, 0, 4); V.Reallocate(0, 1, 2); V.Reallocate(0, 2, 3);
V.Reallocate(1, 2);
V.Reallocate(1, 0, 5); V.Reallocate(1, 1, 7);

// if you want to initialize all the elements to 0 :
V.Fill(0.0);

Related topics :

Reallocate
GetSize

Location :

Class Vector3
Vector3.hxx Vector3.cxx

GetVector

Syntax :

  Vector<Vector<Vector<T> > >& GetVector();
  Vector<Vector<T> >& GetVector(int i);
  Vector<T>& GetVector(int i, int j);

This method returns the vector of vectors of the structure or an intermediate vector or a single inner vector.

Example :

Vector3<double> V;
// structure with two intermediate vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(0, 0, 4); V.Reallocate(0, 1, 2); V.Reallocate(0, 2, 3);
V.Reallocate(1, 2);
V.Reallocate(1, 0, 5); V.Reallocate(1, 1, 7);

// if you wish, you can retrieve the vector of vectors of vectors
Vector<Vector<Vector<double> > > >& vec = V.GetVector();

// or an intermediate vector
Vector<Vector<double> > >& x = V.GetVector(1);

// or a single inner vector
Vector<double>& inn = V.GetVector(0, 2);

Related topics :

Reallocate
operator()

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Print

Syntax :

  void Print()

This method displays the structure

Example :

Vector3<double> V;
// structure with two intermediate vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(0, 0, 4); V.Reallocate(0, 1, 2); V.Reallocate(0, 2, 3);
V.Reallocate(1, 2);
V.Reallocate(1, 0, 5); V.Reallocate(1, 1, 7);

V.Print();

Related topics :

Reallocate
operator =

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Write

Syntax :

  void Write(string file_name, with_size = true);
  void Write(ostream& output_stream, with_size = true);

This method writes the structure in a file or in an output stream. The second argument is optional, by default the size of the vector is written at the beginning of the stream.

Example :

Vector3<double> V;
// structure with two intermediate vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(0, 0, 4); V.Reallocate(0, 1, 2); V.Reallocate(0, 2, 3);
V.Reallocate(1, 2);
V.Reallocate(1, 0, 5); V.Reallocate(1, 1, 7);

// writes V in a file
V.Write("v.dat");

// or using an output stream
ofstream file_out("v2.dat");
V.Write(file_out);
file_out.close();

Related topics :

Reallocate
operator =

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Read

Syntax :

  void Read(string file_name, with_size = true);
  void Read(istream& output_stream, with_size = true);

This method reads the structure in a file or in an input stream. The second argument is optional, by default the size of the vector is read at the beginning of the stream.

Example :

Vector3<double> V;

// reads V in a file
V.Read("v.dat");

// or using an input stream
ifstream file_in("v2.dat");
V.Read(file_in);
file_in.close();

Related topics :

Reallocate
operator =

Location :

Class Vector3
Vector3.hxx Vector3.cxx

Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Vector3
Vector of vectors of vectors.
Definition: Vector3.hxx:65
Seldon
Seldon namespace.
Definition: Array.cxx:24