Linear Algebra and the C Language/a0do
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00a.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
void fun(int r,int c)
{
double **B = r_mR( i_mR(c,r),9.);
double **B_T = transpose_mR(B, i_mR(r,c));
double **U = svd_U_Rn_mR(B_T, i_mR(c,r));
double **U_T = transpose_mR( U, i_mR(r,c));
double **U_TB = i_mR(r,r);
double **V = svd_V_Rn_mR(B_T, i_mR(r,r));
double **S = svds_mR(B, i_mR(r,C1)); /* SD = U_T B V */
double **SD = i_mR(r,r); /* rc cr rr */
/* S = U_T B V */
mul_mR(U_T,B,U_TB);
mul_mR(U_TB,V,SD);
clrscrn();
printf(" Copy/Paste into the octave windows \n\n\n");
p_Octave_mR(B,"B",P2);
printf(" [U, S, V] =svd (B,10)\n\n\n");
stop();
clrscrn();
printf(" U :");
p_mR(U,S5,P5,C10);
printf(" S :");
p_mR(S,S5,P5,C10);
printf(" V:");
p_mR(V,S5,P5,C10);
stop();
clrscrn();
printf(" S : svds_mR();");
p_mR(S,S5,P5,C10);
printf(" S = U_T B V : Sign Problems");
p_mR(SD,S10,P5,C10);
f_mR(B);
f_mR(B_T);
f_mR(U);
f_mR(U_T);
f_mR(U_TB);
f_mR(V);
f_mR(S);
f_mR(SD);
}
/* ------------------------------------ */
int main(void)
{
time_t t;
srand(time(&t));
int i;
do
{
i = rp_I(R3)+R1;
fun(i,i+R2);
} while(stop_w());
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
Screen output example:
Copy/Paste into the octave windows
B=[
-1.00,+4.00,-6.00,-5.00;
+9.00,+2.00,+2.00,-9.00;
-6.00,+5.00,+5.00,+4.00;
-7.00,-7.00,+9.00,+5.00;
+3.00,-9.00,-1.00,-5.00;
-2.00,+7.00,+1.00,-3.00]
[U, S, V] =svd (B,10)
Press return to continue.
U :
-0.30749 -0.28090 -0.32773 +0.65796
-0.54490 +0.12797 +0.76763 -0.06523
+0.37558 -0.41620 +0.30157 +0.10485
+0.65295 +0.33788 +0.33575 +0.38298
-0.18398 +0.64679 -0.06205 +0.46550
-0.08530 -0.44610 +0.30955 +0.43415
S :
+19.55504
+15.11631
+9.44603
+5.73324
V:
-0.60353 -0.29089 +0.24047 +0.70236
-0.20219 +0.94319 +0.22308 +0.14052
+0.44021 -0.11963 +0.88956 +0.02416
+0.63331 +0.10706 -0.31795 +0.69740
Press return to continue.
S : svds_mR();
+19.55504
+15.11631
+9.44603
+5.73324
S = U_T B V : Sign Problems
+19.55504 -0.00000 -0.00000 +0.00000
-0.00000 -15.11631 +0.00000 +0.00000
-0.00000 -0.00000 +9.44603 -0.00000
-0.00000 +0.00000 -0.00000 -5.73324
Press return to continue
Press X return to stop