home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / matrixmath.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-18  |  4.6 KB  |  221 lines

  1. /*!
  2. \file matrixmath.cpp
  3. \author Karsten Schwenk
  4.  
  5. See header for description (matrixmath.h).
  6.  
  7. */
  8.  
  9. #include "matrixmath.h"
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <float.h>
  13.  
  14. #define SQUARE(x) ((x)*(x))
  15. #define EPSILON FLT_EPSILON
  16. #define EEQUAL(a,b) ( fabs(a-b)<EPSILON )
  17.  
  18. mat2x2_t id2x2={1,0,0,1};
  19. mat3x3_t id3x3={1,0,0,0,1,0,0,0,1};
  20. mat4x4_t id4x4={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
  21.  
  22. //int matinv;
  23.  
  24. /*!
  25. This function prints a matrix to stdout.
  26. \param a matrix that should be printed
  27. \param n dimension of \a a
  28. */
  29. void matrixPrint(matbase_t *a, int n){
  30.     int i,j;
  31.  
  32.     for(i=0;i<n;i++){
  33.         for(j=0;j<n; j++){
  34.             printf("%f ", a[i+j*n]);
  35.         }
  36.         printf("\n");
  37.     }
  38. }
  39.  
  40. /*!
  41. This function multiplies vector \a b by a matrix \a a and stores the resulting vector in r (r=ab).
  42. \param a matrix to multiply \a b by
  43. \param b vector to be multiplied by \a a
  44. \param n dimension of \a a and /a b
  45. \param r the resulting vector is returned here
  46. */
  47. void matrixMultVector(matbase_t *a, matbase_t *b, int n, float *r){
  48.     int i, j;
  49.  
  50.     float *t=new matbase_t[n];
  51.  
  52.     for(i=0; i<n; i++){
  53.         t[i] = 0.0;
  54.         for(j=0; j<n; j++)
  55.             t[i]+=a[i+j*n]*b[j];
  56.     }
  57.  
  58.     for(i=0;i<n;i++)
  59.         r[i]=t[i];
  60.  
  61.     delete[] t;
  62. }
  63.  
  64. /*!
  65. This function multiplies matrix \a a by a matrix \a b and stores the resulting vector in r (r=ab).
  66. This is used mostly to concatenate transformations.
  67. \param a matrix to multiply \a b by
  68. \param b vector to be multiplied by \a a
  69. \param n dimension of \a a and /a b
  70. \param r the resulting matrix is returned here
  71. */
  72. void matrixMultMatrix(matbase_t *a, matbase_t *b, int n, matbase_t *r){
  73.     int i, j, k;
  74.     float* ret=new matbase_t[n*n];
  75.     
  76.  
  77.     for(i=0; i<n; i++){
  78.         for(j=0; j<n; j++){
  79.             ret[i+j*n]=0;
  80.  
  81.             for(k=0;k<n;k++)
  82.                 ret[i+j*n]+=a[i+k*n]*b[k+j*n];
  83.         }
  84.     }
  85.     matrixCopy(ret, r, n);
  86.     delete[] ret;
  87. }
  88.  
  89. /*!
  90. This function copies matrix \a a into matrix \a b.
  91. \param a source matrix
  92. \param b destination matrix
  93. \param n dimension of \a a and \a b
  94. */
  95. void matrixCopy(matbase_t *a, matbase_t *b, int n){
  96.     for(int i=0;i<n*n;i++){
  97.         b[i]=a[i];
  98.     }
  99. }
  100.  
  101.  
  102.  
  103. /*!
  104. This function calculates the tranpose of matrix \a a and stores it in \a t.
  105. This is although a very fast way to calculate the inverse of a (nearly) orthonormal matrix. (For orthonormal matrices the inverse IS the tranpose)
  106. \param a matrix to be transposed
  107. \param n dimension of \a a
  108. \param t tranpose of \a a is returned here
  109. */
  110. void matrixTranspose(matbase_t *a, int n, matbase_t *t){
  111.     int i,j;
  112.  
  113.     for(i=0;i<n;i++){
  114.         for(j=0;j<n;j++){
  115.             t[i*n+j]=a[j*n+i];
  116.         }
  117.     }
  118. }
  119.  
  120.  
  121.  
  122. /*!
  123. This function calculates the inverse of matrix \a a and stores it in \a u.
  124. Since this uses LR-decomposisition and solves a complete system of linear equations it isn't really a cheap function zu call.
  125. If you have a orthonormal matrix you should use matrixTranspose() instead.
  126. \param a matrix to be inverted
  127. \param n dimension of \a a
  128. \param u inverse of \a a is returned here
  129. */
  130. void matrixInvert(matbase_t *a, int n, matbase_t *u){
  131. //    matrixPrint(a, n);
  132.     int i;
  133.     float *t=new matbase_t[n*n];
  134.     for(i=0; i<n*n; i++){
  135.         t[i]=a[i];
  136.     }
  137.     matrixLU(t, n);
  138. //    matrixPrint(t, n);
  139.     for(i=0; i<n; i++){
  140.         matrixSolve(t, &(id4x4[i*4]), n, &(u[i*n]));    //FIXME: nxn-E-mat
  141.     }
  142.  
  143. //    mat4x4_t test;
  144. //    matrixMultMatrix(a, u, 4, test);
  145. //    matrixPrint(test, 4);
  146.  
  147.     delete[] t;
  148. }
  149.  
  150.  
  151.  
  152. void matrixLU(matbase_t *a, int n){    // FIXME: irgendwie falsch!!!
  153.     int k,i,j;
  154. //    printf("Be!\n");
  155. //    matrixPrint(a, n);
  156.     for(k=0; k<n-1; k++){
  157.         if( fabs(a[k+k*n]) < EPSILON ){
  158.             //printf("AH!\n");
  159.             a[k+k*n] = EPSILON;
  160.         }
  161.  
  162.         for(i=k+1; i<n; i++){
  163.             a[i+k*n]=a[i+k*n]/a[k+k*n];
  164.             for(j=k+1; j<n; j++){
  165.                 a[i+j*n]=a[i+j*n]-a[i+k*n]*a[k+j*n];
  166.             }
  167.         }
  168.     }
  169. //    matrixPrint(a,n);
  170. //    printf("\n");
  171. }
  172.  
  173. void matrixSolveForward(matbase_t *a, matbase_t *b, int n, matbase_t *c){
  174.     int i,j;
  175.  
  176.     for(i=0; i<n; i++){
  177.         c[i]=b[i];
  178.         for(j=0; j<i; j++){
  179.             c[i]=c[i]-a[i+j*n]*c[j];
  180.         }
  181.     }
  182. }
  183.  
  184.  
  185. void matrixSolveBackward(matbase_t *a, matbase_t *c, int n, matbase_t *x){
  186.     int i,k;
  187.     float s;
  188.  
  189.     for(i=n-1; i>=0; i--){
  190.         s=c[i];
  191.         for(k=i+1; k<n; k++){
  192.             s=s-a[i+k*n]*x[k];
  193.         }
  194.         x[i]=s/a[i+i*n];
  195.     }
  196. }
  197.  
  198. /*!
  199. This functions solves the system of linear equations Ax=b.
  200. It uses matrixLR(), matrixSolveForward() and matrixSolveBackward().
  201. \param a matrix describing the linear equations
  202. \param b the vector b (right side)
  203. \param n dimension of \a a and \a b
  204. \param x the solution (if there is any) is returned here
  205. */
  206. void matrixSolve(matbase_t *a, matbase_t *b, int n, matbase_t *x){
  207.     float *c=new matbase_t[n];
  208.  
  209.     matrixSolveForward(a, b, n, c);
  210.     matrixSolveBackward(a, c, n, x);
  211.  
  212.     delete[] c;
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.