Linear Algebra and the C Language/a06i


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00a.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
void fun(int rc)
{
double **A   =      i_mR(rc,rc);
double **Inv =      i_mR(rc,rc);
double **B   = r_mR(i_mR(rc,C1),999.);
double **X   =      i_mR(rc,C1);

 do
  {
   r_mR(A,999.);
   printf(".");
  }while(!det_R(A));


  clrscrn();
  printf(" We want to find X such as,   \n\n");
  printf("         AX = B               \n\n");
  printf(" If A is a square matrix and, \n\n");
  printf(" If A has an inverse matrix,  \n\n");
  printf(" you can find X by this method\n\n");
  printf("         X = inv(A) B       \n\n\n");
  printf(" To verify the result you can \n\n");
  printf(" multiply the matrix A by X.  \n\n");
  printf(" You must refind B.  \n\n");
  stop();

  clrscrn();
  printf(" A :\n");
  p_mR(A,S5,P0,C7);
  printf(" B :\n");
  p_mR(B,S5,P0,C7);
  stop();

  clrscrn();
  printf(" inv(A) :\n");
  pE_mR(inv_mR(A,Inv),S1,P3,C7);
  printf(" X = inv(A) * B :\n");
  p_mR(mul_mR(Inv,B,X),S13,P4,C7);
  stop();

  clrscrn();
  printf(" B :\n");
  p_mR(B,S5,P0,C7);
  printf("  AX :\n");
  p_mR(mul_mR(A,X,B),S5,P0,C7);

  f_mR(X);
  f_mR(B);
  f_mR(Inv);
  f_mR(A);
}
/* ------------------------------------ */
int main(void)
{
time_t t;

  srand(time(&t));
do
{
  fun(rp_I(RC6)+RC1);

} while(stop_w());

  return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */


Screen output example:

 We want to find X such as,   

         AX = B               

 If A is a square matrix and, 

 If A has an inverse matrix,  

 you can find X by this method

         X = inv(A) B       


 To verify the result you can 

 multiply the matrix A by X.  

 You must refind B.  

 Press return to continue. 


 A :

 -851  +472   +65   -48  +536  -411  +890 
 -679  -265  +995   -60  -557  +798  +609 
 -474  +863   +45  +678  -428  -548  +325 
 +708  +958  -657  +789  +415   -90  -566 
 -218  +366  +766  +735  -564  -356  -857 
 -392   -19  -115  +924  -636  -143  -999 
 -459  +556  +179   +88  +870  +192  +673 

 B :

 -287 
 +491 
 +722 
 -222 
 +971 
 +525 
 +263 


 inv(A) :

-1.017e-02 -4.423e-03 +5.716e-03 -5.982e-03 -8.544e-04 -3.336e-05 +8.520e-03 
+1.619e-02 +7.547e-03 -8.913e-03 +1.093e-02 +1.912e-03 -1.589e-03 -1.466e-02 
-5.317e-03 -2.292e-03 +2.782e-03 -3.529e-03 +3.579e-04 -2.087e-04 +4.940e-03 
-1.961e-02 -8.718e-03 +1.127e-02 -1.241e-02 -2.222e-03 +1.886e-03 +1.791e-02 
-5.296e-03 -2.842e-03 +2.492e-03 -3.578e-03 -3.260e-04 +4.865e-04 +5.670e-03 
+6.932e-03 +4.009e-03 -4.378e-03 +5.153e-03 +3.148e-04 -2.208e-04 -6.273e-03 
-1.146e-02 -4.972e-03 +7.075e-03 -7.395e-03 -1.635e-03 +5.326e-04 +1.022e-02 

 X = inv(A) * B :

      +7.5949 
     -12.6374 
      +4.7303 
     +15.7849 
      +4.1481 
      -5.7861 
      +8.9774 

 Press return to continue. 


 B :

 -287 
 +491 
 +722 
 -222 
 +971 
 +525 
 +263 

  AX :

 -287 
 +491 
 +722 
 -222 
 +971 
 +525 
 +263 


 Press   return to continue
 Press X return to stop