Navigation

Main Page
Download
Support
Installation
Tutorial
Examples
Reference Manual
   Version 1.0.6dev
      Class Index
      File List
   Version 1.0.6
   Version 1.0.5new_solver
   Version 1.0.5dev
   Version 1.0.5b
   Version 1.0.4dev
   Version 1.0.4
Publications


Hosted by Get Ion Beam Simulator at SourceForge.net. Fast, secure and Free Open Source software downloads
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 
71 class Vector {
72  int _n;
73  double *_val;
74 
75  void allocate( void );
76  void callocate( void );
77  void reallocate( void );
78 
79 public:
80 
86  struct VectorRef {
87  const Vector *_vec;
88  double _coef;
89 
92  VectorRef() : _vec(NULL), _coef(1.0) {}
93 
96  VectorRef( const Vector *vec, double coef ) : _vec(vec), _coef(coef) {}
97  };
98 
105  struct VectorLA {
106  std::vector<VectorRef> _refs;
107 
110  VectorLA() {}
111 
114  VectorLA( const VectorLA &vecla ) : _refs(vecla._refs) {}
115 
118  VectorLA( const Vector &vec ) {
119  VectorRef ref( &vec, 1.0 );
120  _refs.push_back( ref );
121  }
122 
125  VectorLA( const Vector &vec, double coef ) {
126  VectorRef ref( &vec, coef );
127  _refs.push_back( ref );
128  }
129 
140  double operator[]( int i ) const;
141 
148  double operator()( int i ) const;
149 
152  VectorLA operator+( const VectorLA &vecla ) const;
153 
156  VectorLA operator-( const VectorLA &vecla ) const;
157 
160  VectorLA operator-() const;
161 
164  VectorLA operator*( double x ) const;
165 
168  friend VectorLA operator*( double x, const VectorLA &vecla );
169  };
170 
171 /* ************************************** *
172  * Constructors and destructor *
173  * ************************************** */
174 
179  Vector() : _n(0), _val(NULL) {}
180 
186  Vector( int n );
187 
195  Vector( int n, const double *val );
196 
204  Vector( int n, double val );
205 
211  Vector( const Vector &vec );
212 
216  Vector( const VectorLA &vecla );
217 
221  Vector( const struct MatrixMulVec &matvec );
222 
225  ~Vector();
226 
229  int size( void ) const { return( _n ); }
230 
238  void resize( int n );
239 
245  void clear( void );
246 
253  void merge( Vector &vec );
254 
261  double *get_data( void ) { return( _val ); }
262 
266  const double *get_data( void ) const { return( _val ); }
267 
270  VectorLA operator+( const VectorLA &vecla ) const;
271 
274  VectorLA operator-( const VectorLA &vecla ) const;
275 
278  VectorLA operator-() const;
279 
282  VectorLA operator*( double x ) const;
283 
286  Vector &operator+=( const VectorLA &vecla );
287 
290  Vector &operator-=( const VectorLA &vecla );
291 
294  Vector &operator*=( double x );
295 
298  Vector &operator=( double x );
299 
302  Vector &operator=( const Vector &vec );
303 
306  Vector &operator=( const VectorLA &vecla );
307 
310  Vector &operator=( const struct MatrixMulVec &matvec );
311 
320  bool operator==( const Vector &vec ) const;
321 
330  bool operator!=( const Vector &vec ) const;
331 
338  double &operator[]( int i );
339 
346  double &operator()( int i );
347 
354  double operator[]( int i ) const;
355 
362  double operator()( int i ) const;
363 
364  friend class HBIO;
365  friend class Matrix;
366  friend class CRowMatrix;
367  friend class CColMatrix;
368  friend class CoordMatrix;
369 
372  friend VectorLA operator*( double x, Vector &vec );
373 
376  friend std::ostream &operator<<( std::ostream &os, const Vector &vec );
377 
380  friend double dot_prod( const Vector &vec1, const Vector &vec2 );
381 
386  friend double norm1( const Vector &vec );
387 
392  friend double norm2( const Vector &vec );
393 
398  friend double ssqr( const Vector &vec );
399 
402  friend double min( const Vector &vec );
403 
406  friend double min_abs( const Vector &vec );
407 
410  friend double max( const Vector &vec );
411 
414  friend double max_abs( const Vector &vec );
415 
418  friend void swap( Vector &vec1, Vector &vec2 );
419 };
420 
421 
422 inline double Vector::VectorLA::operator[]( int i ) const {
423 #ifdef SPM_RANGE_CHECK
424  if( i >= _refs[0]._vec->_n )
425  throw( ErrorRange( ERROR_LOCATION, i, _refs[0]._vec->_n ) );
426 #endif
427  double res = 0.0;
428  std::vector<VectorRef>::const_iterator itend = _refs.end();
429  for( std::vector<VectorRef>::const_iterator it = _refs.begin(); it != itend; it++ )
430  res += (it->_coef) * (*(it->_vec))[i];
431  return( res );
432 }
433 
434 
435 inline double Vector::VectorLA::operator()( int i ) const {
436 #ifdef SPM_RANGE_CHECK
437  if( i >= _refs[0]._vec->_n )
438  throw( ErrorRange( ERROR_LOCATION, i, _refs[0]._vec->_n ) );
439 #endif
440  double res = 0.0;
441  std::vector<VectorRef>::const_iterator itend = _refs.end();
442  for( std::vector<VectorRef>::const_iterator it = _refs.begin(); it != itend; it++ )
443  res += (it->_coef) * (*(it->_vec))[i];
444  return( res );
445 }
446 
447 
448 inline double &Vector::operator[]( int i ) {
449 #ifdef SPM_RANGE_CHECK
450  if( i >= _n )
451  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
452 #endif
453  return( _val[i] );
454 }
455 
456 
457 inline double &Vector::operator()( int i ) {
458 #ifdef SPM_RANGE_CHECK
459  if( i >= _n )
460  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
461 #endif
462  return( _val[i] );
463 }
464 
465 
466 inline double Vector::operator[]( int i ) const {
467 #ifdef SPM_RANGE_CHECK
468  if( i >= _n )
469  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
470 #endif
471  return( _val[i] );
472 }
473 
474 
475 inline double Vector::operator()( int i ) const {
476 #ifdef SPM_RANGE_CHECK
477  if( i >= _n )
478  throw( ErrorRange( ERROR_LOCATION, i, _n ) );
479 #endif
480  return( _val[i] );
481 }
482 
483 
484 
485 
486 
487 #endif
488 
489 
490 
491 
492 
493 
494 
495 
496 
497 
498 
499 
500 
501 
502 
503 
504 
505 
506 
507 
Compressed column sparse matrix class.
Definition: ccolmatrix.hpp:75
Compressed row sparse matrix class.
Definition: crowmatrix.hpp:76
Coordinate sparse matrix class.
Definition: coordmatrix.hpp:72
Error class for index range checking errors.
Definition: error.hpp:283
Harwell Boeing sparse matrix file format I/O class.
Definition: hbio.hpp:66
Base matrix class.
Definition: matrix.hpp:76
Dense math vector class.
Definition: mvector.hpp:71
friend double norm2(const Vector &vec)
Returns 2-norm of vector.
Definition: mvector.cpp:474
void resize(int n)
Resizes a vector.
Definition: mvector.cpp:220
VectorLA operator*(double x) const
Operator for multiplying vector with a constant.
Definition: mvector.cpp:276
~Vector()
Destructor for vectors.
Definition: mvector.cpp:214
bool operator!=(const Vector &vec) const
Operator for comparing vectors.
Definition: mvector.cpp:415
friend double dot_prod(const Vector &vec1, const Vector &vec2)
Returns dot product of vector vec1 and vector vec2.
Definition: mvector.cpp:442
VectorLA operator-() const
Operator for unary minus.
Definition: mvector.cpp:269
friend double ssqr(const Vector &vec)
Returns square of 2-norm of vector.
Definition: mvector.cpp:487
friend void swap(Vector &vec1, Vector &vec2)
Swaps contents of vector vec1 and vector vec2.
Definition: mvector.cpp:544
double & operator[](int i)
Operator for pointing to vector elements.
Definition: mvector.hpp:448
Vector & operator-=(const VectorLA &vecla)
Operator for subtracting vectors.
Definition: mvector.cpp:301
double & operator()(int i)
Operator for pointing to vector elements.
Definition: mvector.hpp:457
Vector()
Default constructor.
Definition: mvector.hpp:179
friend double min_abs(const Vector &vec)
Returns the minimum vector element absolute value.
Definition: mvector.cpp:507
void merge(Vector &vec)
Merges vector vec into the vector leaving vec empty.
Definition: mvector.cpp:235
friend double min(const Vector &vec)
Returns the minimum vector element value.
Definition: mvector.cpp:496
const double * get_data(void) const
Returns a const pointer to the coordinate value data of the vector.
Definition: mvector.hpp:266
VectorLA operator+(const VectorLA &vecla) const
Operator for adding vectors.
Definition: mvector.cpp:245
Vector & operator+=(const VectorLA &vecla)
Operator for adding vectors.
Definition: mvector.cpp:283
void clear(void)
Clears the vector.
Definition: mvector.cpp:229
friend double max(const Vector &vec)
Returns the maximum vector element value.
Definition: mvector.cpp:520
friend double max_abs(const Vector &vec)
Returns the maximum vector element absolute value.
Definition: mvector.cpp:531
friend double norm1(const Vector &vec)
Returns 1-norm of vector.
Definition: mvector.cpp:461
friend std::ostream & operator<<(std::ostream &os, const Vector &vec)
Operator for printing a vector.
Definition: mvector.cpp:434
Vector & operator=(double x)
Operator for setting all vector elements to value x.
Definition: mvector.cpp:331
Vector & operator*=(double x)
Operator for multiplying vector with a constant.
Definition: mvector.cpp:319
bool operator==(const Vector &vec) const
Operator for comparing vectors.
Definition: mvector.cpp:403
double * get_data(void)
Returns a pointer to the coordinate value data of the vector.
Definition: mvector.hpp:261
int size(void) const
Returns the size of vector.
Definition: mvector.hpp:229
Error classes and handling
#define ERROR_LOCATION
Macro for setting error location when throwing errors.
Definition: error.hpp:83
Basis for matrix implementations.
Container object for matrix-vector multiplication operation.
Definition: matrix.hpp:57
Container object for linear algebra operations.
Definition: mvector.hpp:105
VectorLA(const VectorLA &vecla)
Copy constructor.
Definition: mvector.hpp:114
VectorLA(const Vector &vec, double coef)
Constructor for VectorLA with vector vec with coefficient coef.
Definition: mvector.hpp:125
VectorLA operator*(double x) const
Operator for multiplying vector with a constant.
Definition: mvector.cpp:95
VectorLA()
Default constructor.
Definition: mvector.hpp:110
VectorLA operator+(const VectorLA &vecla) const
Operator for adding vectors.
Definition: mvector.cpp:61
double operator()(int i) const
Operator for pointing to elements of linear algebra operations.
Definition: mvector.hpp:435
std::vector< VectorRef > _refs
List of linear algebra operations.
Definition: mvector.hpp:106
VectorLA operator-() const
Operator for unary minus.
Definition: mvector.cpp:85
double operator[](int i) const
Operator for pointing to elements of linear algebra operations.
Definition: mvector.hpp:422
VectorLA(const Vector &vec)
Constructor for VectorLA with vector vec with coefficient 1.
Definition: mvector.hpp:118
Container object for coefficient-vector pairs.
Definition: mvector.hpp:86
const Vector * _vec
Pointer to vector.
Definition: mvector.hpp:87
VectorRef()
Constructor for empty VectorRef.
Definition: mvector.hpp:92
double _coef
Coefficient for vector vec.
Definition: mvector.hpp:88
VectorRef(const Vector *vec, double coef)
Constructor for VectorRef with vector vec and coefficient coef.
Definition: mvector.hpp:96


Reference manual for Ion Beam Simulator 1.0.6dev
Generated by Doxygen 1.9.1 on Thu Sep 11 2025 09:37:24.