Linear Algebra and the C Language/a06j


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00b.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
#define    RC  RC3
#define    Cb   C1
/* ------------------------------------ */
int main(void)
{
double ta[RC*RC] ={ 4,2,3,
                   5,3,1,
                   8,2,2};
                   
double tb[RC*Cb] ={ 3,
                    2,
                    1};  
                                         
double **A   = ca_A_mR(ta,i_mR(RC,RC));
double **b   = ca_A_mR(tb,i_mR(RC,Cb));	
	
double **InvA = inv_mR(A,      i_mR(RC,RC));
double **X    = mul_mR(InvA,b, i_mR(RC,Cb));

  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:");
  p_mR(A, S5,P0,C7);
  printf(" b:");
  p_mR(b, S5,P0,C7);
  stop();

  clrscrn();
  printf(" inv(A):");
  pE_mR(InvA, S1,P3,C7);
  printf(" X = inv(A) b");
  p_mR(X, S13,P4,C7);
  stop();

  clrscrn();
  printf("  b:");
  p_mR(b, S5,P0,C7);
  printf(" AX:");
  p_mR(mul_mR(A,X,b), S5,P0,C7);
  stop();
  
  f_mR(X);
  f_mR(b);
  f_mR(InvA);
  f_mR(A);

  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 :

   +4    +2    +3 
   +5    +3    +1 
   +8    +2    +2 

 B :

   +3 
   +2 
   +1 

 Press return to continue. 


 inv(A) :

-1.333e-01 -6.667e-02 +2.333e-01 
+6.667e-02 +5.333e-01 -3.667e-01 
+4.667e-01 -2.667e-01 -6.667e-02 

 X = inv(A) * B :

      -0.3000 
      +0.9000 
      +0.8000 

 Press return to continue. 


 B :

   +3 
   +2 
   +1 

  AX :

   +3 
   +2 
   +1 

 Press return to continue.