Linear Algebra and the C Language/a0lc
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as: c00d1.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define RC RC3
/* ------------------------------------ */
/* ------------------------------------ */
void fun(void)
{
double **u = r_mR( i_mR(RC,C1),9.);
double **v = r_mR( i_mR(RC,C1),9.);
double **Q = r_Q_mR( i_mR(RC,RC),9);
double **Qu = mul_mR(Q,u, i_mR(RC,C1));
double **Qv = mul_mR(Q,v, i_mR(RC,C1));
clrscrn();
printf(" Calculate the cosine of the angle"
" between vectors U and V: \n\n");
printf(" u:");
p_mR(u, S3,P0,C6);
printf(" v:");
p_mR(v, S3,P0,C6);
printf(" <u,v> \n"
" cos(alpha) = ----------- = %+.4f \n"
" ||u|| ||v|| \n\n\n\n\n",
dot_R(u,v)
/
( norm_R(u) * norm_R(v) ));
stop();
clrscrn();
printf(" Q:");
p_mR(Q, S3,P4,C6);
printf("\n\n\n\n\n");
stop();
clrscrn();
printf(" Calculate the cosine of the angle"
" between vectors U and V: \n\n");
printf(" Qu:");
p_mR(Qu, S3,P4,C6);
printf(" Qv:");
p_mR(Qv, S3,P4,C6);
printf(" <Qu,Qv> \n"
" cos(alpha) = ------------- = %+.4f \n"
" ||Qu|| ||Qv|| \n\n\n\n\n",
dot_R(Qu,Qv)
/
( norm_R(Qu) * norm_R(Qv) ));
stop();
clrscrn();
printf(" Orthonormal matrices preserve angles.\n\n"
" Calculate the cosine of the angle between"
" vectors U and V: \n\n"
" <u,v> \n"
" cos(alpha) = ----------- = %+.4f \n"
" ||u|| ||v|| \n\n\n\n\n",
dot_R(u,v)
/
( norm_R(u) * norm_R(v) ));
printf(" <Qu,Qv> \n"
" cos(alpha) = ------------- = %+.4f \n"
" ||Qu|| ||Qv|| \n\n\n\n\n",
dot_R(Qu,Qv)
/
( norm_R(Qu) * norm_R(Qv) ));
f_mR(u);
f_mR(v);
f_mR(Qu);
f_mR(Qv);
f_mR(Q);
}
/* ------------------------------------ */
int main(void)
{
time_t t;
srand(time(&t));
do
{
fun();
} while(stop_w());
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
Screen output example:
Calculate the cosine of the angle between vectors U and V:
u:
+5
-7
+3
v:
+4
-4
+3
<u,v>
cos(alpha) = ----------- = +0.9771
||u|| ||v||
Press return to continue.
Q:
-0.2673 +0.7379 +0.6198
-0.5345 +0.4216 -0.7325
+0.8018 +0.5270 -0.2817
Press return to continue.
Calculate the cosine of the angle between vectors U and V:
Qu:
-4.6420
-7.8215
-0.5256
Qv:
-2.1612
-6.0220
+0.2538
<Qu,Qv>
cos(alpha) = ------------- = +0.9771
||Qu|| ||Qv||
Press return to continue.
Orthonormal matrices preserve angles.
Calculate the cosine of the angle between vectors U and V:
<u,v>
cos(alpha) = ----------- = +0.9771
||u|| ||v||
<Qu,Qv>
cos(alpha) = ------------- = +0.9771
||Qu|| ||Qv||
Press return to continue
Press X return to stop