home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / LIBMAT.ZIP / MATMAP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-16  |  6.3 KB  |  241 lines

  1. /*****************************************************/
  2. /*           matmap.c source for matMap class        */
  3. /*****************************************************/
  4.  
  5.  
  6. /**************************************************/
  7. /*            MatClass Source File                */
  8. /*       Copyright of C. R. Birchenhall           */
  9. /*       University of Manchester, UK.            */
  10. /*   MatClass is freeware. This file should be    */
  11. /* made freely available to users of any software */
  12. /* whose creation is wholly or partly dependent   */
  13. /*                on this file.                   */
  14. /**************************************************/
  15.  
  16. #include "matrix.hpp"
  17.  
  18. /***********************************************************/
  19. /*  Basic constructor generates the underlying array as    */
  20. /*  well as the map for the matrix.                        */
  21. /***********************************************************/
  22.  
  23.    /****************************************/
  24.    /* Both pa and map are offset back by   */
  25.    /* one to facilitate unit base address- */
  26.    /* ing of elements. In particular pa[j] */
  27.    /* will refer to the jth realArray and  */
  28.    /* map[j][i] to the ith element of the  */
  29.    /* jth array.                           */
  30.    /****************************************/
  31.  
  32. #ifdef __cplusplus
  33. matMap::matMap( INDEX nr, INDEX nc ) : matObject()
  34. #else
  35. matMap::matMap( INDEX nr, INDEX nc ) : ()
  36. #endif
  37. {
  38.    static char *mName = "matMap(,)" ;
  39.    matFunc func( mName ) ;
  40.    INDEX i ;
  41.    pa = new realArray(nr,nc) ;
  42.    if ( ( map = new REAL*[nc] ) == 0 )
  43.       errorExit( mName, NOMEM ) ;
  44.    map-- ;
  45.    for ( i = 1 ; i <= nc ; i++ )
  46.       map[i] = pa->base[i] - 1 ;
  47.    nrow = nr ;
  48.    ncol = nc ;
  49.    mapSize = nc ;
  50.    ref = 1 ;
  51.    func.trace(TRUE) ; // debugInfo( func ) ;
  52. } // matMap::matMap( INDEX, INDEX )
  53.  
  54. #ifdef __cplusplus
  55. matMap::matMap( void ) : matObject()
  56. #else
  57. matMap::matMap( void ) : ()
  58. #endif
  59. {
  60.    static char *mName = "matMap()" ;
  61.    matFunc func( mName ) ;
  62.    pa = 0 ;
  63.    map = 0 ;
  64.    nrow = 0 ;
  65.    ncol = 0 ;
  66.    mapSize = 0 ;
  67.    ref = 1 ;
  68.    func.trace(TRUE) ; // debugInfo( func ) ;
  69. } // matMap::matMap( INDEX, INDEX )
  70.  
  71. #ifdef __cplusplus
  72. matMap::matMap( const matMap& m ) : matObject()
  73. #else
  74. matMap::matMap( const matMap& m ) : ()
  75. #endif
  76. {
  77.    static char *mName = "matMap(&m)" ;
  78.    matFunc func( mName ) ;
  79.    nrow = m.nrow ;
  80.    ncol = m.ncol ;
  81.    if ( ( map = new REAL*[ncol] ) == 0 )
  82.       errorExit( mName, NOMEM ) ;
  83.    map-- ;
  84.    for ( INDEX i = 1 ; i <= ncol ; i++ )
  85.       map[i] = m.map[i] ;
  86.    pa = m.pa ;
  87.    if ( pa != 0 )
  88.       pa->ref++ ;
  89.    mapSize = ncol ;
  90.    ref = 1 ;
  91.    func.trace(TRUE) ; // debugInfo( func ) ;
  92. } // matMap::matMap( matMap& )
  93.  
  94. /************************************************/
  95. /* The following constructor generates a subMap */
  96. /* for an existing map to facilitate reference  */
  97. /* matrices.                                    */
  98. /************************************************/
  99.  
  100. #ifdef __cplusplus
  101. matMap::matMap( const matMap& m, INDEX r1, INDEX r2,
  102.         INDEX c1, INDEX c2 ) : matObject()
  103. #else
  104. matMap::matMap( const matMap& m, INDEX r1, INDEX r2,
  105.         INDEX c1, INDEX c2 ) : ()
  106. #endif
  107. {
  108.    static char *mName = "matMap(&m,,,)" ;
  109.    matFunc func( mName ) ;
  110.    if ( r1 < 1 || r1 > r2 || r2 > m.nRows() ||
  111.     c1 < 1 || c1 > c2 || c2 > m.nCols() )
  112.       m.errorExit( mName, NRANG ) ;
  113.    INDEX nr = r2 - r1 + 1, nc = c2 - c1 + 1 ;
  114.    if ( ( map = new REAL*[nc] ) == 0 )
  115.       errorExit( mName, NOMEM ) ;
  116.    map-- ;
  117.    for ( INDEX i = 1 ; i <= nc ; i++ )
  118.       map[i] = m.map[c1+i-1] + r1 - 1 ;
  119.    pa = m.pa ;
  120.    if ( pa != 0 )
  121.       pa->ref++ ;
  122.    nrow = nr ;
  123.    ncol = nc ;
  124.    mapSize = nc ;
  125.    ref = 1 ;
  126.    func.trace(TRUE) ; // debugInfo( func ) ;
  127. } // matMap::matMap( matMap& ,,, )
  128.  
  129. matMap::~matMap( void )
  130. {
  131.    static char *mName = "~matMap" ;
  132.    matFunc func( mName ) ; debugInfo( func ) ;
  133.    clear() ;
  134. } // matMap::~matMap
  135.  
  136. void matMap::reset( INDEX nr, INDEX nc )
  137. {
  138.    static char *mName = "reset" ;
  139.    matFunc func( mName ) ; debugInfo( func ) ;
  140.    if ( pa != 0 && --pa->ref == 0 )
  141.       delete pa ;
  142.    pa = new realArray(nr,nc) ;
  143.    if ( map != 0 ) {
  144.       map++ ;
  145.       delete map ;
  146.    } // if
  147.    if ( ( map = new REAL*[nc] ) == 0 )
  148.       errorExit( mName, NOMEM ) ;
  149.    map-- ;
  150.    for ( INDEX i = 1 ; i <= nc ; i++ )
  151.       map[i] = pa->base[i] - 1 ;
  152.    nrow = nr ;
  153.    ncol = nc ;
  154.    mapSize = nc ;
  155. } // matMap reset
  156.  
  157. REAL& matMap::elem( INDEX i, INDEX j ) M_CONST
  158. {
  159.    if ( i < 1 || i > nrow || j < 1 || j > ncol )
  160.       errorExit( "elem(,)", NRANG ) ;
  161.    return map[j][i] ;
  162. } //matMap()
  163.  
  164. matMap& matMap::operator = ( const matMap& m )
  165. {
  166.    static char *mName = "op =" ;
  167.    matFunc func( mName ) ; debugInfo( func ) ;
  168.    INDEX i ;
  169.    if ( map != 0 ) {
  170.       map++ ;
  171.       delete map ;
  172.    } // if
  173.    clear() ;
  174.    nrow = m.nrow ;
  175.    ncol = m.ncol ;
  176.    mapSize = ncol ;
  177.    if ( ( map = new REAL*[ncol] ) == 0 )
  178.       errorExit( mName, NOMEM ) ;
  179.    map-- ;
  180.    for ( i = 1 ; i <= ncol ; i++ )
  181.       map[i] = m.map[i] ;
  182.    pa = m.pa ;
  183.    if ( pa != 0 )
  184.       pa->ref++ ;
  185.    return *this ;
  186. } // matMap =
  187.  
  188. charArray& matMap::name( char * name )
  189. {
  190.    nm = name ;
  191.    return nm ;
  192. } // matMap name
  193.  
  194. outFile& matMap::info( outFile& f ) M_CONST
  195. {
  196.    if ( matListCtrl() > 2 ) 
  197.       return f ;
  198.    objectInfo( f ) ;
  199.    putName( nm.array(), f ) ;
  200.    putName( "matMap", f ) ;
  201.    putField( nrow, f ) ;
  202.    putField( ncol, f ) ;
  203.    if ( pa != 0 )
  204.       putField( pa->identity(), f ) ;
  205.    putField( ref, f );
  206.    f.newLine() ;
  207.    return f ;
  208. } // matMap::info
  209.  
  210. outFile& matMap::put( outFile& f ) M_CONST
  211. {
  212.    static char *mName = "put" ;
  213.    matFunc func( mName ) ; debugInfo( func ) ;
  214.    f.write( "\n\n****matMap.put not implemented****\n\n") ;
  215.    return f ;
  216. } // matMap::put
  217.  
  218. inFile& matMap::get( inFile& f )
  219. {
  220.    static char *mName = "get" ;
  221.    matFunc func( mName ) ; debugInfo( func ) ;
  222.    errout.write( "\n\n****matMap.get not implemented****\n\n") ;
  223.    return f ;
  224. } // matMap::get
  225.  
  226. void matMap::clear( void )
  227. {
  228.    static char *mName = "clear" ;
  229.    matFunc func( mName ) ; debugInfo( func ) ;
  230.    if ( pa != 0  && --pa->ref == 0 )
  231.       delete pa ;
  232.    pa = 0 ;
  233.    if ( map != 0 ) {
  234.       map++ ;
  235.       delete map ;
  236.    } // if
  237.    map = 0 ;
  238.    nrow = 0 ;
  239.    ncol = 0 ;
  240. } // matMap::clear
  241.