coordmatrix.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2005-2009,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 COORDMATRIX_HPP
44 #define COORDMATRIX_HPP 1
45 
46 
47 #include <cstdlib>
48 #include <iostream>
49 #include "matrix.hpp"
50 #include "error.hpp"
51 
52 
72 class CoordMatrix : public Matrix {
73  int _n;
74  int _m;
75  int _nz;
76  int _asize;
77  int *_row;
78  int *_col;
79  double *_val;
80 
81  void allocate( void );
82  void reallocate( void );
83 
84  double get_check( int i, int j ) const;
85  double &set_check( int i, int j );
86  double get_no_check( int i, int j ) const;
87  double &set_no_check( int i, int j );
88 
89  void clear_check( int i, int j );
90  void clear_no_check( int i, int j );
91 
92  void build( const class CColMatrix &mat );
93  void build( const class CRowMatrix &mat );
94  void build( const class CoordMatrix &mat );
95 
96 public:
97 
98 /* ************************************** *
99  * Constructors and destructor *
100  * ************************************** */
101 
104  CoordMatrix() : _n(0), _m(0), _nz(0), _asize(0), _row(NULL), _col(NULL), _val(NULL) { }
105 
108  CoordMatrix( int n, int m );
109 
118  CoordMatrix( int n, int m, int nz,
119  const int *row, const int *col, const int *val );
120 
123  CoordMatrix( const CoordMatrix &mat );
124 
127  CoordMatrix( const class CRowMatrix &mat );
128 
131  CoordMatrix( const class CColMatrix &mat );
132 
135  CoordMatrix( const class Matrix &mat );
136 
139  ~CoordMatrix();
140 
141 /* ************************************** *
142  * Access and information *
143  * ************************************** */
144 
147  int columns( void ) const { return( _m ); }
148 
151  int rows( void ) const { return( _n ); }
152 
155  void size( int &n, int &m ) const { n = _n; m = _m; }
156 
159  int nz_elements( void ) const { return( _nz ); }
160 
163  int capacity( void ) const { return( _asize ); }
164 
165 /* ************************************** *
166  * User level control *
167  * ************************************** */
168 
173  void resize( int n, int m );
174 
181  void merge( CoordMatrix &mat );
182 
185  void clear( void );
186 
191  void clear( int i, int j );
192 
195  void reserve( int size );
196 
200  void order_ascending_row_column( void );
201 
205  void order_ascending_column_row( void );
206 
209  void debug_print( std::ostream &os ) const;
210 
211 /* ************************************** *
212  * User level matrix element access *
213  * ************************************** */
214 
220  double get( int i, int j ) const;
221 
240  double &set( int i, int j );
241 
248  void set_no_duplicate_check( int i, int j, double vval );
249 
250 /* ************************************** *
251  * Low level access *
252  * ************************************** */
253 
257  int &row( int i ) { return( _row[i] ); }
258 
262  int &col( int i ) { return( _col[i] ); }
263 
267  double &val( int i ) { return( _val[i] ); }
268 
272  const int &row( int i ) const { return( _row[i] ); }
273 
277  const int &col( int i ) const { return( _col[i] ); }
278 
282  const double &val( int i ) const { return( _val[i] ); }
283 
290  void set_nz( int nz );
291 
292 /* ************************************** *
293  * Assignent operators *
294  * ************************************** */
295 
296  CoordMatrix &operator=( const CoordMatrix &mat );
297  CoordMatrix &operator=( const CColMatrix &mat );
298  CoordMatrix &operator=( const CRowMatrix &mat );
299  CoordMatrix &operator=( const Matrix &mat );
300 
301 /* ************************************** *
302  * Matrix-Vector operations *
303  * ************************************** */
304 
305  /* \brief Calculates \a x = \a A*b.
306  */
307  void multiply_by_vector( Vector &res, const Vector &rhs ) const;
308  void lower_unit_solve( Vector &y, const Vector &b ) const;
309  void upper_diag_solve( Vector &x, const Vector &y ) const;
310 
311 
312  friend class CRowMatrix;
313  friend class CColMatrix;
314 };
315 
316 
317 inline double CoordMatrix::get( int i, int j ) const
318 {
319 #ifdef SPM_RANGE_CHECK
320  return( get_check( i, j ) );
321 #else
322  return( get_no_check( i, j ) );
323 #endif
324 }
325 
326 
327 inline double &CoordMatrix::set( int i, int j )
328 {
329 #ifdef SPM_RANGE_CHECK
330  return( set_check( i, j ) );
331 #else
332  return( set_no_check( i, j ) );
333 #endif
334 }
335 
336 
337 inline void CoordMatrix::clear( int i, int j )
338 {
339 #ifdef SPM_RANGE_CHECK
340  clear_check( i, j );
341 #else
342  clear_no_check( i, j );
343 #endif
344 }
345 
346 
347 #endif
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
358 
359 
360 
361 
362 
363 
364 
365 
366 
367 
368 
369 
int nz_elements(void) const
Returns the number of non-zero elements in the matrix.
Definition: coordmatrix.hpp:159
Compressed row sparse matrix class.
Definition: crowmatrix.hpp:76
const double & val(int i) const
Returns a const reference to the to the internal value data of the matrix.
Definition: coordmatrix.hpp:282
void multiply_by_vector(Vector &res, const Vector &rhs) const
void order_ascending_column_row(void)
Order (sort) matrix data in ascending (column,row) index order.
void order_ascending_row_column(void)
Order (sort) matrix data in ascending (row,column) index order.
Basis for matrix implementations.
void upper_diag_solve(Vector &x, const Vector &y) const
CoordMatrix()
Default constructor.
Definition: coordmatrix.hpp:104
Dense math vector class.
Definition: mvector.hpp:68
void debug_print(std::ostream &os) const
Print debugging information to os.
void set_nz(int nz)
Set number of non-zero elements in the matrix.
int columns(void) const
Returns the number of columns in the matrix.
Definition: coordmatrix.hpp:147
int & row(int i)
Returns a reference to the to the internal row data of the matrix.
Definition: coordmatrix.hpp:257
const int & row(int i) const
Returns a const reference to the to the internal row data of the matrix.
Definition: coordmatrix.hpp:272
int rows(void) const
Returns the number of rows in the matrix.
Definition: coordmatrix.hpp:151
void merge(CoordMatrix &mat)
Merges matrix mat into the matrix leaving mat empty.
void clear(void)
Clear non-zero matrix elements, set all elements to zero.
Base matrix class.
Definition: matrix.hpp:76
int & col(int i)
Returns a reference to the to the internal column data ptr of the matrix.
Definition: coordmatrix.hpp:262
const int & col(int i) const
Returns a const reference to the to the internal column data ptr of the matrix.
Definition: coordmatrix.hpp:277
Error classes and handling
void reserve(int size)
Reserve memory for size matrix elements.
double & set(int i, int j)
Function to get a reference to matrix element value at (i,j).
Definition: coordmatrix.hpp:327
CoordMatrix & operator=(const CoordMatrix &mat)
int capacity(void) const
Returns the number of elements allocated for matrix.
Definition: coordmatrix.hpp:163
void resize(int n, int m)
Resizes the matrix to size n x m.
double & val(int i)
Returns a reference to the to the internal value data of the matrix.
Definition: coordmatrix.hpp:267
void set_no_duplicate_check(int i, int j, double vval)
Set element with no checks.
void lower_unit_solve(Vector &y, const Vector &b) const
Compressed column sparse matrix class.
Definition: ccolmatrix.hpp:75
void size(int &n, int &m) const
Returns the number of columns and number of columns in nn and mm.
Definition: coordmatrix.hpp:155
Coordinate sparse matrix class.
Definition: coordmatrix.hpp:72
double get(int i, int j) const
Function to get a matrix element value at (i,j).
Definition: coordmatrix.hpp:317
~CoordMatrix()
Destructor.