home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ANUMR5.ZIP / INVMAIN.C < prev    next >
C/C++ Source or Header  |  1991-05-25  |  2KB  |  86 lines

  1. /* OK */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <mem.h>
  7. #include <alloc.h>
  8.  
  9.  
  10. #include "anum.h"
  11. #include "sysio.h"
  12.  
  13. #define MAXDIM 5
  14.  
  15.  
  16. void affmat(int dim, double *matrix)
  17.  
  18.  
  19. {    int i,j;
  20.  
  21.     for (i=0;i<dim;i++)
  22.     {    for (j=0;j<dim;j++) printf("%10.4g ",*(matrix+i*dim+j));
  23.         putchar('\n');
  24.     }
  25. }
  26.  
  27.  
  28.  
  29. void results(int dim, double *invmat, int errcode)
  30.  
  31. {    printf("Error code : %d\nInverted matrix :\n",errcode);
  32.     affmat(dim,invmat);
  33.     printf("Core left : %ld bytes.\n",farcoreleft());
  34. }
  35.  
  36.  
  37.  
  38.  
  39. void main(void)
  40.  
  41.  
  42. {    double *matrix, *invmat, *product;
  43.     int dim, i, j, k, errcode;
  44.  
  45.  
  46.     puts("Test program for function inverse");
  47.     puts("---------------------------------\n");
  48.     for (dim=1;dim<=MAXDIM;dim++)
  49.     {    matrix=farcalloc(dim*dim,sizeof(double));
  50.         invmat=farcalloc(dim*dim,sizeof(double));
  51.         product=farcalloc(dim*dim,sizeof(double));
  52.  
  53.         for (i=0;i<dim;i++)
  54.             for(j=0;j<dim;j++)
  55.                 *(matrix+i*dim+j)=(double)(rand()%10)-5;
  56.  
  57.         printf("\nMatrix order %d\n",dim);
  58.         affmat(dim,matrix);
  59.  
  60.         inverse(dim,matrix,invmat,&errcode);
  61.                 SYSMSG(errcode,stderr);
  62.  
  63.         results(dim,invmat,errcode);
  64.  
  65.         if (errcode==ENOERROR)
  66.         {    for (i=0; i<dim; i++)
  67.                 for(j=0; j<dim; j++)
  68.                 {    *(product+i*dim+j)=0.0;
  69.                     for (k=0; k<dim; k++)
  70.                         *(product+i*dim+j) +=
  71.                           (*(matrix+i*dim+k))*
  72.                             (*(invmat+k*dim+j));
  73.                 }
  74.             affmat(dim,product);
  75.         }
  76.  
  77.         farfree(product);
  78.         farfree(matrix);
  79.         farfree(invmat);
  80.     }
  81. }
  82.  
  83.  
  84.  
  85.  
  86.