Linear Algebra and the C Language/a0bf


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00b.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define   RA R5
#define   CA C5
#define   Cb C1 
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
double   xy[8] ={
   1,     -8,
   2,      2,
   3,      1,
   4,      2  };

   
double ab[RA*(CA+Cb)]={
/* x**2    y**2    x       y       e    =  0   */
  +1,     +0,     +0,     +0,     +0,     +1, 
  +1,    +64,     +1,     -8,     +1,     +0, 
  +4,     +4,     +2,     +2,     +1,     +0, 
  +9,     +1,     +3,     +1,     +1,     +0, 
 +16,     +4,     +4,     +2,     +1,     +0 
};

double **XY = ca_A_mR(xy,i_mR(R4,C2));

double **Ab = ca_A_mR(ab,i_Abr_Ac_bc_mR(RA,CA,Cb));
double **A  = c_Ab_A_mR(Ab,i_mR(RA,CA));
double **b  = c_Ab_b_mR(Ab,i_mR(RA,Cb));

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

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


double **invR_Q_T = i_mR(CA,RA);
double **x        = i_mR(CA,Cb); // x = invR * Q_T * b

  clrscrn();
  printf("\n");
  printf(" Find the coefficients a, b, c, d, e, of the curve \n\n");
  printf("     ax**2 + by**2 + cx + dy + e  = 0 \n\n");
  printf(" that passes through these four points.\n\n");
  printf("    x     y");
  p_mR(XY,S5,P0,C6);
  printf(" Using the given points, we obtain this matrix.\n");
  printf("  (a = 1. This is my choice)\n\n");  
  printf(" Ab :\n");
  printf("   x**2    y**2    x       y       e    =  0     ");
  p_mR(Ab,S7,P2,C6);
  stop();

    
  clrscrn();
  QR_mR(A,Q,R);    
  printf(" Q :");
  p_mR(Q,S10,P4,C6); 
  printf(" R :");
  p_mR(R,S10,P4,C6);
  stop();

  clrscrn();
  transpose_mR(Q,Q_T);   
  printf(" Q_T :");
  pE_mR(Q_T,S12,P4,C6); 
  inv_mR(R,invR); 
  printf(" invR :");
  pE_mR(invR,S12,P4,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,S10,P2,C6);
  printf(" The coefficients a, b, c, d, e, of the curve are : \n\n"
         "  %+.2fx**2 %+.2fy**2 %+.2fx %+.2fy %+.2f = 0\n\n"
            ,x[R1][C1],x[R2][C1],x[R3][C1],x[R4][C1],x[R5][C1]);  
  
  stop();

  f_mR(XY);  
  f_mR(A);
  f_mR(b);
  f_mR(Ab);
  f_mR(Q);
  f_mR(Q_T);
  f_mR(R);
  f_mR(invR);  
  f_mR(invR_Q_T); 
  f_mR(x); 

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


Screen output example:
 Find the coefficients a, b, c, d, e, of the curve 

     ax**2 + by**2 + cx + dy + e  = 0 

 that passes through these four points.

    x     y
   +1    -8 
   +2    +2 
   +3    +1 
   +4    +2 

 Using the given points, we obtain this matrix.
  (a = 1. This is my choice)

 Ab :
   x**2    y**2    x       y       e    =  0     
  +1.00   +0.00   +0.00   +0.00   +0.00   +1.00 
  +1.00  +64.00   +1.00   -8.00   +1.00   +0.00 
  +4.00   +4.00   +2.00   +2.00   +1.00   +0.00 
  +9.00   +1.00   +3.00   +1.00   +1.00   +0.00 
 +16.00   +4.00   +4.00   +2.00   +1.00   +0.00 

 Press return to continue. 


 Q :
   +0.0531    -0.0068    -0.2458    +0.1164    +0.9608 
   +0.0531    +0.9973    -0.0277    -0.0429    +0.0022 
   +0.2123    +0.0357    +0.7522    +0.6136    +0.1066 
   +0.4777    -0.0452    +0.4432    -0.7365    +0.1759 
   +0.8492    -0.0454    -0.4203    +0.2563    -0.1858 

 R :
  +18.8414    +8.1204    +5.3074    +2.1761    +1.5922 
   +0.0000   +63.7421    +0.7515    -8.0429    +0.9424 
   -0.0000    -0.0000    +1.1253    +1.3284    +0.7475 
   +0.0000    +0.0000    +0.0000    +1.3462    +0.0905 
   +0.0000    +0.0000    +0.0000    -0.0000    +0.0989 

 Press return to continue. 


 Q_T :
 +5.3074e-02  +5.3074e-02  +2.1230e-01  +4.7767e-01  +8.4919e-01 
 -6.7614e-03  +9.9728e-01  +3.5707e-02  -4.5164e-02  -4.5430e-02 
 -2.4581e-01  -2.7670e-02  +7.5217e-01  +4.4320e-01  -4.2025e-01 
 +1.1638e-01  -4.2860e-02  +6.1361e-01  -7.3649e-01  +2.5628e-01 
 +9.6082e-01  +2.1987e-03  +1.0664e-01  +1.7589e-01  -1.8579e-01 

 invR :
 +5.3074e-02  -6.7614e-03  -2.4581e-01  +1.1638e-01  +9.6082e-01 
 +0.0000e+00  +1.5688e-02  -1.0477e-02  +1.0407e-01  -1.6551e-01 
 +0.0000e+00  +0.0000e+00  +8.8867e-01  -8.7694e-01  -5.9111e+00 
 -0.0000e+00  -0.0000e+00  -0.0000e+00  +7.4285e-01  -6.7975e-01 
 -0.0000e+00  -0.0000e+00  -0.0000e+00  +0.0000e+00  +1.0107e+01 

 Press return to continue. 


 Solving this system yields a unique
 least squares solution, namely   

 x = invR * Q_T * b :
     +1.00 
     -0.14 
     -6.00 
     -0.57 
     +9.71 

 The coefficients a, b, c, d, e, of the curve are : 

  +1.00x**2 -0.14y**2 -6.00x -0.57y +9.71 = 0

 Press return to continue.


Copy and paste in Octave:
function xy = f (x,y)
  xy = +1.000000000*x^2 -0.144444444*y^2 -6.000000000*x -0.566666667*y +9.711111111;
endfunction

f (1, -8)
f (2,  2) 
f (3,  1) 
f (4,  2)