Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1.  
  2.  
  3. int expm3(double t, double *a, double *aa, double X[3], double dis)
  4. /*
  5. computes exp(t a)
  6.  
  7. input
  8. t double
  9. a 3x3 double matrix
  10. aa a*a
  11. dis negative, if matrix has complex conjugate pair, zero if two eigenvalues are equal and positive, if all eigenvalues are real and different
  12. X double[3], containing either:
  13. the three real eigenvalues in ascending order
  14. or
  15. first the real eigenvalue, then the real component and the absolute value of the complex component of the complex conjugate eigenvalues
  16.  
  17. output
  18. aa the exponential
  19.  
  20. return unspecified integer
  21.  
  22. */
  23. {
  24.  
  25. double la1; // and la2 la3: eigenvalues, la2 and la3 might be complex
  26. double r1, r3, R; // and r2: putzer coefficients, r2 might be complex
  27. int ca = 0;
  28.  
  29.  
  30. /* aa = a.a */
  31. int i, j, k;
  32. for (i = 0; i < 9; i++) aa[i] = 0.0;
  33.  
  34. for (i = 0; i < 3; i++)
  35. for (k = 0; k < 3; k++)
  36. for (j = 0; j < 3; j++)
  37. aa[i*3 + j] += a[i*3 + k]*a[k*3 + j];
  38.  
  39.  
  40. if (dis == 0.) // real roots, at least x[1]=x[2]
  41. {
  42. double la2, r2;
  43. la1 = X[0];
  44. la2 = X[1];
  45. if (cabs(la1 - la2) < DBL_EPSILON ) // la1 = la2 = la3
  46. {
  47. ca = 1;
  48. r1 = exp(la1 * t);
  49. r2 = t * exp(la1 * t);
  50. r3 = 0.5 * t * t * exp(la1 * t);
  51. R = r2 - la2*r3;
  52.  
  53. } else
  54. {
  55. ca = 2;
  56. r1 = exp(la1 * t);
  57. r2 = (expm1(la1 * t) - expm1(la2 * t)) / (la1 - la2); // la1 != la2
  58. r3 = (exp(la1 * t) - exp(la2 * t)*(1.+t*(la1-la2))) / square(la1-la2);
  59. R = r2 - la2*r3;
  60. }
  61.  
  62. } else if (dis > 0.0) // three distinct real roots
  63. {
  64. double la2, la3;
  65. double r2;
  66. ca = 3;
  67. la1 = X[0];
  68. la2 = X[1];
  69. la3 = X[2];
  70. r1 = exp(la1 * t);
  71. r2 = (expm1(la1 * t) - expm1(la2 * t))/ (la1 - la2); // expm1(a*t) - expm1(b*t) is more stable then exp(a*t) - exp(b*t)
  72. r3 = -((la2 - la3)*exp(la1*t) + (la3 - la1)*exp(la2*t) + (la1 - la2)*exp(la3*t))
  73. / ((la1 - la2)*(la2 - la3)*(la3 - la1));
  74. R = r2 - la2*r3;
  75.  
  76. } else // One real and a complex conjugate pair of roots, all different
  77. {
  78. double complex la2, la3; // only these values are possibly complex
  79. double complex r2;
  80. ca = 4;
  81.  
  82. la1 = X[0];
  83. la2 = X[1] + I*X[2];
  84. la3 = X[1] - I*X[2];
  85.  
  86. r1 = exp(la1 * t);
  87. r2 = (cexp(la1 * t) - cexp(la2 * t))/ ((complex) la1 - la2); // possibly complex, cexpm1 still missing, so use cexp(a*t) - cexp(b*t)
  88. r3 = -((la2 - la3)*exp(la1*t) + (la3 - la1)*cexp(la2*t) + (la1 - la2)*cexp(la3*t)) // r3 is real, see short comm. by r.r. huilgol
  89. / ((la1 - la2)*(la2 - la3)*(la3 - la1));
  90. R = r2 - la2*r3; // also real
  91. }
  92.  
  93.  
  94. for (i = 0;i < 3; i++) for (j = 0;j < 3;j++)
  95. {
  96. aa[i*3+j] = r3 * aa[i*3+j] + (R - la1*r3)* a[i*3+j] ;
  97. if (i == j) aa[i*3+j] += r1 - la1 * R;
  98. //= r1 I + (A - la1 I)*(r2 I + r3*(A - la2 I))
  99. }
  100. return ca;
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement