Linear Algebra and the C Language/a0dw


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 **U    =  svd_U_Cn_mR(A,   i_mR(r,r));
double **U_T  = transpose_mR(U,   i_mR(r,r));  /*  U_T A          */
double **U_TA =                   i_mR(r,c);   /*  rr  rc         */
double **V    =  svd_V_Cn_mR(A,   i_mR(c,c));

double **S    =      svds_mR(A_T, i_mR(r,C1)); /* S2 = U_T A  V   */
double **S_2  =                   i_mR(r,c);   /*      rr  rc cc  */

/* S = U_T * A * V */
  mul_mR(U_T,A,U_TA);
  mul_mR(U_TA,V,S_2);
        
  clrscrn();  
  printf(" Copy/Paste into the octave windows \n\n\n");
  p_Octave_mR(A,"A",P2);
  printf(" [U, S, V] =svd (A,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 :");
  p_mR(S,S5,P5,C10);
  
  printf(" S = U_T A V :");
  p_mR(S_2,S10,P5,C10);  
    
  f_mR(A);
  f_mR(A_T);
  f_mR(U);
  f_mR(U_T);
  f_mR(U_TA);  
  f_mR(V); 
  f_mR(S); 
  f_mR(S_2); 
}
/* ------------------------------------ */
int main(void)
{
time_t t;

  srand(time(&t));

int i;

do
{
  i = rp_I(R3)+R1;
  
  fun(i,i+C3);

} while(stop_w());

  return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */


Screen output example:

                                                                                       
 Copy/Paste into the octave windows 


 A=[
-8.00,-5.00,+9.00,+9.00,-6.00,-4.00;
-1.00,-6.00,-8.00,-9.00,-7.00,+7.00;
-5.00,-8.00,+6.00,+9.00,-2.00,-2.00]

 [U, S, V] =svd (A,10)


 Press return to continue. 


 U :
+0.70298 +0.30396 -0.64299 
-0.41665 +0.90870 -0.02595 
+0.57639 +0.28614 +0.76544 

 S :
+23.57003 
+14.90917 
+4.37841 

 V:
-0.34320 +0.32001 +0.30665 +0.00000 +0.00000 +0.76556 
-0.23870 +0.62117 -0.62874 -0.28055 +0.36338 +0.02767 
+0.55657 +0.18895 -0.22535 +0.66531 -0.00000 -0.00000 
+0.64761 +0.19232 +0.30504 -0.62879 +0.47175 +0.32693 
-0.10412 +0.58735 +0.57297 +0.28857 -0.12431 -0.55342 
-0.29195 -0.30671 +0.19629 +0.00000 +0.79369 +0.00000 

 Press return to continue. 


 S :
+23.57003 
+14.90917 
+4.37841 

 S = U_T A V :
 +23.57003   -0.00000   +0.00000   -0.00000   -0.00000   +0.00000 
  +0.00000  -14.90917   -0.00000   +0.00000   -0.00000   +0.00000 
  +0.00000   -0.00000   +4.37841   +0.00000   +0.00000   +0.00000 


 Press   return to continue
 Press X return to stop