Linear Algebra and the C Language/a09z


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00a.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
#define   Cb C6
/* ------------------------------------ */
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("                                                 \n");
  printf(" Linear systems with common coefficient matrix.\n\n");
  printf("                Ax1=b1                           \n");
  printf("                Ax2=b2                           \n");
  printf("                ...                              \n");
  printf("                Axn=bn                         \n\n");
  printf(" We can write these equalities in this maner.  \n\n");
  printf("    A|x1|x2|...|xn| = b1|b2|...|bn|            \n\n");
  printf("  or simply :                                  \n\n");
  printf("              AX = B                           \n\n");
  printf("  where B = b1|b2|...|bn                       \n\n");
  printf("  and   X = x1|x2|...|xn                       \n\n");
  stop();

  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,C6);
  printf("       b1       b2      ...      bn:");
  p_mR(B,S9,P0,C6);
  stop();

  clrscrn();
  printf(" InvA:");
  pE_mR(InvA,S1,P3,C7);
  printf(" X = InvA B:");
  printf("    x1       x2       ...       xn:");
  p_mR(X,S9,P4,C6);
  stop();

  clrscrn();
  printf("       b1       b2      ...      bn:");
  p_mR(B,S9,P0,C6);
  printf("      Ax1      Ax2      ...     Axn:");
  p_mR(T,S9,P0,C6);

  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:

          
 Linear systems with common coefficient matrix.

                Ax1=b1                           
                Ax2=b2                           
                ...                              
                Axn=bn                         

 We can write these equalities in this maner.  

    A|x1|x2|...|xn| = b1|b2|...|bn|            

  or simply :                                  

              AX = B                           

  where B = b1|b2|...|bn                       

  and   X = x1|x2|...|xn                       


 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 :
 -958  -989  +776  +885  -818 
 +506  +369   +41  -454  +292 
 -500  -728  +530  -442  +355 
 +415  -309  +606  +560  +786 
 -680  -747  +497  -332  -931 

       b1       b2      ...      bn :
     +375      +239      -694      +932       +35      +875 
     -557      -549      -530      +253      -322      -500 
     +957      -718      +115      -830      +898      +730 
     -770      -586      -115      +981      -197      -385 
     -758      +151      -633       -95      -667      +160 


 inv(A) :
-2.0406e-03 -1.7849e-03 -1.4648e-03 +2.0454e-03 +2.4014e-03 
+2.8712e-03 +4.1837e-03 +7.2322e-04 -2.2605e-03 -2.8432e-03 
+2.2049e-03 +3.6716e-03 +5.4489e-04 -8.4155e-04 -1.2884e-03 
+4.0053e-04 -4.2286e-04 -4.0146e-04 +2.3031e-04 -4.4318e-04 
+2.2088e-04 +5.7626e-05 +9.2362e-04 -2.1160e-04 -1.0766e-03 

 X = inv(A) * B :

    x1       x2       ...      xn
  -4.5680   +0.7079   +0.4384   +0.6408   -2.8167   -2.3656 
  +3.3342   -1.2346   -2.0672   +1.1868   +1.7445   +1.3638 
  +0.9278   -1.5814   -2.5012   +1.8284   +0.4093   +0.6091 
  +0.1601   +0.4142   +0.1540   +0.8676   +0.0399   +0.1092 
  +1.9136   -0.6806   +0.6282   -0.6515   +1.5783   +0.7479 


       b1       b2      ...      bn :
     +375      +239      -694      +932       +35      +875 
     -557      -549      -530      +253      -322      -500 
     +957      -718      +115      -830      +898      +730 
     -770      -586      -115      +981      -197      -385 
     -758      +151      -633       -95      -667      +160 

      Ax1      Ax2      ...     Axn :
     +375      +239      -694      +932       +35      +875 
     -557      -549      -530      +253      -322      -500 
     +957      -718      +115      -830      +898      +730 
     -770      -586      -115      +981      -197      -385 
     -758      +151      -633       -95      -667      +160 


 Press   return to continue
 Press X return to stop