Linear Algebra and the C Language/a0b8


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00b.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,   +1,   +0,   +0,   +1,   -1,   
      -1,   +0,   +0,   +1,   +0,   +1,   
      +0,  -50,   +0,   +0,   +0,  -20,   
      +0,  +50,  -20,   +0,  -10,   +0,   
      +0,   +0,   +0,  -50,  +10,  +20,   
      +0,   +0,  -20,  -50,   +0,   +0,   
};

double tb[RA*(CA+Cb)]={
      0,
      0,
      0,
      0,
    -90,
      0, 
      0,
    -90,
};                       
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,+1,+0,+0,+1,-1;
-1,+0,+0,+1,+0,+1;
+0,-50,+0,+0,+0,-20;
+0,+50,-20,+0,-10,+0;
+0,+0,+0,-50,+10,+20;
+0,+0,-20,-50,+0,+0]

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

 Press return to continue. 


 Q :
   +0.7071    -0.0071    -0.0245    +0.0259    +0.1704    +0.4687 
   +0.0000    +0.0000    +0.0408    -0.0460    -0.8338    -0.2257 
   +0.0000    +0.0141    +0.0081    -0.0057    +0.4931    -0.7117 
   -0.7071    -0.0071    -0.0245    +0.0259    +0.1704    +0.4687 
   +0.0000    -0.7070    -0.4073    +0.2866    -0.0253    -0.0385 
   +0.0000    +0.7070    -0.4080    +0.2872    -0.0317    -0.0149 
   +0.0000    +0.0000    +0.0000    -0.8645    +0.0386    +0.0337 
   +0.0000    +0.0000    -0.8153    -0.2908    -0.0185    -0.0198 

 R :
   +1.4142    -0.7071    -0.7071    -0.7071    +0.0000    -0.7071 
   +0.0000   +70.7213   -14.1329    -0.0071    -7.0559   +14.1188 
   +0.0000    -0.0000   +24.5308   +40.6999    +4.0472    +8.1139 
   +0.0000    +0.0000    +0.0000   +57.8362   -11.4767   -22.9897 
   +0.0000    +0.0000    +0.0000    -0.0000    +2.0299    +0.9540 
   +0.0000    -0.0000    +0.0000    -0.0000    -0.0000    +2.6246 

 Press return to continue. 


 Q_T :
+7.071e-01 +0.000e+00 +0.000e+00 -7.071e-01 +0.000e+00 +0.000e+00 
-7.070e-03 +0.000e+00 +1.414e-02 -7.070e-03 -7.070e-01 +7.070e-01 
-2.446e-02 +4.077e-02 +8.146e-03 -2.446e-02 -4.073e-01 -4.080e-01 
+2.585e-02 -4.598e-02 -5.731e-03 +2.585e-02 +2.866e-01 +2.872e-01 
+1.704e-01 -8.338e-01 +4.931e-01 +1.704e-01 -2.528e-02 -3.174e-02 
+4.687e-01 -2.257e-01 -7.117e-01 +4.687e-01 -3.854e-02 -1.493e-02 

+0.000e+00 +0.000e+00 
+0.000e+00 +0.000e+00 
+0.000e+00 -8.153e-01 
-8.645e-01 -2.908e-01 
+3.855e-02 -1.847e-02 
+3.368e-02 -1.979e-02 

 invR :
+7.071e-01 +7.070e-03 +2.446e-02 -8.564e-03 -7.260e-02 +2.824e-02 
+0.000e+00 +1.414e-02 +8.146e-03 -5.731e-03 +5.057e-04 -1.516e-01 
+0.000e+00 +0.000e+00 +4.077e-02 -2.869e-02 -2.435e-01 -2.888e-01 
-0.000e+00 -0.000e+00 -0.000e+00 +1.729e-02 +9.775e-02 +1.159e-01 
-0.000e+00 -0.000e+00 -0.000e+00 +0.000e+00 +4.926e-01 -1.791e-01 
-0.000e+00 -0.000e+00 -0.000e+00 +0.000e+00 -0.000e+00 +3.810e-01 

 Press return to continue. 


 Solving this system yields a unique
 least squares solution, namely   

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

 Press return to continue.