Linear Algebra and the C Language/a09x


Install and compile this file in your working directory.

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

double **InvA = invgj_mR(A,      i_mR(r,r));
double **X    =   mul_mR(InvA,B, i_mR(r,Cb)) ;
double **T    =   mul_mR(A,X,    i_mR(r,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(" InvA:");
  pE_mR(InvA,S1,P3,C7);
  printf(" X = InvA B:");
  p_mR(X,S13,P4,C7);
  stop();

  clrscrn();
  printf("  B:");
  p_mR(B,S5,P0,C7);
  printf(" AX:");
  p_mR(T,S5,P0,C7);

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

  srand(time(&t));
do
{
  fun(rp_I(C2)+C4);

} 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.  


 A :
 +311  +927  +224  -208  -261 
 +906  -624  -417  +261   +91 
  +83  +955  +721  +767  -585 
 +840  +628  -226  +379    -2 
 +998  +169  -373  +968  -845 

 B :
 -534 
 +773 
  -19 
 -165 
 -157 


 inv(A) :
+8.492e-04 +1.361e-03 +2.837e-04 -2.492e-04 -3.116e-04 
+9.374e-06 -9.298e-04 -2.145e-04 +9.692e-04 +4.318e-05 
+6.680e-04 +1.687e-03 +1.329e-03 -1.078e-03 -9.425e-04 
-1.505e-03 -4.684e-04 +5.209e-04 +9.497e-04 +5.147e-05 
-1.014e-03 +1.398e-04 +3.021e-04 +1.463e-03 -1.068e-03 

 X = inv(A) * B :
      +0.6829 
      -0.8864 
      +1.2477 
      +0.2667 
      +0.5699 


 B :
 -534 
 +773 
  -19 
 -165 
 -157 

  AX :
 -534 
 +773 
  -19 
 -165 
 -157 


 Press   return to continue
 Press X return to stop