IBSimu  1.0.4
crowmatrix.hpp
Go to the documentation of this file.
00001 
00005 /* Copyright (c) 2005-2010 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 CROWMATRIX_HPP
00044 #define CROWMATRIX_HPP 1
00045 
00046 
00047 #include <cstdlib>
00048 #include <iostream>
00049 #include "matrix.hpp"
00050 #include "error.hpp"
00051 
00052 
00076 class CRowMatrix : public Matrix {
00077     int       _n;      
00078     int       _m;      
00079     int       _nz;     
00080     int       _asize;  
00081     int      *_ptr;    
00082     int      *_col;    
00083     double   *_val;    
00084 
00085     void reallocate( void );
00086     void allocate( void );
00087 
00088     double get_check( int i, int j ) const;
00089     double &set_check( int i, int j );
00090     double get_no_check( int i, int j ) const;
00091     double &set_no_check( int i, int j );
00092 
00093     void clear_check( int i, int j );
00094     void clear_no_check( int i, int j );
00095 
00096     void build( const class CColMatrix &mat );
00097     void build( const class CRowMatrix &mat );
00098     void build( const class CoordMatrix &mat );
00099 
00100 public:
00101 
00102 /* ************************************** *
00103  * Constructors and destructor            *
00104  * ************************************** */
00105 
00108     CRowMatrix();
00109 
00112     CRowMatrix( int n, int m );
00113 
00122     CRowMatrix( int n, int m, int nz, 
00123                 int *ptr, int *col, double *val );
00124 
00127     CRowMatrix( const CRowMatrix &mat );
00128 
00134     CRowMatrix( const class CColMatrix &mat );
00135 
00138     CRowMatrix( const class CoordMatrix &mat );
00139 
00142     CRowMatrix( const class Matrix &mat );
00143 
00146     ~CRowMatrix();
00147 
00148 /* ************************************** *
00149  * Access and information                 *
00150  * ************************************** */
00151 
00154     int columns( void ) const { return( _m ); }
00155 
00158     int rows( void ) const { return( _n ); }
00159 
00163     void size( int &n, int &m ) const { n = _n; m = _m; }
00164 
00167     int nz_elements( void ) const { return( _nz ); }
00168 
00171     int capacity( void ) const { return( _asize ); }
00172 
00173 /* ************************************** *
00174  * User level control                     *
00175  * ************************************** */
00176 
00181     void resize( int n, int m );
00182 
00189     void merge( CRowMatrix &mat );
00190 
00193     void clear( void );
00194 
00199     void clear( int i, int j );
00200 
00203     void reserve( int size );
00204 
00208     void order_ascending( void );
00209 
00213     bool check_ascending( void );
00214 
00217     void debug_print( void ) const;
00218 
00219 /* ************************************** *
00220  * User level matrix element access       *
00221  * ************************************** */
00222 
00228     double get( int i, int j ) const;
00229 
00248     double &set( int i, int j );
00249 
00258     void set_row( int i, int N, const int *col, const double *val );
00259 
00275     void construct_add( int i, int j, double val );
00276 
00277 /* ************************************** *
00278  * Low level access                       *
00279  * ************************************** */
00280 
00284     int &ptr( int i ) { return( _ptr[i] ); }
00285 
00289     int &col( int i ) { return( _col[i] ); }
00290 
00294     double &val( int i ) { return( _val[i] ); }
00295 
00299     const int &ptr( int i ) const { return( _ptr[i] ); }
00300 
00304     const int &col( int i ) const { return( _col[i] ); }
00305 
00309     const double &val( int i ) const { return( _val[i] ); }
00310 
00318     void set_nz( int nz );
00319 
00320 /* ************************************** *
00321  * Assignent operators                    *
00322  * ************************************** */
00323 
00326     CRowMatrix &operator=( const CRowMatrix &mat );
00327 
00334     CRowMatrix &operator=( const class CColMatrix &mat );
00335 
00338     CRowMatrix &operator=( const class CoordMatrix &mat );
00339 
00343     CRowMatrix &operator=( const class Matrix &mat );
00344 
00345 /* ************************************** *
00346  * Matrix-Vector operations               *
00347  * ************************************** */
00348 
00349     /*  \brief Calculates \a x = \a A*b.
00350      */
00351     void multiply_by_vector( Vector &x, const Vector &b ) const;
00352 
00358     void lower_unit_solve( Vector &x, const Vector &b ) const;
00359 
00366     void upper_diag_solve( Vector &x, const Vector &b ) const;
00367 
00368 
00369     friend class CColMatrix;
00370     friend class CoordMatrix;
00371     friend class Vector;
00372 };
00373 
00374 
00375 inline double CRowMatrix::get( int i, int j ) const
00376 {
00377 #ifdef SPM_RANGE_CHECK
00378     return( get_check( i, j ) );
00379 #else
00380     return( get_no_check( i, j ) );
00381 #endif
00382 }    
00383 
00384 
00385 inline double &CRowMatrix::set( int i, int j )
00386 {
00387 #ifdef SPM_RANGE_CHECK
00388     return( set_check( i, j ) );
00389 #else
00390     return( set_no_check( i, j ) );
00391 #endif
00392 }    
00393 
00394 
00395 inline void CRowMatrix::clear( int i, int j )
00396 {
00397 #ifdef SPM_RANGE_CHECK
00398     clear_check( i, j );
00399 #else
00400     clear_no_check( i, j );
00401 #endif
00402 }
00403 
00404 
00405 #endif
00406 
00407 
00408 
00409 
00410 
00411 
00412 
00413 
00414 
00415 
00416 
00417 
00418 
00419 
00420 
00421 
00422 
00423 
00424 
00425 
00426