linalg::vector Class Reference

A wrapper class for GSL vectors. More...

#include <linalg.hpp>

Inheritance diagram for linalg::vector:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 vector ()
 Allocate zero vector of size one.
 vector (const size_t n, const double fillvalue=0)
 Allocate GSL vector of size n, filled with fillvalue.
 vector (gsl_vector *y)
 Associate a vector to a GSL vector pointer.
 vector (const gsl_vector *y)
 Associate a vector to a constant GSL vector pointer.
 vector (const vector &y)
 Copy contstructor.
vectoroperator= (const vector &y)
 Assignment.
 ~vector ()
 Clear memory assigned to vector by GSL.
size_t precision () const
 Number of decimal digits to output.
size_t size () const
 Number of elements.
double & operator() (const size_t i)
 Fortran-style parenthetical indexing (hence Octave-style too). Indices start at 1.
const double & operator() (const size_t i) const
 Constant version of previous function.
vector_view operator() (const slice &v)
 Indexing for vectorisation. The GSL provides limited vectorisation routines which can be useful for operating on blocks of vectors all at once.
const vector_view operator() (const slice &v) const
 Constant version of previous function.
vector operator* (const double a) const
 Scale the vector.
vector operator+ (const vector &w) const
 Vector addition.
vector operator- (const vector &w) const
 Vector subtration.
double operator* (const vector &w) const
 xDot product.
vector operator* (const matrix &M) const
 Computes vM where v is treated as a row vector.
bool operator== (const vector &w) const
 Compares vectors elementwise, using machine epsilon.
bool operator< (const vector &w) const
 Lexicographical order, used for putting into STL sets or maps.
double norm () const
 Euclidean norm.

Static Public Member Functions

static void set_precision (size_t p)
 Set the number of decimal digits to output.

Private Attributes

gsl_vector * x
 Pointer to associated GSL vector.

Static Private Attributes

static size_t precsn = 4
 Output precision.
static const double eps = std::numeric_limits<double>::epsilon()
 Machine epsilon.

Friends

class vector_view
class matrix


Detailed Description

A wrapper class for GSL vectors.

Constructor & Destructor Documentation

linalg::vector::vector (  ) 

Allocate zero vector of size one.

00426                 {
00427     x = gsl_vector_calloc(1); //Allocate zero vector of size one.
00428   }

linalg::vector::vector ( const size_t  n,
const double  fillvalue = 0 
)

Allocate GSL vector of size n, filled with fillvalue.

Parameters:
n - Size of new vector
fillvalue - Value for filling vector, default is 0.
00430                                                       {
00431     x = gsl_vector_alloc(n);
00432     gsl_vector_set_all(x,fillvalue);
00433   }

linalg::vector::vector ( gsl_vector *  y  ) 

Associate a vector to a GSL vector pointer.

00435                              {
00436     x = y;
00437   }

linalg::vector::vector ( const gsl_vector *  y  ) 

Associate a vector to a constant GSL vector pointer.

00439                                    {
00440     x = gsl_vector_alloc(y -> size);
00441     gsl_vector_memcpy(x,y);
00442   }

Here is the call graph for this function:

linalg::vector::vector ( const vector y  ) 

Copy contstructor.

00444                                {
00445     x = gsl_vector_alloc(y.x->size);
00446     gsl_vector_memcpy(x,y.x);
00447   }

linalg::vector::~vector (  ) 

Clear memory assigned to vector by GSL.

00458                  {
00459     if(x != 0) //Has subclass vector_view already deleted this vector?
00460       gsl_vector_free(x);
00461   }


Member Function Documentation

vector & linalg::vector::operator= ( const vector y  ) 

Assignment.

Reimplemented in linalg::vector_view.

00449                                           {
00450     if(this != &y){
00451       gsl_vector_free(x);
00452       x = gsl_vector_alloc(y.x -> size);
00453       gsl_vector_memcpy(x,y.x);
00454     }
00455     return *this;
00456   }

Here is the call graph for this function:

size_t linalg::vector::precision (  )  const

Number of decimal digits to output.

00466                                 {
00467     return precsn;
00468   }

void linalg::vector::set_precision ( size_t  p  )  [static]

Set the number of decimal digits to output.

00470                                     {
00471     precsn = p;
00472   }

size_t linalg::vector::size (  )  const

Number of elements.

00474                            {
00475     return x->size;
00476   }

double & linalg::vector::operator() ( const size_t  i  )  [inline]

Fortran-style parenthetical indexing (hence Octave-style too). Indices start at 1.

Parameters:
i - Index number.
Returns:
A reference to the vector element.
00447                                                   {
00448     try{
00449       return *gsl_vector_ptr(x,i-1);
00450     }
00451     catch(indexOutOfRange& exc){
00452       exc.i = i;
00453       exc.n = x -> size;
00454       throw exc;
00455     }
00456   }

Here is the call graph for this function:

const double & linalg::vector::operator() ( const size_t  i  )  const [inline]

Constant version of previous function.

