Linear Algebra and the C Language/a0bc
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00c.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define RA R5
#define CA C5
#define Cb C1
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
double xy[6] ={
1, 10,
2, 1,
3, -10 };
double ab[RA*(CA+Cb)]={
/* x**2 y**2 x y = 0 */
+1, +0, +0, +0, +0, +1,
+0, +1, +0, +0, +0, +1,
+1, +100, +1, +10, +1, +0,
+4, +1, +2, +1, +1, +0,
+9, +100, +3, -10, +1, +0,
};
double **XY = ca_A_mR(xy,i_mR(R3,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 of a circle \n\n");
printf(" ax**2 + ay**2 + bx + cy + d = 0 \n\n");
printf(" that passes through these three XY. \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 = 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);
invgj_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 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 of a circle
ax**2 + ay**2 + bx + cy + d = 0
that passes through these three XY.
x y
+1 +10
+2 +1
+3 -10
Using the given points, we obtain this matrix.
(a = 1. This is my choice)
Ab :
x**2 y**2 x y = 0
+1.00 +0.00 +0.00 +0.00 +0.00 +1.00
+0.00 +1.00 +0.00 +0.00 +0.00 +1.00
+1.00 +100.00 +1.00 +10.00 +1.00 +0.00
+4.00 +1.00 +2.00 +1.00 +1.00 +0.00
+9.00 +100.00 +3.00 -10.00 +1.00 +0.00
Press return to continue.
Q :
+0.1005 -0.1023 -0.3709 +0.9175 -0.0082
+0.0000 +0.0101 -0.0043 -0.0095 -0.9999
+0.1005 +0.9068 +0.3402 +0.2277 +0.0055
+0.4020 -0.3993 +0.7909 +0.2310 -0.0096
+0.9045 +0.0881 -0.3481 -0.2299 +0.0046
R :
+9.9499 +100.9058 +3.6181 -7.6383 +1.4071
-0.0000 +99.0960 +0.3725 +7.7879 +0.5956
-0.0000 +0.0000 +0.8777 +7.6742 +0.7830
+0.0000 -0.0000 +0.0000 +4.8075 +0.2288
+0.0000 +0.0000 +0.0000 +0.0000 +0.0005
Press return to continue.
Q_T :
+1.0050e-01 +0.0000e+00 +1.0050e-01 +4.0202e-01 +9.0453e-01
-1.0234e-01 +1.0091e-02 +9.0678e-01 -3.9927e-01 +8.8069e-02
-3.7088e-01 -4.2823e-03 +3.4023e-01 +7.9089e-01 -3.4810e-01
+9.1750e-01 -9.5115e-03 +2.2771e-01 +2.3104e-01 -2.2993e-01
-8.1722e-03 -9.9989e-01 +5.5283e-03 -9.6144e-03 +4.5668e-03
invR :
+1.0050e-01 -1.0234e-01 -3.7088e-01 +9.1750e-01 -8.1722e-03
+0.0000e+00 +1.0091e-02 -4.2823e-03 -9.5115e-03 -9.9989e-01
+0.0000e+00 -0.0000e+00 +1.1393e+00 -1.8187e+00 -9.9009e+02
+0.0000e+00 -0.0000e+00 +0.0000e+00 +2.0801e-01 -9.9012e+01
-0.0000e+00 +0.0000e+00 -0.0000e+00 +0.0000e+00 +2.0802e+03
Press return to continue.
Solving this system yields a unique
least squares solution, namely
x = invR * Q_T * b :
+1.00
+1.00
+996.00
+100.00
-2097.00
The coefficients a, b, c, d of the curve are :
+1.00x**2 +1.00y**2 +996.00x +100.00y -2097.00 = 0
Press return to continue.
Copy and paste in Octave:
function xy = f (x,y)
xy = +1.00*x^2 +1.00*y^2 +996.00*x +100.00*y -2097.00;
endfunction
f (+1,+10)
f (+2,+1)
f (+3,-10)