crowmatrix.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2005-2011 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 CROWMATRIX_HPP
44 #define CROWMATRIX_HPP 1
45 
46 
47 #include <cstdlib>
48 #include <iostream>
49 #include "matrix.hpp"
50 #include "error.hpp"
51 
52 
76 class CRowMatrix : public Matrix {
77  int _n;
78  int _m;
79  int _nz;
80  int _asize;
81  int *_ptr;
82  int *_col;
83  double *_val;
84 
85  void reallocate( void );
86  void allocate( void );
87 
88  double get_check( int i, int j ) const;
89  double &set_check( int i, int j );
90  double get_no_check( int i, int j ) const;
91  double &set_no_check( int i, int j );
92 
93  void clear_check( int i, int j );
94  void clear_no_check( int i, int j );
95 
96  void build( const class CColMatrix &mat );
97  void build( const class CRowMatrix &mat );
98  void build( const class CoordMatrix &mat );
99 
100 public:
101 
102 /* ************************************** *
103  * Constructors and destructor *
104  * ************************************** */
105 
108  CRowMatrix();
109 
112  CRowMatrix( int n, int m );
113 
122  CRowMatrix( int n, int m, int nz,
123  int *ptr, int *col, double *val );
124 
127  CRowMatrix( const CRowMatrix &mat );
128 
134  CRowMatrix( const class CColMatrix &mat );
135 
138  CRowMatrix( const class CoordMatrix &mat );
139 
142  CRowMatrix( const class Matrix &mat );
143 
146  ~CRowMatrix();
147 
148 /* ************************************** *
149  * Access and information *
150  * ************************************** */
151 
154  int columns( void ) const { return( _m ); }
155 
158  int rows( void ) const { return( _n ); }
159 
163  void size( int &n, int &m ) const { n = _n; m = _m; }
164 
167  int nz_elements( void ) const { return( _nz ); }
168 
171  int capacity( void ) const { return( _asize ); }
172 
173 /* ************************************** *
174  * User level control *
175  * ************************************** */
176 
181  void resize( int n, int m );
182 
189  void merge( CRowMatrix &mat );
190 
193  void clear( void );
194 
199  void clear( int i, int j );
200 
203  void reserve( int size );
204 
208  void order_ascending( void );
209 
213  bool check_ascending( void );
214 
217  void debug_print( std::ostream &os ) const;
218 
219 /* ************************************** *
220  * User level matrix element access *
221  * ************************************** */
222 
228  double get( int i, int j ) const;
229 
248  double &set( int i, int j );
249 
258  void set_row( int i, int N, const int *col, const double *val );
259 
275  void construct_add( int i, int j, double val );
276 
277 /* ************************************** *
278  * Low level access *
279  * ************************************** */
280 
284  int &ptr( int i ) { return( _ptr[i] ); }
285 
289  int &col( int i ) { return( _col[i] ); }
290 
294  double &val( int i ) { return( _val[i] ); }
295 
299  const int &ptr( int i ) const { return( _ptr[i] ); }
300 
304  const int &col( int i ) const { return( _col[i] ); }
305 
309  const double &val( int i ) const { return( _val[i] ); }
310 
318  void set_nz( int nz );
319 
320 /* ************************************** *
321  * Assignent operators *
322  * ************************************** */
323 
326  CRowMatrix &operator=( const CRowMatrix &mat );
327 
334  CRowMatrix &operator=( const class CColMatrix &mat );
335 
338  CRowMatrix &operator=( const class CoordMatrix &mat );
339 
343  CRowMatrix &operator=( const class Matrix &mat );
344 
345 /* ************************************** *
346  * Matrix-Vector operations *
347  * ************************************** */
348 
349  /* \brief Calculates \a x = \a A*b.
350  */
351  void multiply_by_vector( Vector &x, const Vector &b ) const;
352 
358  void lower_unit_solve( Vector &x, const Vector &b ) const;
359 
366  void upper_diag_solve( Vector &x, const Vector &b ) const;
367 
368 
369  friend class CColMatrix;
370  friend class CoordMatrix;
371  friend class Vector;
372 };
373 
374 
375 inline double CRowMatrix::get( int i, int j ) const
376 {
377 #ifdef SPM_RANGE_CHECK
378  return( get_check( i, j ) );
379 #else
380  return( get_no_check( i, j ) );
381 #endif
382 }
383 
384 
385 inline double &CRowMatrix::set( int i, int j )
386 {
387 #ifdef SPM_RANGE_CHECK
388  return( set_check( i, j ) );
389 #else
390  return( set_no_check( i, j ) );
391 #endif
392 }
393 
394 
395 inline void CRowMatrix::clear( int i, int j )
396 {
397 #ifdef SPM_RANGE_CHECK
398  clear_check( i, j );
399 #else
400  clear_no_check( i, j );
401 #endif
402 }
403 
404 
405 #endif
406 
407 
408 
409 
410 
411 
412 
413 
414 
415 
416 
417 
418 
419 
420 
421 
422 
423 
424 
425 
426 
427 
Compressed row sparse matrix class.
Definition: crowmatrix.hpp:76
void upper_diag_solve(Vector &x, const Vector &b) const
Solves A*x = b for upper diagonal matrix.
int columns(void) const
Returns the number of columns in the matrix.
Definition: crowmatrix.hpp:154
Basis for matrix implementations.
void size(int &n, int &m) const
Returns the number of columns and number of columns in nn and mm.
Definition: crowmatrix.hpp:163
const int & ptr(int i) const
Returns a const reference to the to the internal pointer index data ptr of the matrix.
Definition: crowmatrix.hpp:299
void multiply_by_vector(Vector &x, const Vector &b) const
Dense math vector class.
Definition: mvector.hpp:68
int rows(void) const
Returns the number of rows in the matrix.
Definition: crowmatrix.hpp:158
void resize(int n, int m)
Resizes the matrix to size n x m.
void order_ascending(void)
Order (sort) matrix data in ascending column index order within each row.
void construct_add(int i, int j, double val)
Adds an element to matrix while constructing the whole matrix.
CRowMatrix()
Default constructor.
CRowMatrix & operator=(const CRowMatrix &mat)
Assignment operator.
const double & val(int i) const
Returns a const reference to the to the internal value data of the matrix.
Definition: crowmatrix.hpp:309
void set_row(int i, int N, const int *col, const double *val)
Function to set matrix row elements.
~CRowMatrix()
Destructor.
int & col(int i)
Returns a reference to the to the internal column data of the matrix.
Definition: crowmatrix.hpp:289
int & ptr(int i)
Returns a reference to the to the internal pointer index data ptr of the matrix.
Definition: crowmatrix.hpp:284
double & set(int i, int j)
Function to get a reference to matrix element value at (i,j).
Definition: crowmatrix.hpp:385
double & val(int i)
Returns a reference to the to the internal value data of the matrix.
Definition: crowmatrix.hpp:294
int capacity(void) const
Returns the number of elements allocated for matrix.
Definition: crowmatrix.hpp:171
Base matrix class.
Definition: matrix.hpp:76
void reserve(int size)
Reserve memory for size matrix elements.
void set_nz(int nz)
Set number of non-zero elements in the matrix.
Error classes and handling
void clear(void)
Clear non-zero matrix elements, set all elements to zero.
bool check_ascending(void)
Check if matrix data is in ascending column index order within each row.
void merge(CRowMatrix &mat)
Merges matrix mat into the matrix leaving mat empty.
int nz_elements(void) const
Returns the number of non-zero elements in the matrix.
Definition: crowmatrix.hpp:167
void lower_unit_solve(Vector &x, const Vector &b) const
Solves A*x = b for lower unit diagonal matrix.
double get(int i, int j) const
Function to get a matrix element value at (i,j).
Definition: crowmatrix.hpp:375
const int & col(int i) const
Returns a const reference to the to the internal column data of the matrix.
Definition: crowmatrix.hpp:304
Compressed column sparse matrix class.
Definition: ccolmatrix.hpp:75
void debug_print(std::ostream &os) const
Print debugging information to os.
Coordinate sparse matrix class.
Definition: coordmatrix.hpp:72