00458                                                               {
00459     try{
00460       return *gsl_vector_const_ptr(x,i-1);
00461     }
00462     catch(indexOutOfRange& exc){
00463       exc.i = i;
00464       exc.n = x -> size;
00465       throw exc;
00466     }
00467   }

Here is the call graph for this function:

vector_view linalg::vector::operator() ( const slice v  ) 

Indexing for vectorisation. The GSL provides limited vectorisation routines which can be useful for operating on blocks of vectors all at once.

Parameters:
v - Slice range.
Returns:
A vector_view of the slice.
00478                                                {
00479     vector_view x_sub(x,s);
00480     return x_sub;
00481   }

const vector_view linalg::vector::operator() ( const slice v  )  const

Constant version of previous function.

00483                                                            {
00484     vector_view x_sub(x,s);
00485     return x_sub;
00486   }

vector linalg::vector::operator* ( const double  a  )  const

Scale the vector.

00490                                               {
00491     vector v = *this;
00492     gsl_vector_scale(v.x, a);
00493     return v;
00494   }

vector linalg::vector::operator+ ( const vector w  )  const

Vector addition.

00496                                                {
00497     if(x -> size != w.x->size){
00498       inconformantSizes exc;
00499       exc.reason = "Cannot add vectors of different sizes.";
00500       exc.file = __FILE__;
00501       exc.line = __LINE__;
00502       exc.n_A = x->size;
00503       exc.n_B = w.x->size;
00504       throw exc;
00505     }
00506     vector u = *this;
00507     gsl_vector_add(u.x,w.x);
00508     return u;    
00509   }

Here is the call graph for this function:

vector linalg::vector::operator- ( const vector w  )  const

Vector subtration.

00511                                                {
00512     if(x -> size != w.x->size){
00513       inconformantSizes exc;
00514       exc.reason = "Cannot subtract vectors of different sizes.";
00515       exc.file = __FILE__;
00516       exc.line = __LINE__;
00517       exc.n_A = x->size;
00518       exc.n_B = w.x->size;
00519       throw exc;
00520     }
00521     vector u = *this;
00522     gsl_vector_sub(u.x,w.x);
00523     return u;    
00524   }

Here is the call graph for this function:

double linalg::vector::operator* ( const vector w  )  const

xDot product.

00526                                                {
00527     double a;
00528     try{
00529       gsl_blas_ddot(x, w.x, &a);
00530     }
00531     catch(inconformantSizes& exc){
00532       exc.reason = "Can't dot product vectors of different sizes.";
00533       exc.file = __FILE__;
00534       exc.line = __LINE__;
00535       exc.n_A = x->size;
00536       exc.n_B = w.x -> size;
00537       throw exc;
00538     }
00539     return a;
00540   }

Here is the call graph for this function:

vector linalg::vector::operator* ( const matrix M  )  const

Computes vM where v is treated as a row vector.

00542                                                {
00543     return M* (*this);
00544   }

bool linalg::vector::operator== ( const vector w  )  const

Compares vectors elementwise, using machine epsilon.

00551                                               {
00552     if(x -> size != w.x -> size){
00553       badArgument exc;
00554       exc.reason = "Cannot compare vectors of different sizes.";
00555       exc.file = __FILE__;
00556       exc.line = __LINE__;
00557       throw exc;
00558     }
00559 
00560     for(size_t i = 0; i < x -> size; i++)
00561       //if(gsl_fcmp(gsl_vector_get(x,i), gsl_vector_get(w.x,i), eps) != 0)
00562       if (gsl_vector_get(x,i) != gsl_vector_get(w.x,i))
00563         return false;
00564     
00565     return true;
00566   }

Here is the call graph for this function:

bool linalg::vector::operator< ( const vector w  )  const

Lexicographical order, used for putting into STL sets or maps.

Lexicographical ordering of vectors. Vectors of smaller dimension are smaller.

00568                                              {
00569     if(x -> size < w.x -> size) //Smaller vectors go first in this order.
00570        return true;
00571 
00572     for(size_t i = 0; i < x -> size; i++){
00573       double L = gsl_vector_get(x,i);
00574       double R = gsl_vector_get(w.x,i);
00575       if(L < R )
00576         return true;
00577       if(L > R )
00578         return false;
00579     }
00580    
00581     return false; //Then vectors are equal.
00582   }

Here is the call graph for this function:

double linalg::vector::norm (  )  const

Euclidean norm.

00546                            {
00547     return gsl_blas_dnrm2(x);
00548   }


Friends And Related Function Documentation

friend class vector_view [friend]

friend class matrix [friend]

Reimplemented in linalg::vector_view.


Member Data Documentation

gsl_vector* linalg::vector::x [private]

Pointer to associated GSL vector.

size_t linalg::vector::precsn = 4 [static, private]

Output precision.

const double linalg::vector::eps = std::numeric_limits<double>::epsilon() [static, private]

Machine epsilon.


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

Generated on Fri Jun 6 17:28:30 2008 by  doxygen 1.5.6