| [8ac6106] | 1 | /* http://web.mit.edu/10.001/Web/Course_Notes/NRC_Notes/Gauss_Jordan_NRC.HTML */
|
|---|
| 2 | /* Program Driver file for gaussj.c routine */
|
|---|
| 3 | /* Driver for routine for gaussj.c */
|
|---|
| 4 | /* Program gauss_driver.c */
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include "nr.h"
|
|---|
| 8 | #include "nrutil.h" /* Utilities program, listed in Appendix B of NRC book */
|
|---|
| 9 | /* NRC stands for Numerical Recipes in C */
|
|---|
| 10 |
|
|---|
| 11 | #define MAXSTR 80
|
|---|
| 12 |
|
|---|
| 13 | int main(void)
|
|---|
| 14 | {
|
|---|
| 15 | int j,k,l,m,n,NP,MP;
|
|---|
| 16 | float **a,**ai,**u,**b,**x,**t;
|
|---|
| 17 | char dummy[MAXSTR];
|
|---|
| 18 | FILE *fp;
|
|---|
| 19 | /*
|
|---|
| 20 | * a is the coefficient matrix.
|
|---|
| 21 | * ai=a before gaussj is called, ai = inverse(a) after the function call.
|
|---|
| 22 | * u = ai*a, we define this to test the program,
|
|---|
| 23 | * if correct u should be the unit matrix.
|
|---|
| 24 | * b = matrix of dimension n*m where m is the number of r.h.s. vectors for which
|
|---|
| 25 | * you want to solve A.x = b.
|
|---|
| 26 | * x = b before gaussj is called, x is the solution vector after gaussj is called.
|
|---|
| 27 | * t is an n*m matrix defined to test the solution.
|
|---|
| 28 | */
|
|---|
| 29 | /*
|
|---|
| 30 | * Read NP to allocate space for the matrices.
|
|---|
| 31 | */
|
|---|
| 32 | printf("Input the dimension of the largest square matrix to be used\n");
|
|---|
| 33 | scanf("%d",&NP);
|
|---|
| 34 | printf("Input the maximum number of r.h.s. vectors\n");
|
|---|
| 35 | scanf("%d",&MP);
|
|---|
| 36 |
|
|---|
| 37 | a=matrix(1,NP,1,NP); /* These commands have the same function as calloc */
|
|---|
| 38 | ai=matrix(1,NP,1,NP);/* They are provided by the utility programs in NRC */
|
|---|
| 39 | u=matrix(1,NP,1,NP);
|
|---|
| 40 | b=matrix(1,NP,1,MP);
|
|---|
| 41 | x=matrix(1,NP,1,MP);
|
|---|
| 42 | t=matrix(1,NP,1,MP);
|
|---|
| 43 |
|
|---|
| 44 | if ((fp = fopen("gaussj.dat","r")) == NULL)
|
|---|
| 45 | nrerror("Data file gaussj.dat not found\n");
|
|---|
| 46 | /* See a typical data file appended */
|
|---|
| 47 | while (!feof(fp)) {
|
|---|
| 48 | fgets(dummy,MAXSTR,fp);
|
|---|
| 49 | fgets(dummy,MAXSTR,fp);
|
|---|
| 50 | fscanf(fp,"%d %d ",&n,&m);
|
|---|
| 51 | fgets(dummy,MAXSTR,fp);
|
|---|
| 52 | for (k=1;k<=n;k++)
|
|---|
| 53 | for (l=1;l<=n;l++) fscanf(fp,"%f ",&a[k][l]);
|
|---|
| 54 | fgets(dummy,MAXSTR,fp);
|
|---|
| 55 | for (l=1;l<=m;l++)
|
|---|
| 56 | for (k=1;k<=n;k++) fscanf(fp,"%f ",&b[k][l]);
|
|---|
| 57 | /* save matrices for later testing of results */
|
|---|
| 58 | for (l=1;l<=n;l++) {
|
|---|
| 59 | for (k=1;k<=n;k++) ai[k][l]=a[k][l];
|
|---|
| 60 | for (k=1;k<=m;k++) x[l][k]=b[l][k];
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /* Call gaussj: note that after the call, a is replaced by its
|
|---|
| 64 | inverse and b is replaced by the solution vector */
|
|---|
| 65 | /*--------------------------------------------------------------------- */
|
|---|
| 66 | gaussj(ai,n,x,m);
|
|---|
| 67 | /*--------------------------------------------------------------------- */
|
|---|
| 68 | printf("\nInverse of matrix a : \n");
|
|---|
| 69 | for (k=1;k<=n;k++) {
|
|---|
| 70 | for (l=1;l<=n;l++) printf("%12.6f",ai[k][l]);
|
|---|
| 71 | printf("\n");
|
|---|
| 72 | }
|
|---|
| 73 | /* check inverse */
|
|---|
| 74 | printf("\na times a-inverse:\n");
|
|---|
| 75 | for (k=1;k<=n;k++) {
|
|---|
| 76 | for (l=1;l<=n;l++) {
|
|---|
| 77 | u[k][l]=0.0;
|
|---|
| 78 | for (j=1;j<=n;j++)
|
|---|
| 79 | u[k][l] += (a[k][j]*ai[j][l]);
|
|---|
| 80 | }
|
|---|
| 81 | for (l=1;l<=n;l++) printf("%12.6f",u[k][l]);
|
|---|
| 82 | printf("\n");
|
|---|
| 83 | }
|
|---|
| 84 | /* check vector solutions */
|
|---|
| 85 | printf("\nCheck the following for equality:\n");
|
|---|
| 86 | printf("%21s %14s\n","original","matrix*sol'n");
|
|---|
| 87 | for (l=1;l<=m;l++) {
|
|---|
| 88 | printf("vector %2d: \n",l);
|
|---|
| 89 | for (k=1;k<=n;k++) {
|
|---|
| 90 | t[k][l]=0.0;
|
|---|
| 91 | for (j=1;j<=n;j++)
|
|---|
| 92 | t[k][l] += (a[k][j]*x[j][l]);
|
|---|
| 93 | printf("%8s %12.6f %12.6f\n"," ",
|
|---|
| 94 | b[k][l],t[k][l]);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | fclose(fp);
|
|---|
| 99 | free_matrix(t,1,NP,1,MP);
|
|---|
| 100 | free_matrix(x,1,NP,1,MP);
|
|---|
| 101 | free_matrix(b,1,NP,1,MP);
|
|---|
| 102 | free_matrix(u,1,NP,1,NP);
|
|---|
| 103 | free_matrix(ai,1,NP,1,NP);
|
|---|
| 104 | free_matrix(a,1,NP,1,NP);
|
|---|
| 105 | return 0;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|