Linear Algebra and the C Language/a0nd
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00a.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
void fun(int r,int c)
{
double **A = r_mR( i_mR(r,c),9.);
double **A_T = transpose_mR(A, i_mR(c,r));
double **Svalues = svds_mR(A, i_mR(c,C1));
double s = 0;
int i = 0;
clrscrn();
printf(" A:");
p_mR(A, S5,P0,C10);
printf(" Singular values:");
p_mR(Svalues, S5,P5,C10);
stop();
clrscrn();
for(i=R1;i<=c;i++)
s += Svalues[i][C1];
printf(" Sum of the singular values of A : s = %.3f\n\n\n", s);
printf(" The contribution of each singular value in percentage\n\n");
for(i=R1;i<=c;i++)
printf(" 100 Svalues[%d]/s = %+.3f%%\n",i,(100*Svalues[i][C1])/s);
printf("\n\n\n"
" The contribution of each singular value in proportion\n\n");
for(i=R1;i<=c;i++)
printf(" Svalues[%d]/s = %+.3f\n",i,(Svalues[i][C1])/s);
f_mR(A);
f_mR(A_T);
f_mR(Svalues);
}
/* ------------------------------------ */
int main(void)
{
time_t t;
srand(time(&t));
do
{
fun(R6,C4);
} while(stop_w());
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
Screen output example:
A:
-6 -6 -4 -1
-8 -5 -8 +6
-3 +2 +3 +9
-3 -4 +5 +6
+4 +4 -2 -7
+1 -2 -3 -1
Singular values:
+18.46583
+13.36103
+6.34425
+2.69192
Press return to continue.
Sum of the singular values of A : s = 40.863
The contribution of each singular value in percentage
100 Svalues[1]/s = +45.190%
100 Svalues[2]/s = +32.697%
100 Svalues[3]/s = +15.526%
100 Svalues[4]/s = +6.588%
The contribution of each singular value in proportion
Svalues[1]/s = +0.452
Svalues[2]/s = +0.327
Svalues[3]/s = +0.155
Svalues[4]/s = +0.066
Press return to continue
Press X return to stop