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

  1. /**************************************************/
  2. /*  matmath.c source for transcendental 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 "matmath.hpp"
  17.  
  18. #include <math.h>
  19.  
  20. /***********************************************/
  21. /*            Transcendental Functions         */
  22. /***********************************************/
  23.  
  24. enum transcendental {
  25.    LOG, LOG10, EXP, SIN, COS, TAN, ASIN, ACOS, ATAN,
  26.    SINH, COSH, TANH
  27. } ; // transcendental
  28.  
  29. static char* transcNames[] = {
  30.    "Log", "Log10", "Exp",
  31.    "Sin", "Cos", "Tan",
  32.    "ArcSin", "ArcCos", "ArcTan",
  33.    "Sinh", "Cosh", "Tanh"
  34. } ;
  35.  
  36. void transcend( matrix& z, const matrix& x, int fn )
  37. {
  38.    static char *mName = "transcend" ;
  39.    matFunc func( mName ) ; x.debugInfo( func ) ;
  40.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  41.    z.reset( nr, nc ) ;
  42.    int error ;
  43.    REAL eps = matrixEps(), maxExp = -log( eps ) ;
  44.    // do tests ---- these need more work !!!
  45.    switch ( fn ) {
  46.       case LOG :
  47.       case LOG10 :
  48.          error = x.compare( eps ) != GREATER ;
  49.          break ;
  50.       case EXP :
  51.       case SINH :
  52.       case COSH :
  53.       case TANH :
  54.          error = x.compare( maxExp ) != LESS || 
  55.                  x.compare( -maxExp ) != GREATER ;
  56.          break ;
  57.       case ASIN :
  58.       case ACOS :
  59.          error = x.compare( 1.0 ) & GREATER  ||
  60.                  x.compare( -1.0 ) & LESS ;
  61.          break ;
  62.       case TAN :     // ????
  63.       default :
  64.          error = FALSE ;
  65.          break ;
  66.    } // switch
  67.    if ( error )
  68.       x.errorExit( transcNames[ fn ], TRANS ) ;
  69.    /*****************************/
  70.    /* Loop can be optimised if  */
  71.    /* willing to exploit col    */
  72.    /* major form of matrices    */
  73.    /*****************************/
  74.    for ( j = 1 ; j <= nc ; j++ ) {
  75.       switch ( fn ) {
  76.          case LOG :
  77.             for ( i = 1 ; i <= nr ; i++ )
  78.                z(i,j) = (REAL) log( x(i,j) ) ;
  79.             break ;
  80.          case LOG10 :
  81.             for ( i = 1 ; i <= nr ; i++ )
  82.                z(i,j) = (REAL) log10( x(i,j) ) ;
  83.             break ;
  84.          case EXP :
  85.             for ( i = 1 ; i <= nr ; i++ )
  86.                z(i,j) = (REAL) exp( x(i,j) ) ;
  87.             break ;
  88.          case SIN :
  89.             for ( i = 1 ; i <= nr ; i++ )
  90.                z(i,j) = (REAL) sin( x(i,j) ) ;
  91.             break ;
  92.          case COS :
  93.             for ( i = 1 ; i <= nr ; i++ )
  94.                z(i,j) = (REAL) cos( x(i,j) ) ;
  95.             break ;
  96.          case TAN :
  97.             for ( i = 1 ; i <= nr ; i++ )
  98.                z(i,j) = (REAL) tan( x(i,j) ) ;
  99.             break ;
  100.          case ASIN :
  101.             for ( i = 1 ; i <= nr ; i++ )
  102.                z(i,j) = (REAL) asin( x(i,j) ) ;
  103.             break ;
  104.          case ACOS :
  105.             for ( i = 1 ; i <= nr ; i++ )
  106.                z(i,j) = (REAL) acos( x(i,j) ) ;
  107.             break ;
  108.          case ATAN :
  109.             for ( i = 1 ; i <= nr ; i++ )
  110.                z(i,j) = (REAL) atan( x(i,j) ) ;
  111.             break ;
  112.          case SINH :
  113.             for ( i = 1 ; i <= nr ; i++ )
  114.                z(i,j) = (REAL) sinh( x(i,j) ) ;
  115.             break ;
  116.          case COSH :
  117.             for ( i = 1 ; i <= nr ; i++ )
  118.                z(i,j) = (REAL) cosh( x(i,j) ) ;
  119.             break ;
  120.          case TANH :
  121.             for ( i = 1 ; i <= nr ; i++ )
  122.                z(i,j) = (REAL) tanh( x(i,j) ) ;
  123.             break ;
  124.       } // switch
  125.    } // for i
  126.    return ;
  127. } // matrix transcend
  128.  
  129. #ifdef __cplusplus
  130.  
  131. matrix abs( matrix& x )
  132. {
  133.    static char *mName = "abs" ;
  134.    matFunc func( mName ) ; x.debugInfo( func ) ;
  135.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  136.    matrix z( nr, nc ) ;
  137.    for ( i = 1 ; i <= nr ; i++ )
  138.       for ( j = 1 ; j <= nc ; j++ )
  139.          z(i,j) = (REAL) fabs( x(i,j) ) ;
  140.    return z ;
  141. } // matrix abs
  142.  
  143.  
  144. matrix log( const matrix& x )
  145.    static char *mName = "log" ;
  146.    matFunc func( mName ) ; x.debugInfo( func ) ;
  147.    matrix z( x.nRows(), x.nCols() ) ;
  148.    transcend( z, x, LOG ) ;
  149.    return z ;
  150. } // log
  151.  
  152. matrix exp( const matrix& x )
  153.    static char *mName = "exp" ;
  154.    matFunc func( mName ) ; x.debugInfo( func ) ;
  155.    matrix z( x.nRows(), x.nCols() ) ;
  156.    transcend( z, x, EXP ) ;
  157.    return z ;
  158. } // exp
  159.  
  160. matrix log10( const matrix& x )
  161.    static char *mName = "log10" ;
  162.    matFunc func( mName ) ; x.debugInfo( func ) ;
  163.    matrix z( x.nRows(), x.nCols() ) ;
  164.    transcend( z, x, LOG10 ) ;
  165.    return z ;
  166. } // log10m
  167.  
  168. matrix ln( const matrix& x )
  169.    static char *mName = "ln" ;
  170.    matFunc func( mName ) ; x.debugInfo( func ) ;
  171.    matrix z( x.nRows(), x.nCols() ) ;
  172.    transcend( z, x, LOG ) ;
  173.    return z ;
  174. } // ln
  175.  
  176. matrix sin( const matrix& x )
  177.    static char *mName = "sin" ;
  178.    matFunc func( mName ) ; x.debugInfo( func ) ;
  179.    matrix z( x.nRows(), x.nCols() ) ;
  180.    transcend( z, x, SIN ) ;
  181.    return z ;
  182. } //sin
  183.  
  184. matrix cos( const matrix& x )
  185.    static char *mName = "cos" ;
  186.    matFunc func( mName ) ; x.debugInfo( func ) ;
  187.    matrix z( x.nRows(), x.nCols() ) ;
  188.    transcend( z, x, COS ) ;
  189.    return z ;
  190. } // cos
  191.  
  192. matrix tan( const matrix& x )
  193.    static char *mName = "tan" ;
  194.    matFunc func( mName ) ; x.debugInfo( func ) ;
  195.    matrix z( x.nRows(), x.nCols() ) ;
  196.    transcend( z, x, TAN ) ;
  197.    return z ;
  198. } // tan
  199.  
  200. matrix asin( const matrix& x )
  201.    static char *mName = "asin" ;
  202.    matFunc func( mName ) ; x.debugInfo( func ) ;
  203.    matrix z( x.nRows(), x.nCols() ) ;
  204.    transcend( z, x, ASIN ) ;
  205.    return z ;
  206. } // asinm
  207.  
  208. matrix acos( const matrix& x )
  209.    static char *mName = "acos" ;
  210.    matFunc func( mName ) ; x.debugInfo( func ) ;
  211.    matrix z( x.nRows(), x.nCols() ) ;
  212.    transcend( z, x, ACOS ) ;
  213.    return z ;
  214. } // acos
  215.  
  216. matrix atan( const matrix& x )
  217.    static char *mName = "atan" ;
  218.    matFunc func( mName ) ; x.debugInfo( func ) ;
  219.    matrix z( x.nRows(), x.nCols() ) ;
  220.    transcend( z, x, ATAN ) ;
  221.    return z ;
  222. } // atan
  223.  
  224. matrix sinh( const matrix& x )
  225.    static char *mName = "sinh" ;
  226.    matFunc func( mName ) ; x.debugInfo( func ) ;
  227.    matrix z( x.nRows(), x.nCols() ) ;
  228.    transcend( z, x, SINH ) ;
  229.    return z ;
  230. } // sinh
  231.  
  232. matrix cosh( const matrix& x )
  233.    static char *mName = "cosh" ;
  234.    matFunc func( mName ) ; x.debugInfo( func ) ;
  235.    matrix z( x.nRows(), x.nCols() ) ;
  236.    transcend( z, x, COSH ) ;
  237.    return z ;
  238. } // cosh
  239.  
  240. matrix tanh( const matrix& x )
  241.    static char *mName = "tanh" ;
  242.    matFunc func( mName ) ; x.debugInfo( func ) ;
  243.    matrix z( x.nRows(), x.nCols() ) ;
  244.    transcend( z, x, TANH ) ;
  245.    return z ;
  246. } // tanh
  247.  
  248. #else
  249.  
  250. matrix absm( matrix& x )
  251. {
  252.    static char *mName = "absm" ;
  253.    matFunc func( mName ) ; x.debugInfo( func ) ;
  254.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  255.    matrix z( nr, nc ) ;
  256.    for ( i = 1 ; i <= nr ; i++ )
  257.       for ( j = 1 ; j <= nc ; j++ )
  258.          z(i,j) = (REAL) fabs( x(i,j) ) ;
  259.    return z ;
  260. } // matrix absm
  261.  
  262.  
  263. matrix logm( const matrix& x )
  264.    static char *mName = "log" ;
  265.    matFunc func( mName ) ; x.debugInfo( func ) ;
  266.    matrix z( x.nRows(), x.nCols() ) ;
  267.    transcend( z, x, LOG ) ;
  268.    return z ;
  269. } // log
  270.  
  271. matrix expm( const matrix& x )
  272.    static char *mName = "exp" ;
  273.    matFunc func( mName ) ; x.debugInfo( func ) ;
  274.    matrix z( x.nRows(), x.nCols() ) ;
  275.    transcend( z, x, EXP ) ;
  276.    return z ;
  277. } // exp
  278.  
  279. matrix log10m( const matrix& x )
  280.    static char *mName = "log10" ;
  281.    matFunc func( mName ) ; x.debugInfo( func ) ;
  282.    matrix z( x.nRows(), x.nCols() ) ;
  283.    transcend( z, x, LOG10 ) ;
  284.    return z ;
  285. } // log10m
  286.  
  287. matrix lnm( const matrix& x )
  288.    static char *mName = "ln" ;
  289.    matFunc func( mName ) ; x.debugInfo( func ) ;
  290.    matrix z( x.nRows(), x.nCols() ) ;
  291.    transcend( z, x, LOG ) ;
  292.    return z ;
  293. } // ln
  294.  
  295. matrix sinm( const matrix& x )
  296.    static char *mName = "sin" ;
  297.    matFunc func( mName ) ; x.debugInfo( func ) ;
  298.    matrix z( x.nRows(), x.nCols() ) ;
  299.    transcend( z, x, SIN ) ;
  300.    return z ;
  301. } //sin
  302.  
  303. matrix cosm( const matrix& x )
  304.    static char *mName = "cos" ;
  305.    matFunc func( mName ) ; x.debugInfo( func ) ;
  306.    matrix z( x.nRows(), x.nCols() ) ;
  307.    transcend( z, x, COS ) ;
  308.    return z ;
  309. } // cos
  310.  
  311. matrix tanm( const matrix& x )
  312.    static char *mName = "tan" ;
  313.    matFunc func( mName ) ; x.debugInfo( func ) ;
  314.    matrix z( x.nRows(), x.nCols() ) ;
  315.    transcend( z, x, TAN ) ;
  316.    return z ;
  317. } // tan
  318.  
  319. matrix asinm( const matrix& x )
  320.    static char *mName = "asin" ;
  321.    matFunc func( mName ) ; x.debugInfo( func ) ;
  322.    matrix z( x.nRows(), x.nCols() ) ;
  323.    transcend( z, x, ASIN ) ;
  324.    return z ;
  325. } // asinm
  326.  
  327. matrix acosm( const matrix& x )
  328.    static char *mName = "acos" ;
  329.    matFunc func( mName ) ; x.debugInfo( func ) ;
  330.    matrix z( x.nRows(), x.nCols() ) ;
  331.    transcend( z, x, ACOS ) ;
  332.    return z ;
  333. } // acos
  334.  
  335. matrix atanm( const matrix& x )
  336.    static char *mName = "atan" ;
  337.    matFunc func( mName ) ; x.debugInfo( func ) ;
  338.    matrix z( x.nRows(), x.nCols() ) ;
  339.    transcend( z, x, ATAN ) ;
  340.    return z ;
  341. } // atan
  342.  
  343. matrix sinhm( const matrix& x )
  344.    static char *mName = "sinh" ;
  345.    matFunc func( mName ) ; x.debugInfo( func ) ;
  346.    matrix z( x.nRows(), x.nCols() ) ;
  347.    transcend( z, x, SINH ) ;
  348.    return z ;
  349. } // sinh
  350.  
  351. matrix coshm( const matrix& x )
  352.    static char *mName = "cosh" ;
  353.    matFunc func( mName ) ; x.debugInfo( func ) ;
  354.    matrix z( x.nRows(), x.nCols() ) ;
  355.    transcend( z, x, COSH ) ;
  356.    return z ;
  357. } // cosh
  358.  
  359. matrix tanhm( const matrix& x )
  360.    static char *mName = "tanh" ;
  361.    matFunc func( mName ) ; x.debugInfo( func ) ;
  362.    matrix z( x.nRows(), x.nCols() ) ;
  363.    transcend( z, x, TANH ) ;
  364.    return z ;
  365. } // tanh
  366.  
  367. #endif
  368.  
  369.  
  370. matrix& matrix::roundOf( const matrix& x, truncation option )
  371. {
  372.    static char *mName = "roundOf" ;
  373.    matFunc func( mName ) ; debugInfo( func ) ;
  374.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  375.    if ( x.isNull() )
  376.       x.errorExit( mName, NULRF ) ;
  377.    reset( nr, nc ) ;
  378.    DOUBLE d, tmp ;
  379.    for ( i = 1 ; i <= nr ; i++ ) {
  380.       for ( j = 1 ; j <= nc ; j++ ) {
  381.          d = (DOUBLE) x(i,j) ;
  382.          switch ( option ) {
  383.             case ROUND :
  384.                tmp = floor( d ) ;
  385.                d = ( d - tmp > 0.5 ) ? tmp + 1.0 : tmp ;
  386.                break ;
  387.             case FIX :
  388.                d = ( d < 0 ) ? ceil( d ) : floor( d ) ;
  389.                break ;
  390.             case FLOOR :
  391.                d = floor( d ) ;
  392.                break ;
  393.             case CEIL :
  394.                d = ceil( d ) ;
  395.                break ;
  396.          } // switch
  397.          mat(i,j) = (REAL) d ;
  398.       } // for j
  399.    } // for i
  400.    return *this ;
  401. } // matrix roundOf
  402.  
  403. #ifdef __cplusplus
  404. matrix sign( const matrix& x )
  405. #else
  406. matrix signm( const matrix& x )
  407. #endif
  408. {
  409.    static char *mName = "signm(matrix)" ;
  410.    matFunc func( mName ) ; x.debugInfo( func ) ;
  411.    matrix z( x.nRows(), x.nCols() ) ;
  412.    z.signOf( x ) ;
  413.    return z ;
  414. } // sign
  415.  
  416. #ifdef __cplusplus
  417. matrix round( const matrix& x )
  418. #else
  419. matrix roundm( const matrix& x )
  420. #endif
  421. {
  422.    static char *mName = "roundm(matrix)" ;
  423.    matFunc func( mName ) ; x.debugInfo( func ) ;
  424.    matrix z( x.nRows(), x.nCols() ) ;
  425.    z.roundOf( x, ROUND ) ;
  426.    return z ;
  427. } // round
  428.  
  429. #ifdef __cplusplus
  430. matrix fix( const matrix& x )
  431. #else
  432. matrix fixm( const matrix& x )
  433. #endif
  434. {
  435.    static char *mName = "fixm(matrix)" ;
  436.    matFunc func( mName ) ; x.debugInfo( func ) ;
  437.    matrix z( x.nRows(), x.nCols() ) ;
  438.    z.roundOf( x, FIX ) ;
  439.    return z ;
  440. } //  fix
  441.  
  442. #ifdef __cplusplus
  443. matrix floor( const matrix& x )
  444. #else
  445. matrix floorm( const matrix& x )
  446. #endif
  447. {
  448.    static char *mName = "floorm(matrix)" ;
  449.    matFunc func( mName ) ; x.debugInfo( func ) ;
  450.    matrix z( x.nRows(), x.nCols() ) ;
  451.    z.roundOf( x, FLOOR ) ;
  452.    return z ;
  453. } // floor
  454.  
  455. #ifdef __cplusplus
  456. matrix ceil( const matrix& x )
  457. #else
  458. matrix ceilm( const matrix& x )
  459. #endif
  460. {
  461.    static char *mName = "ceilm(matrix)" ;
  462.    matFunc func( mName ) ; x.debugInfo( func ) ;
  463.    matrix z( x.nRows(), x.nCols() ) ;
  464.    z.roundOf( x, CEIL ) ;
  465.    return z ;
  466. } // ceil
  467.  
  468. matrix& matrix::remOf( const matrix& x, const matrix& y )
  469. {
  470.    char *mName = "remOf" ;
  471.    matFunc func( mName ) ; debugInfo( func ) ;
  472.    x.checkDims( y, mName ) ;
  473.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  474.    reset( nr, nc ) ;
  475.    DOUBLE d ;
  476.    for ( i = 1 ; i <= nr ; i++ ) {
  477.       for ( j = 1 ; j <= nc ; j++ ) {
  478.          if ( y(i,j) == 0 )
  479.             errorExit( mName, ZEROD ) ;
  480.          d = x(i,j) / y(i,j) ;
  481.          d = ( d < 0.0 ) ? ceil( d ) : ( ( d > 0.0 ) ? floor( d ) : d ) ;
  482.          mat(i,j) = x(i,j) - y(i,j) * d ;
  483.       } // for j
  484.    } // for j
  485.    return *this ;
  486. } // matrix remOf
  487.  
  488. matrix matrix::rem( const matrix& y ) M_CONST
  489. {
  490.    char *mName = "rem" ;
  491.    matFunc func( mName ) ; debugInfo( func ) ;
  492.    matrix z( nRows(), nCols() ) ;
  493.    z.remOf( *this, y ) ;
  494.    return z ;
  495. } // matrix::rem
  496.  
  497. matrix& matrix::signOf( const matrix& x )
  498. {
  499.    static char *mName = "signOf" ;
  500.    matFunc func( mName ) ; debugInfo( func ) ;
  501.    INDEX nr = x.nRows(), nc = x.nCols(), i, j ;
  502.    if ( x.isNull() )
  503.       x.errorExit( mName, NULRF ) ;
  504.    reset( nr, nc ) ;
  505.    REAL d ;
  506.    for ( i = 1 ; i <= nr ; i++ ) {
  507.       for ( j = 1 ; j <= nc ; j++ ) {
  508.          d = x(i,j) ;
  509.          mat(i,j) = ( d < 0.0 ) ? -1.0 : ( ( d > 0.0 ) ? 1.0 : 0.0 ) ;
  510.       } // for j
  511.    } // for j
  512.    return *this ;
  513. } // matrix signOf
  514.