source: branches/better_matrix_class/lib/gslapi/vector.h @ 413

Last change on this file since 413 was 413, checked in by Jari Häkkinen, 17 years ago

All check passes, but more matrix checks needed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 KB
Line 
1// $Id: vector.h 413 2005-11-30 21:35:19Z jari $
2
3#ifndef _theplu_gslapi_vector_
4#define _theplu_gslapi_vector_
5
6#include <c++_tools/utility/Exception.h>
7
8#include <iostream>
9#include <vector>
10#include <utility>
11
12#include <gsl/gsl_vector.h>
13
14namespace theplu {
15namespace gslapi {
16
17  class matrix;
18
19  ///
20  /// This is the C++ tools interface to GSL vector. 'double' is the
21  /// only type supported, maybe we should add a 'complex' type as
22  /// well in the future.
23  ///
24  /// \par[File streams] Reading and writing vectors to file streams
25  /// are of course supported. These are implemented without using GSL
26  /// functionality, and thus binary read and write to streams are not
27  /// supported.
28  ///
29  /// \par[Vector views] GSL vector views are supported and these are
30  /// disguised as ordinary gslapi::vectors. A support function is
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.
39  ///
40  class vector
41  {
42  public:
43
44    ///
45    /// The default constructor.
46    ///
47    inline vector(void) : v_(NULL), view_(NULL), const_view_(NULL) {}
48
49    ///
50    /// Constructor. Allocates memory space for \a n elements, and
51    /// sets all elements to \a init_value.
52    ///
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); }
56
57    ///
58    /// The copy constructor.
59    ///
60    /// @note If the object to be copied is a vector view, the values
61    /// of the view will be copied, i.e. the view is not copied.
62    ///
63    inline vector(const vector& other) : view_(NULL), const_view_(NULL)
64      { v_ = other.gsl_vector_copy(); }
65
66    ///
67    /// Vector view constructor.
68    ///
69    /// Create a view of vector \a v, with starting index \a offset,
70    /// size \a n, and an optional \a stride.
71    ///
72    /// A vector view can be used as any vector with the difference
73    /// that changes made to the view will also change the object that
74    /// is viewed. Also, using the copy constructor will create a new
75    /// vector object that is a copy of whatever is viewed. If a copy
76    /// of the view is needed then you should use this constructor to
77    /// obtain a copy.
78    ///
79    /// @note If the object viewed by the view goes out of scope or is
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);
144
145    ///
146    /// The istream constructor.
147    ///
148    /// Elements should be separated with white space characters, the
149    /// end of input to the vector is at end of file marker.
150    ///
151    explicit vector(std::istream &) throw (utility::IO_error,std::exception);
152
153    ///
154    /// The destructor.
155    ///
156    ~vector(void);
157
158    ///
159    /// Vector addition, \f$this_i = this_i + other_i \; \forall i\f$.
160    ///
161    /// @return GSL_SUCCESS on normal exit.
162    ///
163    // Jari, group as vector_operators
164    inline int add(const vector& other) { return gsl_vector_add(v_,other.v_); }
165
166    ///
167    /// Add a constant to a vector, \f$this_i = this_i + term \;
168    /// \forall i\f$.
169    ///
170    /// @return GSL_SUCCESS on normal exit.
171    ///
172    // Jari, group as vector_operators
173    inline int add(double term) { return gsl_vector_add_constant(v_,term); }
174
175    ///
176    /// This function performs element-wise division, \f$this_i =
177    /// this_i/other_i \; \forall i\f$.
178    ///
179    /// @return GSL_SUCCESS on normal exit.
180    ///
181    // Jari, doxygen group as Vector operators
182    inline int div(const vector& other) { return gsl_vector_div(v_,other.v_); }
183
184    ///
185    /// @return A const pointer to the internal GSL vector,
186    ///
187    // Jari, is this needed?
188    inline const gsl_vector* gsl_vector_pointer(void) const { return v_; }
189
190    ///
191    /// @return A pointer to the internal GSL vector,
192    ///
193    // Jari, is this needed?
194    inline gsl_vector* TEMP_gsl_vector_pointer(void) { return v_; }
195
196    ///
197    /// @return True if all elements in the vector is zero, false
198    /// othwerwise;
199    ///
200    inline bool isnull(void) const { return gsl_vector_isnull(v_); }
201
202    ///
203    /// Check if the vector object is a view (sub-vector) to another
204    /// vector.
205    ///
206    /// @return True if the object is a view, false othwerwise.
207    ///
208    inline bool isview(void) const { return view_ || const_view_; }
209
210    ///
211    /// @return The maximum value of the vector.
212    ///
213    // Jari, doxygen group as Finding maximum and minimum elements
214    inline double max(void) const { return gsl_vector_max(v_); }
215
216    ///
217    /// @return The element index to the maximum value of the
218    /// vector. The lowest index has precedence.
219    ///
220    // Jari, doxygen group as Finding maximum and minimum elements
221    inline size_t max_index(void) const { return gsl_vector_max_index(v_); }
222
223    ///
224    /// @return The minimum value of the vector.
225    ///
226    // Jari, doxygen group as Finding maximum and minimum elements
227    inline double min(void) const { return gsl_vector_min(v_); }
228
229    ///
230    /// @return The element index to the minimum value of the
231    /// vector. The lowest index has precedence.
232    ///
233    // Jari, doxygen group as Finding maximum and minimum elements
234    inline size_t min_index(void) const { return gsl_vector_min_index(v_); }
235
236    ///
237    /// @return The minimum and maximum values of the vector, as the
238    /// \a first and \a second member of the returned \a pair,
239    /// respectively.
240    ///
241    // Jari, doxygen group as Finding maximum and minimum elements
242    std::pair<double,double> minmax(void) const;
243
244    ///
245    /// @return The indecies to the minimum and maximum values of the
246    /// vector, as the \a first and \a second member of the returned
247    /// \a pair, respectively. The lowest index has precedence.
248    ///
249    // Jari, doxygen group as Finding maximum and minimum elements
250    std::pair<size_t,size_t> minmax_index(void) const;
251
252    ///
253    /// This function performs element-wise multiplication, \f$this_i =
254    /// this_i * other_i \; \forall i\f$.
255    ///
256    /// @return GSL_SUCCESS on normal exit.
257    ///
258    // Jari, doxygen group as Vector operators
259    inline int mul(const vector& other) { return gsl_vector_mul(v_,other.v_); }
260
261    ///
262    /// Reverse the order of elements in the vector.
263    ///
264    /// @return GSL_SUCCESS on normal exit.
265    ///
266    // Jari, doxygen group as Exchanging elements
267    inline int reverse(void) { return gsl_vector_reverse(v_);}
268
269    ///
270    /// Rescale vector, \f$this_i = this_i * factor \; \forall i\f$.
271    ///
272    /// @return GSL_SUCCESS on normal exit.
273    ///
274    // Jari, doxygen group as Vector operators
275    inline int scale(double factor) { return gsl_vector_scale(v_,factor); }
276
277    ///
278    /// Set all elements to \a value.
279    ///
280    // Jari, doxygen group as Initializing vector elements
281    inline void set_all(const double& value) { gsl_vector_set_all(v_,value); }
282
283    ///
284    /// Makes a basis vector by setting all elements to
285    /// zero except the \a i-th element which is set to
286    /// one.
287    ///
288    // Jari, doxygen group as Initializing vector elements
289    inline void set_basis(const size_t i) { gsl_vector_set_basis(v_,i); }
290   
291    ///
292    /// Set all elements to zero.
293    ///
294    // Jari, doxygen group as Initializing vector elements
295    inline void set_zero(void) { gsl_vector_set_zero(v_); }
296
297    ///
298    /// @return the number of elements in the vector.
299    ///
300    inline size_t size(void) const { return v_->size; }
301
302    ///
303    /// Vector subtraction, \f$this_i = this_i - other_i \; \forall i\f$.
304    ///
305    /// @return GSL_SUCCESS on normal exit.
306    ///
307    // Jari, doxygen group as Vector operators
308    inline int sub(const vector& other) { return gsl_vector_sub(v_,other.v_); }
309
310    ///
311    /// Calculate the sum of all vector elements.
312    ///
313    /// @return The sum.
314    ///
315    // Jari, doxygen group as "Extends GSL".
316    double sum(void) const;
317
318    ///
319    /// Swap vector elements by copying. The two vectors must have the
320    /// same length.
321    ///
322    /// @return GSL_SUCCESS on normal exit.
323    ///
324    inline int swap(vector& other) { return gsl_vector_swap(v_,other.v_); }
325
326    ///
327    /// Exchange elements \a i and \a j.
328    ///
329    /// @return GSL_SUCCESS on normal exit.
330    ///
331    // Jari, doxygen group as Exchanging elements
332    inline int
333    swap_elements(size_t i,size_t j) { return gsl_vector_swap_elements(v_,i,j);}
334
335    ///
336    /// Element access operator.
337    ///
338    /// @return Reference to element \a i.
339    ///
340    // Jari, doxygen group as Accessing vector elements
341    inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); }
342
343    ///
344    /// Const element access operator.
345    ///
346    /// @return The value of element \a i.
347    ///
348    // Jari, doxygen group as Accessing vector elements
349    inline const double&
350    operator()(size_t i) const { return *gsl_vector_const_ptr(v_,i); }
351
352    ///
353    /// Element access operator.
354    ///
355    /// @return Reference to element \a i.
356    ///
357    // Jari, doxygen group as Accessing vector elements
358    inline double& operator[](size_t i) { return *gsl_vector_ptr(v_,i); }
359
360    ///
361    /// Const element access operator.
362    ///
363    /// @return The value of element \a i.
364    ///
365    // Jari, doxygen group as Accessing vector elements
366    inline const double&
367    operator[](size_t i) const { return *gsl_vector_const_ptr(v_,i); }
368
369    ///
370    /// @return The dot product.
371    ///
372    double operator*( const vector &other ) const;
373
374    ///
375    /// Vector with scalar multiplication.
376    ///
377    /// @note This operator is not implemented!
378    ///
379    vector operator*(const double d) const;
380
381    ///
382    /// Vector addition.
383    ///
384    /// @return The resulting vector.
385    ///
386    vector operator+( const vector& other ) const;
387
388    ///
389    /// Vector subtraction.
390    ///
391    /// @return The resulting vector.
392    ///
393    vector operator-( const vector& other ) const;
394
395    ///
396    /// Comparison operator. Takes linear time.
397    ///
398    /// @return True if the sequence of the elements in the vectors
399    /// are element/wise equal.
400    ///
401    bool operator==(const vector&) const;
402
403    ///
404    /// The assignment operator. There is no requirements on
405    /// dimensions.
406    ///
407    /// @return A const reference to the resulting vector.
408    ///
409    const vector& operator=(const vector&);
410
411    ///
412    /// Addition and assign operator.
413    ///
414    /// @return A const reference to the resulting vector.
415    ///
416    inline const vector&
417    operator+=(const vector& other) { gsl_vector_add(v_,other.v_); return *this;}
418
419    ///
420    /// Subtract and assign operator.
421    ///
422    /// @return A const reference to the resulting vector.
423    ///
424    inline const vector&
425    operator-=(const vector& other) { gsl_vector_sub(v_,other.v_); return *this;}
426
427    ///
428    /// Multiply with scalar and assign operator.
429    ///
430    /// @return A const reference to the resulting vector.
431    ///
432    inline const vector&
433    operator*=(const double d) { gsl_vector_scale(v_,d); return *this; }
434
435
436  private:
437
438    ///
439    /// Create a new copy of the internal GSL vector.
440    ///
441    /// Necessary memory for the new GSL vector is allocated and the
442    /// caller is responsible for freeing the allocated memory.
443    ///
444    /// @return A pointer to a copy of the internal GSL vector.
445    ///
446    gsl_vector* gsl_vector_copy(void) const;
447
448    gsl_vector* v_;
449    gsl_vector_view* view_;
450    gsl_vector_const_view* const_view_;
451  };
452
453  ///
454  /// The output operator for the vector class.
455  ///
456  std::ostream& operator<<(std::ostream&, const vector& );
457
458
459}} // of namespace gslapi and namespace theplu
460
461#endif
Note: See TracBrowser for help on using the repository browser.