mfuntort.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**************************************************************************
  2. **
  3. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  4. **
  5. ** Meschach Library
  6. **
  7. ** This Meschach Library is provided "as is" without any express
  8. ** or implied warranty of any kind with respect to this software.
  9. ** In particular the authors shall not be liable for any direct,
  10. ** indirect, special, incidental or consequential damages arising
  11. ** in any way from use of the software.
  12. **
  13. ** Everyone is granted permission to copy, modify and redistribute this
  14. ** Meschach Library, provided:
  15. ** 1. All copies contain this copyright notice.
  16. ** 2. All modified copies shall carry a notice stating who
  17. ** made the last modification and the date of such modification.
  18. ** 3. No charge is made for this software or works derived from it.
  19. ** This clause shall not be construed as constraining other software
  20. ** distributed on the same medium as this software, nor is a
  21. ** distribution fee considered a charge.
  22. **
  23. ***************************************************************************/
  24. /* mfuntort.c, 10/11/93 */
  25. static char rcsid[] = "$Id: mfuntort.c,v 1.2 1994/01/14 01:08:06 des Exp $";
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include "matrix.h"
  29. #include "matrix2.h"
  30. #define errmesg(mesg) printf("Error: %s error: line %d\n",mesg,__LINE__)
  31. #define notice(mesg) printf("# Testing %s...\n",mesg);
  32. #define DIM 10
  33. void main()
  34. {
  35. MAT *A, *B, *C, *OUTA, *OUTB, *TMP;
  36. MAT *exp_A_expected, *exp_A;
  37. VEC *x, *b;
  38. double c, eps = 1e-10;
  39. int i, j, q_out, j_out;
  40. mem_info_on(TRUE);
  41. A = m_get(DIM,DIM);
  42. B = m_get(DIM,DIM);
  43. C = m_get(DIM,DIM);
  44. OUTA = m_get(DIM,DIM);
  45. OUTB = m_get(DIM,DIM);
  46. TMP = m_get(DIM,DIM);
  47. x = v_get(DIM);
  48. b = v_get(6);
  49. notice("exponent of a matrix");
  50. m_ident(A);
  51. mem_stat_mark(1);
  52. _m_exp(A,eps,OUTA,&q_out,&j_out);
  53. printf("# q_out = %d, j_out = %d\n",q_out,j_out);
  54. m_exp(A,eps,OUTA);
  55. sm_mlt(exp(1.0),A,A);
  56. m_sub(OUTA,A,TMP);
  57. printf("# ||exp(I) - e*I|| = %g\n",m_norm_inf(TMP));
  58. m_rand(A);
  59. m_transp(A,TMP);
  60. m_add(A,TMP,A);
  61. B = m_copy(A,B);
  62. m_exp(A,eps,OUTA);
  63. symmeig(B,OUTB,x);
  64. m_zero(TMP);
  65. for (i=0; i < x->dim; i++)
  66. TMP->me[i][i] = exp(x->ve[i]);
  67. m_mlt(OUTB,TMP,C);
  68. mmtr_mlt(C,OUTB,TMP);
  69. m_sub(TMP,OUTA,TMP);
  70. printf("# ||exp(A) - Q*exp(lambda)*Q^T|| = %g\n",m_norm_inf(TMP));
  71. notice("polynomial of a matrix");
  72. m_rand(A);
  73. m_transp(A,TMP);
  74. m_add(A,TMP,A);
  75. B = m_copy(A,B);
  76. v_rand(b);
  77. m_poly(A,b,OUTA);
  78. symmeig(B,OUTB,x);
  79. m_zero(TMP);
  80. for (i=0; i < x->dim; i++) {
  81. c = b->ve[b->dim-1];
  82. for (j=b->dim-2; j >= 0; j--)
  83. c = c*x->ve[i] + b->ve[j];
  84. TMP->me[i][i] = c;
  85. }
  86. m_mlt(OUTB,TMP,C);
  87. mmtr_mlt(C,OUTB,TMP);
  88. m_sub(TMP,OUTA,TMP);
  89. printf("# ||poly(A) - Q*poly(lambda)*Q^T|| = %g\n",m_norm_inf(TMP));
  90. mem_stat_free(1);
  91. /* Brook Milligan's test */
  92. M_FREE(A);
  93. M_FREE(B);
  94. M_FREE(C);
  95. notice("exponent of a nonsymmetric matrix");
  96. A = m_get (2, 2);
  97. A -> me [0][0] = 1.0;
  98. A -> me [0][1] = 1.0;
  99. A -> me [1][0] = 4.0;
  100. A -> me [1][1] = 1.0;
  101. exp_A_expected = m_get(2, 2);
  102. exp_A_expected -> me [0][0] = exp (3.0) / 2.0 + exp (-1.0) / 2.0;
  103. exp_A_expected -> me [0][1] = exp (3.0) / 4.0 - exp (-1.0) / 4.0;
  104. exp_A_expected -> me [1][0] = exp (3.0) - exp (-1.0);
  105. exp_A_expected -> me [1][1] = exp (3.0) / 2.0 + exp (-1.0) / 2.0;
  106. printf ("A:\n");
  107. for (i = 0; i < 2; i++)
  108. {
  109. for (j = 0; j < 2; j++)
  110. printf (" %15.8e", A -> me [i][j]);
  111. printf ("\n");
  112. }
  113. printf ("\nexp(A) (expected):\n");
  114. for (i = 0; i < 2; i++)
  115. {
  116. for (j = 0; j < 2; j++)
  117. printf (" %15.8e", exp_A_expected -> me [i][j]);
  118. printf ("\n");
  119. }
  120. mem_stat_mark(3);
  121. exp_A = m_exp (A, 1e-16,NULL);
  122. mem_stat_free(3);
  123. printf ("\nexp(A):\n");
  124. for (i = 0; i < 2; i++)
  125. {
  126. for (j = 0; j < 2; j++)
  127. printf (" %15.8e", exp_A -> me [i][j]);
  128. printf ("\n");
  129. }
  130. printf ("\nexp(A) - exp(A) (expected):\n");
  131. for (i = 0; i < 2; i++)
  132. {
  133. for (j = 0; j < 2; j++)
  134. printf (" %15.8e", exp_A -> me [i][j] - exp_A_expected -> me [i][j]);
  135. printf ("\n");
  136. }
  137. M_FREE(A);
  138. M_FREE(B);
  139. M_FREE(C);
  140. M_FREE(exp_A);
  141. M_FREE(exp_A_expected);
  142. M_FREE(OUTA);
  143. M_FREE(OUTB);
  144. M_FREE(TMP);
  145. V_FREE(b);
  146. V_FREE(x);
  147. mem_info();
  148. }