Ignore:
Timestamp:
Dec 2, 2005, 1:15:50 AM (16 years ago)
Author:
Jari Häkkinen
Message:

Merged better_matrix_class changes r402:419 into the trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/gslapi/vector.h

    r357 r420  
    11// $Id$
    22
    3 #ifndef _theplu_gslapi_vector_ 
     3#ifndef _theplu_gslapi_vector_
    44#define _theplu_gslapi_vector_
     5
     6#include <c++_tools/utility/Exception.h>
    57
    68#include <iostream>
     
    911
    1012#include <gsl/gsl_vector.h>
    11 #include <c++_tools/utility/Exception.h>
    1213
    1314namespace theplu {
    1415namespace gslapi {
     16
     17  class matrix;
     18
    1519  ///
    1620  /// This is the C++ tools interface to GSL vector. 'double' is the
     
    2529  /// \par[Vector views] GSL vector views are supported and these are
    2630  /// disguised as ordinary gslapi::vectors. A support function is
    27   /// added, gslapi::vector::is_view(), that can be used to check if
    28   /// the vector object is a view. Note that view vectors do not own
    29   /// the undelying data, and is not valid if the original vector is
    30   /// deallocated.
     31  /// added, gslapi::vector::isview(), that can be used to check if a
     32  /// vector object is a view. Note that view vectors do not own the
     33  /// undelying data, and a view is not valid if the vector owning the
     34  /// data is deallocated.
     35  ///
     36  /// @note Missing support to underlying GSL vector features can in
     37  /// principle be included to this class if requested by the user
     38  /// community.
    3139  ///
    3240  class vector
     
    3745    /// The default constructor.
    3846    ///
    39     vector(void);
     47    inline vector(void) : v_(NULL), view_(NULL), const_view_(NULL) {}
    4048
    4149    ///
     
    4351    /// sets all elements to \a init_value.
    4452    ///
    45     vector(const size_t n, const double init_value=0);
     53    inline vector(const size_t n,const double init_value=0)
     54      : view_(NULL), const_view_(NULL)
     55      { v_ = gsl_vector_alloc(n); set_all(init_value); }
    4656
    4757    ///
     
    5161    /// of the view will be copied, i.e. the view is not copied.
    5262    ///
    53     vector(const vector&);
    54 
    55     ///
    56     /// The vector view constructor.
     63    inline vector(const vector& other) : view_(NULL), const_view_(NULL)
     64      { v_ = other.create_gsl_vector_copy(); }
     65
     66    ///
     67    /// Vector view constructor.
    5768    ///
    5869    /// Create a view of vector \a v, with starting index \a offset,
     
    6374    /// is viewed. Also, using the copy constructor will create a new
    6475    /// vector object that is a copy of whatever is viewed. If a copy
    65     /// of the view is needed the you should use this consturctor to
    66     /// get one.
     76    /// of the view is needed then you should use this constructor to
     77    /// obtain a copy.
    6778    ///
    6879    /// @note If the object viewed by the view goes out of scope or is
    69     /// deleted, the view becomes invalid.
    70     ///
    71     vector(vector& v, size_t offset, size_t n, size_t stride=1);
    72 
    73     ///
    74     /// Constructor that imports a GSL vector. The GSL object is owned
    75     /// by the created object.
    76     ///
    77     vector(gsl_vector*);
     80    /// deleted, the view becomes invalid and the result of further
     81    /// use is undefined.
     82    ///
     83    vector(vector& v, size_t offset, size_t n, size_t stride=1);
     84
     85    ///
     86    /// Vector const view constructor.
     87    ///
     88    /// Create a view of vector \a v, with starting index \a offset,
     89    /// size \a n, and an optional \a stride.
     90    ///
     91    /// A vector view can be used as any const vector. Using the copy
     92    /// constructor will create a new vector object that is a copy of
     93    /// whatever is viewed. If a copy of the view is needed then you
     94    /// should use this constructor to obtain a copy.
     95    ///
     96    /// @note If the object viewed by the view goes out of scope or is
     97    /// deleted, the view becomes invalid and the result of further
     98    /// use is undefined.
     99    ///
     100    vector(const vector& v, size_t offset, size_t n, size_t stride=1);
     101
     102    ///
     103    /// Matrix row/column view constructor.
     104    ///
     105    /// Create a row/column vector view of matrix \a m, pointing at
     106    /// row/column \a i. The parameter \a row is used to set whether
     107    /// the view should be a row or column view. If \a row is set to
     108    /// true, the view will be a row view (default behaviour), and,
     109    /// naturally, a column view otherwise.
     110    ///
     111    /// A vector view can be used as any vector with the difference
     112    /// that changes made to the view will also change the object that
     113    /// is viewed. Also, using the copy constructor will create a new
     114    /// vector object that is a copy of whatever is viewed. If a copy
     115    /// of the view is needed then you should use the vector view
     116    /// constructor to obtain a copy.
     117    ///
     118    /// @note If the object viewed by the view goes out of scope or is
     119    /// deleted, the view becomes invalid and the result of further
     120    /// use is undefined.
     121    ///
     122    vector(matrix& m, size_t i, bool row=true);
     123
     124    ///
     125    /// Matrix row/column const view constructor.
     126    ///
     127    /// Create a row/column vector view of matrix \a m, pointing at
     128    /// row/column \a i. The parameter \a row is used to set whether
     129    /// the view should be a row or column view. If \a row is set to
     130    /// true, the view will be a row view (default behaviour), and,
     131    /// naturally, a column view otherwise.
     132    ///
     133    /// A const vector view can be used as any const vector. Using the
     134    /// copy constructor will create a new vector object that is a
     135    /// copy of whatever is viewed. If a copy of the view is needed
     136    /// then you should use the vector view constructor to obtain a
     137    /// copy.
     138    ///
     139    /// @note If the object viewed by the view goes out of scope or is
     140    /// deleted, the view becomes invalid and the result of further
     141    /// use is undefined.
     142    ///
     143    vector(const matrix& m, size_t i, bool row=true);
    78144
    79145    ///
     
    117183
    118184    ///
     185    /// @return A const pointer to the internal GSL vector,
     186    ///
     187    inline const gsl_vector* gsl_vector_p(void) const { return v_; }
     188
     189    ///
     190    /// @return A pointer to the internal GSL vector,
     191    ///
     192    inline gsl_vector* gsl_vector_p(void) { return v_; }
     193
     194    ///
    119195    /// @return True if all elements in the vector is zero, false
    120196    /// othwerwise;
     
    123199
    124200    ///
    125     /// Check if the vector object is a view (sub vector) to another
     201    /// Check if the vector object is a view (sub-vector) to another
    126202    /// vector.
    127203    ///
    128     /// @return True if the object is a view, false othwerwise;
    129     ///
    130     inline bool isview(void) const { return view_; }
    131 
    132     ///
    133     /// @return A const pointer to the internal GSL vector,
    134     ///
    135     inline const gsl_vector* gsl_vector_pointer(void) const { return v_; }
    136 
    137     ///
    138     /// @return A pointer to the internal GSL vector,
    139     ///
    140     inline gsl_vector* TEMP_gsl_vector_pointer(void) { return v_; }
     204    /// @return True if the object is a view, false othwerwise.
     205    ///
     206    inline bool isview(void) const { return view_ || const_view_; }
    141207
    142208    ///
     
    208274
    209275    ///
     276    /// Set element values to values in \a vec. This function is
     277    /// needed for assignment of viewed elements.
     278    ///
     279    /// @return Whatever GSL returns.
     280    ///
     281    /// @note No check on size is done.
     282    ///
     283    /// @see const vector& operator=(const vector&)
     284    ///
     285    inline int set(const vector& vec) { return gsl_vector_memcpy(v_,vec.v_); }
     286
     287    ///
    210288    /// Set all elements to \a value.
    211289    ///
     
    279357    ///
    280358    // Jari, doxygen group as Accessing vector elements
    281     inline const double& operator()(size_t i) const
    282       { return *gsl_vector_const_ptr(v_,i); }
    283    
     359    inline const double&
     360    operator()(size_t i) const { return *gsl_vector_const_ptr(v_,i); }
     361
    284362    ///
    285363    /// Element access operator.
     
    296374    ///
    297375    // Jari, doxygen group as Accessing vector elements
    298     inline const double& operator[](size_t i) const
    299       { return *gsl_vector_const_ptr(v_,i); }
    300    
     376    inline const double&
     377    operator[](size_t i) const { return *gsl_vector_const_ptr(v_,i); }
     378
    301379    ///
    302380    /// @return The dot product.
    303381    ///
    304382    double operator*( const vector &other ) const;
    305 
    306     ///
    307     /// Vector with scalar multiplication.
    308     ///
    309     /// @note This operator is not implemented!
    310     ///
    311     vector operator*(const double d) const;
    312 
    313     ///
    314     /// Vector addition.
    315     ///
    316     /// @return The resulting vector.
    317     ///
    318     vector operator+( const vector& other ) const;
    319 
    320     ///
    321     /// Vector subtraction.
    322     ///
    323     /// @return The resulting vector.
    324     ///
    325     vector operator-( const vector& other ) const;
    326383
    327384    ///
     
    335392    ///
    336393    /// The assignment operator. There is no requirements on
    337     /// dimensions.
     394    /// dimensions, i.e. the vector is remapped in memory if
     395    /// necessary. This implies that in general views cannot be
     396    /// assigned using this operator. Views will be mutated into
     397    /// normal vectors. The only exception to this behaviour on views
     398    /// is when self-assignemnt is done, since self-assignment is
     399    /// ignored.
    338400    ///
    339401    /// @return A const reference to the resulting vector.
    340402    ///
     403    /// @see int set(const vector&)
     404    ///
    341405    const vector& operator=(const vector&);
    342406
     
    346410    /// @return A const reference to the resulting vector.
    347411    ///
    348     inline const vector& operator+=( const vector& other )
    349     { gsl_vector_add(v_,other.v_); return *this; }
     412    inline const vector&
     413    operator+=(const vector& other) { gsl_vector_add(v_,other.v_); return *this;}
    350414
    351415    ///
     
    354418    /// @return A const reference to the resulting vector.
    355419    ///
    356     inline const vector& operator-=( const vector& other )
    357     { gsl_vector_sub(v_,other.v_); return *this; }
     420    inline const vector&
     421    operator-=(const vector& other) { gsl_vector_sub(v_,other.v_); return *this;}
    358422
    359423    ///
     
    362426    /// @return A const reference to the resulting vector.
    363427    ///
    364     inline const vector& operator*=(const double d)
    365     { gsl_vector_scale(v_,d); return *this; }
    366 
    367 
    368   private:
     428    inline const vector&
     429    operator*=(const double d) { gsl_vector_scale(v_,d); return *this; }
     430
     431
     432  private:
    369433
    370434    ///
     
    376440    /// @return A pointer to a copy of the internal GSL vector.
    377441    ///
    378     gsl_vector* TEMP_gsl_vector_copy(void) const;
    379 
    380     gsl_vector* v_;
     442    gsl_vector* create_gsl_vector_copy(void) const;
     443
     444    gsl_vector* v_;
    381445    gsl_vector_view* view_;
    382   };
     446    gsl_vector_const_view* const_view_;
     447  };
    383448
    384449  ///
    385450  /// The output operator for the vector class.
    386   /// 
     451  ///
    387452  std::ostream& operator<<(std::ostream&, const vector& );
    388453
     
    390455}} // of namespace gslapi and namespace theplu
    391456
    392 #endif 
     457#endif
Note: See TracChangeset for help on using the changeset viewer.