coordmatrix.hpp
Go to the documentation of this file.
00001 
00005 /* Copyright (c) 2005-2009,2011 Taneli Kalvas. All rights reserved.
00006  *
00007  * You can redistribute this software and/or modify it under the terms
00008  * of the GNU General Public License as published by the Free Software
00009  * Foundation; either version 2 of the License, or (at your option)
00010  * any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this library (file "COPYING" included in the package);
00019  * if not, write to the Free Software Foundation, Inc., 51 Franklin
00020  * Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  * 
00022  * If you have questions about your rights to use or distribute this
00023  * software, please contact Berkeley Lab's Technology Transfer
00024  * Department at TTD@lbl.gov. Other questions, comments and bug
00025  * reports should be sent directly to the author via email at
00026  * taneli.kalvas@jyu.fi.
00027  * 
00028  * NOTICE. This software was developed under partial funding from the
00029  * U.S.  Department of Energy.  As such, the U.S. Government has been
00030  * granted for itself and others acting on its behalf a paid-up,
00031  * nonexclusive, irrevocable, worldwide license in the Software to
00032  * reproduce, prepare derivative works, and perform publicly and
00033  * display publicly.  Beginning five (5) years after the date
00034  * permission to assert copyright is obtained from the U.S. Department
00035  * of Energy, and subject to any subsequent five (5) year renewals,
00036  * the U.S. Government is granted for itself and others acting on its
00037  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
00038  * the Software to reproduce, prepare derivative works, distribute
00039  * copies to the public, perform publicly and display publicly, and to
00040  * permit others to do so.
00041  */
00042 
00043 #ifndef COORDMATRIX_HPP
00044 #define COORDMATRIX_HPP 1
00045 
00046 
00047 #include <cstdlib>
00048 #include <iostream>
00049 #include "matrix.hpp"
00050 #include "error.hpp"
00051 
00052 
00072 class CoordMatrix : public Matrix {
00073     int       _n;      
00074     int       _m;      
00075     int       _nz;     
00076     int       _asize;  
00077     int      *_row;    
00078     int      *_col;    
00079     double   *_val;    
00080 
00081     void allocate( void );
00082     void reallocate( void );
00083 
00084     double get_check( int i, int j ) const;
00085     double &set_check( int i, int j );
00086     double get_no_check( int i, int j ) const;
00087     double &set_no_check( int i, int j );
00088 
00089     void clear_check( int i, int j );
00090     void clear_no_check( int i, int j );
00091 
00092     void build( const class CColMatrix &mat );
00093     void build( const class CRowMatrix &mat );
00094     void build( const class CoordMatrix &mat );
00095 
00096 public:
00097 
00098 /* ************************************** *
00099  * Constructors and destructor            *
00100  * ************************************** */
00101 
00104     CoordMatrix() : _n(0), _m(0), _nz(0), _asize(0), _row(NULL), _col(NULL), _val(NULL) { }
00105 
00108     CoordMatrix( int n, int m );
00109 
00118     CoordMatrix( int n, int m, int nz, 
00119                  const int *row, const int *col, const int *val );
00120 
00123     CoordMatrix( const CoordMatrix &mat );
00124 
00127     CoordMatrix( const class CRowMatrix &mat );
00128 
00131     CoordMatrix( const class CColMatrix &mat );
00132 
00135     CoordMatrix( const class Matrix &mat );
00136 
00139     ~CoordMatrix();
00140 
00141 /* ************************************** *
00142  * Access and information                 *
00143  * ************************************** */
00144 
00147     int columns( void ) const { return( _m ); }
00148 
00151     int rows( void ) const { return( _n ); }
00152 
00155     void size( int &n, int &m ) const { n = _n; m = _m; }
00156 
00159     int nz_elements( void ) const { return( _nz ); }
00160 
00163     int capacity( void ) const { return( _asize ); }
00164 
00165 /* ************************************** *
00166  * User level control                     *
00167  * ************************************** */
00168 
00173     void resize( int n, int m );
00174 
00181     void merge( CoordMatrix &mat );
00182 
00185     void clear( void );
00186 
00191     void clear( int i, int j );
00192 
00195     void reserve( int size );
00196 
00200     void order_ascending_row_column( void );
00201 
00205     void order_ascending_column_row( void );
00206 
00209     void debug_print( std::ostream &os ) const;
00210 
00211 /* ************************************** *
00212  * User level matrix element access       *
00213  * ************************************** */
00214 
00220     double get( int i, int j ) const;
00221 
00240     double &set( int i, int j );
00241 
00248     void set_no_duplicate_check( int i, int j, double vval );
00249 
00250 /* ************************************** *
00251  * Low level access                       *
00252  * ************************************** */
00253 
00257     int &row( int i ) { return( _row[i] ); }
00258 
00262     int &col( int i ) { return( _col[i] ); }
00263 
00267     double &val( int i ) { return( _val[i] ); }
00268 
00272     const int &row( int i ) const { return( _row[i] ); }
00273 
00277     const int &col( int i ) const { return( _col[i] ); }
00278 
00282     const double &val( int i ) const { return( _val[i] ); }
00283 
00290     void set_nz( int nz );
00291 
00292 /* ************************************** *
00293  * Assignent operators                    *
00294  * ************************************** */
00295 
00296     CoordMatrix &operator=( const CoordMatrix &mat );
00297     CoordMatrix &operator=( const CColMatrix &mat );
00298     CoordMatrix &operator=( const CRowMatrix &mat );
00299     CoordMatrix &operator=( const Matrix &mat );
00300 
00301 /* ************************************** *
00302  * Matrix-Vector operations               *
00303  * ************************************** */
00304 
00305     /*  \brief Calculates \a x = \a A*b.
00306      */
00307     void multiply_by_vector( Vector &res, const Vector &rhs ) const;
00308     void lower_unit_solve( Vector &y, const Vector &b ) const;
00309     void upper_diag_solve( Vector &x, const Vector &y ) const;
00310 
00311 
00312     friend class CRowMatrix;
00313     friend class CColMatrix;
00314 };
00315 
00316 
00317 inline double CoordMatrix::get( int i, int j ) const
00318 {
00319 #ifdef SPM_RANGE_CHECK
00320     return( get_check( i, j ) );
00321 #else
00322     return( get_no_check( i, j ) );
00323 #endif
00324 }    
00325 
00326 
00327 inline double &CoordMatrix::set( int i, int j )
00328 {
00329 #ifdef SPM_RANGE_CHECK
00330     return( set_check( i, j ) );
00331 #else
00332     return( set_no_check( i, j ) );
00333 #endif
00334 }    
00335 
00336 
00337 inline void CoordMatrix::clear( int i, int j )
00338 {
00339 #ifdef SPM_RANGE_CHECK
00340     clear_check( i, j );
00341 #else
00342     clear_no_check( i, j );
00343 #endif
00344 }
00345 
00346 
00347 #endif
00348 
00349 
00350 
00351 
00352 
00353 
00354 
00355 
00356 
00357 
00358 
00359 
00360 
00361 
00362 
00363 
00364 
00365 
00366 
00367 
00368 
00369