Linear Algebra and the C Language/a0dp


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00b.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_TB =                   i_mR(r,r);  /* rc cr     */

double **U    =  svd_U_Rn_mR(B_T, i_mR(c,r)); 
double **U_T  = transpose_mR(U,   i_mR(r,c)); 
double **US   =                   i_mR(c,r);  /* cr rr  U*S */
double **V    =  svd_V_Rn_mR(B_T, i_mR(r,r));
double **V_T  = transpose_mR(V,   i_mR(r,r));

double **S    =                   i_mR(r,r);
  
  clrscrn(); 
  printf(" B :");
  p_mR(B,S10,P5,C10);
  stop();

  clrscrn();   
  printf(" U :");
  p_mR(U,S10,P5,C10);   

/* S = U_T B V */
  mul_mR(U_T,B,U_TB);
  mul_mR(U_TB,V,S);
      
  printf(" S :");
  p_mR(S,S10,P5,C10);
   
  printf(" V:");
  p_mR(V,S10,P5,C10);
  stop();

  clrscrn();
  printf(" B :");
  p_mR(B,S10,P5,C10);

/* B = U S V_T */  
   mul_mR(U,S,US);
   mul_mR(US,V_T,B);
         
  printf(" B = U S V_T ");
  p_mR(B,S10,P5,C10);  
    
  f_mR(B);
  f_mR(B_T); 
  f_mR(U);
  f_mR(U_T);
  f_mR(U_TB);
  f_mR(US); 
  f_mR(V);
  f_mR(V_T);
  f_mR(S);
}
/* ------------------------------------ */
int main(void)
{
time_t t;

  srand(time(&t));

int i;

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

} while(stop_w());

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


Screen output example:

                                                                                       
 B :
  +4.00000   +3.00000 
  +9.00000   +9.00000 
  -3.00000   -2.00000 
  +9.00000   +9.00000 

 U :
  +0.26106   -0.65832 
  +0.66970   +0.22351 
  -0.18665   +0.68315 
  +0.66970   +0.22351 

 S :
 +19.00263   +0.00000 
  +0.00000   +0.94870 

 V:
  +0.71879   -0.69523 
  +0.69523   +0.71879 

 Press return to continue. 


 B :
  +4.00000   +3.00000 
  +9.00000   +9.00000 
  -3.00000   -2.00000 
  +9.00000   +9.00000 

 B = U S V_T 
  +4.00000   +3.00000 
  +9.00000   +9.00000 
  -3.00000   -2.00000 
  +9.00000   +9.00000 


 Press   return to continue
 Press X return to stop