Linear Algebra and the C Language/a0js


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00c.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define   RA R5
#define   CA C5       
/* ------------------------------------ */
int main(void)
{
double   xy[8] ={
   1,       4,
   2,       5,
   3,      -7,
   4,       5  };
   
double tA[RA*CA]={
/* x**2    y**2    x       y       e     */
  +1,     +0,     +0,     +0,     +0,        
  +1,    +16,     +1,     +4,     +1,        
  +4,    +25,     +2,     +5,     +1,        
  +9,    +49,     +3,     -7,     +1,        
 +16,    +25,     +4,     +5,     +1,                
};

double tb[RA*C1]={
/*    = 0   */
       +1,   
       +0,   
       +0,   
       +0,   
       +0 
};

double **XY      = ca_A_mR(xy,i_mR(R4,C2));
double **A       = ca_A_mR(tA,i_mR(RA,CA));
double **b       = ca_A_mR(tb,i_mR(RA,C1));
double **Inv    = i_mR(CA,RA);         
double **Invb   = i_mR(CA,C1);         

  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,S10,P0,C6);
  stop();
  
  clrscrn();
  printf(" Using the given points, we obtain this matrix.\n");
  printf("  (a = 1. This is my choice)\n\n");  
  printf(" A :");
  p_mR(A,S10,P2,C7);
  printf(" b :");
  p_mR(b,S10,P2,C7);
   
  printf(" Inv ");
  invgj_mR(A,Inv);  
  pE_mR(Inv,S12,P4,C10); 
  stop();
  
  clrscrn();
  printf(" x = Inv * b ");   
  mul_mR(Inv,b,Invb); 
  p_mR(Invb,S10,P4,C10);
  printf(" The coefficients a, b, c, d, e, of the curve are : \n\n"
         "  %+.2fx**2 %+.2fy**2 %+.2fx %+.2fy %+.2f = 0\n\n"
            ,Invb[R1][C1],Invb[R2][C1],Invb[R3][C1],
             Invb[R4][C1],Invb[R5][C1]);      
  stop();  

  f_mR(XY);
  f_mR(b);
  f_mR(A);
  f_mR(Inv);
  f_mR(Invb); 

  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         +4 
        +2         +5 
        +3         -7 
        +4         +5 

 Press return to continue. 


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

 A :
     +1.00      +0.00      +0.00      +0.00      +0.00 
     +1.00     +16.00      +1.00      +4.00      +1.00 
     +4.00     +25.00      +2.00      +5.00      +1.00 
     +9.00     +49.00      +3.00      -7.00      +1.00 
    +16.00     +25.00      +4.00      +5.00      +1.00 

 b :
     +1.00 
     +0.00 
     +0.00 
     +0.00 
     +0.00 

 Inv 
 +1.0000e+00  +0.0000e+00  -2.7756e-17  +0.0000e+00  +6.9389e-18 
 +2.8030e-01  -9.0909e-02  +1.3258e-01  +7.5758e-03  -4.9242e-02 
 -6.0000e+00  -0.0000e+00  -5.0000e-01  +4.1633e-17  +5.0000e-01 
 +4.7727e-01  -1.8182e-01  +3.0682e-01  -6.8182e-02  -5.6818e-02 
 -1.3939e+00  +3.1818e+00  -2.8485e+00  +1.5152e-01  +5.1515e-01 

 Press return to continue. 


 x = Inv * b 
   +1.0000 
   +0.2803 
   -6.0000 
   +0.4773 
   -1.3939 

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

  +1.00x**2 +0.28y**2 -6.00x +0.48y -1.39 = 0

 Press return to continue.


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

f (+1,+4) 
f (+2,+5) 
f (+3,-7)
f (+4,+5)