mvector.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2005-2010 Taneli Kalvas. All rights reserved.
6  *
7  * You can redistribute this software and/or modify it under the terms
8  * of the GNU General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this library (file "COPYING" included in the package);
19  * if not, write to the Free Software Foundation, Inc., 51 Franklin
20  * Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * If you have questions about your rights to use or distribute this
23  * software, please contact Berkeley Lab's Technology Transfer
24  * Department at TTD@lbl.gov. Other questions, comments and bug
25  * reports should be sent directly to the author via email at
26  * taneli.kalvas@jyu.fi.
27  *
28  * NOTICE. This software was developed under partial funding from the
29  * U.S. Department of Energy. As such, the U.S. Government has been
30  * granted for itself and others acting on its behalf a paid-up,
31  * nonexclusive, irrevocable, worldwide license in the Software to
32  * reproduce, prepare derivative works, and perform publicly and
33  * display publicly. Beginning five (5) years after the date
34  * permission to assert copyright is obtained from the U.S. Department
35  * of Energy, and subject to any subsequent five (5) year renewals,
36  * the U.S. Government is granted for itself and others acting on its
37  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
38  * the Software to reproduce, prepare derivative works, distribute
39  * copies to the public, perform publicly and display publicly, and to
40  * permit others to do so.
41  */
42 
43 #ifndef MVECTOR_HPP
44 #define MVECTOR_HPP 1
45 
46 
47 #include <vector>
48 #include <iostream>
49 #include "matrix.hpp"
50 #include "error.hpp"
51 
52 
68 class Vector {
69  int _n;
70  double *_val;
71 
72  void allocate( void );
73  void callocate( void );
74  void reallocate( void );
75 
76 public:
77 
83  struct VectorRef {
84  const Vector *_vec;
85  double _coef;
86 
89  VectorRef() : _vec(NULL), _coef(1.0) {}
90 
93  VectorRef( const Vector *vec, double coef ) : _vec(vec), _coef(coef) {}
94  };
95 
102  struct VectorLA {
103  std::vector<VectorRef> _refs;
104 
107  VectorLA() {}
108 
111  VectorLA( const VectorLA &vecla ) : _refs(vecla._refs) {}
112 
115  VectorLA( const Vector &vec ) {
116  VectorRef ref( &vec, 1.0 );
117  _refs.push_back( ref );
118  }
119 
122  VectorLA( const Vector &vec, double coef ) {
123  VectorRef ref( &vec, coef );
124  _refs.push_back( ref );
125  }
126 
137  double operator[]( int i ) const;
138 
145  double operator()( int i ) const;
146 
149  VectorLA operator+( const VectorLA &vecla ) const;
150 
153  VectorLA operator-( const VectorLA &vecla ) const;
154 
157  VectorLA operator-() const;
158 
161  VectorLA operator*( double x ) const;
162 
165  friend VectorLA operator*( double x, const VectorLA &vecla );
166  };
167 
168 /* ************************************** *
169  * Constructors and destructor *
170  * ************************************** */
171 
176  Vector() : _n(0), _val(NULL) {}
177 
183  Vector( int n );
184 
192  Vector( int n, const double *val );
193 
201  Vector( int n, double val );
202 
208  Vector( const Vector &vec );
209 
212  Vector( const VectorLA &vecla );
213 
216  Vector( const struct MatrixMulVec &matvec );
217 
220  ~Vector();
221 
224  int size( void ) const { return( _n ); }
225 
233  void resize( int n );
234 
240  void clear( void );
241 
248  void merge( Vector &vec );
249 
256  double *get_data( void ) { return( _val ); }
257 
261  const double *get_data( void ) const { return( _val ); }
262 
265  VectorLA operator+( const VectorLA &vecla ) const;
266 
269  VectorLA operator-( const VectorLA &vecla ) const;
270 
273  VectorLA operator-() const;
274 
277  VectorLA operator*( double x ) const;
278 
281  Vector &operator+=( const VectorLA &vecla );
282 
285  Vector &operator-=( const VectorLA &vecla );
286 
289  Vector &operator*=( double x );
290 
293  Vector &operator=( double x );
294 
297  Vector &operator=( const Vector &vec );
298 
301  Vector &operator=( const VectorLA &vecla );
302 
305  Vector &operator=( const struct MatrixMulVec &matvec );
306 
315  bool operator==( const Vector &vec ) const;
316 
325  bool operator!=( const Vector &vec ) const;
326 
333  double &operator[]( int i );
334 
341  double &operator()( int i );
342 
349  double operator[]( int i ) const;
350 
357  double operator()( int i ) const;
358 
359  friend class HBIO;
360  friend class Matrix;
361  friend class CRowMatrix;
362  friend class CColMatrix;
363  friend class CoordMatrix;
364 
367  friend VectorLA operator*( double x, Vector &vec );
368 
371  friend std::ostream &operator<<( std::ostream &os, const Vector &vec );
372 
375  friend double dot_prod( const Vector &vec1, const Vector &vec2 );
376 
381  friend double norm1( const Vector &vec );
382 
387  friend double norm2( const Vector &vec );
388 
393  friend double ssqr( const Vector &vec );
394 
397  friend double min( const Vector &vec );
398 
401  friend double min_abs( const Vector &vec );
402 
405  friend double max( const Vector &vec );
406 
409  friend double max_abs( const Vector &vec );
410 
413  friend void swap( Vector &vec1, Vector &vec2 );
414 };
415 
416 
417 inline double Vector::VectorLA::operator[]( int i ) const {
418 #ifdef SPM_RANGE_CHECK
419  if( i >= _refs[0]._vec->_n )
420  throw( ErrorRange( ERROR_LOCATION, i, _refs[0]._vec->_n ) );
421 #endif
422  double res = 0.0;
423  std::vector<VectorRef>::const_iterator itend = _refs.end();
424  for( std::vector<VectorRef>::const_iterator it = _refs.begin(); it != itend; it++ )
425  res += (it->_coef) * (*(it->_vec))[i];
426  return( res );
427 }
428 
429 
430 inline double Vector::VectorLA::operator()( int i ) const {
431 #ifdef SPM_RANGE_CHECK
432  if( i >= _refs[0]._vec->_n )
433  throw( ErrorRange( ERROR_LOCATION, i, _refs[0]._vec->_n ) );
434 #endif
435  double res = 0.0;
436  std::vector<VectorRef>::const_iterator itend = _refs.end();
437  for( std::vector<VectorRef>::const_iterator it = _refs.begin(); it != itend; it++ )
438  res += (it->_coef) * (*(it->_vec))[i];
439  return( res );
440 }
441 
442 
443 inline double &Vector::operator[]( int i ) {
444 #ifdef SPM_RANGE_CHECK
445  if( i >= _n )
446  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
447 #endif
448  return( _val[i] );
449 }
450 
451 
452 inline double &Vector::operator()( int i ) {
453 #ifdef SPM_RANGE_CHECK
454  if( i >= _n )
455  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
456 #endif
457  return( _val[i] );
458 }
459 
460 
461 inline double Vector::operator[]( int i ) const {
462 #ifdef SPM_RANGE_CHECK
463  if( i >= _n )
464  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
465 #endif
466  return( _val[i] );
467 }
468 
469 
470 inline double Vector::operator()( int i ) const {
471 #ifdef SPM_RANGE_CHECK
472  if( i >= _n )
473  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
474 #endif
475  return( _val[i] );
476 }
477 
478 
479 
480 
481 
482 #endif
483 
484 
485 
486 
487 
488 
489 
490 
491 
492 
493 
494 
495 
496 
497 
498 
499 
500 
501 
502