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

  1. /**************************************************/
  2. /*     ludec.c source for luDec 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 "ludec.hpp"
  17.  
  18. /****************************************************/
  19. /*                  luDec methods                   */
  20. /****************************************************/
  21.  
  22.  
  23. #ifdef __cplusplus
  24. luDec::luDec( void ) : matDec()
  25. #else
  26. luDec::luDec( void ) : ()
  27. #endif
  28. {
  29.    indexp.name( "decInd" ) ;
  30. } // luDec
  31.  
  32.  
  33. #ifdef __cplusplus
  34. luDec::luDec( luDec& lu ) : matDec(lu)
  35. #else
  36. luDec::luDec( luDec& lu ) : (lu)
  37. #endif
  38. {
  39.    indexp = lu.indexp ;
  40.    sign = lu.sign ;
  41. } // luDec
  42.  
  43. #ifdef __cplusplus
  44. luDec::luDec( const matrix& x ) : matDec(x)
  45. #else
  46. luDec::luDec( const matrix& x ) : (x)
  47. #endif
  48. {
  49.    static char *mName = "luDec(x)" ;
  50.    matFunc func( mName ) ;
  51.    indexp.reset( x.nRows() ) ;
  52.    indexp.name( "decInd" ) ;
  53.    func.trace(TRUE) ; // debugInfo( func ) ;
  54. } // luDec( matrix& )
  55.  
  56. luDec::~luDec( void )
  57. {
  58.    static char *mName = "~luDec" ;
  59.    matFunc func( mName ) ; debugInfo( func ) ;
  60. } // ~luDec
  61.  
  62. void luDec::assign( const matrix& x )
  63. {
  64.    static char *mName = "assign" ;
  65.    matFunc func( mName ) ; debugInfo( func ) ;
  66.    matDec::assign(x) ;
  67.    indexp.reset( x.nRows() ) ;
  68. } // luDec::assign
  69.  
  70. void luDec::operator = ( const luDec& lu )
  71. {
  72.    static char *mName = "op = lu&" ;
  73.    matFunc func( mName ) ; debugInfo( func ) ;
  74.    matDec::operator = ( lu ) ;
  75.    indexp.reset( lu.m.nRows() ) ;
  76.    sign = lu.sign ;
  77. } // luDec = lu&
  78.  
  79. void luDec::capture( matrix& x )
  80. {
  81.    static char *mName = "capture" ;
  82.    matFunc func( mName ) ; debugInfo( func ) ;
  83.    matDec::capture(x) ;
  84.    indexp.reset( x.nRows() ) ;
  85. } // luDec::capture
  86.  
  87. void luDec::clear( void )
  88. {
  89.    static char* mName = "clear" ;
  90.    matFunc func( mName ) ; debugInfo( func ) ;
  91.    matDec::clear() ;
  92.    indexp.clear() ;
  93.    sign = 0.0 ;
  94. } // clear
  95.  
  96. void luDec::decompose( void )
  97. {
  98.    static char *mName = "decompose" ;
  99.    matFunc func( mName ) ; debugInfo( func ) ;
  100.    if ( !( status & ASSIGNED ) )
  101.       errorExit( mName, UNASS ) ;
  102.    if ( !( status & DECOMPOSED ) ) {
  103.       crout( m, indexp, sign, tol, error ) ;
  104.       status |= DECOMPOSED ;
  105.    } // if
  106. } // luDec::decompose
  107.  
  108. void luDec::lu( matrix& x, indexArray& index )
  109. {
  110.    static char *mName = "lu" ;
  111.    matFunc func( mName ) ; debugInfo( func ) ;
  112.    if ( ok( mName ) ) { 
  113.       x = m ;
  114.       index = indexp ;
  115.    } // if
  116.    return ;
  117. } // luDec::lu
  118.  
  119. void luDec::release( matrix& x, indexArray& index )
  120. {
  121.    static char *mName = "release" ;
  122.    matFunc func( mName ) ; debugInfo( func ) ;
  123.    if ( ok( mName ) ) { 
  124.       x.refer(m) ;
  125.       index = indexp ;
  126.       clear() ;
  127.    } // if
  128.    return ;
  129. } // luDec::release
  130.  
  131. void luDec::solve( matrix& b )
  132. {
  133.    static char *mName = "solve" ;
  134.    matFunc func( mName ) ; debugInfo( func ) ;
  135.    if ( ok( mName ) ) {
  136.       luSolve( m, indexp, b, tol, error ) ;
  137.       if ( error )
  138.          errorExit( mName, error ) ;
  139.    } // else
  140. } // luDec::solve
  141.  
  142. void luDec::transSolve( matrix& b )
  143. {
  144.    static char *mName = "transSolve" ;
  145.    matFunc func( mName ) ; debugInfo( func ) ;
  146.    if ( ok( mName ) ) {
  147.       lutSolve( m, indexp, b, tol, error ) ;
  148.       if ( error )
  149.      errorExit( mName, error ) ;
  150.    } // else
  151. } // luDec::transSolve
  152.  
  153. void luDec::det( REAL& d1, REAL& d2 )
  154. {
  155.    static char *mName = "det" ;
  156.    matFunc func( mName ) ; debugInfo( func ) ;
  157.    if ( !( status & DETERMINED ) ) {
  158.       matDec::det(d1,d2) ;
  159.       // sign det as calculated by above
  160.       det1 *= sign ;
  161.    } // if
  162.    d1 = det1 ;
  163.    d2 = det2 ;
  164. } // luDec::det
  165.  
  166. outFile& luDec::info( outFile& f ) M_CONST
  167. {
  168.    return matDec::decInfo( f, "luDec" ) ;
  169. } // luDec::info
  170.  
  171. outFile& luDec::put( outFile& f ) M_CONST
  172. {
  173.    static char *mName = "put" ;
  174.    matFunc func( mName ) ; debugInfo( func ) ;
  175.    matDec::put(f) ;
  176.    indexp.put(f) ;
  177.    f.putReal( sign ).newLine() ;
  178.    return f ;
  179. } // luDec::put
  180.  
  181. outFile& luDec::print( char* decName, outFile& f ) M_CONST
  182. {
  183.    static char *mName = "print" ;
  184.    matFunc func( mName ) ; debugInfo( func ) ;
  185.    f.write( "LU decomposition : " ) ;
  186.    if ( decName != 0 )
  187.       f.write( decName ) ;
  188.    f.newLine(2) ;
  189.    matDec::print( "LU", f ) ;
  190.    indexp.print( "Pivot Map", f ) ;
  191.    f.write( "Sign : " ).writeReal( sign ).newLine(2) ;
  192.    return f ;
  193. } // luDec::print
  194.  
  195. inFile& luDec::get( inFile& f )
  196. {
  197.    static char *mName = "get" ;
  198.    matFunc func( mName ) ; debugInfo( func ) ;
  199.    matDec::get(f) ;
  200.    indexp.reset( m.nRows() ) ;
  201.    indexp.get(f) ;
  202.    f.getReal( sign ) ;
  203.    return f ;
  204. } // luDec::get
  205.  
  206. matrix squareEqn( const matrix& x, const matrix& b )
  207. {
  208.    static char *mName = "squareEqn" ;
  209.    matFunc func( mName ) ; x.debugInfo( func ) ;
  210.    matrix z( b.nRows(), b.nCols() ) ;
  211.    z = b ;
  212.    luDec xlu( x ) ;
  213.    xlu.multiSolve( z ) ;
  214.    return z ;
  215. } // squareEqn
  216.  
  217. matrix operator / ( const matrix& b, const matrix& x )
  218. {
  219.    static char *mName = "matrix op /" ;
  220.    matFunc func( mName ) ; x.debugInfo( func ) ;
  221.    if ( x.nRows() == x.nCols() )
  222.       return squareEqn( x, b ) ;
  223.    else
  224.       return normalEqn( x, b ) ;
  225. } // matrix "projection" /
  226.  
  227. matrix matrix::inv( void )
  228. {
  229.    static char *mName = "inv" ;
  230.    matFunc func( mName ) ; debugInfo( func ) ;
  231.    matrix z( nRows(), nCols() ) ;
  232.    luDec xlu( *this ) ;
  233.    xlu.inverse( z ) ;
  234.    return z ;
  235. } // matrix inv
  236.  
  237.  
  238.  
  239.