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"
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;
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);
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.
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; }
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; }
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
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;
Many operations are suitable for interval vectors only. The most important are
// create an interval vector
interval d1[] = {interval(-1.,1.),interval(2.,2.),interval(3.,3.1), interval(4.,4.1)};
IVector v1(4,d1);
std::cout << midVector(v1);
// on the screen should be {[0,0],[2,2],[3.05,3.05],[4.05,4.05]}
v2 = v1-midVector(v1); v1 = midVector(v1);
After calling
// assume v1 is as in the previous example split(v1,v2); std::cout << "v1=" << v1 << std::endl; std::cout << "v2=" << v2 << std::endl;one should obtain on the screen
v1={[0,0],[2,2],[3.05,3.05],[4.05,4.05]} v2={[-1,1],[0,0],[-0.05,0.05],[-0.05,0.05]}
std::cout << "\nleftVector(v2)= " << leftVector(v2) << std::endl; std::cout << "rightVector(v2)= " << rightVector(v2) << std::endl;one should obtain on the screen
leftVector(v2)= {[-1,-1],[0,0],[-0.05,-0.05],[-0.05,-0.05]} rightVector(v2)= {[1,1],[0,0],[0.05,0.05],[0.05,0.05]}
std::cout << "diam(v2) = " << diam(v2) << std::endl;
one should obtain
diam(v2) = {[2,2],[0,0],[0.1,0.1],[0.1,0.1]}
template<typename IVector> IVector intersection(const IVector& x, const IVector& y);returns an intersection of two interval vectors or throws an exception if this intersection is empty.
Author: Daniel Wilczak, last modified on July 7, 2008.