Changeset 42 for trunk/src/vector.h


Ignore:
Timestamp:
Feb 26, 2004, 4:06:20 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Made a major revision of matrix and vector classes. Everything compiles
but the binaries have not been tested.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/vector.h

    r40 r42  
    11// $Id$
    22
    3 #ifndef _thep_gsl_api_vector_
    4 #define _thep_gsl_api_vector_
     3#ifndef _theplu_gslapi_vector_
     4#define _theplu_gslapi_vector_
    55
    66#include <iostream>
    7 #include <fstream>
    8 #include <iomanip>
    9 #include <cmath>
    10 #include <cstdlib>
    11 #include <cassert>
    127
    13 #include <gsl/gsl_math.h>
    148#include <gsl/gsl_vector.h>
    15 #include <gsl/gsl_linalg.h>
    16 #include <gsl/gsl_blas.h>
     9
     10namespace theplu {
     11// Jari, this should probably go somewhere else for doxygen to process.
     12///
     13/// All GSL wrapper functionality is to be defined within gslapi
     14/// namespace.
     15///
     16/// There are a number of support operators and functions for the
     17/// wrapper classes that do not belong to the classes themselves, but
     18/// still are defined in this namespace.
     19///
     20namespace gslapi {
     21
     22  ///
     23  /// This is the C++ tools interface to GSL vector.
     24  ///
     25
     26  class vector
     27  {
     28  public:
     29
     30    ///
     31    /// The default constructor.
     32    ///
     33    vector(void);
     34
     35    ///
     36    /// Constructor. Allocates memory space for \a n elements, and
     37    /// sets all elements to \a init_value.
     38    ///
     39    vector(const size_t n, const double init_value=0);
     40
     41    ///
     42    /// The copy constructor.
     43    ///
     44    vector(const vector&);
     45
     46    ///
     47    /// Constructor that imports a GSL vector. The GSL object is owned
     48    /// by the created object.
     49    ///
     50    vector(gsl_vector*);
     51
     52    ///
     53    /// The istream constructor.
     54    ///
     55    /// Elements should be separated with white space characters, the
     56    /// end of input to the vector is at end of file marker.
     57    ///
     58    explicit vector(std::istream &);
     59
     60    ///
     61    /// The destructor.
     62    ///
     63    ~vector(void);
     64
     65    ///
     66    /// Create a new copy of the internal GSL vector.
     67    ///
     68    /// Necessary memory for the new GSL vector is allocated and the
     69    /// caller is responsible for freeing the allocated memory.
     70    ///
     71    /// @return A pointer to a copy of the internal GSL vector.
     72    ///
     73    gsl_vector* gsl_vector_copy(void) const;
     74
     75    ///
     76    /// @return A const pointer to the internal GSL vector,
     77    ///
     78    inline const gsl_vector* gsl_vector_pointer(void) const { return v_; }
     79
     80    ///
     81    /// @return A pointer to the internal GSL vector,
     82    ///
     83    inline gsl_vector* gsl_vector_pointer(void) { return v_; };
     84
     85    ///
     86    /// This function multiplies all elements between two vectors and
     87    /// returns a third vector containing the result, \f$a_i = b_i *
     88    /// c_i \; \forall i\f$.
     89    ///
     90    /// @return The result vector.
     91    ///
     92    vector vector::mul_elements( const vector& other ) const;
     93
     94    ///
     95    /// Set all elements to \a value.
     96    ///
     97    inline void set_all(const double& value) { gsl_vector_set_all(v_,value); }
     98
     99    ///
     100    /// @return the number of elements in the vector.
     101    ///
     102    inline size_t size(void) const { return v_->size; }
     103
     104    ///
     105    /// Calculate the sum of all vector elements.
     106    ///
     107    /// @return The sum.
     108    ///
     109    double vector::sum(void) const;
     110
     111    ///
     112    /// Element access operator.
     113    ///
     114    /// @return Reference to element \a i.
     115    ///
     116    inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); }
     117
     118    ///
     119    /// Const element access operator.
     120    ///
     121    /// @return The value of element \a i.
     122    ///
     123    inline const double& operator()(size_t i) const
     124      { return *gsl_vector_ptr(v_,i); }
     125   
     126    ///
     127    /// Element access operator.
     128    ///
     129    /// @return Reference to element \a i.
     130    ///
     131    inline double& operator[](size_t i) { return (*this)(i); }
     132
     133    ///
     134    /// Const element access operator.
     135    ///
     136    /// @return The value of element \a i.
     137    ///
     138    inline const double& operator[](size_t i) const { return (*this)(i); }
     139   
     140    ///
     141    /// @return The negation of the vector.
     142    ///
     143    vector operator-(void) const;
     144
     145    ///
     146    /// @return The dot product.
     147    ///
     148    double operator*( const vector &other ) const;
     149
     150    ///
     151    /// Vector addition.
     152    ///
     153    /// @return The resulting vector.
     154    ///
     155    vector operator+( const vector& other ) const;
     156
     157    ///
     158    /// Vector subtraction.
     159    ///
     160    /// @return The resulting vector.
     161    ///
     162    vector operator-( const vector& other ) const;
     163
     164    ///
     165    /// The assignment operator. There is no requirements on
     166    /// dimensions.
     167    ///
     168    vector& operator=(const vector&);
     169
     170    ///
     171    /// Multiply and assign operator.
     172    ///
     173    vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; }
    17174
    18175
    19 namespace thep_gsl_api
    20 {
     176  private:
    21177
    22   /**
    23      This is a vector interface to GSL for c++.
    24      Important: vector has two dimenstions depending on
    25      whether it's a "row" vector or a "column" vector.
    26      A column vector is created by default.
    27      Note that the namespace is "thep_gsl_api".
    28   */
     178    gsl_vector* v_;
    29179
    30   class vector
    31   {
    32   public:
    33     typedef double Type;
    34  
    35     /**
    36        Default constructor
    37        Does not allocate any memory
    38     */
    39     vector();
     180  };
    40181
    41     /**
    42        Constructor taking three argumens:
    43        N = Number of elements
    44        init_to_zero, if true vector will be initialized with zeros.
    45     */
    46     vector( const size_t&, bool is_col_vector = true,
    47       bool init_to_zero = true );
     182  ///
     183  /// The output operator for the vector class.
     184  ///
     185  std::ostream& operator<< ( std::ostream&, const vector& );
    48186
    49187
    50 
    51     /**
    52   Copy constructor
    53     */
    54     vector( const vector& other );
    55 
    56 
    57     /**
    58        Constructor taking as only argument a matrix of gsl type.
    59        Be causious here! Do not delete this matrix outside of
    60        class: NO COPY IS MADE!!!
    61     */
    62     vector( gsl_vector*, bool is_col = true );
    63 
    64     /**
    65        Constructor taking as only argument a an istream that should
    66        contain the vector. Column vector should be sepated with '\\n'
    67        and row vector should be separeted with whitespace (not '\\n').       
    68 
    69     */
    70     vector(std::istream &);
    71 
    72 
    73     /**
    74        Destructor will clear matrix's allocated memory
    75     */
    76     ~vector();
    77 
    78 
    79     /**
    80        The most important function when adding new classes to
    81        c++_tools. This function will return a pointer to the
    82        inner data of the class. Should be forbidden in a strictly
    83        object-orientated environment.
    84        Motivation: Effeciency! ( CAN IT BE MADE CONST? )
    85        To be used when writting new classes using gsl-functions.
    86     */
    87     inline gsl_vector* get_gsl_vector() const { return v_; }
    88 
    89 
    90     /**
    91        Returns the number of rows in the vector.
    92     */
    93     size_t size() const { return v_->size; }
    94 
    95 
    96     /**
    97        Tells whether a vector is column or not
    98      */
    99     bool is_column() const { return is_col_; }
    100 
    101 
    102     /**
    103        get( i ) will return the vi element in vector v
    104        where i = column (if row vector) or row (if column vector)
    105     */
    106     inline Type get( const size_t& i ) const { return gsl_vector_get(v_,i); }
    107 
    108 
    109     /**
    110        set( i, val ) will make vi = val in vector v
    111        where i = column (if row vector) or row (if column vector)
    112     */
    113     inline void set( const size_t& i, const Type& val )
    114       { gsl_vector_set(v_,i,val); }
    115 
    116     /**
    117        set_all( val ) will make vi = val in vector v
    118        for all "i"
    119     */
    120     inline void set_all( const Type& val ) { gsl_vector_set_all(v_,val); }
    121 
    122 
    123     /**
    124        Assignment-operator. No requirements on dimensions,
    125        i.e. if a = b, then a will be a copy of b with b:s
    126        dimensions.
    127      */
    128     vector& operator=( const vector& other );
    129 
    130 
    131     /**
    132        Access element-operator
    133     */
    134     inline double &operator[](size_t i)  { return *gsl_vector_ptr( v_, i ); }
    135     const double &operator[](size_t i) const { return *gsl_vector_ptr( v_, i ); }
    136 
    137    
    138     /**
    139        operators for comparison are: \n
    140        == equal \n
    141        != not equal
    142     */
    143     bool operator==( const vector &other ) const;
    144     bool operator!=( const vector &other ) const;
    145 
    146     /**
    147        operators for basic arithmetics are: \n
    148        \f$+\f$ vector-vector addition \n
    149        \f$-\f$ vector-vector subtraction \n
    150        \f$/=\f$ divide all elements with a constant
    151     */
    152     vector operator+( const vector& other ) const;
    153     vector operator-( const vector& other ) const;
    154     int operator/=( const double& c );
    155     int operator/=( const vector& other ) const;
    156     vector operator-() const;
    157     /**
    158        This operator is implemented as dot-product.
    159        I case of discussions later on; Jari decided
    160        this!
    161     */
    162     double operator*( const vector &other ) const;
    163 
    164     /**
    165        This function multiplies all elements in two vectors
    166        and returns a third vector containing the result.
    167        res = a*b; where size(a) = size(b) = size(res)
    168     */
    169     vector vector::mul_elements( const vector& other ) const;
    170    
    171    
    172     /**
    173        Calculates sum of all elements in vector
    174     */
    175     double vector::sum() const;
    176    
    177     /**
    178         standard output operator is defined
    179     */
    180     friend std::ostream& operator<< ( std::ostream&, const vector& );
    181    
    182 
    183   private:
    184     gsl_vector* new_copy( const gsl_vector* );
    185 
    186     gsl_vector* v_;
    187     bool is_col_;
    188   };
    189 
    190 
    191 };
    192 
    193 
    194 
     188}} // of namespace gslapi and namespace thep
    195189
    196190#endif
Note: See TracChangeset for help on using the changeset viewer.