KrisLibrary  1.0.0
Public Types | Public Member Functions | Public Attributes | List of all members
Math::VectorTemplate< T > Class Template Reference

A vector over the field T. More...

#include <VectorTemplate.h>

Inheritance diagram for Math::VectorTemplate< T >:
Math::DiagonalMatrixTemplate< T >

Public Types

typedef VectorTemplate< T > MyT
 
typedef VectorIterator< T > ItT
 

Public Member Functions

 VectorTemplate (const MyT &)
 
 VectorTemplate (MyT &&)
 
 VectorTemplate (int n)
 
 VectorTemplate (int n, T initval)
 
 VectorTemplate (int n, const T *vals)
 
 VectorTemplate (const std::vector< T > &vals)
 
T * getPointer () const
 
int getCapacity () const
 
T * getStart () const
 
ItT begin () const
 
ItT end () const
 
int size () const
 
bool empty () const
 
void resize (int size)
 
void resize (int size, T initval)
 
void resizePersist (int size)
 
void resizePersist (int size, T initval)
 
void clear ()
 
MyToperator= (const MyT &v)
 
MyToperator= (MyT &&v)
 
bool operator== (const MyT &) const
 
bool operator!= (const MyT &a) const
 
 operator T * ()
 
 operator const T * () const
 
 operator std::vector< T > () const
 
MyToperator= (const std::vector< T > &v)
 
const T & operator() (int i) const
 
T & operator() (int i)
 
const T & operator[] (int i) const
 
T & operator[] (int i)
 
void operator+= (const MyT &a)
 
void operator-= (const MyT &a)
 
void operator*= (T c)
 
void operator/= (T c)
 
void copy (const MyT &)
 
template<class T2 >
void copy (const VectorTemplate< T2 > &)
 
void copy (const T *vals)
 
template<class T2 >
void copy (const std::vector< T2 > &vals)
 
void copySubVector (int i, const MyT &)
 
void swap (MyT &)
 
void swapCopy (MyT &)
 
void add (const MyT &, const MyT &)
 
void sub (const MyT &, const MyT &)
 
void mul (const MyT &, T)
 
void div (const MyT &, T)
 
void axpby (T a, const MyT &x, T b, const MyT &y)
 
void inc (const T &)
 
void inc (const MyT &)
 
void dec (const MyT &)
 
void madd (const MyT &, T)
 
void setRef (const MyT &, int base=0, int stride=1, int size=-1)
 
void setRef (T *vals, int length, int base=0, int stride=1, int size=-1)
 
void set (T)
 
void setZero ()
 
void setNegative (const MyT &)
 
void setNormalized (const MyT &)
 
void setConjugate (const MyT &)
 
void inplaceNegative ()
 
void inplaceMul (T)
 
void inplaceDiv (T)
 
void inplaceNormalize ()
 
void inplaceConjugate ()
 
void getCopy (MyT &) const
 
void getCopy (T *vals) const
 
void getSubVectorCopy (int i, MyT &) const
 
void getRef (MyT &, int base=0, int stride=1, int size=-1) const
 
void getNegative (MyT &v) const
 
void getNormalized (MyT &v) const
 
void getConjugate (MyT &v) const
 
bool isReference () const
 
bool isUninitialized () const
 
bool isValidIndex (int i) const
 
bool isCompact () const
 
bool isValid () const
 
bool isZero (T eps=0) const
 
bool isEqual (const MyT &, T eps=0) const
 
dot (const MyT &) const
 
dotSelf () const
 
norm () const
 
normSquared () const
 
distance (const MyT &) const
 
distanceSquared (const MyT &) const
 
minElement (int *index=NULL) const
 
maxElement (int *index=NULL) const
 
minAbsElement (int *index=NULL) const
 
maxAbsElement (int *index=NULL) const
 
bool Read (File &)
 
bool Write (File &) const
 
void componentMul (const MyT &a, const MyT &b)
 
void componentDiv (const MyT &a, const MyT &b)
 
void componentMadd (const MyT &a, const MyT &b)
 
void inplaceComponentMul (const MyT &c)
 
