Aller au contenu

Mathc complexes/a301

Un livre de Wikilivres.


La QR décomposition


Installer et compiler ces fichiers dans votre répertoire de travail.


c00b.c
/* ------------------------------------ */
/*  Save as :   c00b.c                  */
/* ------------------------------------ */
#include "w_a.h"
/* ------------------------------------ */
void fun(int r,int rn)
{
double **A    = i_mZ(r+rn,r);
double **b    = i_mZ(r+rn,C1);
double **Ab   = i_Abr_Ac_bc_mZ(r+rn,r,C1);

double **Q    = i_mZ(r+rn,r);
double **R    = i_mZ(r,r);

double **invR = i_mZ(r,r);
double **Q_T  = i_mZ(r,r+rn);


double **invR_Q_T = i_mZ(r,r+rn);
double **x        = i_mZ(r,C1); // x invR * Q_T * b

  clrscrn();
  printf(" Find the unique Least Squares Solution :\n\n");
  printf(" A :");
  p_mZ(r_mZ(A,99.),S6,P0,S4,P0,C6);
  printf(" b :");
  p_mZ(r_mZ(b,99.),S6,P0,S4,P0,C6);
  printf(" Ab :");
  c_A_b_Ab_mZ(A,b,Ab);
  p_mZ(Ab,S6,P0,S4,P0,C6);
  stop();
        
  clrscrn();
  QR_mZ(A,Q,R);    
  printf(" Q :");
  p_mZ(Q,S11,P5,S9,P5,C6);
  printf(" R :");
  p_mZ(R,S11,P5,S9,P5,C6);
  stop();

  clrscrn();
  ctranspose_mZ(Q,Q_T);   
  printf(" Q_T :");
  pE_mZ(Q_T,S9,P5,S7,P5,C3);
  inv_mZ(R,invR); 
  printf(" invR :");
  pE_mZ(invR,S9,P5,S7,P5,C6);
  stop();
  
  clrscrn();
  printf(" Solving this system yields a unique\n"
         " least squares solution, namely   \n\n");
  mul_mZ(invR,Q_T,invR_Q_T);
  mul_mZ(invR_Q_T,b,x);
  printf(" x = invR * Q_T * b :");
  p_mZ(x,S9,P5,S7,P5,C6);
  stop();
    
  clrscrn();
  printf(" Copy/Past into the octave window.\n\n");
  p_Octave_mZ(Ab,"Ab",P0,P0);
  printf(" rref(Ab,.00000000001)\n\n");
  stop();

  clrscrn();
  p_Octave_mZ(A,"A",P0,P0);
  p_Octave_mZ(b,"b",P0,P0);
  printf(" format short e;\n");
  printf(" [Q, R] = qr (A,0)\n");
  printf("  inv(R)*Q'*b\n\n");

  printf(" x = invR * Q_T * b :");
  pE_mZ(x,S9,P5,S7,P5,C6);
  getchar();
  
  f_mZ(A);
  f_mZ(b);
  f_mZ(Ab);
  f_mZ(Q);
  f_mZ(Q_T);
  f_mZ(R);
  f_mZ(invR);  
  f_mZ(invR_Q_T); 
  f_mZ(x); 
}
/* ------------------------------------ */
int main(void)
{
time_t t;

  srand(time(&t));
do
{
  fun(R3,R2);


} while(stop_w());

  return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */


Ici le nombre de lignes doit être supérieurs aux nombres de colonnes. La fonction Gauss-Jordan ne fonctionne pas dans cette situation.


Exemple de sortie écran :
 Find the unique Least Squares Solution :
 A :
  -94.000  -7.00i   +84.000  -7.00i 
  +35.000 +67.00i   +46.000 -82.00i 
  +54.000 +52.00i   +39.000 +77.00i 
  +22.000 -84.00i   -39.000 +74.00i 
  +61.000 -58.00i   +21.000  +1.00i 

 b :
 -666.000-506.00i 
 -668.000+634.00i 
 -389.000 -45.00i 
 +255.000+384.00i 
 -934.000+476.00i 

 Ab :
  -94.000  -7.00i   +84.000  -7.00i  -666.000-506.00i 
  +35.000 +67.00i   +46.000 -82.00i  -668.000+634.00i 
  +54.000 +52.00i   +39.000 +77.00i  -389.000 -45.00i 
  +22.000 -84.00i   -39.000 +74.00i  +255.000+384.00i 
  +61.000 -58.00i   +21.000  +1.00i  -934.000+476.00i 

 Press return to continue. 


 Q :
   -0.504  -0.04i    +0.328  -0.11i 
   +0.188  +0.36i    +0.317  -0.35i 
   +0.289  +0.28i    +0.320  +0.60i 
   +0.118  -0.45i    -0.151  +0.29i 
   +0.327  -0.31i    +0.281  -0.08i 

 R :
 +186.665  +0.00i   -61.458 -15.78i 
   -0.000  +0.00i  +163.732  -0.00i 

 Press return to continue. 


 Q_T :
-5.036e-01-3.75e-02i +1.875e-01+3.59e-01i +2.893e-01+2.79e-01i 
+3.276e-01-1.05e-01i +3.167e-01-3.48e-01i +3.199e-01+6.03e-01i 

+1.179e-01-4.50e-01i +3.268e-01-3.11e-01i 
-1.506e-01+2.94e-01i +2.809e-01-7.90e-02i 

 invR :
+5.357e-03-3.08e-20i +2.011e-03+5.16e-04i 
-0.000e+00+0.00e+00i +6.108e-03+3.13e-20i 

 Press return to continue. 


 Solving this system yields a unique
 least squares solution, namely   

 x = invR * Q_T * b :
   -2.129  +2.47i 
   -4.495  +1.92i 


 Press   return to continue
 Press X return to stop