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

  1. /***************************************************/
  2. /* matcomp.c source for MatClass logical functions */
  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. /*                  Comparison Methods                  */
  20. /********************************************************/
  21.  
  22. matrix matrix::and( const matrix& y ) M_CONST
  23. {
  24.    static char *mName = "and" ;
  25.    matFunc func( mName ) ; debugInfo( func ) ;
  26.    checkDims( y, mName ) ;
  27.    INDEX nr = nRows(), nc = nCols() ;
  28.    matrix z( nr, nc ) ;
  29.    INDEX i, j ;
  30.    for ( j = 1 ; j <= nc ; j++ )
  31.       for ( i = 1 ; i <= nr ; i++ )
  32.          z(i,j) = (REAL) ( ( mat(i,j) != 0 ) && ( y(i,j) != 0 ) ) ;
  33.    return z ;
  34. } // matrix:and
  35.  
  36. matrix matrix::or( const matrix& y ) M_CONST
  37. {
  38.    static char *mName = "or" ;
  39.    matFunc func( mName ) ; debugInfo( func ) ;
  40.    checkDims( y, mName ) ;
  41.    INDEX nr = nRows(), nc = nCols() ;
  42.    matrix z( nr, nc ) ;
  43.    INDEX i, j ;
  44.    for ( j = 1 ; j <= nc ; j++ )
  45.       for ( i = 1 ; i <= nr ; i++ )
  46.          z(i,j) = (REAL) ( ( mat(i,j) != 0 ) || ( y(i,j) != 0 ) ) ;
  47.    return z ;
  48. } // matrix:or
  49.  
  50. matrix not( const matrix& x )
  51. {
  52.    static char *mName = "not" ;
  53.    matFunc func( mName ) ; x.debugInfo( func ) ;
  54.    INDEX nr = x.nRows(), nc = x.nCols() ;
  55.    matrix z( nr, nc ) ;
  56.    INDEX i, j ;
  57.    for ( j = 1 ; j <= nc ; j++ )
  58.       for ( i = 1 ; i <= nr ; i++ )
  59.          z(i,j) = (REAL) ( x(i,j) != 0 ) ;
  60.    return z ;
  61. } // matrix::not
  62.  
  63. matrix operator == ( const matrix& x, const matrix& y )
  64. {
  65.    static char *mName = "op ==" ;
  66.    matFunc func( mName ) ; x.debugInfo( func ) ;
  67.    x.checkDims( y, mName  ) ;
  68.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  69.    matrix z( nr, nc ) ;
  70.    for ( j = 1 ; j <= nc ; j++ )
  71.       for ( i = 1 ; i <= nr ; i++ )
  72.          z(i,j) = (REAL) ( x(i,j) == y(i,j) ) ;
  73.    return z ;
  74. } // matrix op ==
  75.  
  76. matrix operator > ( const matrix& x, const matrix& y )
  77. {
  78.    static char *mName = "op >" ;
  79.    matFunc func( mName ) ; x.debugInfo( func ) ;
  80.    x.checkDims( y, mName  ) ;
  81.    INDEX nr = x.nRows(), nc = x.nCols(),i, j ;
  82.    matrix z( nr, nc ) ;
  83.    for ( j = 1 ; j <= nc ; j++ )
  84.       for ( i = 1 ; i <= nr ; i++ )
  85.          z(i,j) = (REAL) ( x(i,j) > y(i,j) ) ;
  86.    return z ;
  87. } // matrix op >
  88.  
  89. matrix operator >= ( const matrix& x, const matrix& y )
  90. {
  91.    char *mName = "op >" ;
  92.    matFunc func( mName ) ; x.debugInfo( func ) ;
  93.    return not( y > x ) ;
  94. } // matrix op >=
  95.  
  96. matrix operator <  ( const matrix& x, const matrix& y )
  97. {
  98.    char *mName = "op >" ;
  99.    matFunc func( mName ) ; x.debugInfo( func ) ;
  100.    return ( y > x ) ;
  101. } // matrix op <
  102.  
  103. matrix operator <= ( const matrix& x, const matrix& y )
  104. {
  105.    char *mName = "op <=" ;
  106.    matFunc func( mName ) ; x.debugInfo( func ) ;
  107.    return not( x > y ) ;
  108. } // matrix op <=
  109.  
  110. matrix operator != ( const matrix& x, const matrix& y )
  111. {
  112.    static char *mName = "op <=" ;
  113.    matFunc func( mName ) ; x.debugInfo( func ) ;
  114.    return not( x == y ) ;
  115. } // matrix op !=
  116.  
  117. INDEX any( const matrix& x )
  118. {
  119.    static char *mName = "any" ;
  120.    matFunc func( mName ) ; x.debugInfo( func ) ;
  121.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  122.    INDEX zero = TRUE ;
  123.    for ( j = 1 ; zero && j <= nc; j++ )
  124.       for ( i = 1 ; zero && i <= nr ; i++ )
  125.          zero = ( x(i,j) == 0 ) ;
  126.    return !zero ;
  127. } // any()
  128.  
  129. INDEX all( const matrix& x )
  130. {
  131.    static char *mName = "all" ;
  132.    matFunc func( mName ) ; x.debugInfo( func ) ;
  133.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  134.    INDEX nonzero = TRUE ;
  135.    for ( j = 1 ; nonzero && j <= nc; j++ )
  136.       for ( i = 1 ; nonzero && i <= nr ; i++ )
  137.          nonzero = ( x(i,j) != 0 ) ;
  138.    return nonzero ;
  139. } // matrix::all()
  140.  
  141. comparison matrix::compare( const matrix& y ) M_CONST
  142. {
  143.    static char *mName = "compare" ;
  144.    matFunc func( mName ) ; debugInfo( func ) ;
  145.    checkDims( y, mName ) ;
  146.    INDEX nr = nRows(), nc = nCols(), i, j ;
  147.    int comp = 0 ;
  148.    REAL r, s ;
  149.    for ( j = 1 ; j <= nc ; j++ ) {
  150.       for ( i = 1 ; i <= nr ; i++ ) {
  151.          s = mat(i,j) ;
  152.          r = y(i,j) ;
  153.          if ( s < r )
  154.             comp |= LESS ;
  155.          else if ( s > r )
  156.             comp |= GREATER ;
  157.          else
  158.             comp |= EQUAL ;
  159.       } // for j
  160.    } // for i
  161.    return (comparison) comp ;
  162. } // compare matrix
  163.  
  164. matrix operator == ( const matrix& x, REAL r )
  165. {
  166.    static char *mName = "op == r" ;
  167.    matFunc func( mName ) ; x.debugInfo( func ) ;
  168.    INDEX nr = x.nRows(), nc = x.nCols() ;
  169.    matrix z( nr, nc ) ;
  170.    INDEX i, j ;
  171.    for ( j = 1 ; j <= nc ; j++ )
  172.       for ( i = 1 ; i <= nr ; i++ )
  173.          z(i,j) = (REAL) ( x(i,j) == r ) ;
  174.    return z ;
  175. } // matrix op == r
  176.  
  177. matrix operator > ( const matrix& x, REAL r )
  178. {
  179.    static char *mName = "op > r" ;
  180.    matFunc func( mName ) ; x.debugInfo( func ) ;
  181.    INDEX nr = x.nRows(), nc = x.nCols() ;
  182.    matrix z( nr, nc ) ;
  183.    INDEX i, j ;
  184.    for ( j = 1 ; j <= nc ; j++ )
  185.       for ( i = 1 ; i <= nr ; i++ )
  186.          z(i,j) = (REAL) ( x(i,j) > r ) ;
  187.    return z ;
  188. } // matrix op > r
  189.  
  190. matrix operator < ( const matrix& x, REAL r )
  191. {
  192.    static char *mName = "op < r" ;
  193.    matFunc func( mName ) ; x.debugInfo( func ) ;
  194.    INDEX nr = x.nRows(), nc = x.nCols() ;
  195.    matrix z( nr, nc ) ;
  196.    INDEX i, j ;
  197.    for ( j = 1 ; j <= nc ; j++ )
  198.       for ( i = 1 ; i <= nr ; i++ )
  199.          z(i,j) = (REAL) ( x(i,j) < r ) ;
  200.    return z ;
  201. } // matrix op < r
  202.  
  203. matrix operator >= ( const matrix& x, REAL r )
  204. {
  205.    static char *mName = "op >= r" ;
  206.    matFunc func( mName ) ; x.debugInfo( func ) ;
  207.    return  not( x < r ) ;
  208. } // matrix op >= r
  209.  
  210. matrix operator <= ( const matrix& x, REAL r )
  211. {
  212.    static char *mName = "op <= r" ;
  213.    matFunc func( mName ) ; x.debugInfo( func ) ;
  214.    return not( x > r ) ;
  215. } // matrix op <= r
  216.  
  217. matrix operator != ( const matrix& x, REAL r )
  218. {
  219.    static char *mName = "op != r" ;
  220.    matFunc func( mName ) ; x.debugInfo( func ) ;
  221.    return not( x == r ) ;
  222. } // matrix op != r
  223.  
  224. comparison matrix::compare( const REAL r ) M_CONST
  225. {
  226.    char *mName = "compare(r)" ;
  227.    matFunc func( mName ) ; debugInfo( func ) ;
  228.    INDEX nr = nRows(), nc = nCols(), i, j ;
  229.    int comp = 0 ;
  230.    REAL s ;
  231.    for ( j = 1 ; j <= nc ; j++ ) {
  232.       for ( i = 1 ; i <= nr ; i++ ) {
  233.          s = mat(i,j) ;
  234.          if ( s < r )
  235.             comp |= LESS ;
  236.          else if ( s > r )
  237.             comp |= GREATER ;
  238.          else
  239.             comp |= EQUAL ;
  240.       } // for j
  241.    } // for i
  242.    return (comparison) comp ;
  243. } // compare REAL
  244.  
  245. INDEX matrix::countTrue( void )  M_CONST
  246. {
  247.    static char *mName = "countTrue" ;
  248.    matFunc func( mName ) ; debugInfo( func ) ;
  249.    INDEX n = 0, nr = nRows(), nc = nCols(), i, j ;
  250.    for ( j = 1 ; j <= nc ; j++ )
  251.       for ( i = 1; i <= nr; i++ )
  252.          if ( mat(i,j) != 0.0 ) ++n ;
  253.    return n ;
  254. } // countTrue
  255.  
  256. indexArray matrix::mapTrue( void )  M_CONST
  257. {
  258.    // needs rethinking !!!
  259.    static char *mName = "mapTrue" ;
  260.    matFunc func( mName ) ; debugInfo( func ) ;
  261.    if ( nCols() != 1 )
  262.       errorExit( mName , NOTVC ) ;
  263.    INDEX nr = nRows(), n = countTrue(), i, j = 0 ;
  264.    indexArray map( n ) ;
  265.    for ( i = 1; i <= nr && j < n ; i++ ) {
  266.       if ( mat(i,1) != 0.0 )
  267.          map(++j) = i ;
  268.    } // for
  269.    return map ;
  270. } // matrix::mapTrue
  271.