Dense Vectors
|
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 | |
Get | returns a reference to an element of the vector |
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 |
GetMemorySize | returns the memory used to store the vector in bytes |
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 | appends an element to the end of the vector |
Append | appends 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 VectorExpression& ) Vector& operator =(const T0& alpha) Vector& operator *=(const T0& alpha) Vector& operator +=(const VectorExpression<T0, E>& V) Vector& operator -=(const VectorExpression<T0, E>& V)
VectorExpression operator+(const VectorExpression<T0, E>& x, const VectorExpression<T1, E>& y); VectorExpression operator-(const VectorExpression<T0, E>& x, const VectorExpression<T1, E>& y); VectorExpression operator*(const VectorExpression<T0, E>& x, const VectorExpression<T1, E>& y); VectorExpression operator/(const VectorExpression<T0, E>& x, const VectorExpression<T1, E>& y);
A VectorExpression is an expression involving vectors (for example a sum of two vectors). Template expressions are used here to avoid any allocation of an intermediate vector. As a result, the expression is evaluated at the end (this process is called lazy evaluation) when the expression is affected to a given vector.
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; // adding two vectors Vector<double> U2(3); U2.FillRand(); U += U2; Vector<double> X(3), Y(3); X.FillRand(); Y.FillRand(); // you can combine vectors (that will create VectorExpression objects): // the effective computation is performed when the operator // =, -= or += is called U = 2.0*X - Y*4.0; U2 += X + Y; U -= 2.5*Y; // the operator * and / are elementwise operators (.* and ./ in Matlab) U = X*Y; // u_i = x_i * y_i U2 = X/Y + 2.0; // u2_i = x_i / y_i + 2 // scalars can be used in operators / * - + U = 1.5 + Y/3.0; // u_i = 1.5 + y_i/3
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
const T& Get(int) const; T& Get(int);
This method has been added for compatibility with sparse vectors.
Vector<double> V(3); // use of operator () to modify vector V(0) = 1.4; V(2) = -0.5; // you can also use Get V.Get(1) = 2.3; // V should be equal to [1.4, 2.3, -0.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
size_t GetMemorySize() const;
This method returns the memory used by a vector in bytes (by using sizeof function). It will work only if the vector stores non-pointers objects or types, i.e. objects that do not store dynamic arrays.
Vector<float> V(3); cout << "Memory used to store V = " << V.GetMemorySize() << " bytes " << endl; Vector<Vector<double> > W(5); W(0).Reallocate(10); W(1).Reallocate(5); // Here GetMemorySize would not take into account the dynamic allocations that // occured in the elements of W, it will only display the memory used to store usual attributes cout << "Minimal memory used to store W = " << W.GetMemorySize() << " bytes " << 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. If you have selected CallocAlloc or MallocAlloc allocator, previous elements are kept.
Vector<long int> V(5); V.Fill(); // resizes vector V V.Reallocate(20); // you need to initialize all vector V.Zero(); // same behaviour as Resize with CallocAlloc Vector<double, VectFull, CallocAlloc<double> > U(3); U.Fill(); U.Resize(4); U(3) = -2.0; // U should be equal to [0, 1, 2, -2]
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 Append(const T0&);
This method inserts a single element to the end of the vector. This method can be used with allocators CallocAlloc, MallocAlloc, but not with NewAlloc. For this last allocator, prefer the method PushBack.
// using an allocator allowing that functionality Vector<double, VectFull, CallocAlloc<double> > V; // a single element is appended V.Append(1.0); // now other elements V.Append(1.5); W.Append(-1.0); // 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 (modulus for complex numbers) whereas GetNormInfIndex
returns the index where this maximum is reached.
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; void Write(ofstream&, bool with_size) 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. You can also write the list of elements without the size.
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(); // you can ask to write the values without the size at the beginning // in this example, we write two vectors of the same size file_out.open("vector2.dat"); Vector<double> U(V); // first vector is written with its size U.Write(file_out); // second vector has the same size, elements are written directly V.Write(file_out, false); file_out.close();
Class Vector<T, VectFull>
Vector.hxx
Vector.cxx
void Read(string); void Read(istream&); void Read(istream&, bool with_size);
This method sets the vector from a file/stream in binary format. The file contains the number of elements, then the list of elements. If with_size is set to false, the number of elements is not expected in the stream (and not read).
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(); // we read two vectors of the same size Vector<double> U; file_in.open("vector2.dat"); V.Read(file_in); // second vector has same size U.Reallocate(V.GetM()); U.Read(file_in, false); file_in.close();
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. 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.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(); // 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