Linear Algebra and the C Language/a0ba
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00a.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define RA R5
#define CA C5
#define Cb C1
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
double xy[6] ={
1, -2,
2, -3,
3, 6 };
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, +4, +1, -2, +1, +0,
+4, +9, +2, -3, +1, +0,
+9, +36, +3, +6, +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);
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");
printf("invR*Q_T :");
mul_mR(invR,Q_T,invR_Q_T);
p_mR(invR_Q_T,S10,P4,C6);
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 -2
+2 -3
+3 +6
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 +4.00 +1.00 -2.00 +1.00 +0.00
+4.00 +9.00 +2.00 -3.00 +1.00 +0.00
+9.00 +36.00 +3.00 +6.00 +1.00 +0.00
Press return to continue.
Q :
+0.1005 -0.4928 -0.5780 -0.3248 +0.5544
+0.0000 +0.1340 +0.0472 -0.9182 -0.3696
+0.1005 +0.0433 +0.7229 -0.2169 +0.6468
+0.4020 -0.7650 +0.3370 +0.0544 -0.3696
+0.9045 +0.3899 -0.1659 +0.0360 +0.0308
R :
+9.9499 +36.5834 +3.6181 +4.0202 +1.4071
-0.0000 +7.4603 -0.3168 +4.5480 -0.3317
-0.0000 -0.0000 +0.8993 -3.4522 +0.8940
+0.0000 +0.0000 -0.0000 +0.4863 -0.1264
+0.0000 +0.0000 +0.0000 +0.0000 +0.3080
Press return to continue.
Q_T :
+1.0050e-01 +0.0000e+00 +1.0050e-01 +4.0202e-01 +9.0453e-01
-4.9284e-01 +1.3404e-01 +4.3327e-02 -7.6499e-01 +3.8994e-01
-5.7800e-01 +4.7225e-02 +7.2290e-01 +3.3703e-01 -1.6589e-01
-3.2485e-01 -9.1825e-01 -2.1687e-01 +5.4449e-02 +3.5992e-02
+5.5444e-01 -3.6962e-01 +6.4684e-01 -3.6962e-01 +3.0802e-02
invR :
+1.0050e-01 -4.9284e-01 -5.7800e-01 -3.2485e-01 +5.5444e-01
+0.0000e+00 +1.3404e-01 +4.7225e-02 -9.1825e-01 -3.6962e-01
-0.0000e+00 +0.0000e+00 +1.1120e+00 +7.8932e+00 +1.2321e-02
-0.0000e+00 +0.0000e+00 -0.0000e+00 +2.0561e+00 +8.4398e-01
-0.0000e+00 +0.0000e+00 -0.0000e+00 -0.0000e+00 +3.2465e+00
Press return to continue.
Solving this system yields a unique
least squares solution, namely
invR*Q_T :
+1.0000 -0.0000 -0.0000 +0.0000 +0.0000
-0.0000 +1.0000 -0.0000 -0.0000 -0.0000
-3.2000 -7.2000 -0.9000 +0.8000 +0.1000
-0.2000 -2.2000 +0.1000 -0.2000 +0.1000
+1.8000 -1.2000 +2.1000 -1.2000 +0.1000
x = invR * Q_T * b :
+1.00
+1.00
-10.40
-2.40
+0.60
The coefficients a, b, c, d of the curve are :
+1.00x**2 +1.00y**2 -10.40x -2.40y +0.60 = 0
Press return to continue.
Copy and paste in Octave:
function xy = f (x,y)
xy = +1.00*x^2 +1.00*y^2 -10.40*x -2.40*y +0.60;
endfunction
f (+1,-2)
f (+2,-3)
f (+3,+6)