Linear Algebra and the C Language/a0hp


The Maclaurin series of the exponential function is :

      Exp(A) = Ide + A + (1/2) A2 + (1/6) A3 + ... + 1/(n-1)! A(n-1)

Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00b.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
#define   RCA        RC4
/* ------------------------------------ */
void fun(void)
{
int n = 10; 
int i =  0; 

double **A   = rE_mR(i_mR(RCA,RCA),999,+1.E-3); 
double **A_n =       i_mR(RCA,RCA);
double **sA  =       i_mR(RCA,RCA);

double **ExpA      = i_mR(RCA,RCA);   
double **NewExpA   = i_mR(RCA,RCA);   
       
   clrscrn();   
   printf(" Copy/Paste into the octave window.       \n\n");
   p_Octave_mR(A, "A", P3);
   printf(" expm (A)\n\n");
   stop();  
   
   clrscrn();    
   printf(" A:");
   p_mR(A, S4,P4,C6);
  
  for(i = 0; i <= n; i++)
   {
     pow_mR(i, A, A_n);    
    smul_mR(1./factorial(i), A_n, sA);    
     add_mR(ExpA,sA,NewExpA);
     
     printf(" NewExpA   n = %d:",i);
     p_mR(NewExpA, S8,P6,C6);       
     c_mR(NewExpA,ExpA);
     stop();
   }

  clrscrn();    
  printf(" ExpA:");
  p_mR(ExpA, S8,P4,C6);

  f_mR(A);
  f_mR(A_n);
  f_mR(sA);
  f_mR(ExpA);
  f_mR(NewExpA);
}
/* ------------------------------------ */
int main(void)
{
time_t t;

  srand(time(&t));

do
{
    fun();
    
} while(stop_w());

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

Screen output example:

                                                                                       
 Copy/Paste into the octave window.       

 A=[
+0.127,+0.018,-0.685,+0.637;
+0.063,+0.368,-0.814,+0.623;
-0.898,-0.068,+0.520,-0.615;
+0.040,-0.744,+0.700,+0.268]

 expm (A)

 Press return to continue. 


 A:
+0.1270 +0.0180 -0.6850 +0.6370 
+0.0630 +0.3680 -0.8140 +0.6230 
-0.8980 -0.0680 +0.5200 -0.6150 
+0.0400 -0.7440 +0.7000 +0.2680 

 NewExpA   n = 0:
+1.000000 +0.000000 +0.000000 +0.000000 
+0.000000 +1.000000 +0.000000 +0.000000 
+0.000000 +0.000000 +1.000000 +0.000000 
+0.000000 +0.000000 +0.000000 +1.000000 

 Press return to continue. 
 NewExpA   n = 1:
+1.127000 +0.018000 -0.685000 +0.637000 
+0.063000 +1.368000 -0.814000 +0.623000 
-0.898000 -0.068000 +1.520000 -0.615000 
+0.040000 -0.744000 +0.700000 +1.268000 

 Press return to continue. 
 NewExpA   n = 2:
+1.455936 -0.191219 -0.690974 +0.979052 
+0.456539 +1.232199 -0.978944 +1.091484 
-1.202945 +0.122506 +1.775191 -1.164505 
-0.289836 -1.004032 +1.264908 +0.869646 

 Press return to continue. 
 NewExpA   n = 3:
+1.471817 -0.299603 -0.630536 +1.037230 
+0.525966 +1.105457 -0.951231 +1.222509 
-1.295568 +0.274538 +1.709145 -1.291097 
-0.483667 -0.951921 +1.415744 +0.594218 

 Press return to continue. 
 NewExpA   n = 4:
+1.457627 -0.321351 -0.593161 +1.017483 
+0.521263 +1.069267 -0.910797 +1.218343 
-1.282552 +0.312777 +1.663328 -1.280495 
-0.525617 -0.899334 +1.409742 +0.529822 

 Press return to continue. 
 NewExpA   n = 5:
+1.450122 -0.320573 -0.586554 +1.007310 
+0.513392 +1.066656 -0.900639 +1.208038 
-1.273427 +0.314684 +1.652039 -1.267868 
-0.525457 -0.885951 +1.397288 +0.528317 

 Press return to continue. 
 NewExpA   n = 6:
+1.448915 -0.319361 -0.586417 +1.005463 
+0.511609 +1.067635 -0.899708 +1.205430 
-1.271440 +0.313390 +1.651233 -1.264980 
-0.523460 -0.884801 +1.394199 +0.530933 

 Press return to continue. 
 NewExpA   n = 7:
+1.448876 -0.319106 -0.586615 +1.005378 
+0.511451 +1.067950 -0.899839 +1.205173 
-1.271295 +0.313028 +1.651418 -1.264733 
-0.523002 -0.884984 +1.393902 +0.531588 

 Press return to continue. 
 NewExpA   n = 8:
+1.448899 -0.319085 -0.586657 +1.005407 
+0.511465 +1.067990 -0.899889 +1.205187 
-1.271315 +0.312987 +1.651476 -1.264756 
-0.522959 -0.885050 +1.393920 +0.531655 

 Press return to continue. 
 NewExpA   n = 9:
+1.448904 -0.319086 -0.586661 +1.005414 
+0.511470 +1.067991 -0.899895 +1.205194 
-1.271322 +0.312987 +1.651483 -1.264765 
-0.522961 -0.885058 +1.393929 +0.531655 

 Press return to continue. 
 NewExpA   n = 10:
+1.448904 -0.319086 -0.586661 +1.005415 
+0.511471 +1.067990 -0.899895 +1.205195 
-1.271323 +0.312988 +1.651483 -1.264766 
-0.522962 -0.885058 +1.393930 +0.531654 

 Press return to continue. 

 ExpA:
+1.4489 -0.3191 -0.5867 +1.0054 
+0.5115 +1.0680 -0.8999 +1.2052 
-1.2713 +0.3130 +1.6515 -1.2648 
-0.5230 -0.8851 +1.3939 +0.5317 


 Press   return to continue
 Press X return to stop