source: trunk/yat/utility/vector.h @ 775

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

Fixes #200.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1#ifndef _theplu_yat_utility_vector_
2#define _theplu_yat_utility_vector_
3
4// $Id: vector.h 775 2007-03-01 23:53:03Z jari $
5
6/*
7  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
8  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
10  Copyright (C) 2006, 2007 Jari Häkkinen
11
12  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 2 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
30#include "Exception.h"
31
32#include <iostream>
33#include <vector>
34#include <utility>
35
36#include <gsl/gsl_vector.h>
37#include <gsl/gsl_sort_vector.h>
38
39namespace theplu {
40namespace yat {
41namespace utility {
42
43  class matrix;
44
45  /**
46     @brief This is the yat interface to GSL vector.
47
48     For time being 'double' is the only type supported.
49
50     \par File streams:
51     Reading and writing vectors to file streams are of course
52     supported. These are implemented without using GSL functionality,
53     and thus binary read and write to streams are not supported.
54
55     \par Vector views:
56     GSL vector views are supported and these are disguised as
57     ordinary utility::vectors. A support function is added,
58     utility::vector::isview(), that can be used to check if a vector
59     object is a view. Note that view vectors do not own the
60     underlying data, and a view is not valid if the vector owing the
61     data is deallocated.
62
63     \par
64     Currently there is no restriction on how a vector is used in
65     cases when the vector is a const view into another vector or
66     matrix. To avoid unexpected runtime errors, the programmer must
67     declare const view vectors as const in order to get compile time
68     diagnostics about improper use of a const view vector object. An
69     example of improper use of a const view vector is assignment of
70     values to an element in the object. If the const view object was
71     declared const the assigment will be caught by the compiler
72     whereas it will cause a (un-catchable) runtime error. Example:
73     @code
74  const vector vm(13,1.0);
75  vector v1(vm,2,4);       // bad code! not const!
76  v1(0)=-123;              // accepted by compiler, runtime abort will occur
77  const vector v2(vm,2,4); // proper code
78  v2(0)=-123;              // not acceptable for the compiler
79     @endcode
80
81     @note Missing support to underlying GSL vector features can in
82     principle be included to this class if requested by the user
83     community.
84  */
85
86  class vector
87  {
88  public:
89
90    /**
91       \brief The default constructor.
92    */
93    vector(void);
94
95    /**
96       \brief Allocates memory space for \a n elements, and sets all
97       elements to \a init_value.
98       
99       \throw GSL_error if memory allocation fails.
100    */
101    vector(size_t n, double init_value=0);
102
103    /**
104       \brief The copy constructor.
105
106       \note If the object to be copied is a vector view, the values
107       of the view will be copied, i.e. the view is not copied.
108
109       \throw A GSL_error is indirectly thrown if memory allocation
110       fails.
111    */
112    vector(const vector& other);
113
114    /**
115       \brief Vector view constructor.
116
117       Create a view of vector \a v, with starting index \a offset,
118       size \a n, and an optional \a stride.
119
120       A vector view can be used as any vector with the difference
121       that changes made to the view will also change the object that
122       is viewed. Also, using the copy constructor will create a new
123       vector object that is a copy of whatever is viewed. If a copy
124       of the view is needed then you should use this constructor to
125       obtain a copy.
126
127       \note If the object viewed by the view goes out of scope or is
128       deleted, the view becomes invalid and the result of further use
129       is undefined.
130
131       \throw GSL_error if a view cannot be set up.
132    */
133    vector(vector& v, size_t offset, size_t n, size_t stride=1);
134
135    /**
136       \brief Vector const view constructor.
137
138       Create a view of vector \a v, with starting index \a offset,
139       size \a n, and an optional \a stride.
140
141       A vector view can be used as any const vector. Using the copy
142       constructor will create a new vector object that is a copy of
143       whatever is viewed. If a copy of the view is needed then you
144       should use this constructor to obtain a copy.
145
146       \note If the object viewed by the view goes out of scope or is
147       deleted, the view becomes invalid and the result of further use
148       is undefined.
149
150       \throw GSL_error if a view cannot be set up.
151    */
152    vector(const vector& v, size_t offset, size_t n, size_t stride=1);
153
154    ///
155    /// Matrix row/column view constructor.
156    ///
157    /// Create a row/column vector view of matrix \a m, pointing at
158    /// row/column \a i. The parameter \a row is used to set whether
159    /// the view should be a row or column view. If \a row is set to
160    /// true, the view will be a row view (default behaviour), and,
161    /// naturally, a column view otherwise.
162    ///
163    /// A vector view can be used as any vector with the difference
164    /// that changes made to the view will also change the object that
165    /// is viewed. Also, using the copy constructor will create a new
166    /// vector object that is a copy of whatever is viewed. If a copy
167    /// of the view is needed then you should use the vector view
168    /// constructor to obtain a copy.
169    ///
170    /// @note If the object viewed by the view goes out of scope or is
171    /// deleted, the view becomes invalid and the result of further
172    /// use is undefined.
173    ///
174    vector(matrix& m, size_t i, bool row=true);
175
176    ///
177    /// Matrix row/column const view constructor.
178    ///
179    /// Create a row/column vector view of matrix \a m, pointing at
180    /// row/column \a i. The parameter \a row is used to set whether
181    /// the view should be a row or column view. If \a row is set to
182    /// true, the view will be a row view (default behaviour), and,
183    /// naturally, a column view otherwise.
184    ///
185    /// A const vector view can be used as any const vector. Using the
186    /// copy constructor will create a new vector object that is a
187    /// copy of whatever is viewed. If a copy of the view is needed
188    /// then you should use the vector view constructor to obtain a
189    /// copy.
190    ///
191    /// @note If the object viewed by the view goes out of scope or is
192    /// deleted, the view becomes invalid and the result of further
193    /// use is undefined.
194    ///
195    vector(const matrix& m, size_t i, bool row=true);
196
197    /**
198       \brief The istream constructor.
199
200       Either elements should be separated with white space characters
201       (default), or elements should be separated by the delimiter \a
202       sep. When delimiter \a sep is used empty elements are stored as
203       NaN's (except that empty lines are ignored). The end of input
204       to the vector is at end of file marker.
205
206       \throw GSL_error if memory allocation fails, IO_error if
207       unexpected input is found in the input stream.
208    */
209    explicit vector(std::istream &, char sep='\0')
210      throw(utility::IO_error, std::exception);
211
212    ///
213    /// The destructor.
214    ///
215    ~vector(void);
216
217    /**
218       \brief This function performs element-wise division, \f$ this_i =
219       this_i/other_i \; \forall i \f$.
220
221       \throw GSL_error if dimensions mis-match.
222    */
223    void div(const vector& other);
224
225    ///
226    /// @return A const pointer to the internal GSL vector,
227    ///
228    const gsl_vector* gsl_vector_p(void) const;
229
230    ///
231    /// @return A pointer to the internal GSL vector,
232    ///
233    gsl_vector* gsl_vector_p(void);
234
235    ///
236    /// @return True if all elements in the vector is zero, false
237    /// othwerwise;
238    ///
239    bool isnull(void) const;
240
241    ///
242    /// Check if the vector object is a view (sub-vector) to another
243    /// vector.
244    ///
245    /// @return True if the object is a view, false othwerwise.
246    ///
247    bool isview(void) const;
248
249    ///
250    /// @return The maximum value of the vector.
251    ///
252    double max(void) const;
253
254    ///
255    /// @return The element index to the maximum value of the
256    /// vector. The lowest index has precedence.
257    ///
258    size_t max_index(void) const;
259
260    ///
261    /// @return The minimum value of the vector.
262    ///
263    double min(void) const;
264
265    ///
266    /// @return The element index to the minimum value of the
267    /// vector. The lowest index has precedence.
268    ///
269    size_t min_index(void) const;
270
271    ///
272    /// @return The minimum and maximum values of the vector, as the
273    /// \a first and \a second member of the returned \a pair,
274    /// respectively.
275    ///
276    std::pair<double,double> minmax(void) const;
277
278    ///
279    /// @return The indecies to the minimum and maximum values of the
280    /// vector, as the \a first and \a second member of the returned
281    /// \a pair, respectively. The lowest index has precedence.
282    ///
283    std::pair<size_t,size_t> minmax_index(void) const;
284
285    /**
286       \brief This function performs element-wise multiplication, \f$
287       this_i = this_i * other_i \; \forall i \f$.
288
289       \throw GSL_error if dimensions mis-match.
290    */
291    void mul(const vector& other);
292
293    /**
294       \brief Reverse the order of elements in the vector.
295    */
296    void reverse(void);
297
298    /**
299       \brief Set element values to values in \a vec.
300
301       This function is needed for assignment of viewed elements.
302
303       \see const vector& operator=(const vector&)
304
305       \throw GSL_error if dimensions mis-match.
306    */
307    void set(const vector& vec);
308
309    ///
310    /// Set all elements to \a value.
311    ///
312    void set_all(const double& value);
313
314    ///
315    /// Makes a basis vector by setting all elements to
316    /// zero except the \a i-th element which is set to
317    /// one.
318    ///
319    void set_basis(const size_t i);
320   
321    ///
322    /// Set all elements to zero.
323    ///
324    void set_zero(void);
325
326    ///
327    /// @return the number of elements in the vector.
328    ///
329    size_t size(void) const;
330
331    ///
332    /// Sort the elements in the vector.
333    ///
334    /// Bug in gsl: if vector contains NaN an infinite loop is entered.
335    ///
336    void sort(void);
337
338    ///
339    /// Calculate the sum of all vector elements.
340    ///
341    /// @return The sum.
342    ///
343    double sum(void) const;
344
345    /**
346       \brief Swap vector elements by copying.
347
348       The two vectors must have the same length.
349
350       \throw GSL_error if vector lengths differs.
351    */
352    void swap(vector& other);
353
354    /**
355       \brief Exchange elements \a i and \a j.
356
357       \throw GSL_error if vector lengths differs.
358    */
359    void swap_elements(size_t i, size_t j);
360
361    /**
362       \brief Element access operator.
363
364       \return Reference to element \a i.
365
366       \throw If GSL range checks are enabled in the underlying GSL
367       library a GSL_error exception is thrown if either index is out
368       of range.
369    */
370    double& operator()(size_t i);
371
372    /**
373       \brief Element access operator.
374
375       \return Const reference to element \a i.
376
377       \throw If GSL range checks are enabled in the underlying GSL
378       library a GSL_error exception is thrown if either index is out
379       of range.
380    */
381    const double& operator()(size_t i) const;
382
383    ///
384    /// Element access operator.
385    ///
386    /// @return Reference to element \a i.
387    ///
388    double& operator[](size_t i);
389
390    ///
391    /// Const element access operator.
392    ///
393    /// @return The value of element \a i.
394    ///
395    const double& operator[](size_t i) const;
396
397    ///
398    /// Comparison operator. Takes linear time.
399    ///
400    /// @return True if the sequence of the elements in the vectors
401    /// are element/wise equal.
402    ///
403    bool operator==(const vector&) const;
404
405    ///
406    /// @return The dot product.
407    ///
408    double operator*(const vector&) const;
409
410    /**
411       \brief The assignment operator.
412
413       There is no requirements on dimensions, i.e. the vector is
414       remapped in memory if necessary. This implies that in general
415       views cannot be assigned using this operator. Views will be
416       mutated into normal vectors. The only exception to this
417       behaviour on views is when self-assignemnt is done, since
418       self-assignment is ignored.
419
420       \return A const reference to the resulting vector.
421
422       \see void set(const vector&).
423
424       \throw A GSL_error is indirectly thrown if memory allocation
425       fails, or if dimensions mis-match.
426    */
427    const vector& operator=(const vector&);
428
429    /**
430       \brief Addition and assign operator. Vector addition, \f$
431       this_i = this_i + other_i \; \forall i \f$.
432
433       \return A const reference to the resulting vector.
434
435       \throw GSL_error if dimensions mis-match.
436    */
437    const vector& operator+=(const vector&);
438
439    /**
440       \brief Add a constant to a vector, \f$ this_i = this_i + d \;
441       \forall i \f$.
442
443       \return A const reference to the resulting vector.
444    */
445    const vector& operator+=(double d);
446
447    /**
448       \brief Subtract and assign operator. Vector subtraction, \f$
449       this_i = this_i - other_i \; \forall i \f$.
450
451       \return A const reference to the resulting vector.
452
453       \throw GSL_error if dimensions mis-match.
454    */
455    const vector& operator-=(const vector&);
456
457    /**
458       \brief Subtract a constant to a vector, \f$ this_i = this_i - d
459       \; \forall i \f$.
460
461       \return A const reference to the resulting vector.
462    */
463    const vector& operator-=(double d);
464
465    /**
466       \brief Multiply with scalar and assign operator, \f$ this_i =
467       this_i * d \; \forall i \f$.
468
469       \return A const reference to the resulting vector.
470    */
471    const vector& operator*=(double d);
472
473
474  private:
475
476    /**
477       \brief Create a new copy of the internal GSL vector.
478
479       Necessary memory for the new GSL vector is allocated and the
480       caller is responsible for freeing the allocated memory.
481
482       \return A pointer to a copy of the internal GSL vector.
483
484       \throw GSL_error if memory cannot be allocated for the new
485       copy, or if dimensions mis-match.
486    */
487    gsl_vector* create_gsl_vector_copy(void) const;
488
489    gsl_vector* v_;
490    const gsl_vector* v_const_;
491    gsl_vector_view* view_;
492    gsl_vector_const_view* view_const_;
493    // proxy_v_ is used to access the proper underlying v_ or v_const_
494    // in all const member functions.
495    const gsl_vector* proxy_v_;
496  };
497
498  ///
499  /// The output operator for the vector class.
500  ///
501  std::ostream& operator<<(std::ostream&, const vector& );
502
503
504}}} // of namespace utility, yat, and theplu
505
506#endif
Note: See TracBrowser for help on using the repository browser.