void inplaceComponentDiv (const MyT &c)
 
template<class UnaryOp >
void componentOp (const MyT &a, UnaryOp &f)
 for each element xi, sets xi = f(ai)
 
template<class BinaryOp >
void componentOp (const MyT &a, const MyT &b, BinaryOp &f)
 for each element xi, sets xi = f(ai,bi)
 
template<class BinaryOp >
void componentOp (const MyT &a, T c, BinaryOp &f)
 for each element xi, sets xi = f(ai,c)
 
template<class UnaryOp >
void inplaceComponentOp (UnaryOp &f)
 for each element xi, sets xi = f(xi)
 
template<class BinaryOp >
void inplaceComponentOp (const MyT &c, BinaryOp &f)
 for each element xi, sets xi = f(xi,ci)
 
template<class BinaryOp >
void inplaceComponentOp (T c, BinaryOp &f)
 for each element xi, sets xi = f(xi,c)
 
template<>
void setConjugate (const MyT &a)
 
template<>
void inplaceConjugate ()
 
template<>
Complex dotSelf () const
 
template<>
Complex minElement (int *index) const
 
template<>
Complex maxElement (int *index) const
 
template<>
Complex minAbsElement (int *index) const
 
template<>
Complex maxAbsElement (int *index) const
 

Public Attributes

int base
 
int stride
 
int n
 

Detailed Description

template<class T>
class Math::VectorTemplate< T >

A vector over the field T.

This is a basic vector class used throughout the math library. Element access is either through array notation v[i] or parentheses v(i). The range of element indices is [0,v.n).

The usual usage mode for a VectorTemplate is as both element storage and manipulation. It manages all the internal data, allowing resizing and reallocation of the storage as necessary, and deleting internally managed data on destruction. By default, a VectorTemplate is "empty" and can be initialized by resizing it to the desired size, or by using it as the result of an operator (e.g. a.add(b,c)).

Once a VectorTemplate has been initialized, it will not be automatically resized by any method. To resize, the resize() method should be called directly. To reset it to the empty state (and delete all manged data) the clear() method should be used.

A second usage mode is to point to data managed by some outside source. Altering vector elements will alter the external data, and vice versa. For example,

float a[4] = { 0,1,2,3 }
VectorTemplate<float> v;
v.setRef(a,4); //v now points to the data in a
assert(v(0) == 0); //v0 is a0
a[0] = 4;
assert(v(0) == 4); //since a0 has changed, v0 changes as well
v[0] = 5;
assert(a[0] == 5); //since v0 has changed, a0 changes as well

An empty VectorTemplate can be set to reference outside data using the methods with "Ref" in their names. In this mode, the size of the data is fixed, and it cannot be reallocated, destroyed, or resized. To reset to the empty state, the clear() method should be used.

Standard math operations are provided, add, subtract, multiply, etc. These operations are most often implemented as result.op(arg1,arg2,...). The reason for this interface rather than operators, like a = b+c, is that it avoids unnecessary copying. A smart compiler might be able to optimize the code a = b+c to avoid copying the result of b+c into a, but this is by no means necessarily implemented. Specifying the operation directly is necessarily efficient.

When an operator is called on an empty vector, the vector is resized to the proper dimensions. If the vector is non-empty, the vector is checked for the proper dimensions. On incorrect dimensions, the operator will abort.

After resize(n), the contents of a vector are undefined. With resize(n,initVal) every value is initialized to initVal. To keep existing elements, use resizePersist (this works like the resize method on STL vectors). resizePersist(n,initVal) only sets uninitialized elements to initVal.

Another feature is the ability to access elements stored non-contiguously in memory. This is handy for accessing matrix columns or diagonals directly, as though they were normal vectors. This depends on two additional parameters, 'base' and 'stride', that control how elements access the data in 'data'. Element i accesses data[base+i*stride]. So to access every 2nd element of the data array, set stride to 2, to access every 3rd, set stride to 3, etc. It is important to make sure element accesses do not exceed the bounds of the array, so the isValid() and isValidIndex() methods are handy here.


The documentation for this class was generated from the following files: