1 | #ifndef _theplu_yat_utility_vector_ |
---|
2 | #define _theplu_yat_utility_vector_ |
---|
3 | |
---|
4 | // $Id: vector.h 755 2007-02-18 00:01:39Z 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 | |
---|
39 | namespace theplu { |
---|
40 | namespace yat { |
---|
41 | namespace utility { |
---|
42 | |
---|
43 | class matrix; |
---|
44 | |
---|
45 | /** |
---|
46 | This is the yat interface to GSL vector. 'double' is the only |
---|
47 | type supported, maybe we should add a 'complex' type as well in |
---|
48 | the future. |
---|
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. |
---|
207 | */ |
---|
208 | explicit vector(std::istream &, char sep='\0') |
---|
209 | throw(utility::IO_error, std::exception); |
---|
210 | |
---|
211 | /// |
---|
212 | /// The destructor. |
---|
213 | /// |
---|
214 | ~vector(void); |
---|
215 | |
---|
216 | /** |
---|
217 | \brief Vector addition, \f$ this_i = this_i + other_i \; |
---|
218 | \forall i \f$. |
---|
219 | |
---|
220 | \throw GSL_error if dimensions mis-match. |
---|
221 | */ |
---|
222 | void add(const vector& other); |
---|
223 | |
---|
224 | /** |
---|
225 | \brief Add a constant to a vector, \f$ this_i = this_i + term \; |
---|
226 | \forall i \f$. |
---|
227 | */ |
---|
228 | void add(double term); |
---|
229 | |
---|
230 | /** |
---|
231 | \brief This function performs element-wise division, \f$ this_i = |
---|
232 | this_i/other_i \; \forall i \f$. |
---|
233 | |
---|
234 | \throw GSL_error if dimensions mis-match. |
---|
235 | */ |
---|
236 | void div(const vector& other); |
---|
237 | |
---|
238 | /// |
---|
239 | /// @return A const pointer to the internal GSL vector, |
---|
240 | /// |
---|
241 | const gsl_vector* gsl_vector_p(void) const; |
---|
242 | |
---|
243 | /// |
---|
244 | /// @return A pointer to the internal GSL vector, |
---|
245 | /// |
---|
246 | gsl_vector* gsl_vector_p(void); |
---|
247 | |
---|
248 | /// |
---|
249 | /// @return True if all elements in the vector is zero, false |
---|
250 | /// othwerwise; |
---|
251 | /// |
---|
252 | bool isnull(void) const; |
---|
253 | |
---|
254 | /// |
---|
255 | /// Check if the vector object is a view (sub-vector) to another |
---|
256 | /// vector. |
---|
257 | /// |
---|
258 | /// @return True if the object is a view, false othwerwise. |
---|
259 | /// |
---|
260 | bool isview(void) const; |
---|
261 | |
---|
262 | /// |
---|
263 | /// @return The maximum value of the vector. |
---|
264 | /// |
---|
265 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
266 | double max(void) const; |
---|
267 | |
---|
268 | /// |
---|
269 | /// @return The element index to the maximum value of the |
---|
270 | /// vector. The lowest index has precedence. |
---|
271 | /// |
---|
272 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
273 | size_t max_index(void) const; |
---|
274 | |
---|
275 | /// |
---|
276 | /// @return The minimum value of the vector. |
---|
277 | /// |
---|
278 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
279 | double min(void) const; |
---|
280 | |
---|
281 | /// |
---|
282 | /// @return The element index to the minimum value of the |
---|
283 | /// vector. The lowest index has precedence. |
---|
284 | /// |
---|
285 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
286 | size_t min_index(void) const; |
---|
287 | |
---|
288 | /// |
---|
289 | /// @return The minimum and maximum values of the vector, as the |
---|
290 | /// \a first and \a second member of the returned \a pair, |
---|
291 | /// respectively. |
---|
292 | /// |
---|
293 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
294 | std::pair<double,double> minmax(void) const; |
---|
295 | |
---|
296 | /// |
---|
297 | /// @return The indecies to the minimum and maximum values of the |
---|
298 | /// vector, as the \a first and \a second member of the returned |
---|
299 | /// \a pair, respectively. The lowest index has precedence. |
---|
300 | /// |
---|
301 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
302 | std::pair<size_t,size_t> minmax_index(void) const; |
---|
303 | |
---|
304 | /** |
---|
305 | \brief This function performs element-wise multiplication, \f$ |
---|
306 | this_i = this_i * other_i \; \forall i \f$. |
---|
307 | |
---|
308 | \throw GSL_error if dimensions mis-match. |
---|
309 | */ |
---|
310 | void mul(const vector& other); |
---|
311 | |
---|
312 | /** |
---|
313 | \brief Reverse the order of elements in the vector. |
---|
314 | */ |
---|
315 | void reverse(void); |
---|
316 | |
---|
317 | /** |
---|
318 | \brief Rescale vector, \f$ this_i = this_i * factor \; \forall i \f$. |
---|
319 | */ |
---|
320 | void scale(double factor); |
---|
321 | |
---|
322 | /** |
---|
323 | \brief Set element values to values in \a vec. |
---|
324 | |
---|
325 | This function is needed for assignment of viewed elements. |
---|
326 | |
---|
327 | \see const vector& operator=(const vector&) |
---|
328 | |
---|
329 | \throw GSL_error if dimensions mis-match. |
---|
330 | */ |
---|
331 | void set(const vector& vec); |
---|
332 | |
---|
333 | /// |
---|
334 | /// Set all elements to \a value. |
---|
335 | /// |
---|
336 | // Jari, doxygen group as Initializing vector elements |
---|
337 | void set_all(const double& value); |
---|
338 | |
---|
339 | /// |
---|
340 | /// Makes a basis vector by setting all elements to |
---|
341 | /// zero except the \a i-th element which is set to |
---|
342 | /// one. |
---|
343 | /// |
---|
344 | // Jari, doxygen group as Initializing vector elements |
---|
345 | void set_basis(const size_t i); |
---|
346 | |
---|
347 | /// |
---|
348 | /// Set all elements to zero. |
---|
349 | /// |
---|
350 | // Jari, doxygen group as Initializing vector elements |
---|
351 | void set_zero(void); |
---|
352 | |
---|
353 | /// |
---|
354 | /// @return the number of elements in the vector. |
---|
355 | /// |
---|
356 | size_t size(void) const; |
---|
357 | |
---|
358 | /// |
---|
359 | /// Sort the elements in the vector. |
---|
360 | /// |
---|
361 | /// Bug in gsl: if vector contains NaN an infinite loop is entered. |
---|
362 | /// |
---|
363 | // Markus to Jari, doxygen group as Exchanging elements ???? |
---|
364 | void sort(void); |
---|
365 | |
---|
366 | /** |
---|
367 | \brief Vector subtraction, \f$ this_i = this_i - other_i \; |
---|
368 | \forall i \f$. |
---|
369 | |
---|
370 | \throw GSL_error if dimensions mis-match. |
---|
371 | */ |
---|
372 | void sub(const vector& other); |
---|
373 | |
---|
374 | /// |
---|
375 | /// Calculate the sum of all vector elements. |
---|
376 | /// |
---|
377 | /// @return The sum. |
---|
378 | /// |
---|
379 | double sum(void) const; |
---|
380 | |
---|
381 | /** |
---|
382 | \brief Swap vector elements by copying. |
---|
383 | |
---|
384 | The two vectors must have the same length. |
---|
385 | |
---|
386 | \throw GSL_error if vector lengths differs. |
---|
387 | */ |
---|
388 | void swap(vector& other); |
---|
389 | |
---|
390 | /** |
---|
391 | \brief Exchange elements \a i and \a j. |
---|
392 | |
---|
393 | \throw GSL_error if vector lengths differs. |
---|
394 | */ |
---|
395 | void swap_elements(size_t i, size_t j); |
---|
396 | |
---|
397 | /** |
---|
398 | \brief Element access operator. |
---|
399 | |
---|
400 | \return Reference to element \a i. |
---|
401 | |
---|
402 | \throw If GSL range checks are enabled in the underlying GSL |
---|
403 | library a GSL_error exception is thrown if either index is out |
---|
404 | of range. |
---|
405 | */ |
---|
406 | double& operator()(size_t i); |
---|
407 | |
---|
408 | /** |
---|
409 | \brief Element access operator. |
---|
410 | |
---|
411 | \return Const reference to element \a i. |
---|
412 | |
---|
413 | \throw If GSL range checks are enabled in the underlying GSL |
---|
414 | library a GSL_error exception is thrown if either index is out |
---|
415 | of range. |
---|
416 | */ |
---|
417 | const double& operator()(size_t i) const; |
---|
418 | |
---|
419 | /// |
---|
420 | /// Element access operator. |
---|
421 | /// |
---|
422 | /// @return Reference to element \a i. |
---|
423 | /// |
---|
424 | // Jari, doxygen group as Accessing vector elements |
---|
425 | double& operator[](size_t i); |
---|
426 | |
---|
427 | /// |
---|
428 | /// Const element access operator. |
---|
429 | /// |
---|
430 | /// @return The value of element \a i. |
---|
431 | /// |
---|
432 | // Jari, doxygen group as Accessing vector elements |
---|
433 | const double& operator[](size_t i) const; |
---|
434 | |
---|
435 | /// |
---|
436 | /// Comparison operator. Takes linear time. |
---|
437 | /// |
---|
438 | /// @return True if the sequence of the elements in the vectors |
---|
439 | /// are element/wise equal. |
---|
440 | /// |
---|
441 | bool operator==(const vector&) const; |
---|
442 | |
---|
443 | /// |
---|
444 | /// @return The dot product. |
---|
445 | /// |
---|
446 | double operator*(const vector&) const; |
---|
447 | |
---|
448 | /** |
---|
449 | \brief The assignment operator. |
---|
450 | |
---|
451 | There is no requirements on dimensions, i.e. the vector is |
---|
452 | remapped in memory if necessary. This implies that in general |
---|
453 | views cannot be assigned using this operator. Views will be |
---|
454 | mutated into normal vectors. The only exception to this |
---|
455 | behaviour on views is when self-assignemnt is done, since |
---|
456 | self-assignment is ignored. |
---|
457 | |
---|
458 | \return A const reference to the resulting vector. |
---|
459 | |
---|
460 | \see void set(const vector&). |
---|
461 | |
---|
462 | \throw A GSL_error is indirectly thrown if memory allocation |
---|
463 | fails, or if dimensions mis-match. |
---|
464 | */ |
---|
465 | const vector& operator=(const vector&); |
---|
466 | |
---|
467 | /** |
---|
468 | \brief Addition and assign operator. |
---|
469 | |
---|
470 | \return A const reference to the resulting vector. |
---|
471 | |
---|
472 | \throw GSL_error if dimensions mis-match. |
---|
473 | */ |
---|
474 | const vector& operator+=(const vector&); |
---|
475 | |
---|
476 | /** |
---|
477 | \brief Subtract and assign operator. |
---|
478 | |
---|
479 | \return A const reference to the resulting vector. |
---|
480 | |
---|
481 | \throw GSL_error if dimensions mis-match. |
---|
482 | */ |
---|
483 | const vector& operator-=(const vector&); |
---|
484 | |
---|
485 | /// |
---|
486 | /// Multiply with scalar and assign operator. |
---|
487 | /// |
---|
488 | /// @return A const reference to the resulting vector. |
---|
489 | /// |
---|
490 | const vector& operator*=(const double); |
---|
491 | |
---|
492 | |
---|
493 | private: |
---|
494 | |
---|
495 | /** |
---|
496 | \brief Create a new copy of the internal GSL vector. |
---|
497 | |
---|
498 | Necessary memory for the new GSL vector is allocated and the |
---|
499 | caller is responsible for freeing the allocated memory. |
---|
500 | |
---|
501 | \return A pointer to a copy of the internal GSL vector. |
---|
502 | |
---|
503 | \throw GSL_error if memory cannot be allocated for the new |
---|
504 | copy, or if dimensions mis-match. |
---|
505 | */ |
---|
506 | gsl_vector* create_gsl_vector_copy(void) const; |
---|
507 | |
---|
508 | gsl_vector* v_; |
---|
509 | const gsl_vector* v_const_; |
---|
510 | gsl_vector_view* view_; |
---|
511 | gsl_vector_const_view* view_const_; |
---|
512 | // proxy_v_ is used to access the proper underlying v_ or v_const_ |
---|
513 | // in all const member functions. |
---|
514 | const gsl_vector* proxy_v_; |
---|
515 | }; |
---|
516 | |
---|
517 | /// |
---|
518 | /// The output operator for the vector class. |
---|
519 | /// |
---|
520 | std::ostream& operator<<(std::ostream&, const vector& ); |
---|
521 | |
---|
522 | |
---|
523 | }}} // of namespace utility, yat, and theplu |
---|
524 | |
---|
525 | #endif |
---|