GCpp general purpose C++ library  version 1.0
GVectorT.hh
Go to the documentation of this file.
1 //======================================================================
2 /*! \file GVectorT.hh
3  *
4  * Include file for a template vector class.
5  */
6 //======================================================================
7 
8 #ifndef G_VECTOR_T_HH
9 #define G_VECTOR_T_HH
10 
11 #include "GGlobal.hh"
12 #include "GLogMessage.hh"
13 #include "GBaseFunctions.hh"
14 #include "GStringFunctions.hh"
15 
16 template <typename T> class GMatrixT;
17 
18 //======================================================================
19 /*! Template class defining a vector for numerical types.
20  *
21  * The basic vector operations are defined, with the assumption that
22  * the standard algebaric operation of the template class are defined.
23  *
24  * For effective instantiation of a class, the following functions
25  * should be specialised (see file GVectorD.hh):
26  * - Norm2: it is well defined for real numbers, but should be precised
27  * for other types
28  *
29  * \note
30  * - the initialisation functions InitElement and InitData clear the
31  * elements memory with 0; if elements initialisation requires
32  * more sophisticated actions, due to class type, the template
33  * class must be specialised.
34  */
35 template <typename T> class GVectorT
36 {
37  //----------------------------------------------------------
39  //----------------------------------------------------------
40 
41  static string vect_open; ///< Pattern for string representation
42  static string vect_close; ///< Pattern for string representation
43  static string vect_separ; ///< Pattern for string representation
44  static string vect_format; ///< Pattern for string representation
45 
46 
47  protected:
48  bool change_dim; ///< Indicates if dimension can be changed
49  size_t dimension; ///< Vector dimension
50  T * element; ///< Vector elements array
51 
52  public:
53  //----------------------------------------------------------
54  // Constructors / Affectation / Destructor
55 
56  GVectorT ( size_t n = 1 );
57  GVectorT ( size_t n, const T & x );
58  GVectorT ( size_t n, const T x[] );
59  GVectorT ( const GVectorT<T> & v );
60 
61  GVectorT & operator = ( const GVectorT<T> & v );
62 
63  virtual ~GVectorT ( ); // inline
64 
65  //----------------------------------------------------------
66  // Initialisation functions
67 
68  protected:
69  virtual T * Allocate ( size_t n );
70  virtual void Reset ( ); // inline
71 
72  public:
73 
74  virtual void InitElement ( size_t i ); // inline
75  virtual void InitData ( ); // inline
76  virtual void InitData ( const T x ); // inline
77  virtual void InitData ( const T x[] ); // inline
78  virtual void InitData ( size_t n, const T x[] ); // inline
79 
80  //----------------------------------------------------------
81  // Check functions
82 
83  virtual bool CheckIndex ( size_t i ) const; // inline
84  virtual bool CheckDimension ( const GVectorT<T> & v ) const; // inline
85  virtual bool CheckProduct ( const GMatrixT<T> & m ) const; // inline
86 
87  //----------------------------------------------------------
88  size_t SetDimension ( size_t n );
89  size_t GetDimension ( ) const; // inline
90  const T * Data ( ) const; // inline
91  T * Data ( ); // inline
92 
93  //----------------------------------------------------------
94  // Functions to get components
95  virtual const T & operator () ( size_t i ) const;
96  virtual T & operator () ( size_t i );
97  virtual const T & operator [] ( size_t i ) const;
98  virtual T & operator [] ( size_t i );
99 
100 
101  //----------------------------------------------------------
102  // Algebra
103 
104  // Addition / subtraction, scaling.
105  virtual GVectorT<T> operator + ( const GVectorT<T> & v ) const;
106  virtual GVectorT<T> operator - ( const GVectorT<T> & v ) const;
107  virtual GVectorT<T> operator * ( const T k ) const;
108  virtual GVectorT<T> operator / ( const T k ) const;
109 
110  virtual GVectorT<T> & operator += ( const GVectorT<T> & v );
111  virtual GVectorT<T> & operator -= ( const GVectorT<T> & v );
112  virtual GVectorT<T> & operator *= ( const T k );
113  virtual GVectorT<T> & operator /= ( const T k );
114 
115  // Unary minus.
116  virtual GVectorT<T> operator - ( ) const;
117 
118  // Scalar product.
119  virtual T operator * ( const GVectorT<T> & v ) const;
120 
121  // Matrix product.
122  virtual GVectorT<T> operator * ( const GMatrixT<T> & m ) const;
123 
124  //----------------------------------------------------------
125  virtual double Norm2Squared ( ) const; // inline
126  virtual double Norm2 ( ) const; // inline
127  virtual double LengthSquared ( ) const; // inline
128  virtual double Length ( ) const; // inline
129 
130  //----------------------------------------------------------
131  virtual string GetString ( ) const; // inline
132  virtual string GetString ( const char * fmt ) const;
133 };
134 
135 
136 //======================================================================
137 // Related functions declaration
138 
139 /*! Multiplication scaling.
140  * \param k scaling factor
141  * \param v vector to scale
142  */
143 template <typename T> GVectorT<T> operator * ( const double k, const GVectorT<T> & v );
144 
145 
146 //======================================================================
147 // required for template functions code
148 #include "GMatrixT.hh"
149 
150 //----------------------------------------------------------------------
151 // Inline and template functions
152 #include "icc/GVectorT.icc"
153 
154 
155 //======================================================================
156 #endif
157 
const T * Data() const
Definition: GVectorT.icc:146
virtual bool CheckDimension(const GVectorT< T > &v) const
Definition: GVectorT.icc:120
virtual string GetString() const
Definition: GVectorT.icc:367
virtual double LengthSquared() const
Definition: GVectorT.icc:357
static string vect_open
Pattern for string representation.
Definition: GVectorT.hh:41
virtual void InitElement(size_t i)
Definition: GVectorT.icc:75
virtual void Reset()
Definition: GVectorT.icc:69
size_t SetDimension(size_t n)
Definition: GVectorT.icc:157
size_t dimension
Vector dimension.
Definition: GVectorT.hh:49
bool change_dim
Indicates if dimension can be changed.
Definition: GVectorT.hh:48
#define GObject(T)
Definition: GClassDefine.hh:65
virtual double Norm2Squared() const
Definition: GVectorT.icc:349
static string vect_format
Pattern for string representation.
Definition: GVectorT.hh:44
virtual T * Allocate(size_t n)
Definition: GVectorT.icc:63
virtual bool CheckProduct(const GMatrixT< T > &m) const
Definition: GVectorT.icc:131
virtual bool CheckIndex(size_t i) const
Definition: GVectorT.icc:109
T * element
Vector elements array.
Definition: GVectorT.hh:50
static string vect_close
Pattern for string representation.
Definition: GVectorT.hh:42
virtual void InitData()
Definition: GVectorT.icc:79
virtual double Length() const
Definition: GVectorT.icc:361
virtual double Norm2() const
Definition: GVectorT.icc:353
size_t GetDimension() const
Definition: GVectorT.icc:142
Definition: GMatrixT.hh:35
Definition: GMatrixT.hh:16
static string vect_separ
Pattern for string representation.
Definition: GVectorT.hh:43