matrix.c
Upload User: shhuayu888
Upload Date: 2013-03-28
Package Size: 1788k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Unix_Linux

  1. /****************************************************************************
  2.  File Name  : matrix.c
  3.  Purpose    : provides advanced matrix manipulation routines for IMAGE object
  4.  Release    : Version 1.0
  5.  Date     : Aug 31,1995
  6. GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang Technological
  7. University (NTU), Singapore. 
  8. These software programs are available to the user without any license or royalty fees. Permission is hereby granted to use, copy, modify, and distribute this software and its documentation for any purpose. ITI and NTU gives no warranty, express, implied, or statuary for the software and/or documentation provided, including, without limitation, waranty of merchantibility and warranty of fitness for a particular purpose. The software provided hereunder is on an "as is"  basis, and ITI and NTU has no obligation to provide maintenance, support, updates, enhancements, or modifications.
  9. GSNAKE API is available for any UNIX compatible system. User feedback, bugs, or software and manual suggestions should be sent via electronic mail to one of the following, who may or may not act on them as he/she desires :
  10. asschan@ntu.ac.sg
  11. kflai@iti.gov.sg
  12. ***************************************************************************/
  13. #include "gsnake.h"
  14. void MATRIX::dump(char *header)
  15. {
  16. if(header) printf("n<%s>n", header) ;
  17. IMAGE::print() ;
  18. }
  19. MATRIX::init(short _row, short _col, char identity)
  20. {
  21. reset() ;
  22. if( !(data = (float *) _iMemAlloc(_row*_col*sizeof(float))))
  23. return MEMORYERROR ;
  24. row = _row ;
  25. col = _col ;
  26. memset(data, 0, row*col*sizeof(float));
  27. if(identity) {
  28.      register short i;
  29.      for(i=0; i<row; i++)
  30.      put(i, i, 1);
  31. }
  32. return NOERROR ;
  33. }
  34. MATRIX * MATRIX::transpose(void)
  35. {
  36. MATRIX *out = new MATRIX ;
  37. if( out->init(col, row) != NOERROR )
  38. return NULL ;
  39. register short i, j ;
  40. for(i=0; i<row; i++)
  41.    for(j=0; j<col; j++)
  42.     out->put(j, i, get(i, j)) ;
  43. return out ;
  44. }
  45. MATRIX * MATRIX::operator+ (MATRIX& mtx)
  46. {
  47. MATRIX *out = new MATRIX;
  48. if(mtx.getRow() != row || mtx.getCol() != col)
  49. return NULL ;
  50. if( out->init(row, col) != NOERROR ) 
  51. return NULL;
  52. register short i, j ;
  53. for(i=0; i<row; i++)
  54.    for(j=0; j<col; j++)
  55.     out->put(i, j, get(i, j) + mtx.get(i, j) ) ;
  56. return out ;
  57. }
  58. MATRIX * MATRIX::operator- (MATRIX& mtx)
  59. {
  60. MATRIX *out = new MATRIX;
  61. if(mtx.getRow() != row || mtx.getCol() != col)
  62. return NULL ;
  63. if( out->init(row, col) != NOERROR ) 
  64. return NULL ;
  65. register short i, j ;
  66. for(i=0; i<row; i++)
  67.    for(j=0; j<col; j++)
  68.     out->put(i, j, get(i, j) - mtx.get(i, j) ) ;
  69. return out ;
  70. }
  71. MATRIX * MATRIX::operator* (MATRIX& mtx)
  72. {
  73. MATRIX *out = new MATRIX ;
  74. if(col != mtx.getRow() )
  75. return NULL ;
  76. if( out->init(row, mtx.getCol()) != NOERROR )
  77. return NULL ;
  78. register short i, j, k ;
  79. for(i=0; i<row; i++)
  80.    for(j=0; j<out->getCol(); j++) {
  81.     float sum = 0 ;
  82.     for(k=0; k<col; k++)
  83.     sum += get(i, k) * mtx.get(k, j) ;
  84.     out->put(i, j, sum) ;
  85.    }
  86. return out ;
  87. }
  88. MATRIX::swapRow(short i, short j) 
  89. {
  90. if( i != CAP(0, i, row-1) || j != CAP(0, j, row-1) )
  91. return PARAMERROR ;
  92. register short k  ;
  93. float temp ;
  94. for(k=0; k<col; k++) {
  95. temp = get(i, k) ;
  96. put(i, k, get(j, k) ) ;
  97. put(j, k, temp) ;
  98. }
  99. return NOERROR ;
  100. }
  101. MATRIX * MATRIX::inverse (void)
  102. {
  103. MATRIX *out = new MATRIX ;
  104. MATRIX *org = new MATRIX ;
  105. register short i, j, k;
  106. if( !row || row != col) {
  107.   fprintf(stderr, "ERROR <MATRIX::inverse()> : Not a Square Matrixn");
  108.   return NULL ;
  109. }
  110. if( out->init(row, col, 1) != NOERROR ||
  111.     org->init(row, col) != NOERROR ) {
  112.   fprintf(stderr, "ERROR <MATRIX::inverse()> : Memory Errorn");
  113.   return NULL ;
  114. }
  115. for(i=0; i<row; i++) /* duplicate original matrix */
  116. for(j=0; j<col; j++)
  117. org->put(i, j, get(i, j) );
  118. /* start from first column to the next */
  119. int pivotal ;
  120. for(i=0; i<row; i++) {
  121.     float div = org->get(i, i) ;
  122.     if( fabs(div) < 1e-10) { /* pivotal element too small */
  123.        for(j=i+1; j<row; j++) 
  124.            if( fabs(div = org->get(j, i)) > 1e-10 ) {
  125.             org->swapRow(i, j) ;
  126.             out->swapRow(i, j) ;
  127.             break ;
  128.            }
  129.        if( j == row) {
  130.              fprintf(stderr, 
  131.           "ERROR <MATRIX::inverse()> : Singular Matrixn");
  132.              return NULL ;
  133.        }
  134.     }
  135.     for(j=0; j<col; j++) {  /* divide entire row by pivotal element */
  136.         org->put(i, j, org->get(i, j)/div) ;
  137.         out->put(i, j, out->get(i, j)/div) ;
  138.     }
  139.     
  140.     for(k=0; k<row; k++) { /* row operation now */
  141.         if( k == i ) continue ;
  142.         
  143.         float mult = org->get(k, i) ;
  144.         if( fabs(mult) < 1e-10) continue ;
  145.         for(j=0; j<col; j++) {
  146.          org->put(k, j, org->get(k, j) - mult*org->get(i, j)) ;
  147.          out->put(k, j, out->get(k, j) - mult*out->get(i, j));
  148.         }
  149.     }
  150. }
  151. delete org ;
  152. return out ;
  153. }