Functions for Vectors

In that page, we detail functions that are not related to Blas.

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
HasElement returns true if the vector contains a given value

QuickSort

Syntax :

  void QuickSort(int, int, Vector&);
  void QuickSort(int, int, Vector&, Vector&);
  void QuickSort(int, int, Vector&, Vector&, Vector&);

This function sorts a vector with quick sort algorithm, and affects the permutation to other vectors.

Example :

Vector<int> X(4), Y(4), Z(4);
X(0) = 3;
X(1) = 0;
X(2) = 2;
X(3) = 1;
Y = X;
 
// if you want to sort all elements of X
// the start index is 0, the final index is X.GetM()-1 
QuickSort(0, Y.GetM()-1, Y);

// if you want to retrieve the permutation vector
// sorting operations will affect vector Y
Y.Fill();
QuickSort(0, X.GetM()-1, X, Y);

// you can ask that a third vector is affected like the second vector
QuickSort(0, X.GetM()-1, X, Y, Z);

Location :

Functions_Arrays.cxx

MergeSort

Syntax :

  void MergeSort(int, int, Vector&);
  void MergeSort(int, int, Vector&, Vector&);
  void MergeSort(int, int, Vector&, Vector&, Vector&);

This function sorts a vector with merge sort algorithm, and affects the permutation to other vectors.

Example :

Vector<int> X(4), Y(4), Z(4);
X(0) = 3;
X(1) = 0;
X(2) = 2;
X(3) = 1;
Y = X;

// if you want to sort all elements of X
// the start index is 0, the final index is X.GetM()-1 
MergeSort(0, Y.GetM()-1, Y);

// if you want to retrieve the permutation vector
// sorting operations will affect vector Y
Y.Fill();
MergeSort(0, X.GetM()-1, X, Y);

// you can ask that a third vector is affected like the second vector
MergeSort(0, X.GetM()-1, X, Y, Z);

Location :

Functions_Arrays.cxx

Sort

Syntax :

  void Sort(int, int, Vector&);
  void Sort(int, int, Vector&, Vector&);
  void Sort(int, int, Vector&, Vector&, Vector&);
  void Sort(int, Vector&);
  void Sort(int, Vector&, Vector&);
  void Sort(int, Vector&, Vector&, Vector&);
  void Sort(Vector&);
  void Sort(Vector&, Vector&);
  void Sort(Vector&, Vector&, Vector&);

This function sorts a vector and affects the permutation to other vectors.

Example :

Vector<int> X(4), Y(4), Z(4);
X(0) = 3;
X(1) = 0;
X(2) = 2;
X(3) = 1;
Y = X;

Sort(Y);

// if you want to retrieve the permutation vector
// sorting operations will affect vector Y
Y.Fill();
Sort(X, Y);
// you should get X = [0 1 2 3], Y = [1 3 2 0]

// you can ask that a third vector is affected like the second vector
Sort(X, Y, Z);

// you can ask to sort only the first n elements of vector
int n = 3;
Sort(n, X);

// or ask to sort the elements between two indices of the vector
int start_index = 1, end_index = 3;
Sort(start_index, end_index, X);

Location :

Functions_Arrays.cxx

Assemble

Syntax :

  void Assemble(int&, Vector&);
  void Assemble(int&, Vector&, Vector&);

This function sorts the first vector and removes duplicate elements. If a second vector is present in the arguments, the elements of the second vector are added when there are duplicate elements in the first vector. The vectors are not resized, and the first argument is an integer that will contain the new number of elements.

Example :

Vector<int> num(4);
Vector<double> values(4);
// we initialize with duplicate numbers
num(0) = 2;
num(1) = 1;
num(2) = 3;
num(3) = 1;
values(0) = 0.2;
values(1) = 0.7;
values(2) = -1.2;
values(3) = 0.8;

// you provide the number of elements to assemble
int nb = num.GetM();
Assemble(nb, num, values);

// in this example, there is one duplicate element -> 1
// as a result, after the call to Assemble, you should have
// nb = 3,  num = [1 2 3],  values = [1.5 0.2 -1.2]

Location :

Functions_Arrays.cxx

RemoveDuplicate

Syntax :

  void RemoveDuplicate(int&, Vector&);
  void RemoveDuplicate(int&, Vector&, Vector&);
  void RemoveDuplicate(Vector&);
  void RemoveDuplicate(Vector&, Vector&);

This function sorts the first vector and removes duplicate elements. If an integer is given as first argument, the vectors are not resized, and the first argument will contain the new number of elements. The second vector is affected by the operations made on the first vector.

Example :

Vector<int> num(4), permutation(4);
// we initialize with duplicate numbers
num(0) = 2;
num(1) = 1;
num(2) = 3;
num(3) = 1;
permutation.Fill();

// you remove duplicate elements, and find the permutation vector
// vectors are resized
RemoveDuplicate(num, permutation);


// you can also provide the number of elements to treat
// and vectors will not be resized, but you get the new number of elements
int nb_elt = num.GetM();
RemoveDuplicate(nb_elt, num, permutation);

Location :

Functions_Arrays.cxx

HasElement

Syntax :

  bool HasElement(const Vector& u, const amp&;  a);

This function returns true if the vector u contains a value equal to a, false otherwise.

Example :

Vector<int> num(4);
num(0) = 2;
num(1) = 1;
num(2) = 3;
num(3) = 1;

// num contains 2, HasElement should return true here
bool ok = HasElement(num, 2);

Location :

Functions_Arrays.cxx