Vectors contains contiguous elements. In that page, methods and functions related to dense vectors are detailed.
// dense vector of doubles Vector<double> U; // dense vector of integers : IVect or Vector<int> IVect num; // with a different allocator Vector<float, VectFull, NewAlloc<float> > V;
Vector constructors | |
Vector operators | |
GetM | returns the number of elements in the vector |
GetLength | returns the number of elements in the vector |
GetSize | returns the number of elements in the vector |
GetData | returns a pointer to the array contained in the vector |
GetDataConst | returns a pointer to the array contained in the vector |
GetDataVoid | returns a pointer to the array contained in the vector |
GetDataConstVoid | returns a pointer to the array contained in the vector |
Clear | removes all elements of the vector |
Reallocate | changes the size of vector (removes previous elements) |
Resize | changes the size of vector (keeps previous elements) |
SetData | sets the pointer to the array contained in the vector |
Nullify | clears the vector without releasing memory |
Copy | copies a vector |
PushBack | adds an element to the end of the vector |
GetDataSize | returns the number of elements in the vector |
Zero | sets all elements to zero |
Fill | sets all elements to a given value |
FillRand | fills randomly the vector |
GetNormInf | returns highest absolute value |
GetNormInfIndex | returns the index of the highest absolute value |
displays the vector | |
Write | writes the vector in binary format |
Read | reads the vector in binary format |
WriteText | writes the vector in text format |
ReadText | reads the vector in text format |
Mlt | multiplies the elements of the vector by a scalar |
Add | adds two vectors |
Copy | copies one vector into another one |
Swap | exchanges two vectors |
ApplyRot | applies rotation to 2-D points |
ApplyModifRot | applies rotation to 2-D points |
DotProd | scalar product between two vectors |
DotProdConj | scalar product between two vectors, first vector being conjugated |
Conjugate | conjugates a vector |
GetMaxAbsIndex | returns index where highest absolute value is reached |
Norm1 | returns 1-norm of a vector |
Norm2 | returns 2-norm of a vector |
QuickSort | sorts a vector with quick sort algorithm |
MergeSort | sorts a vector with merge sort algorithm |
Sort | sorts a vector |
Assemble | assembles a vector |
RemoveDuplicate | sorts and removes duplicate elements of a vector |
Vector(); Vector(const Vector& X ); Vector(int n);
// default constructor -> empty vector Vector<int> V; cout << "Number of elements "<< V.GetM() << endl; // It should return 0 // then you can use Reallocate to set the size V.Reallocate(3); V.Fill(); // copy constructor (V -> U) Vector<int> V = U; // constructor specifying the size of V Vector<double> W(4); // W is not initialized, you have to fill it W.Fill(1.0);
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
const T& operator (int) const; T& operator (int); Vector& operator =(const Vector& ) Vector& operator =(const T0& alpha) Vector& operator *=(const T0& alpha)
Vector<string> V(3); // use of operator () to modify vector V(0) = string("element 0"); V(1) = V(0) + string(" and 1"); Vector<string> W; // use of operator = to copy contents of vector V W = V; // you can set all elements to a given value Vector<double> U(3); U = 1; // all elements of U are equal to 1 // multiplication by a scalar U.Fill(); U *= 1.5;
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
int GetM() const; int GetLength() const; int GetSize() const; int GetDataSize() const;
All those methods are identic and return the number of elements contained in the vector.
Vector<float> V(3); cout << "Number of elements of V " << V.GetM() << endl; V.Reallocate(5); cout << "Number of elements of V " << V.GetSize() << endl;
Class Vector_Base
Vector.hxx
Vector.cxx
T* GetData() const; const T* GetDataConst() const; void* GetDataVoid() const; const void* GetDataConstVoid() const;
Those methods are useful to retrieve the pointer to the array. In practice, you can use those methods in order to interface with C/fortran subroutines or to perform some low level operations. But in this last case, you have to be careful, because debugging operations will be more tedious.
Vector<double> V(3); V.Fill(); double* data = V.GetData(); // you can use data as a normal C array // here the sum of elements is computed double sum = 0; for (int i = 0; i < V.GetM(); i++) sum += data[i]; // this would be equivalent and safer to write sum = 0; for (int i = 0; i < V.GetM(); i++) sum += V(i); // if you want to call a fortran subroutine daxpy Vector<double> X(3), Y(3); double coef = 2.0; // in this case, X is constant int n = X.GetM(); daxpy_(& coef, & n, X.GetDataConst(), Y.GetData()); // for complex numbers, conversion to void* is needed : Vector<complex<double> > Xc(4), Yc(4); complex<double> beta(1,1); zaxpy(reinterpret_cast<const void*>(beta), Xc.GetDataConstVoid(), Yc.GetDataVoid());
Class Vector_Base
Vector.hxx
Vector.cxx
void Clear();
This method removes all the elements of the vector.
Vector<double> V(3); V.Fill(); // clears vector V V.Clear();
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Reallocate(int);
This method changes the size of the vector, but removes previous elements.
Vector<long int> V(5); V.Fill(); // resizes vector V V.Reallocate(20); // you need to initialize all vector V.Zero();
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Resize(int);
This method changes the size of the vector, and keeps previous elements.
Vector<long double> V(5); V.Fill(); // resizes vector V V.Resize(20); // you need to initialize new elements if there are new for (int i = 5; i < 20; i++) V(i) = 0;
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void SetData(int, T*);
This method sets the pointer to the array containing elements. This method should be used carefully, and generally in conjunction with method Nullify.
// for example, you can define a function with a pointer as argument void f(double* data) { // and sets this array into a Vector instance Vector<double> V; V.SetData(5, data); // then you use a C++ method double rhs = Norm2(V); // you don't release memory, because data is used after the function V.Nullify(); }
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Nullify();
This method clears the vector without releasing memory. This method should be used carefully, and generally in conjunction with method SetData. You can look at the example shown in the explanation of method SetData.
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Copy(const Vector<T>&);
This method copies a vector into the current vector.
// copy of a vector V Vector<double> V(10), W; V.FillRand(); W.Copy(V); // this is equivalent to use operator = W = V;
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void PushBack(const T0&); void PushBack(const Vector<T>&);
This method inserts a single element, or a vector to the end of the vector.
Vector<double> V; // a single element is appended V.PushBack(1.0); // now another vector is appended Vector<double> W(2); W(0) = 1.5; W(1) = -1.0; V.PushBack(W); // W should contain [1.0 1.5 -1.0]
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Zero();
This method fills memory of 0, is convenient for vector made of doubles, integers, floats, but not for more complicated types. In that case, it is better to use the method Fill.
Vector<double> V(5); // initialization V.Fill(); Vector<IVect> W(10); // W.Zero() is incorrect and would generate an error at the execution // a good initialization is to use Fill IVect zero(5); zero.Zero(); W.Fill(zero);
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Fill(); void Fill(const T0& );
This method fills vector with 0, 1, 2, etc or with a given value.
Vector<int> V(5); V.Fill(); // V should contain [0 1 2 3 4] V.Fill(2); // V should contain [2 2 2 2 2]
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void FillRand();
This method fills the vector randomly.
Vector<double> V(5); V.FillRand(); // V should contain 5 random values
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
T GetNormInf() const; int GetNormInfIndex() const;
GetNormInf
returns the highest absolute value whereas GetNormInfIndex
returns the index where this maximum is reached. This method does not work for complex numbers.
Vector<int> V(3); V(0) = 1; V(1) = -2; V(3) = 0; int imax = V.GetNormInf(); // should return 2 int pos = V.GetNormInfIndex(); // should return 1
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Print() const;
This method displays the vector.
Vector<string> V(2); V.PushBack("hello"); V.PushBack("world"); V.Print(); // should display "hello world"
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Write(string) const; void Write(ofstream&) const;
This method writes the vector on a file/stream in binary format. The file will contain the number of elements, then the list of elements.
Vector<double> V(2); // you can write directly in a file V.Fill(); V.Write("vector.dat"); // or open a stream with other datas ofstream file_out("vector.dat"); int my_info = 3; file_out.write(reinterpret_cast<char*>(&my_info), sizeof(int)); V.Write(file_out); file_out.close();
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Read(string); void Read(istream&);
This method sets the vector from a file/stream in binary format. The file contains the number of elements, then the list of elements. The method ReadText can also be useful to initialize an array with a string.
Vector<double> V; // you can read directly on a file V.Read("vector.dat"); // or read from a stream ifstream file_in("vector.dat"); int my_info; file_in.read(reinterpret_cast<char*<(&my_info), sizeof(int)); V.Read(file_in); file_in.close(); // or initialize values of a vector with a string string values("1.23 -4.1 2.5 0.1 0.6 -0.7"); istringstream stream_data(values); V.ReadText(stream_data);
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void WriteText(string) const; void WriteText(ofstream&) const;
This method writes the vector on a file/stream in text format. The file will contain the list of elements.
Vector<double> V(2); // you can write directly in a file V.Fill(); V.WriteText("vector.dat"); // or open a stream with other datas ofstream file_out("vector.dat"); int my_info = 3; file_out << my_info << '\n'; V.WriteText(file_out); file_out.close();
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void ReadText(string); void ReadText(istream&);
This method sets the vector from a file/stream in text format. The file contains the list of elements.
Vector<double> V; // you can read directly on a file V.ReadText("vector.dat"); // or read from a stream ifstream file_in("vector.dat"); int my_info; file_in >> my_info; V.ReadText(file_in); file_in.close();
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx