OrthPolTEST.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <gtest/gtest.h>
  2. #include "modules/OrthPol.cpp"
  3. #include "ExplicitExpressions.h"
  4. class OrthPolTest : public testing::Test {};
  5. // Test that OrthPol returns correct values if x=0
  6. TEST_F(OrthPolTest, ChebPolZeroCase) {
  7. double zero = 0.0;
  8. for (int i=0; i<=50; ++i)
  9. {
  10. std::vector<double> res = OrthPol(1, i, 0);
  11. if (i % 2 == 1)
  12. {
  13. EXPECT_DOUBLE_EQ(res[0], zero) << "Chebyshev polinom of the first kind " << i << "th-order are not equal 0";
  14. 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;
  15. }
  16. else
  17. {
  18. 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;
  19. EXPECT_DOUBLE_EQ(res[1], zero) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal 0";
  20. }
  21. }
  22. for (int i=0; i<=50; ++i)
  23. {
  24. std::vector<double> res = OrthPol(2, i, 0);
  25. if (i % 2 == 1)
  26. {
  27. EXPECT_DOUBLE_EQ(res[0], zero) << "Chebyshev polinom of the second kind " << i << "th-order are not equal 0";
  28. 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;
  29. }
  30. else
  31. {
  32. 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;
  33. EXPECT_DOUBLE_EQ(res[1], zero) << "Derivative of Chebyshev polinom of the second kind " << i << "th-order are not equal 0";
  34. }
  35. }
  36. }
  37. // Test that OrthPol returns correct values if x=+-1
  38. TEST_F(OrthPolTest, ChebPolOneCase) {
  39. for (int i=0; i<=50; ++i)
  40. {
  41. std::vector<double> res = OrthPol(1, i, 1);
  42. EXPECT_DOUBLE_EQ(res[0], 1) << "Chebyshev polinom of the first kind " << i << "th-order are not equal " << 1;
  43. 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);
  44. res = OrthPol(2, i, 1);
  45. EXPECT_DOUBLE_EQ(res[0], i+1) << "Chebyshev polinom of the second kind " << i << "th-order are not equal " << i+1;
  46. 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;
  47. res = OrthPol(1, i, -1);
  48. EXPECT_DOUBLE_EQ(res[0], pow(-1, i)) << "Chebyshev polinom of the first kind " << i << "th-order are not equal " << pow(-1, i);
  49. 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);
  50. res = OrthPol(2, i, -1);
  51. 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);
  52. 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);
  53. }
  54. }
  55. TEST_F(OrthPolTest, ExplicitExprChebyshevCase){
  56. double x = -1; // the left boundary
  57. double length = 2; //the length of the segmment
  58. double step = 0.2;
  59. std::vector<double> res_rec;
  60. double res_expl;
  61. for (int i=0; i <= static_cast<int>(length / step); ++i)
  62. {
  63. for (int j=0; j <= 20; ++j)
  64. {
  65. res_rec = OrthPol(1, j, x);
  66. res_expl = ExplicitExpressionCheb(1, j, x);
  67. if (res_rec[0] != 0.0)
  68. {
  69. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_CHEB);
  70. }
  71. else
  72. {
  73. EXPECT_NEAR(res_rec[0], res_expl, EPS_CHEB);
  74. }
  75. res_rec = OrthPol(2, j, x);
  76. res_expl = ExplicitExpressionCheb(2, j, x);
  77. if (res_rec[0] != 0.0)
  78. {
  79. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_CHEB);
  80. }
  81. else
  82. {
  83. EXPECT_NEAR(res_rec[0], res_expl, EPS_CHEB);
  84. }
  85. }
  86. x += step;
  87. }
  88. }
  89. TEST_F(OrthPolTest, ExplicitExprLaguerreCase){
  90. double x = -10; // the left boundary
  91. double length = 20; //the length of the segmment
  92. double step = 0.5;
  93. std::vector<double> res_rec;
  94. double res_expl;
  95. for (int i=0; i <= static_cast<int>(length / step); ++i)
  96. {
  97. for (int j=0; j <= 20; ++j)
  98. {
  99. res_rec = OrthPol(3, j, x);
  100. res_expl = ExplicitExpressionLaguerre(j, x);
  101. if (res_rec[0] != 0.0)
  102. {
  103. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_LAG);
  104. }
  105. else
  106. {
  107. EXPECT_NEAR(res_rec[0], res_expl, EPS_LAG);
  108. }
  109. }
  110. x += step;
  111. }
  112. }
  113. TEST_F(OrthPolTest, ExplicitExprHermiteCase){
  114. double x = -10; // the left boundary
  115. double length = 20; //the length of the segmment
  116. double step = 0.5;
  117. std::vector<double> res_rec;
  118. double res_expl;
  119. for (int i=0; i <= static_cast<int>(length / step); ++i)
  120. {
  121. for (int j=0; j <= 20; ++j)
  122. {
  123. res_rec = OrthPol(4, j, x);
  124. res_expl = ExplicitExpressionHermite(j, x);
  125. if (res_rec[0] != 0.0)
  126. {
  127. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_HERM);
  128. }
  129. else
  130. {
  131. EXPECT_NEAR(res_rec[0], res_expl, EPS_HERM);
  132. }
  133. }
  134. x += step;
  135. }
  136. }
  137. int main(int argc, char **argv) {
  138. testing::InitGoogleTest(&argc, argv);
  139. return RUN_ALL_TESTS();
  140. }