ExplicitExpressions.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "ExplicitExpressions.h"
  2. // Functions from this file are implemented using the book "Handbook of Mathematical Function With Formulas, Graphs and Mathematical Tables"
  3. // by Milton Abramowitz and Irene A. Stegun, tenth printing, 1972.
  4. // Table 22.3 "Explicit Expressions"
  5. double ExplicitExpressionCheb(const int& type, const int& n, const double& x)
  6. {
  7. if (n == 0)
  8. {
  9. return 1;
  10. }
  11. if (type == 1)
  12. {
  13. if (n == 1)
  14. {
  15. return x;
  16. }
  17. int N = n / 2;
  18. double d = static_cast<double>(n) / 2;
  19. double c = pow(-1, 0) * std::tgamma(n) / (std::tgamma(1) * std::tgamma(n + 1));
  20. double g = pow(2*x, n);
  21. double res = 0;
  22. for (int m=0; m<=N; ++m)
  23. {
  24. c = pow(-1, m) * std::tgamma(n - m) / (std::tgamma(m + 1) * std::tgamma(n - 2*m + 1));
  25. g = pow(2*x, n - 2*m);
  26. res += c * g;
  27. }
  28. return d * res;
  29. }
  30. else if (type == 2)
  31. {
  32. if (n == 1)
  33. {
  34. return 2*x;
  35. }
  36. int N = n / 2;
  37. double d = 1;
  38. double c = 1;
  39. double g = pow(2*x, n);
  40. double res = 0;
  41. for (int m=0; m<=N; ++m)
  42. {
  43. c = pow(-1, m) * std::tgamma(n - m + 1) / (std::tgamma(m + 1) * std::tgamma(n - 2*m + 1));
  44. g = pow(2*x, n - 2*m);
  45. res += c * g;
  46. }
  47. return d * res;
  48. }
  49. else
  50. {
  51. printf("The order must be 1 or 2!");
  52. std::exit(9);
  53. }
  54. }
  55. double ExplicitExpressionLaguerre(const int& n, const double& x)
  56. {
  57. if (n == 0)
  58. {
  59. return 1;
  60. }
  61. else if (n == 1)
  62. {
  63. return 1 - x;
  64. }
  65. int N = n;
  66. double d = 1;
  67. double c = pow(-1, 0) * std::tgamma(n) / (std::tgamma(1) * std::tgamma(n + 1));
  68. double g = pow(2*x, n);
  69. double res = 0;
  70. for (int m=0; m<=N; ++m)
  71. {
  72. c = pow(-1, m) * std::tgamma(n + 1) / (pow(std::tgamma(m + 1), 2) * std::tgamma(n - m + 1));
  73. g = pow(x, m);
  74. res += c * g;
  75. }
  76. return d * res;
  77. }
  78. double ExplicitExpressionHermite(const int& n, const double& x)
  79. {
  80. if (n == 0)
  81. {
  82. return 1;
  83. }
  84. else if (n == 1)
  85. {
  86. return 2 * x;
  87. }
  88. int N = n / 2;
  89. double d = std::tgamma(n + 1);
  90. double c = pow(-1, 0) * std::tgamma(n) / (std::tgamma(1) * std::tgamma(n + 1));
  91. double g = pow(2*x, n);
  92. double res = 0;
  93. for (int m=0; m<=N; ++m)
  94. {
  95. c = pow(-1, m) / (std::tgamma(m + 1) * std::tgamma(n - 2 * m + 1));
  96. g = pow(2 * x, n - 2 * m);
  97. res += c * g;
  98. }
  99. return d * res;
  100. }