OrthPolTEST.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <gtest/gtest.h>
  2. #include "modules/OrthPol.cpp"
  3. #include <cmath>
  4. #define EPS_CHEB pow(10, -7)
  5. #define EPS_LAG pow(10, -5)
  6. class OrthPolTest : public testing::Test {};
  7. // Test that OrthPol returns correct values if x=0
  8. // TEST_F(OrthPolTest, ChebPolZeroCase) {
  9. // double zero = 0.0;
  10. // for (int i=0; i<=50; ++i)
  11. // {
  12. // std::vector<double> res = OrthPol(1, i, 0);
  13. // if (i % 2 == 1)
  14. // {
  15. // EXPECT_DOUBLE_EQ(res[0], zero) << "Chebyshev polinom of the first kind " << i << "th-order are not equal 0";
  16. // EXPECT_DOUBLE_EQ(res[1], pow(-1.0, (i-1)/2)*i) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal "<< i << "*(-1)^" << (i-1)/2;
  17. // }
  18. // else
  19. // {
  20. // EXPECT_DOUBLE_EQ(res[0], pow(-1.0, i/2)) << "Chebyshev polinom of the first kind " << i << "th-order are not equal (-1)^" << i/2;
  21. // EXPECT_DOUBLE_EQ(res[1], zero) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal 0";
  22. // }
  23. // }
  24. // for (int i=0; i<=50; ++i)
  25. // {
  26. // std::vector<double> res = OrthPol(2, i, 0);
  27. // if (i % 2 == 1)
  28. // {
  29. // EXPECT_DOUBLE_EQ(res[0], zero) << "Chebyshev polinom of the second kind " << i << "th-order are not equal 0";
  30. // EXPECT_DOUBLE_EQ(res[1], pow(-1.0, (i-1)/2)*(i+1)) << "Derivative of Chebyshev polinom of the second kind " << i << "th-order are not equal "<< (i+1) << "*(-1)^" << (i-1)/2;
  31. // }
  32. // else
  33. // {
  34. // EXPECT_DOUBLE_EQ(res[0], pow(-1.0, i/2)) << "Chebyshev polinom of the second kind " << i << "th-order are not equal (-1)^" << i/2;
  35. // EXPECT_DOUBLE_EQ(res[1], zero) << "Derivative of Chebyshev polinom of the second kind " << i << "th-order are not equal 0";
  36. // }
  37. // }
  38. // }
  39. // Test that OrthPol returns correct values if x=+-1
  40. // TEST_F(OrthPolTest, ChebPolOneCase) {
  41. // for (int i=0; i<=50; ++i)
  42. // {
  43. // std::vector<double> res = OrthPol(1, i, 1);
  44. // EXPECT_DOUBLE_EQ(res[0], 1) << "Chebyshev polinom of the first kind " << i << "th-order are not equal " << 1;
  45. // EXPECT_DOUBLE_EQ(res[1], pow(i, 2)) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal "<< pow(i, 2);
  46. // res = OrthPol(2, i, 1);
  47. // EXPECT_DOUBLE_EQ(res[0], i+1) << "Chebyshev polinom of the second kind " << i << "th-order are not equal " << i+1;
  48. // EXPECT_DOUBLE_EQ(res[1], i*(i+1)*(i+2)/3) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal "<< i*(i+1)*(i+2)/3;
  49. // res = OrthPol(1, i, -1);
  50. // EXPECT_DOUBLE_EQ(res[0], pow(-1, i)) << "Chebyshev polinom of the first kind " << i << "th-order are not equal " << pow(-1, i);
  51. // EXPECT_DOUBLE_EQ(res[1], pow(-1, i-1) * pow(i, 2)) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal "<< pow(-1, i-1) * pow(i, 2);
  52. // res = OrthPol(2, i, -1);
  53. // EXPECT_DOUBLE_EQ(res[0], pow(-1, i)*(i+1)) << "Chebyshev polinom of the first kind " << i << "th-order are not equal " << pow(-1, i)*(i+1);
  54. // EXPECT_DOUBLE_EQ(res[1], pow(-1, i+1) * (i*(i+1)*(i+2)/3)) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal "<< pow(-1, i+1) * (i*(i+1)*(i+2)/3);
  55. // }
  56. // }
  57. double ExplicitExpressionCheb(const int& type, const int& n, const double& x)
  58. {
  59. if (n == 0)
  60. {
  61. return 1;
  62. }
  63. if (type == 1)
  64. {
  65. if (n == 1)
  66. {
  67. return x;
  68. }
  69. int N = n / 2;
  70. double d = static_cast<double>(n) / 2;
  71. double c = pow(-1, 0) * std::tgamma(n) / (std::tgamma(1) * std::tgamma(n + 1));
  72. double g = pow(2*x, n);
  73. double res = 0;
  74. for (int m=0; m<=N; ++m)
  75. {
  76. c = pow(-1, m) * std::tgamma(n - m) / (std::tgamma(m + 1) * std::tgamma(n - 2*m + 1));
  77. g = pow(2*x, n - 2*m);
  78. res += c * g;
  79. }
  80. return d * res;
  81. }
  82. else if (type == 2)
  83. {
  84. if (n == 1)
  85. {
  86. return 2*x;
  87. }
  88. int N = n / 2;
  89. double d = 1;
  90. double c = 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(n - m + 1) / (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. }
  101. else
  102. {
  103. printf("The order must be 1 or 2!");
  104. std::exit(9);
  105. }
  106. }
  107. double ExplicitExpressionLaguerre(const int& n, const double& x)
  108. {
  109. if (n == 0)
  110. {
  111. return 1;
  112. }
  113. else if (n == 1)
  114. {
  115. return 1 - x;
  116. }
  117. int N = n;
  118. double d = 1;
  119. double c = pow(-1, 0) * std::tgamma(n) / (std::tgamma(1) * std::tgamma(n + 1));
  120. double g = pow(2*x, n);
  121. double res = 0;
  122. for (int m=0; m<=N; ++m)
  123. {
  124. c = pow(-1, m) * std::tgamma(n + 1) / (pow(std::tgamma(m + 1), 2) * std::tgamma(n - m + 1));
  125. g = pow(x, m);
  126. res += c * g;
  127. }
  128. return d * res;
  129. }
  130. TEST_F(OrthPolTest, ExplicitExpressionCase){
  131. double x = -1; // the left boundary
  132. double length = 2; //the length of the segmment
  133. double step = 0.2;
  134. std::vector<double> res_rec;
  135. double res_expl;
  136. for (int i=0; i <= static_cast<int>(length / step); ++i)
  137. {
  138. for (int j=0; j <= 20; ++j)
  139. {
  140. res_rec = OrthPol(1, j, x);
  141. res_expl = ExplicitExpressionCheb(1, j, x);
  142. // std::cout << j << " " << x << " " << res_rec[0] << "\n";
  143. EXPECT_NEAR(res_rec[0], res_expl, EPS_CHEB);
  144. res_rec = OrthPol(2, j, x);
  145. res_expl = ExplicitExpressionCheb(2, j, x);
  146. // std::cout << j << " " << x << " " << res_rec[0] << "\n";
  147. EXPECT_NEAR(res_rec[0], res_expl, EPS_CHEB);
  148. }
  149. x += step;
  150. }
  151. x = -10;
  152. length = 20;
  153. step = 0.5;
  154. for (int i=0; i <= static_cast<int>(length / step); ++i)
  155. {
  156. for (int j=0; j <= 20; ++j)
  157. {
  158. res_rec = OrthPol(3, j, x);
  159. res_expl = ExplicitExpressionLaguerre(j, x);
  160. // std::cout << j << " " << x << " " << res_rec[0] << "\n";
  161. EXPECT_NEAR(res_rec[0], res_expl, EPS_LAG);
  162. }
  163. x += step;
  164. }
  165. }
  166. // int main(int argc, char **argv) {
  167. // testing::InitGoogleTest(&argc, argv);
  168. // return RUN_ALL_TESTS();
  169. // }