template class Vector<typename ScalarType,int capacity> - see the c++ source of an example

This file provides a short description on how to use class Vector from CAPD package - for more details see header file "capd/vectalg/Vector.h"

See also

Content of this file:

The template class Vector is defined in the namespace capd::vectalg and it has two parameters - type of elements stored in a vector and capacity. If capacity is larger than zero, vector is represented as an array of given length inside the object. If capacity is set to zero, the capacity of a vector must be set when the object is created.

The following lines define new names for four dimensional vectors

   typedef Vector<double,4> DVector4D;
   typedef Vector<interval,4> IVector4D;

The following lines define new names for vectors of arbitrary length

   typedef Vector<double,0> DVector;
   typedef Vector<interval,0> IVector;

How to create a vector

the following line creates a four dimensional vector filled with zeros

   DVector4D x;

'y' will be a five dimensional interval vector filled by zeros

   IVector y(5);

The following line creates a vector from a given table of numbers. In fact, the dimension is known, but the interfaces for both types of vectors (dynamic table and internal array) are the same - so one must specify the dimension in both cases

   double data1[] = {1.,2.,3.,4.};
   double data2[] = {4.,3.,2.,1.};
   DVector4D a(4,data1);
   DVector4D b(4,data2);

How to create an array of vectors

When one needs to create an array of vectors which has undefined dimension at compilation time, the following solution is available. The class Vector has a static function which sets default dimension for all objects that will be created by default constructor

   DVector::setDefaultDimension(5);
   DVector *tab = new DVector[6];
   // using an array
   delete []tab;

The above can be also written in short form

   tab = new (5) DVector[6];

which means that tab contains an address of a table of six vectors, each of dimension 5. When the same methods are applied to vectors of fixed dimension, there is no effect, i.e. all vectors in array will have dimension as specified in argument of template.

   DVector4D *tab2 = new (5) DVector4D[6];

The dimension of vectors in array tab2 is 4.

Indexing

One can change or access a coefficient in a vector by using operator[] or iterators. The following operations are available for vectors a,b

   std::cout << b[i]; // prints on the screen i-th coordinate of a vector b
   a[i] = i*i;        // sets i-th coordinate of a vector 'a' to be equal to i*i

The index 'i' should be between 0 and the dimension of a vector minus 1, which can be easily obtained by a member function dimension as in the example below

      
   for(int i=0;i<a.dimension();++i)
   {
      std::cout << "a[" << i << "]=" << a[i] << std::endl;
      //one can set a coefficient in nonconstant vector
      a[i] = i*i;
      std::cout << "New value of a[" << i << "]=" << a[i] << std::endl;
   }

Iterators

The other proposed way to access a coefficient in a vector is to use iterators instead of indexing. Class Vector provides type definitions for iterators and constant iterators.

   DVector::iterator bg = b.begin(), en = b.end();
   while(bg!=en) 
   {
      std::cout << "value of an iterator: " << bg << "\ncoefficient in a vector: " << (*bg) << std::endl;
      ++bg;
   }

Constant iterators for constant objects are defined in the similar way

   const DVector c(4,data1);
   DVector::const_iterator p = c.begin(), k = c.end();
   std::cout << "\nCoefficients in constant vector:\n";
   while(p!=k) 
   {
      std::cout << (*p) << std::endl;
      ++p;
   }

Basic operations on vectors

The following standard operations on vectors are available

   sum: a+b
   subtraction: a-b
   euclidean scalar product: a*b
   multiplication by scalar: 2*a, a*2
   unary operator: -a
   equality: a==b, a!=b

Moreover, the standard operations like +=, -= etc. are available when possible

Norms and normalization

The euclidean norm of a vector can be computed be using member function euclNorm

      
   std::cout << "euclidean norm of b: " << b.euclNorm() << std::endl;

The vector can be normalized with respect to the euclidean norm by calling member function normalize

   a.normalize();
   std::cout << "normalization with respect to euclidean norm of vector a: " << a << std::endl;

Operations for interval vectors only

Many operations are suitable for interval vectors only. The most important are


Author: Daniel Wilczak, last modified on July 7, 2008.