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

  1. /***************************************************/
  2. /* matsumm.c source for MatClass summary 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. #include <math.h>
  18.  
  19. /*************************************************/
  20. /*               Summary Methods                 */
  21. /*************************************************/
  22.  
  23. matrix& matrix::rowSumOf( const matrix& x, int opt )
  24. {
  25.    static char *mName = "rowSumOf" ;
  26.    matFunc func( mName ) ; debugInfo( func ) ;
  27.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  28.    DOUBLE sum ;
  29.    reset( nr ) ;
  30.    for ( i = 1; i <= nr; i++ ) {
  31.       sum = 0.0 ;
  32.       for ( j = 1; j <= nc; j++ ) {
  33.          if ( opt & ABS )
  34.             sum += fabs( (DOUBLE) x(i,j) ) ;
  35.          else
  36.             sum += (DOUBLE) x(i,j) ;
  37.       } // for j
  38.       mat(i) = (REAL) sum ;
  39.    } // for i
  40.    return *this ;
  41. } // matrix::rowSumOf
  42.  
  43. matrix matrix::rowSum( int opt ) M_CONST
  44. {
  45.    static char *mName = "rowSum" ;
  46.    matFunc func( mName ) ; debugInfo( func ) ;  
  47.    matrix z( nRows() ) ;
  48.    z.rowSumOf( *this, opt ) ;
  49.    return z ;
  50. } // matrix rowSum
  51.  
  52. matrix& matrix::colSumOf( const matrix& x, int opt )
  53. {
  54.    static char *mName = "colSumOf" ;
  55.    matFunc func( mName ) ; debugInfo( func ) ;
  56.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  57.    DOUBLE sum ;
  58.    reset( nc ) ;
  59.    for ( j = 1; j <= nc; j++ ) {
  60.       sum = 0.0 ;
  61.       for ( i = 1; i <= nr; i++ ) {
  62.          if ( opt & ABS )
  63.             sum += fabs( (DOUBLE) x(i,j) ) ;
  64.          else
  65.             sum += (DOUBLE) x(i,j) ;
  66.       } // for j
  67.       mat(j) = (REAL) sum ;
  68.    } // for i
  69.    return *this ;
  70. } // matrix::colSumOf
  71.  
  72. matrix matrix::colSum( int opt ) M_CONST
  73. {
  74.    static char *mName = "colSum" ;
  75.    matFunc func( mName ) ; debugInfo( func ) ;  
  76.    matrix z( nCols() ) ;
  77.    z.colSumOf( *this, opt ) ;
  78.    return z ;
  79. } // matrix colSum
  80.  
  81. matrix& matrix::rowMeanOf( const matrix& x, int opt ) 
  82. {
  83.    static char *mName = "rowMeanOf" ;
  84.    matFunc func( mName ) ; debugInfo( func ) ;
  85.    INDEX n = x.nCols() ;
  86.    if ( n == 0 )
  87.       errorExit( mName, NPDIM ) ;
  88.    rowSumOf( x, opt ) ;
  89.    *this /= REAL(n) ;
  90.    return *this ;
  91. } // matrix::rowMeanOf
  92.  
  93. matrix matrix::rowMean( int opt ) M_CONST
  94. {
  95.    static char *mName = "rowMean" ;
  96.    matFunc func( mName ) ; debugInfo( func ) ;  
  97.    matrix z( nRows() ) ;
  98.    z.rowMeanOf( *this, opt ) ;
  99.    return z ;
  100. } // matrix rowMean
  101.  
  102. matrix& matrix::colMeanOf( const matrix& x, int opt )
  103. {
  104.    static char *mName = "colMeanOf" ;
  105.    matFunc func( mName ) ; debugInfo( func ) ;
  106.    INDEX n = x.nRows() ;
  107.    if ( n == 0 )
  108.       errorExit( mName, NPDIM ) ;
  109.    colSumOf( x, opt ) ;
  110.    *this /= REAL(n) ;
  111.    return *this ;
  112. } // matrix::colMeanOf
  113.  
  114. matrix matrix::colMean( int opt ) M_CONST
  115. {
  116.    static char *mName = "colMean" ;
  117.    matFunc func( mName ) ; debugInfo( func ) ;  
  118.    matrix z( nCols() ) ;
  119.    z.colMeanOf( *this, opt ) ;
  120.    return z ;
  121. } // matrix colMean
  122.  
  123. matrix& matrix::rowSqrOf( const matrix& x )
  124. {
  125.    static char *mName = "rowSqrOf" ;
  126.    matFunc func( mName ) ; debugInfo( func ) ;
  127.    INDEX nr = x.nRows(), nc = x.nCols() , i, j ;
  128.    REAL r, sum ;
  129.    reset( nr ) ;   
  130.    for ( i = 1; i <= nr; i++ ) {
  131.       sum = 0.0 ;
  132.       for ( j = 1; j <= nc; j++ ) {
  133.      r = x(i,j) ;
  134.          sum += (DOUBLE) r * r ;
  135.       } // for j
  136.       mat(i) = (REAL) sum ;
  137.    } // for i
  138.    return *this ;
  139. } // matrix::rowSqrOf
  140.  
  141. matrix& matrix::colSqrOf( const matrix& x )
  142. {
  143.    static char *mName = "colSqrOf" ;
  144.    matFunc func( mName ) ; debugInfo( func ) ;
  145.    INDEX nr = x.nRows(), nc = x.nCols() , i, j ;
  146.    REAL r, sum ;
  147.    reset( nc ) ;
  148.    for ( j = 1; j <= nc; j++ ) {
  149.       sum = 0.0 ;
  150.       for ( i = 1; i <= nr; i++ ) {
  151.          r = x(i,j) ;
  152.          sum += (DOUBLE) r * r ;
  153.       } // for j
  154.       mat(j) = (REAL) sum ;
  155.    } // for j
  156.    return *this ;
  157. } // matrix::colSqrOf
  158.  
  159. matrix& matrix::rowSqDevOf( const matrix& x, const matrix& avg ) 
  160. {
  161.    static char *mName = "rowSqDevOf" ;
  162.    matFunc func( mName ) ; debugInfo( func ) ;
  163.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  164.    DOUBLE sum ;
  165.    REAL d, m ;
  166.    reset( nr ) ;
  167.    if ( avg.nRows() != nr )
  168.       errorExit( mName, NEDIM ) ;
  169.    for ( i = 1; i <= nr; i++ ) {
  170.       sum = 0.0 ;
  171.       m = avg(i) ;
  172.       for ( j = 1; j <= nc; j++ ) {
  173.          d = x(i,j) - m ;
  174.          sum += d * d ;
  175.       } // for j
  176.       mat(i) = (REAL) sum ;
  177.    } // for i
  178.    return *this ;
  179. } // matrix::rowSqDevOf
  180.  
  181. matrix& matrix::colSqDevOf( const matrix& x, const matrix& avg ) 
  182. {
  183.    static char *mName = "colSqDevOf" ;
  184.    matFunc func( mName ) ; debugInfo( func ) ;
  185.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  186.    DOUBLE sum ;
  187.    REAL d, m ;
  188.    reset( nc ) ;
  189.    if ( avg.nCols() != nc )
  190.       errorExit( mName, NEDIM ) ;
  191.    for ( j = 1; j <= nc; j++ ) {
  192.       sum = 0.0 ;
  193.       m = avg(j) ;
  194.       for ( i = 1; i <= nr; i++ ) {
  195.          d = x(i,j) - m ;
  196.          sum += (DOUBLE) d * d ;
  197.       } // for j
  198.       mat(j) = (REAL) sum ;
  199.    } // for i
  200.    return *this ;
  201. } // matrix::colSqDevOf
  202.  
  203. void matrix::colPlus( const matrix& m )
  204. {
  205.    static char *mName = "colPlus" ;
  206.    matFunc func( mName ) ; debugInfo( func ) ;
  207.    INDEX nr = nRows() , nc = nCols(), i, j ;
  208.    REAL r ;
  209.    if ( m.nRows() != nc )
  210.       errorExit( mName, NEDIM ) ;
  211.    for ( j = 1 ; j <= nc ; j++ ) {
  212.       r = m(j) ;
  213.       for ( i = 1 ; i <= nr ; i++ )
  214.      mat(i,j) += r ;
  215.    } // for j
  216. } // matrix::colPlus
  217.  
  218. void matrix::rowPlus( const matrix& m )
  219. {
  220.    static char *mName = "rowPlus" ;
  221.    matFunc func( mName ) ; debugInfo( func ) ;
  222.    INDEX nr = nRows() , nc = nCols(), i, j ;
  223.    REAL r ;
  224.    if ( m.nRows() != nr )
  225.       errorExit( mName, NEDIM ) ;
  226.    for ( i = 1 ; i <= nr ; i++ )
  227.       r = m(i) ;
  228.       for ( j = 1 ; j <= nc ; j++ ) {
  229.      mat(i,j) += r ;
  230.    } // for j
  231. } // matrix::rowPlus
  232.  
  233. void matrix::colMinus( const matrix& m )
  234. {
  235.    static char *mName = "colMinus" ;
  236.    matFunc func( mName ) ; debugInfo( func ) ;
  237.    INDEX nr = nRows() , nc = nCols(), i, j ;
  238.    REAL r ;
  239.    if ( m.nRows() != nc )
  240.       errorExit( mName, NEDIM ) ;
  241.    for ( j = 1 ; j <= nc ; j++ ) {
  242.       r = m(j) ;
  243.       for ( i = 1 ; i <= nr ; i++ )
  244.      mat(i,j) -= r ;
  245.    } // for j
  246. } // matrix::colMinus
  247.  
  248. void matrix::rowMinus( const matrix& m )
  249. {
  250.    static char *mName = "rowMinus" ;
  251.    matFunc func( mName ) ; debugInfo( func ) ;
  252.    INDEX nr = nRows() , nc = nCols(), i, j ;
  253.    REAL r ;
  254.    if ( m.nRows() != nr )
  255.       errorExit( mName, NEDIM ) ;
  256.    for ( i = 1 ; i <= nr ; i++ )
  257.       r = m(i) ;
  258.       for ( j = 1 ; j <= nc ; j++ ) {
  259.      mat(i,j) -= r ;
  260.    } // for j
  261. } // matrix::rowMinus
  262.