SumFormulas.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Functions from this file are implemented using the book "Handbook of Mathematical Function With Formulas, Graphs and Mathematical Tables"
  2. // by Milton Abramowitz and Irene A. Stegun, tenth printing, 1972.
  3. // Table 22.12 "Sum Formulas"
  4. #pragma once
  5. #include <iostream>
  6. #include <cmath>
  7. #include <modules/OrthPol.h>
  8. double SumFormulaCheb1(const int& n, const double& x) // The left part of 22.12.2
  9. {
  10. double res = 0;
  11. for (int m=0; m<=n; ++m)
  12. {
  13. std::vector<double> T = OrthPol(1, 2*m, x);
  14. res += T[0];
  15. }
  16. return res;
  17. }
  18. double SumFormulaCheb2(const int& n, const double& x) // The left part of 22.12.3
  19. {
  20. double res = 0;
  21. for (int m=0; m<=n-1; ++m)
  22. {
  23. std::vector<double> T = OrthPol(1, 2*m + 1, x);
  24. res += T[0];
  25. }
  26. return res;
  27. }
  28. double SumFormulaCheb3(const int& n, const double& x) // The left part of 22.12.4
  29. {
  30. double res = 0;
  31. for (int m=0; m<=n; ++m)
  32. {
  33. std::vector<double> U = OrthPol(2, 2*m, x);
  34. res += U[0];
  35. }
  36. return res;
  37. }
  38. double SumFormulaCheb4(const int& n, const double& x) // The left part of 22.12.5
  39. {
  40. double res = 0;
  41. for (int m=0; m<=n-1; ++m)
  42. {
  43. std::vector<double> U = OrthPol(2, 2*m + 1, x);
  44. res += U[0];
  45. }
  46. return res;
  47. }
  48. int C_n_k(const int& n, int k)
  49. {
  50. if (k > n) return 0;
  51. if (k * 2 > n) k = n-k;
  52. if (k == 0) return 1;
  53. int result = n;
  54. for( int i = 2; i <= k; ++i ) {
  55. result *= (n-i+1);
  56. result /= i;
  57. }
  58. return result;
  59. }
  60. double SumFormulaLag1(const int& n, const double& mu, const double& x) // The left part of 22.12.7
  61. {
  62. double res = 0;
  63. for (int m=0; m<=n; ++m)
  64. {
  65. std::vector<double> L = OrthPol(3, n - m, x);
  66. int C_nm = C_n_k(n, m);
  67. res += C_nm * pow(mu, n - m) * pow(1 - mu, m) * L[0];
  68. }
  69. return res;
  70. }
  71. double SumFormulaHermite1(const int& n, const double& x) // The right part of 22.12.8, case x = y
  72. {
  73. double res = 0;
  74. for (int k=0; k<=n; ++k)
  75. {
  76. std::vector<double> H_1 = OrthPol(4, k, pow(2, 0.5) * x);
  77. std::vector<double> H_2 = OrthPol(4, n - k, pow(2, 0.5) * x);
  78. int C_nk = C_n_k(n, k);
  79. res += C_nk * H_1[0] * H_2[0];
  80. }
  81. double m = static_cast<double>(n) / 2;
  82. res = res / pow(2, m);
  83. return res;
  84. }