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>. The allocators have default values and do not need to be provided. 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: SeldonDefaultAllocator<VectFull, T>. Allocator1 is the allocator for the vector 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"

Vector2<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;

Vector2<double> V;

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<long> 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:

  • Reallocate(long M) and Reallocate(long i, long n) which allow to reallocate the vector of vectors and the i-th inner vector, respectively.

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

  • GetLength(long i) which returns the length of an inner vector.

  • operator()(long i) which returns the i-th inner vector.

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

Methods of Vector2 :

The other methods are described in the table below.

Vector constructors
Vector operators
IsEmpty returns true if all inner vectors have 0-length
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
Select keeps a subset of 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
Copy copies a vector of vectors
HasSameShape returns true if all inner vectors have the same size
Print displays the object
Write writes the object in an output stream
Read reads the object from an output stream


An Example

doc/example/vector2.cpp
#define SELDON_DEBUG_LEVEL_4
#include "SeldonLib.hxx"
using namespace Seldon;
int main()
{
TRY;
Vector<long> 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       

Vector2 constructors

Syntax :

  Vector2();
  Vector2(long);
  Vector2(Vector<long>);

Example :

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

// then you can use Reallocate to fill the structure
V.Reallocate(3); // 3 inner vectors
V.Reallocate(0, 4); // first inner vector contains 4 elements
V.Reallocate(1, 5); // second inner vector contains 5 elements
V.Reallocate(2, 3); // third inner vector contains 3 elements
// V.GetNelement() should return 3+4+5 = 12 

// constructor specifying only the number of inner vectors
Vector2<double> U(3);
// then each inner vector can be initialized with Reallocate
U.Reallocate(0, 4); // first inner vector contains 4 elements
U.Reallocate(1, 5); // second inner vector contains 5 elements
U.Reallocate(2, 3); // third inner vector contains 3 elements

// constructor specifying the size of all the inner vectors
Vector<long> shape(3);
shape(0) = 4; 
shape(1) = 5;
shape(2) = 3;
Vector2<double> W(shape);

Related topics :

Reallocate
GetNelement

Location :

Class Vector2
Vector2.hxx Vector2.cxx

Vector2 operators

Syntax :

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

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

Example :

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

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

Related topics :

Reallocate
GetVector

Location :

Class Vector2
Vector2.hxx Vector2.cxx

IsEmpty

Syntax :

  bool IsEmpty()

This method returns true if all the inner vectors are empty.

Example :

// declaration of a vector with 3 inner vectors
Vector2<double> U(3);
// filling one inner vector
U(1).Reallocate(5);
// IsEmpty should return false
cout << "U empty ? " << U.IsEmpty() << endl;

U.Clear()
// IsEmpty should return true now
cout << "U empty ? " << U.IsEmpty() << endl;

Related topics :

Reallocate
Clear

Location :

Class Vector2
Vector2.hxx Vector2.cxx

GetLength, GetSize

Syntax :

  long GetLength()
  long GetSize()
  long GetLength(long i)
  long GetSize(long i)

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 inner vectors
Vector2<double> U(3);
// U.GetSize() should return 3
cout << "Number of inner vectors " << U.GetSize() << endl;

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

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

Related topics :

GetShape
GetNelement

Location :

Class Vector2
Vector2.hxx Vector2.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 inner vectors
Vector2<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 Vector2
Vector2.hxx Vector2.cxx

GetNelement

Syntax :

  long GetNelement()
  long GetNelement(long beg, long end)

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

Example :

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

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

Related topics :

GetShape
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

GetShape

Syntax :

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

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

Example :

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

// retrieving the size of all inner vectors
Vector<long> shape
V.GetShape(shape);
// shape should contain [4, 5, 3, 7, 6]

Related topics :

Reallocate
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

Reallocate

Syntax :

  void Reallocate(long n)
  void Reallocate(long i, long n)
  void Reallocate(const Vector<long>& shape)

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

Example :

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

// another way to do that is to specify shape
Vector<long> shape;
shape.Reallocate(5);
shape(0) = 4; 
shape(1) = 5;
shape(2) = 3;
shape(3) = 7;
shape(4) = 6;
V.Clear(); V.Reallocate(shape);

Related topics :

GetShape
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

Select

Syntax :

  void Select(long beg, long end);

This method keeps a subset of inner vectors, while removing the other ones.

Example :

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

// if you want to keep only second, third and fourth inner vector
V.Select(1, 4);
// now the second inner vector has become the first inner vector

Related topics :

Reallocate
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

Flatten

Syntax :

  Vector<T> Flatten();
  void Flatten(Vector<T>& data);
  void Flatten(long beg, long end, 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 :

Vector2<double> V;
// structure with two inner vectors 
V.Reallocate(2);
// V(0) = (x1, x2, ..., xn)
V.Reallocate(0, 4);
// V(1) = (y1, y2, ..., ym)
V.Reallocate(1, 6);

// then you can obtain a simple vector U = (x1, x2, ..., xn, y1, y2, ..., ym)
Vector<double> vec;
V.Flatten(vec);

// if you want to flatten only second, third and fourth inner vector
V.Flatten(1, 4, vec);

Related topics :

Reallocate
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

PushBack

Syntax :

  void PushBack(long i, const T& x)
  void PushBack(const Vector<T>& x)
  void PushBack(const Vector<Vector<T> >& x)
  void PushBack(const Vector2<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 :

Vector2<double> V;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V(0, 0) = 2.2; V(0, 1) = 1.0; V(0, 2) = -3.5;
V.Reallocate(1, 2);
V(1, 0) = 0.8; V(1, 1) = 2.4;

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

// add a third inner vector
Vector<double> vec(4);
vec.Fill();
V.PushBack(vec);

// and add fourth and fifth inner vectors
Vector<Vector<double>, VectFull, NewAlloc<Vector<double> > > U(2);
U(0).Reallocate(4);
U(1).Reallocate(7);
V.PushBack(U);

// or use Vector2 to append several inner vectors
Vector2<double> Uc(2);
Uc.Reallocate(0, 4);
Uc.Reallocate(1, 7);
V.PushBack(Uc);

Related topics :

Reallocate
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

Clear

Syntax :

  void Clear()
  void Clear(long i);

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

Example :

Vector2<double> V;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V(0, 0) = 2.2; V(0, 1) = 1.0; V(0, 2) = -3.5;
V.Reallocate(1, 2);
V(1, 0) = 0.8; V(1, 1) = 2.4;

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

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

Related topics :

Reallocate
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

Fill

Syntax :

  void Fill(const T& x);

This method sets all the elements to the same value

Example :

Vector2<double> V;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(1, 2);

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

Related topics :

Reallocate
GetSize

Location :

Class Vector2
Vector2.hxx Vector2.cxx

GetVector

Syntax :

  Vector<Vector<T> >& GetVector();
  Vector<T>& GetVector(long i);

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

Example :

Vector2<double> V;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(1, 2);

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

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

Related topics :

Reallocate
operator()

Location :

Class Vector2
Vector2.hxx Vector2.cxx

GetVector

Syntax :

  void Copy(const Vector2<T>& U)
  void Copy();

This method copies another vector of vectors.

Example :

Vector2<double> V, W;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(1, 2);

// you can copy contents of V in W
W.Copy(V);

// or create a copy of V on the fly
W = V.Copy();

Related topics :

Reallocate
operator =

Location :

Class Vector2
Vector2.hxx Vector2.cxx

HasSameShape

Syntax :

  bool HasSameShape(const Vector2<T>& )

This method returns if the two structures have the same shape, i.e. all inner vectors have the same size.

Example :

Vector2<double> V, W;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(1, 2);

// W with 3 inner vectors
W.Reallocate(3);
W.Reallocate(0, 3);
W.Reallocate(1, 2);
W.Reallocate(1, 4);
// V.HasSameShape(W) should return false

// X with 2 inner vectors
Vector2<double> X(2);
X.Reallocate(0, 3);
X.Reallocate(1, 4);
// V.HasSameShape(X) should return false

Related topics :

GetSize
GetShape

Location :

Class Vector2
Vector2.hxx Vector2.cxx

Print

Syntax :

  void Print()

This method displays the structure

Example :

Vector2<double> V;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(1, 2);

V.Print();

Related topics :

Reallocate
operator =

Location :

Class Vector2
Vector2.hxx Vector2.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 :

Vector2<double> V;
// structure with two inner vectors 
V.Reallocate(2);
V.Reallocate(0, 3);
V.Reallocate(1, 2);

// 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 Vector2
Vector2.hxx Vector2.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 :

Vector2<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 Vector2
Vector2.hxx Vector2.cxx

Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Vector2
Vector of vectors.
Definition: Vector2.hxx:53
Seldon
Seldon namespace.
Definition: Array.cxx:24