home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj8910.zip / LETTERS.LST < prev    next >
File List  |  1989-09-07  |  2KB  |  68 lines

  1. _LETTERS TO THE EDITOR_
  2.  
  3. [LISTING ONE]
  4.  
  5. /***********************************************************************/
  6. /* Determinant Calculator Using Near Heap and Upper Triangular Matrix  */
  7.  
  8. double det(arg,n)                 /* arg = array name, n = # elements */
  9. char *arg;
  10. int  n;
  11. {
  12.   register int i,t,k,p;
  13.   double   **a, ret, x;     /* Array, Return value of Det, Temp variable */
  14.   char     **sdim2();       /* defined in Listing Three */
  15.   int      * row;           /* row pointer for pivoting */
  16.  
  17.   /* dynamically create 2 dim "array " array a from arg */
  18.   a = (double **)sdim2(arg,n,n,sizeof(double));
  19.  
  20.   row = (int *)malloc(n*sizeof(int)); /* row pointer for pivoting allocation */
  21.   if (row == (int *) NULL) {
  22.     fprintf(stderr,"No heap space for Row Pointers for Pivoting ");
  23.     exit(1);
  24.   }
  25.  
  26.   /* creating upper triangular matrix with test for 0 values on diagonal */
  27.  
  28.   /* first initialize pointer vector */
  29.   for (i = 0; i < n ; i++)
  30.     row[i] = i ;
  31.  
  32.   /* find pivot elements */
  33.   for (p = 0; p < n - 1; p++) {
  34.     for (k = p + 1;k < n ;k++) {
  35.       if ( fabs(a[row[k]][p]) > fabs(a[row[p]][p]) ) { /* switch index */
  36.         t = row[p];
  37.         row[p] = row[k];
  38.         row[k] = t;
  39.       }
  40.       }
  41.       /* In usual application this would be an error message that the
  42.        * matrix is singular and the program would exit here          */
  43.       if ( a[row[p]][p] == 0 )  /* Determinant is 0 */ 
  44.         break;                  /* No need to continue on */
  45.  
  46.        for (k = p+1;k < n; k++) {
  47.         x = a[row[k]][p]/a[row[p]][p];
  48.           for ( i = p + 1; i < n ;i++)    /* do Gauss Jordan elimination */
  49.             a[row[k]][i] = a[row[k]][i] - x * a[row[p]][i];
  50.        }
  51.        }
  52.  
  53. /*   if ( a[row[n-1]][n-1] == 0 ) Determinant is 0 - This would in
  54.  *   normal application be a message Matrix is singular and an exit */
  55.  
  56.  /* value of determinant is product of diagonals of upper triangular matrix */
  57.   for (ret = 1,i = 0;i < n; i++)
  58.       ret *= a[row[i]][i];  /* if any of diagonals are 0 Det = 0 */
  59.  
  60.   free(row);
  61.   free(a);
  62.  
  63.   return ret;
  64. }
  65.  
  66.  
  67.  
  68.