Linear Algebra and the C Language/a06k


Install and compile this file in your working directory.

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

double **InvA = inv_mR(A,      i_mR(rc,rc));
double **X    = mul_mR(InvA,B, i_mR(rc,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(" inv(A):");
  pE_mR(InvA, S1,P4,C6);
  printf(" X = inv(A) B :\n\n");
  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(mul_mR(A,X,B), S9,P0,C6);

  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(RC5)+RC1);

} 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                       

 Press return to continue. 


 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 :

 -173   +63  +636  -397  +559 
 -322  -522  -481  +873  -200 
 +244  +716  -765  +703  -845 
 +746   -16  -157  -486  +891 
 -639  -802  +614  +351  +663 

       b1       b2      ...      bn :

     -948      +537       -12      +266      -632      +903 
     +961      -111      +498      -963      -905      -481 
     +328      +357      +177      -483      +248      +897 
     -449      -252      -762      +362      -231      +182 
     -610      +824      +488       -28       +44      +696 

 Press return to continue. 


 inv(A) :

-5.9536e-03 -5.1707e-03 +1.5358e-03 +8.6743e-04 +4.2516e-03 
+3.0622e-03 +1.3481e-03 +6.7659e-04 -2.5023e-05 -1.2793e-03 
-4.3985e-03 -4.9138e-03 +1.4973e-03 -4.1461e-05 +4.1903e-03 
-2.0700e-03 -2.1302e-03 +1.7881e-03 +4.2410e-04 +2.8117e-03 
+3.1355e-03 +2.3257e-03 -3.4587e-05 +6.1964e-04 -1.3106e-03 

 X = inv(A) * B :

    x1       x2       ...      xn

  -1.8042   +1.2099   -0.8179   +2.8489   +8.8097   +1.6056 
  -0.5939   +0.6885   +0.1492   -0.7837   -3.0381   +1.8287 
  -2.5987   +2.1812   -0.0528   +2.7065   +7.7921   +2.6436 
  -1.4038   +1.9732   +0.3295   +0.7119   +3.7052   +2.7934 
  -0.2276   +0.1772   +0.0027   -1.1278   -4.2958   +0.8823 

 Press return to continue. 


       b1       b2      ...      bn :

     -948      +537       -12      +266      -632      +903 
     +961      -111      +498      -963      -905      -481 
     +328      +357      +177      -483      +248      +897 
     -449      -252      -762      +362      -231      +182 
     -610      +824      +488       -28       +44      +696 

      Ax1      Ax2      ...     Axn :

     -948      +537       -12      +266      -632      +903 
     +961      -111      +498      -963      -905      -481 
     +328      +357      +177      -483      +248      +897 
     -449      -252      -762      +362      -231      +182 
     -610      +824      +488       -28       +44      +696 


 Press   return to continue
 Press X return to stop