Linear Algebra and the C Language/a04x


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   txy[8] ={
   1,      0,
   2,      3,
   3,      4,
   4,      0   };
   
double ab[RA*(CA+Cb)]={
/* x**2     y**2     x        y        e    =   0   */
  +1.00,   +0.00,   +0.00,   +0.00,   +0.00,   +1.00, 
  +1.00,   +0.00,   +1.00,   +0.00,   +1.00,   +0.00, 
  +4.00,   +9.00,   +2.00,   +3.00,   +1.00,   +0.00, 
  +9.00,  +16.00,   +3.00,   +4.00,   +1.00,   +0.00, 
 +16.00,   +0.00,   +4.00,   +0.00,   +1.00,   +0.00, 
};

double **xy         =      ca_A_mR(txy,          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 **A_T        = transpose_mR(A,            i_mR(CA,RA));
double **A_TA       =       mul_mR(A_T,A,        i_mR(CA,CA));  
double **invA_TA    =       inv_mR(A_TA,         i_mR(CA,CA));  
double **invA_TAA_T =       mul_mR(invA_TA,A_T,  i_mR(CA,RA));  

double **x          =       mul_mR(invA_TAA_T,b, i_mR(CA,Cb)); 
      /* x          = inv(A_TA)A_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();
  printf(" A_T:");
  p_mR(A_T, S10,P2,C7);
  
  printf(" A_TA:");
  p_mR(A_TA, S10,P2,C7);
  stop();
  
  clrscrn();
  printf(" inv(A_TA):");
  p_mR(invA_TA, S10,P4,C7);
    
  printf(" inv(A_TA)A_T:");
  p_mR(invA_TAA_T, S10,P4,C7);
  
  printf(" x = inv(A_TA)A_T b:");
  p_mR(x, S10,P4,C7);
  stop();
  
  clrscrn();
  printf(" x = inv(A_TA)A_T b:");
  p_mR(x, S10,P4,C7); 
  
  printf(" The coefficients a, b, c, d, e, of the curve are: \n\n"
         "  %+.9f*x^2 %+.9f*y^2 %+.9f*x %+.9f*y %+.9f = 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(A_T);
  f_mR(A_TA);        
  f_mR(invA_TA);     
  f_mR(invA_TAA_T); 
    
  f_mR(x); 

  return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
Presentation :
 Find the coefficients a, b, c, d, e of the conic,
      
      ax**2 + by**2 + cx + dy + e  = 0 
          
    which crosses these four points   
         
      (x[1],y[1])  (x[2],y[2])  (x[3],y[3])  (x[4],y[4]) 
      
 Using the four points we obtain the matrix.
 
 (a)x**2   (b)y**2   (c)x      (d)y        (e) = 0               
    x[1]**2   y[1]**2   x[1]      y[1]      1    0
    x[2]**2   y[2]**2   x[2]      y[2]      1    0
    x[3]**2   y[3]**2   x[3]      y[3]      1    0
    x[4]**2   y[4]**2   x[4]      y[4]      1    0
    
 This system has four lines and five unknowns (a, b, c, d, e).
 It is a homogeneous system, so it has an infinite number of solutions.
 
 To find a solution, I chose to set a = 1.
 We therefore have five rows and five unknowns.

    1         0         0         0         0    1 
    x[1]**2   y[1]**2   x[1]      y[1]      1    0 
    x[2]**2   y[2]**2   x[2]      y[2]      1    0 
    x[3]**2   y[3]**2   x[3]      y[3]      1    0 
    x[4]**2   y[4]**2   x[4]      y[4]      1    0 

  All that remains is to solve the system.
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    +0 
   +2    +3 
   +3    +4 
   +4    +0 

 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   +0.00   +1.00   +0.00   +1.00   +0.00 
  +4.00   +9.00   +2.00   +3.00   +1.00   +0.00 
  +9.00  +16.00   +3.00   +4.00   +1.00   +0.00 
 +16.00   +0.00   +4.00   +0.00   +1.00   +0.00 

 Press return to continue. 


 A_T :
     +1.00      +1.00      +4.00      +9.00     +16.00 
     +0.00      +0.00      +9.00     +16.00      +0.00 
     +0.00      +1.00      +2.00      +3.00      +4.00 
     +0.00      +0.00      +3.00      +4.00      +0.00 
     +0.00      +1.00      +1.00      +1.00      +1.00 

 A_TA :
   +355.00    +180.00    +100.00     +48.00     +30.00 
   +180.00    +337.00     +66.00     +91.00     +25.00 
   +100.00     +66.00     +30.00     +18.00     +10.00 
    +48.00     +91.00     +18.00     +25.00      +7.00 
    +30.00     +25.00     +10.00      +7.00      +4.00 

 Press return to continue. 


 inv(A_TA) :
   +1.0000    -0.1667    -5.0000    +1.1667    +4.0000 
   -0.1667    +0.2238    +0.7685    -0.9182    -0.4630 
   -5.0000    +0.7685   +25.2222    -5.6019   -20.5556 
   +1.1667    -0.9182    -5.6019    +4.1127    +3.7963 
   +4.0000    -0.4630   -20.5556    +3.7963   +17.8889 

 inv(A_TA)*A_T :
   +1.0000    +0.0000    -0.0000    -0.0000    -0.0000 
   -0.1667    +0.1389    -0.3333    +0.2500    -0.0556 
   -5.0000    -0.3333    +0.0000    +0.0000    +0.3333 
   +1.1667    -0.6389    +1.3333    -0.7500    +0.0556 
   +4.0000    +1.3333    -0.0000    -0.0000    -0.3333 


 x = inv(A_TA)*A_T*b :
   +1.0000 
   -0.1667 
   -5.0000 
   +1.1667 
   +4.0000 

 Press return to continue. 


 x = inv(A_TA)*A_T*b :
   +1.0000 
   -0.1667 
   -5.0000 
   +1.1667 
   +4.0000 

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

  +1.000000000*x^2 -0.166666667*y^2 -5.000000000*x +1.166666667*y +4.000000000 = 0

 Press return to continue.
Copy and paste in Octave:
function xy = f (x,y)
  xy = +1.000000000*x^2 -0.166666667*y^2 -5.000000000*x +1.166666667*y +4.000000000;
endfunction

f (+1,+0)
f (+2,+3) 
f (+3,+4) 
f (+4,+0)