home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ANUMR5.ZIP / LUTEST.C < prev    next >
C/C++ Source or Header  |  1991-04-07  |  2KB  |  108 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. #include <conio.h>
  9. #include <dos.h>
  10.  
  11.  
  12. #include "anum.h"
  13. #include "sysio.h"
  14.  
  15. #define MAXDIM 5
  16.  
  17.  
  18. void affmat(int dim, double *matrix)
  19.  
  20.  
  21. {    int i,j;
  22.  
  23.     for (i=0;i<dim;i++)
  24.     {    for (j=0;j<dim;j++) printf("%8.4g ",*(matrix+i*dim+j));
  25.         putchar('\n');
  26.     }
  27. }
  28.  
  29.  
  30. void affvect(int dim, double *vect)
  31.  
  32.  
  33. {    int i;
  34.  
  35.     for (i=0;i<dim;)
  36.         printf("[%d] = %8.4g\n",++i,*(vect+i));
  37.  
  38. }
  39.  
  40.  
  41.  
  42. void results(int dim, double *vsol)
  43.  
  44. {    puts("Solution of A.X=B :");
  45.     affvect(dim,vsol);
  46.  
  47. }
  48.  
  49.  
  50.     
  51.  
  52. void main(void)
  53.  
  54.  
  55. {    double *mat_A, *vect_B, *vect_X, *mat_P, *mat_D;
  56.     int dim, i, j, errcode, nvect;
  57.  
  58.  
  59.     puts("Test program for matrix solving using LU algorithm");
  60.     puts("--------------------------------------------------\n");
  61.  
  62.     for (dim=2;dim<=MAXDIM;dim++)
  63.     {    mat_A=farcalloc(dim*dim,sizeof(double));
  64.         mat_D=farcalloc(dim*dim,sizeof(double));
  65.         mat_P=farcalloc(dim*dim,sizeof(double));
  66.  
  67.         vect_B=farcalloc(dim,sizeof(double));
  68.         vect_X=farcalloc(dim,sizeof(double));
  69.  
  70.         for (i=0; i<dim; i++)
  71.             for(j=0;j<dim;j++)
  72.                 *(mat_A+i*dim+j)=(double)(rand()%10)-5;
  73.  
  74.         printf("\nMatrix order %d\n",dim);
  75.         puts("Matrix A");
  76.         affmat(dim,mat_A);
  77.  
  78.         lu_decompose(dim, mat_A, mat_D, mat_P, &errcode);
  79.         SYSMSG(errcode,stderr);
  80.  
  81.         if (errcode==ENOERROR)
  82.             for (nvect=1; nvect<=dim; nvect++)
  83.             {    for (i=0; i<dim; i++)
  84.                     *(vect_B+i)=(double)(rand()%10)-5;
  85.      
  86.                 puts("\nVector B");
  87.                 affvect(dim, vect_B);
  88.  
  89.                 lu_solve(dim, mat_D, vect_B, mat_P,
  90.                         vect_X, &errcode);
  91.                 SYSMSG(errcode,stderr);
  92.  
  93.                 results(dim,vect_X);
  94.             }
  95.  
  96.         farfree(mat_A);
  97.         farfree(mat_P);
  98.         farfree(mat_D);
  99.         farfree(vect_X);
  100.         farfree(vect_B);
  101.         printf("Core left : %ld bytes.\n",farcoreleft());
  102.         delay(2000);
  103.     }
  104. }
  105.  
  106.  
  107.  
  108.