Linear Algebra and the C Language/a04f


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00a.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define RA   R4
#define CA   C3 
#define Cb   C1 
/* ------------------------------------ */
#define FACTOR_E        +1.E-0         
/* ------------------------------------ */
int main(void)
{
double ta[RA*(CA+Cb)]={
//  x1    x2    x3       
    +1,   +1,   +0,// A
    +0,   -1,   -1,// B
    +0,   +0,   +1,// C
    -1,   +0,   +0 // D  
};

double tb[RA*(CA+Cb)]={
     +50,   // A
     -40,   // B
 +20 -10,   // C
 -30 +10    // D  
};

double **A       = ca_A_mR(ta,i_mR(RA,CA));    
double **b       = ca_A_mR(tb,i_mR(RA,C1));
double **Pinv    =            i_mR(CA,RA);           
double **x       =            i_mR(CA,C1);          

  clrscrn();
  printf(" A :");
  p_mR(A,S5,P1,C6);
  printf(" b :");
  p_mR(b,S5,P1,C6);
   
  printf(" Pinv = V * invS_T * U_T ");
  Pinv_Rn_mR(A,Pinv,FACTOR_E); 
  pE_mR(Pinv,S12,P4,C6);   
  
  printf(" x = Pinv * b ");   
  mul_mR(Pinv,b,x); 
  p_mR(x,S10,P4,C6);  
  stop();  
  
  f_mR(b);   
  f_mR(A);
  f_mR(Pinv);
  f_mR(x); 

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


Screen output example:

 A :
 +1.0  +1.0  +0.0 
 +0.0  -1.0  -1.0 
 +0.0  +0.0  +1.0 
 -1.0  +0.0  +0.0 

 b :
+50.0 
-40.0 
+10.0 
-20.0 

 Pinv = V * invS_T * U_T 
 +2.5000e-01  +2.5000e-01  +2.5000e-01  -7.5000e-01 
 +5.0000e-01  -5.0000e-01  -5.0000e-01  +5.0000e-01 
 -2.5000e-01  -2.5000e-01  +7.5000e-01  -2.5000e-01 

 x = Pinv * b 
  +20.0000 
  +30.0000 
  +10.0000 

 Press return to continue.