gamma.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Author : philippe.billet@noos.fr
  4. //
  5. // Gamma function gamma(x)
  6. //
  7. //-----------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. #include "defs.h"
  10. void gamma(void);
  11. static void gammaf(void);
  12. static void gamma_of_sum(void);
  13. void
  14. eval_gamma(void)
  15. {
  16. push(cadr(p1));
  17. eval();
  18. gamma();
  19. }
  20. void
  21. gamma(void)
  22. {
  23. save();
  24. gammaf();
  25. restore();
  26. }
  27. static void
  28. gammaf(void)
  29. {
  30. // double d;
  31. p1 = pop();
  32. if (isrational(p1) && MEQUAL(p1->u.q.a, 1) && MEQUAL(p1->u.q.b, 2)) {
  33. push_symbol(PI);;
  34. push_rational(1,2);
  35. power();
  36. return;
  37. }
  38. if (isrational(p1) && MEQUAL(p1->u.q.a, 3) && MEQUAL(p1->u.q.b, 2)) {
  39. push_symbol(PI);;
  40. push_rational(1,2);
  41. power();
  42. push_rational(1,2);
  43. multiply();
  44. return;
  45. }
  46. // if (p1->k == DOUBLE) {
  47. // d = exp(lgamma(p1->u.d));
  48. // push_double(d);
  49. // return;
  50. // }
  51. if (isnegativeterm(p1)) {
  52. push_symbol(PI);
  53. push_integer(-1);
  54. multiply();
  55. push_symbol(PI);
  56. push(p1);
  57. multiply();
  58. sine();
  59. push(p1);
  60. multiply();
  61. push(p1);
  62. negate();
  63. gamma();
  64. multiply();
  65. divide();
  66. return;
  67. }
  68. if (car(p1) == symbol(ADD)) {
  69. gamma_of_sum();
  70. return;
  71. }
  72. push_symbol(GAMMA);
  73. push(p1);
  74. list(2);
  75. return;
  76. }
  77. static void
  78. gamma_of_sum(void)
  79. {
  80. p3 = cdr(p1);
  81. if (isrational(car(p3)) && MEQUAL(car(p3)->u.q.a, 1) && MEQUAL(car(p3)->u.q.b, 1)) {
  82. push(cadr(p3));
  83. push(cadr(p3));
  84. gamma();
  85. multiply();
  86. }
  87. else {
  88. if (isrational(car(p3)) && MEQUAL(car(p3)->u.q.a, -1) && MEQUAL(car(p3)->u.q.b, 1)) {
  89. push(cadr(p3));
  90. gamma();
  91. push(cadr(p3));
  92. push_integer(-1);
  93. add();
  94. divide();
  95. }
  96. else {
  97. push_symbol(GAMMA);
  98. push(p1);
  99. list(2);
  100. return;
  101. }
  102. }
  103. }
  104. #if SELFTEST
  105. static char *s[] = {
  106. "Gamma(a)",
  107. "Gamma(a)",
  108. // "float(gamma(10))",
  109. // "362880",
  110. "Gamma(x+1)",
  111. "x*Gamma(x)",
  112. "Gamma(1/2)",
  113. "pi^(1/2)",
  114. "Gamma(x-1)-Gamma(x)/(-1+x)",
  115. "0",
  116. "Gamma(-x)",
  117. "-pi/(x*Gamma(x)*sin(pi*x))",
  118. };
  119. void
  120. test_gamma(void)
  121. {
  122. test(__FILE__, s, sizeof s / sizeof (char *));
  123. }
  124. #endif