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.
#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);
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.
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 |
displays the object | |
Write | writes the object in an output stream |
Read | reads the object from an output stream |
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(); Vector2(long); Vector2(Vector<long>);
// 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);
Class Vector2
Vector2.hxx Vector2.cxx
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.
// 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;
Class Vector2
Vector2.hxx Vector2.cxx
bool IsEmpty()
This method returns true if all the inner vectors are empty.
// 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;
Class Vector2
Vector2.hxx Vector2.cxx
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.
// 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;
Class Vector2
Vector2.hxx Vector2.cxx
size_t GetMemorySize();
This method returns the memory (in bytes) used to store the object.
// 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;
Class Vector2
Vector2.hxx Vector2.cxx
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.
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;
Class Vector2
Vector2.hxx Vector2.cxx
Vector<long> GetShape() void GetShape(Vector<long>& );
This method returns the shape of the vector, i.e. the size of each inner vector.
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]
Class Vector2
Vector2.hxx Vector2.cxx
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.
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);
Class Vector2
Vector2.hxx Vector2.cxx
void Select(long beg, long end);
This method keeps a subset of inner vectors, while removing the other ones.
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
Class Vector2
Vector2.hxx Vector2.cxx
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.
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);
Class Vector2
Vector2.hxx Vector2.cxx
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.
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);
Class Vector2
Vector2.hxx Vector2.cxx
void Clear() void Clear(long i);
This method clears a single inner vector or all the structure.
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();
Class Vector2
Vector2.hxx Vector2.cxx
void Fill(const T& x);
This method sets all the elements to the same value
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);
Class Vector2
Vector2.hxx Vector2.cxx
Vector<Vector<T> >& GetVector(); Vector<T>& GetVector(long i);
This method returns the vector of vectors of the structure, or a single inner vector.
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);
Class Vector2
Vector2.hxx Vector2.cxx
void Copy(const Vector2<T>& U) void Copy();
This method copies another vector of vectors.
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();
Class Vector2
Vector2.hxx Vector2.cxx
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.
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
Class Vector2
Vector2.hxx Vector2.cxx
void Print()
This method displays the structure
Vector2<double> V; // structure with two inner vectors V.Reallocate(2); V.Reallocate(0, 3); V.Reallocate(1, 2); V.Print();
Class Vector2
Vector2.hxx Vector2.cxx
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.
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();
Class Vector2
Vector2.hxx Vector2.cxx
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.
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();
Class Vector2
Vector2.hxx Vector2.cxx