home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / LIBMAT.ZIP / MATINDEX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-01  |  10.7 KB  |  408 lines

  1. /**************************************************/
  2. /*    matindex.c source for indexArray 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. #include <string.h>
  18. #include <ctype.h>
  19.  
  20. #ifndef __cplusplus
  21. #define NULL 0L
  22. #endif
  23.  
  24. #ifdef __cplusplus
  25. indexArray::indexArray( char* nameStr, INDEX n ) : matObject()
  26. #else
  27. indexArray::indexArray( char* nameStr, INDEX n ) : ()
  28. #endif
  29. {
  30.    static char *mName = "indexAr(name,n)" ;
  31.    matFunc func( mName ) ;
  32.    nm = nameStr ;
  33.    if ( n != 0 ) {
  34.       ip = new INDEX[n] ;
  35.       if ( ip == 0 )
  36.          errorExit( mName, NOMEM ) ;
  37.       len = n;
  38.       memset( (void*) ip, 0, n * sizeof(INDEX) ) ;
  39.       ip-- ; // unit offset
  40.    } else {
  41.      ip = 0 ;
  42.      len = 0;
  43.    } // else
  44.    func.trace(TRUE) ; // debugInfo( func ) ;
  45. } // indexArray
  46.  
  47. #ifdef __cplusplus
  48. indexArray::indexArray( INDEX n ) : matObject()
  49. #else
  50. indexArray::indexArray( INDEX n ) : ()
  51. #endif
  52. {
  53.    static char *mName = "indexAr(n)" ;
  54.    matFunc func( mName ) ;
  55.    if ( n != 0 ) {
  56.       ip = new INDEX[ n ] ;
  57.       if ( ip == 0 )
  58.          errorExit( mName, NOMEM ) ;
  59.       len = n ;
  60.       memset( (void *) ip, 0, n * sizeof( INDEX ) ) ;
  61.       ip-- ; // unit offset
  62.    } else {
  63.       ip = 0 ;
  64.       len = 0 ;
  65.    } // else
  66.    func.trace(TRUE) ; // debugInfo( func ) ;
  67. } // indexArray
  68.  
  69. #if 0
  70.  
  71. #ifdef __cplusplus
  72. indexArray::indexArray( void ) : matObject()
  73. #else
  74. indexArray::indexArray( void ) : ()
  75. #endif
  76. {
  77.    static char *mName = "indexAr(n)" ;
  78.    matFunc func( mName ) ;
  79.    ip = 0 ;
  80.    len = 0 ;
  81.    func.trace(TRUE) ; // debugInfo( func ) ;
  82. } // indexArray
  83.  
  84. #endif
  85.  
  86. #ifdef __cplusplus
  87. indexArray::indexArray( const indexArray& index ) : matObject()
  88. #else
  89. indexArray::indexArray( const indexArray& index ) : ()
  90. #endif
  91. {
  92.    static char *mName = "indexAr(index)" ;
  93.    matFunc func( mName ) ;
  94.    INDEX n = index.length() ;
  95.    if ( n != 0 ) {
  96.       ip = new INDEX[ n ] ;
  97.       if ( ip == 0 )
  98.          errorExit( mName, NOMEM ) ;
  99.       len = n ;
  100.       ip-- ; // unit offset
  101.       for ( INDEX i = 1 ; i <= n ; i++ )
  102.          ip[i] = index.ip[i] ;
  103.    } else {
  104.       ip = 0 ;
  105.       len = 0 ;
  106.    } // else
  107.    func.trace(TRUE) ; // debugInfo( func ) ;
  108. } // indexArray::indexArray( indexArray& )
  109.  
  110. indexArray::~indexArray( void )
  111. {
  112.    static char *mName = "~indexArray" ;
  113.    matFunc func( mName ) ; debugInfo( func ) ;
  114.    if ( ip != 0 ) {
  115.       ip++ ;
  116.       delete ip ;
  117.    } // if
  118. } // ~indexArray
  119.  
  120. void indexArray::clear( void )
  121. {
  122.    static char *mName = "clear" ;
  123.    matFunc func( mName ) ; debugInfo( func ) ;
  124.    if ( ip != 0 ) {
  125.       ip++ ;
  126.       delete ip ;
  127.       ip = 0 ;
  128.       len = 0 ;
  129.    } // if
  130. } // indexArray clear
  131.  
  132. charArray indexArray::name( char *newName )
  133. {
  134.    if ( newName != 0 )
  135.       nm = newName  ;
  136.    return nm ;
  137. } // indexArray::name
  138.  
  139. indexArray& indexArray::reset( INDEX n )
  140. {
  141.    static char *mName = "reset" ;
  142.    matFunc func( mName ) ; debugInfo( func ) ;
  143.    if ( ip != 0 ) {
  144.       ip++ ;
  145.       delete ip ;
  146.    } // if
  147.    ip = new INDEX[n] ;
  148.    if ( ip == 0 )
  149.       errorExit( mName, NOMEM ) ;
  150.    ip-- ;
  151.    len = n ;
  152.    return *this ;
  153. } // indexArray reset
  154.  
  155. INDEX& indexArray::xelem( INDEX i ) M_CONST
  156. {
  157.    if ( i > length() )
  158.       errorExit( "xelem ", NRANG ) ;
  159.    return ip[i] ;
  160. } // indexArray xelem
  161.  
  162. #include <stdarg.h>
  163.  
  164. indexArray& indexArray::assign( INDEX n, ... )
  165. {
  166.    static char *mName = "assign" ;
  167.    matFunc func( mName ) ; debugInfo( func ) ;
  168.    if ( n != length() )
  169.       reset(n) ;
  170.    va_list ap ;
  171.    va_start( ap, n ) ;
  172.    INDEX i ;
  173.    for ( i = 1 ; i <= n ; i++ )
  174.       elem(i) = va_arg( ap , INDEX ) ;
  175.    va_end( ap ) ;
  176.    return *this ;
  177. } // indexArray::assign
  178.  
  179. void indexArray::operator = ( indexArray& array )
  180. {
  181.    static char *mName = "op =" ;
  182.    matFunc func( mName ) ; debugInfo( func ) ;
  183.    INDEX i, len = array.length() ;
  184.    if ( length() != len )
  185.       reset( len ) ;
  186.    for ( i = 1 ; i <= len ; i++ )
  187.       elem(i) = array(i) ;
  188.    return ;
  189. } // matrix = indexArray
  190.  
  191. outFile& indexArray::put( outFile& f ) M_CONST
  192. {
  193.    static char *mName = "put" ;
  194.    matFunc func( mName ) ; debugInfo( func ) ;
  195.    INDEX i, len = length() ;
  196.    int format = matFormat() ;
  197.    if ( format & DISPLAY ) {
  198.       INDEX oldWidth = matField( 7 ) ;
  199.       f.newLine() ;
  200.       f.put( "Element" ).put( "Value" ).newLine() ;
  201.       f.put( "-------" ).put( "-----" ).newLine() ;
  202.       for ( i = 1 ; i <= len ; i++ ) {
  203.          f.putIndex(i) ;
  204.          f.putIndex( elem(i) ).newLine() ;
  205.       } // for
  206.       f.newLine() ;
  207.       matField( oldWidth ) ;
  208.       return f  ;
  209.    } else {
  210.       int cp, n = 0;
  211.       cp = ( matPageWidth() / ( matField() + 2) ) - 1;
  212.       for ( i = 1; i <= len ; i++ ) {
  213.          f.putIndex(elem(i));
  214.          if ( ++n >= cp ) {
  215.             f.newLine();
  216.             n = 0;
  217.          } // if
  218.       } // for
  219.       f.newLine();
  220.       return f ;
  221.    } // else
  222. } // indexArray put
  223.  
  224. outFile& indexArray::print( char* label, outFile& f ) M_CONST
  225. {
  226.    static char *mName = "print" ;
  227.    matFunc func( mName ) ; debugInfo( func ) ;
  228.    f.newLine() ;
  229.    if ( label != 0 )
  230.       f.write( label ).newLine() ;
  231.    int oldform = matFormat(DISPLAY) ;
  232.    put( f ) ;
  233.    matFormat( oldform ) ;
  234.    return f ;
  235. } // indexArray print
  236.  
  237. indexArray& indexArray::read( void )
  238. {
  239.    static char *mName = "read" ;
  240.    matFunc func( mName ) ; debugInfo( func ) ;
  241.    errout.newLine().write( "Input index array with " ) ;
  242.    errout.writeIndex( length() ).write( " elements : " ) ;
  243.    errout.newLine() ;
  244.    get( in ) ;
  245.    errout.write( "Ok.").newLine() ;
  246.    return *this ;
  247. } // indexArray read
  248.  
  249. outFile& indexArray::info( outFile& f ) M_CONST
  250. {
  251.    if ( matListCtrl() > 3 )
  252.       return f ;
  253.    objectInfo( f ) ;
  254.    putName( nm.array(), f ) ;
  255.    putName( "indexAr", f ) ;
  256.    putField( length(), f ) ;
  257.    f.newLine() ;
  258.    return f ;
  259. } // indexArray::info
  260.  
  261. #ifdef __cplusplus
  262. matrix::matrix( indexArray& index ) : matObject()
  263. #else
  264. matrix::matrix( indexArray& index ) : ()
  265. #endif
  266. {
  267.    static char *mName="matrix(ind)" ;
  268.    matFunc func( mName ) ;
  269.    INDEX i, len = index.length() ;
  270.    pm = new matMap( len, 1 ) ;
  271.    for ( i = 1 ; i <= len ; i++ )
  272.        mat(i) = REAL( index(i) ) ;
  273.    debugInfo( func ) ;
  274. } // matrix( indexArray )
  275.  
  276. indexArray& matrix::toIndex( indexArray& index )
  277. {
  278.    static char *mName = "index=mat" ;
  279.    matFunc func( mName ) ; debugInfo( func ) ;
  280.    if ( nCols() != 1 )
  281.       errorExit( mName, NOTVC ) ;
  282.    INDEX n = nRows() ;
  283.    if ( index.length() != n )
  284.       index.reset(n) ;
  285.    for ( INDEX i = 1 ; i <= n ; i++ )
  286.       index(i) = (INDEX) ( mat(i) ) ;
  287.    return index ;
  288. } // matrix :: toIndex
  289.  
  290. void matrix::operator = ( indexArray& array )
  291. {
  292.    static char *mName = "matrix=ind" ;
  293.    matFunc func( mName ) ; debugInfo( func ) ;
  294.    INDEX i, len = array.length() ;
  295.    reset( len, 1 ) ;
  296.    for ( i = 1 ; i <= len ; i++ )
  297.       mat(i) = REAL( array(i) ) ;
  298.    return ;
  299. } // matrix = indexArray
  300.  
  301. /**********************************************************/
  302. /*  Methods to select elements for rows and columns       */
  303. /**********************************************************/
  304.  
  305. matrix& matrix::rowSelectOf( const matrix& x,
  306.                              const indexArray& cindex )
  307. {
  308.    static char *mName = "rowSelectOf" ;
  309.    matFunc func( mName ) ; debugInfo( func ) ;
  310.    INDEX nr = cindex.length(), i, j, xnr = x.nRows(),
  311.          xnc = x.nCols() ;
  312.    if ( nr != xnr ) {
  313.       error( mName, NEDIM ) ;
  314.       errorExit( mName, NEDIM ) ;
  315.    } // if
  316.    reset( nr ) ;
  317.    for ( i = 1; i <= nr ; i++ ) {
  318.       j = cindex(i) ;
  319.       if ( ( j < 1 ) || ( j > xnc ) ) {
  320.          errorij( j, 1 ) ;
  321.          errorExit( mName, NRANG ) ;
  322.       } // if
  323.       mat(i) = x(i,j) ;
  324.    } // for i
  325.    return  *this ;
  326. } // matrix::rowSelectOf
  327.  
  328. matrix& matrix::colSelectOf( const matrix& x,
  329.                             const indexArray& rindex )
  330. {
  331.    static char *mName = "colSelectOf" ;
  332.    matFunc func( mName ) ; debugInfo( func ) ;
  333.    INDEX nr = rindex.length(), i, j, xnr = x.nRows(),
  334.          xnc = x.nCols() ;
  335.    if ( nr != xnc ) {
  336.       error( mName, NEDIM ) ;
  337.       errorExit( mName, NEDIM ) ;
  338.    } // if
  339.    reset( nr ) ;
  340.    if ( nr != xnc ) {
  341.       error( mName, NEDIM ) ;
  342.       errorExit( mName, NEDIM ) ;
  343.    } // if
  344.    for ( j = 1; j <= nr ; j++ ) {
  345.       i = rindex(j) ;
  346.       if ( ( i < 1 ) || ( i > xnr ) ) {
  347.          errorij( i, 1 ) ;
  348.          errorExit( mName, NRANG ) ;
  349.       } // if
  350.       mat(j) = x(i,j) ;
  351.    } // for j
  352.    return *this ;
  353. } // matrix::colSelect
  354.  
  355. matrix& matrix::subOf( const matrix& x, const indexArray& rmap,
  356.                       const indexArray& cmap )
  357. {
  358.    static char *mName = "sub(maps)" ;
  359.    matFunc func( mName ) ; debugInfo( func ) ;
  360.    INDEX nc = cmap.length(), nr = rmap.length(), i, j;
  361.    INDEX xnr = x.nRows(), xnc = x.nCols(), xi, xj ;
  362.    reset( nr, nc ) ;
  363.    for ( i = 1 ; i <= nr; i++ ) {
  364.       xi = rmap(i) ;
  365.       if ( ( xi < 1 ) || ( xi > xnr ) )
  366.          errorExit( mName, NRANG ) ;
  367.       for ( j = 1; j <= nc ; j++ ) {
  368.          xj = cmap(j) ;
  369.          if ( ( xj < 1 ) || ( xj > xnc ) )
  370.             errorExit( mName, NRANG ) ;
  371.          mat(i,j) = x( xi, xj ) ;
  372.       } // for j
  373.    } // for i
  374.    return *this ;
  375. } // matrix::subOf(maps)
  376.  
  377. matrix& matrix::rowsOf( const matrix& x, const indexArray& map )
  378. {
  379.    static char *mName = "rows" ;
  380.    matFunc func( mName ) ; debugInfo( func ) ;
  381.    INDEX n = map.length(), nr = x.nRows(), nc = x.nCols(), i, j, k ;
  382.    reset( n, nc ) ;
  383.    for ( i = 1 ; i <= n; i++ ) {
  384.       k = map(i) ;
  385.       if ( ( k < 1 ) || ( k > nr ) )
  386.          errorExit( mName, NRANG ) ;
  387.       for ( j = 1; j <= nc ; j++ )
  388.          mat(i,j) = x(k,j) ;
  389.    } // for  i
  390.    return *this ;
  391. } // matrix::rowsOf
  392.  
  393. matrix& matrix::colsOf( const matrix& x, const indexArray& map )
  394. {
  395.    static char *mName = "cols" ;
  396.    matFunc func( mName ) ; debugInfo( func ) ;
  397.    INDEX n = map.length(), nr = x.nRows(), nc = x.nCols(), i, j, k ;
  398.    reset( nr, n ) ;
  399.    for ( j = 1; j <= n ; j++ ) {
  400.       k = map(j) ;
  401.       if ( ( k < 1 ) || ( k > nc ) )
  402.          errorExit( mName, NRANG ) ;
  403.       for ( i = 1 ; i <= nr; i++ )
  404.          mat(i,j) = x(i,k) ;
  405.    } // for j
  406.    return *this ;
  407. } // matrix::colsOf
  408.