Dense vectors

Vectors contains contiguous elements. In that page, methods and functions related to dense vectors are detailed.

Basic declaration :

// dense vector of doubles
Vector<double> U;

// dense vector of integers : IVect or Vector<int>
IVect num;

// with a different allocator
Vector<float, Vect_Full, NewAlloc<float> > V;

Methods :

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
Print 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

Functions :

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 constructors

Syntax :

  Vector();
  Vector(const Vector& X );
  Vector(int n);

Example :

// 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);

Related topics :

Reallocate
Fill

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Vector operators

Syntax :

  const T& operator (int) const;
  T& operator (int);
  Vector& operator =(const Vector& )
  Vector& operator =(const T0& alpha)
  Vector& operator *=(const T0& alpha)

Example :

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;

Related topics :

Copy

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

GetM, GetLength, GetSize, GetDataSize

Syntax :

  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.

Example :

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;

Location :

Class Vector_Base
Vector.hxx
Vector.cxx

GetData, GetDataConst, GetDataVoid, GetDataConstVoid

Syntax :

  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.

Example :

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());

Related topics :

SetData
Nullify

Location :

Class Vector_Base
Vector.hxx
Vector.cxx

Clear

Syntax :

  void Clear();

This method removes all the elements of the vector.

Example :

Vector<double> V(3);
V.Fill();
// clears vector V
V.Clear();

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Reallocate

Syntax :

  void Reallocate(int);

This method changes the size of the vector, but removes previous elements.

Example :

Vector<long int> V(5);
V.Fill();
// resizes vector V
V.Reallocate(20);
// you need to initialize all vector
V.Zero();

Related topics :

Resize

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Resize

Syntax :

  void Resize(int);

This method changes the size of the vector, and keeps previous elements.

Example :

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;

Related topics :

Reallocate

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

SetData

Syntax :

  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.

Example :

// 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();
}

Related topics :

GetData
Nullify

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Nullify

Syntax :

  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.

Related topics :

SetData
GetData

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Copy

Syntax :

  void Copy(const Vector<T>&);

This method copies a vector into the current vector.

Example :

// copy of a vector V
Vector<double> V(10), W;
V.FillRand();
W.Copy(V);
// this is equivalent to use operator =
W = V;

Related topics :

Vector operators

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

PushBack

Syntax :

  void PushBack(const T0&);
  void PushBack(const Vector<T>&);

This method inserts a single element, or a vector to the end of the vector.

Example :

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]

Related topics :

Resize

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Zero

Syntax :

  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.

Example :

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

Related topics :

Fill

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Fill

Syntax :

  void Fill();
  void Fill(const T0& );

This method fills vector with 0, 1, 2, etc or with a given value.

Example :

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]

Related topics :

Zero

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

FillRand

Syntax :

  void FillRand();

This method fills the vector randomly.

Example :

Vector<double> V(5);
V.FillRand();
// V should contain 5 random values

Related topics :

Fill

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

GetNormInf, GetNormInfIndex

Syntax :

  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.

Example :

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

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Print

Syntax :

  void Print() const;

This method displays the vector.

Example :

Vector<string> V(2);
V.PushBack("hello");
V.PushBack("world");
V.Print(); // should display "hello world"

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Write

Syntax :

  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.

Example :

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();

Related topics :

Read
WriteText
ReadText

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

Read

Syntax :

  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.

Example :

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

Related topics :

Write
WriteText
ReadText

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

WriteText

Syntax :

  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.

Example :

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();

Related topics :

Write
Read
ReadText

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx

ReadText

Syntax :

  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.

Example :

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();

Related topics :

Write
Read
WriteText

Location :

Class Vector<T, Vect_Full>
Vector.hxx
Vector.cxx