Linear Algebra and the C Language/a0b9


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00c.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define   RA R8
#define   CA C6
#define   Cb C1
/* ------------------------------------ */
int main(void)
{
double ta[RA*(CA+Cb)]={
//   I1    I2    I3    I4    I5    I6    
     -1,   +1,   +1,   +0,   +0,   +0,   
     +0,   +0,   -1,   +1,   -1,   +0,   
     +0,   +0,   +0,   -1,   +1,   +1,   
     +1,   -1,   +0,   +0,   +0,   -1,   
    +15,  +60,   +0,   +0,   +0,   +0,   
     +0,  -60,  +15,  +15,   +0,  +15,   
     +0,   +0,   +0,  -15,  -60,   +0,   
    +15,   +0,  +15,   +0,  -60,  +15
};  

double tb[RA*(CA+Cb)]={
   +0,
   +0,
   +0,
   +0,
  +90,
   +0,
  -90,
   +0
};                     
double **A      =   ca_A_mR(ta,i_mR(RA,CA));
double **b      =   ca_A_mR(tb,i_mR(RA,Cb));

double **Q      =              i_mR(RA,CA);
double **Q_T    =              i_mR(CA,RA);

double **R      =              i_mR(CA,CA);
double **invR   =              i_mR(CA,CA);

double **invR_Q_T = i_mR(CA,RA);
double **x        = i_mR(CA,C1); 

  clrscrn();
  printf(" Copy/Paste into the octave windows \n\n");
  p_Octave_mR(A,"a",P0);  
  printf(" [Q, R] = qr (a,0) \n\n");
  stop(); 
  
  clrscrn();  
  QR_mR(A,Q,R);    
  printf(" Q :");
  p_mR(Q, S10,P4, C10);  
  printf(" R :");
  p_mR(R, S10,P4, C10); 
  stop(); 
  
  clrscrn();
  transpose_mR(Q,Q_T);   
  printf(" Q_T :");
  pE_mR(Q_T,S9,P3, C6);
  invgj_mR(R,invR); 
  printf(" invR :");
  pE_mR(invR,S9,P3, C6);
  stop();

  clrscrn();
  printf(" Solving this system yields a unique\n"
         " least squares solution, namely   \n\n");
  mul_mR(invR,Q_T,invR_Q_T);
  mul_mR(invR_Q_T,b,x);
  printf(" x = invR * Q_T * b :");
  p_mR(x,S9,P4 ,C6);
  stop();
         
  f_mR(A);
  f_mR(b);
  f_mR(Q);
  f_mR(Q_T);
  f_mR(R);
  f_mR(invR);
  f_mR(x);
      
  return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */

Screen output example:

 Copy/Paste into the octave windows 

 a=[
-1,+1,+1,+0,+0,+0;
+0,+0,-1,+1,-1,+0;
+0,+0,+0,-1,+1,+1;
+1,-1,+0,+0,+0,-1;
+15,+60,+0,+0,+0,+0;
+0,-60,+15,+15,+0,+15;
+0,+0,+0,-15,-60,+0;
+15,+0,+15,+0,-60,+15]

 [Q, R] = qr (a,0) 

 Press return to continue. 


 Q :
   -0.0470    +0.0406    +0.8125    +0.0160    -0.1464    -0.2537 
   +0.0000    +0.0000    -0.3633    +0.0631    -0.7414    +0.2537 
   +0.0000    +0.0000    +0.0000    -0.0575    +0.5581    +0.6597 
   +0.0470    -0.0406    -0.4492    -0.0216    +0.3297    -0.6597 
   +0.7055    +0.4103    +0.0210    +0.2881    +0.0177    +0.0101 
   +0.0000    -0.8151    +0.0421    +0.2888    +0.0098    +0.0169 
   +0.0000    +0.0000    +0.0000    -0.8625    -0.0769    -0.0101 
   +0.7055    -0.4049    +0.0631    -0.2856    -0.0494    +0.0169 

 R :
  +21.2603   +42.2384   +10.5361    +0.0000   -42.3324   +10.5361 
   -0.0000   +73.6065   -18.2596   -12.2272   +24.2920   -18.2596 
   -0.0000    -0.0000    +2.7528    +0.2675    -3.4216    +2.0262 
   -0.0000    -0.0000    -0.0000   +17.3904   +68.7702    +0.0112 
   -0.0000    -0.0000    +0.0000    -0.0000    +8.8776    -0.3666 
   -0.0000    -0.0000    -0.0000    +0.0000    +0.0000    +1.8269 

 Press return to continue. 


 Q_T :
-4.704e-02 +0.000e+00 +0.000e+00 +4.704e-02 +7.055e-01 +0.000e+00 
+4.058e-02 +0.000e+00 +0.000e+00 -4.058e-02 +4.103e-01 -8.151e-01 
+8.125e-01 -3.633e-01 +0.000e+00 -4.492e-01 +2.103e-02 +4.205e-02 
+1.603e-02 +6.309e-02 -5.750e-02 -2.162e-02 +2.881e-01 +2.888e-01 
-1.464e-01 -7.414e-01 +5.581e-01 +3.297e-01 +1.769e-02 +9.757e-03 
-2.537e-01 +2.537e-01 +6.597e-01 -6.597e-01 +1.015e-02 +1.692e-02 

+0.000e+00 +7.055e-01 
+0.000e+00 -4.049e-01 
+0.000e+00 +6.308e-02 
-8.625e-01 -2.856e-01 
-7.688e-02 -4.943e-02 
-1.015e-02 +1.692e-02 

 invR :
+4.704e-02 -2.699e-02 -3.591e-01 -1.345e-02 +2.640e-01 -8.974e-02 
-0.000e+00 +1.359e-02 +9.012e-02 +8.166e-03 -6.570e-02 +2.260e-02 
+0.000e+00 -0.000e+00 +3.633e-01 -5.589e-03 +1.833e-01 -3.661e-01 
+0.000e+00 -0.000e+00 +0.000e+00 +5.750e-02 -4.454e-01 -8.974e-02 
-0.000e+00 +0.000e+00 -0.000e+00 -0.000e+00 +1.126e-01 +2.260e-02 
-0.000e+00 +0.000e+00 -0.000e+00 -0.000e+00 +0.000e+00 +5.474e-01 

 Press return to continue. 


 Solving this system yields a unique
 least squares solution, namely   

 x = invR * Q_T * b :
  +2.0000 
  +1.0000 
  +1.0000 
  +2.0000 
  +1.0000 
  +1.0000 

 Press return to continue.