OrthPolTEST.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <gtest/gtest.h>
  2. #include "modules/OrthPol.cpp"
  3. #include "ExplicitExpressions.h"
  4. #include "SumFormulas.cpp"
  5. class OrthPolTest : public testing::Test {};
  6. // Test that OrthPol returns correct values if x=0
  7. TEST_F(OrthPolTest, ChebPolZeroCase) {
  8. double zero = 0.0;
  9. for (int i=0; i<=50; ++i)
  10. {
  11. std::vector<double> res = OrthPol(1, i, 0);
  12. if (i % 2 == 1)
  13. {
  14. EXPECT_DOUBLE_EQ(res[0], zero) << "Chebyshev polinom of the first kind " << i << "th-order are not equal 0";
  15. 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;
  16. }
  17. else
  18. {
  19. 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;
  20. EXPECT_DOUBLE_EQ(res[1], zero) << "Derivative of Chebyshev polinom of the first kind " << i << "th-order are not equal 0";
  21. }
  22. }
  23. for (int i=0; i<=50; ++i)
  24. {
  25. std::vector<double> res = OrthPol(2, i, 0);
  26. if (i % 2 == 1)
  27. {
  28. EXPECT_DOUBLE_EQ(res[0], zero) << "Chebyshev polinom of the second kind " << i << "th-order are not equal 0";
  29. 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;
  30. }
  31. else
  32. {
  33. 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;
  34. EXPECT_DOUBLE_EQ(res[1], zero) << "Derivative of Chebyshev polinom of the second kind " << i << "th-order are not equal 0";
  35. }
  36. }
  37. }
  38. // Test that OrthPol returns correct values if x=+-1
  39. TEST_F(OrthPolTest, ChebPolOneCase) {
  40. for (int i=0; i<=50; ++i)
  41. {
  42. std::vector<double> res = OrthPol(1, i, 1);
  43. EXPECT_DOUBLE_EQ(res[0], 1) << "Chebyshev polinom of the first kind " << i << "th-order are not equal " << 1;
  44. 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);
  45. res = OrthPol(2, i, 1);
  46. EXPECT_DOUBLE_EQ(res[0], i+1) << "Chebyshev polinom of the second kind " << i << "th-order are not equal " << i+1;
  47. 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;
  48. res = OrthPol(1, i, -1);
  49. EXPECT_DOUBLE_EQ(res[0], pow(-1, i)) << "Chebyshev polinom of the first kind " << i << "th-order are not equal " << pow(-1, i);
  50. 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);
  51. res = OrthPol(2, i, -1);
  52. 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);
  53. 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);
  54. }
  55. }
  56. TEST_F(OrthPolTest, ExplicitExprChebyshevCase){
  57. double x = -1; // the left boundary
  58. double length = 2; //the length of the segmment
  59. double step = 0.2;
  60. std::vector<double> res_rec;
  61. double res_expl;
  62. for (int i=0; i <= static_cast<int>(length / step); ++i)
  63. {
  64. for (int j=0; j <= 20; ++j)
  65. {
  66. res_rec = OrthPol(1, j, x);
  67. res_expl = ExplicitExpressionCheb(1, j, x);
  68. if (res_rec[0] != 0.0)
  69. {
  70. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_CHEB);
  71. }
  72. else
  73. {
  74. EXPECT_NEAR(res_rec[0], res_expl, EPS_CHEB);
  75. }
  76. res_rec = OrthPol(2, j, x);
  77. res_expl = ExplicitExpressionCheb(2, j, x);
  78. if (res_rec[0] != 0.0)
  79. {
  80. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_CHEB);
  81. }
  82. else
  83. {
  84. EXPECT_NEAR(res_rec[0], res_expl, EPS_CHEB);
  85. }
  86. }
  87. x += step;
  88. }
  89. }
  90. TEST_F(OrthPolTest, ExplicitExprLaguerreCase){
  91. double x = -10; // the left boundary
  92. double length = 20; //the length of the segmment
  93. double step = 0.5;
  94. std::vector<double> res_rec;
  95. double res_expl;
  96. for (int i=0; i <= static_cast<int>(length / step); ++i)
  97. {
  98. for (int j=0; j <= 20; ++j)
  99. {
  100. res_rec = OrthPol(3, j, x);
  101. res_expl = ExplicitExpressionLaguerre(j, x);
  102. if (res_rec[0] != 0.0)
  103. {
  104. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_LAG);
  105. }
  106. else
  107. {
  108. EXPECT_NEAR(res_rec[0], res_expl, EPS_LAG);
  109. }
  110. }
  111. x += step;
  112. }
  113. }
  114. TEST_F(OrthPolTest, ExplicitExprHermiteCase){
  115. double x = -10; // the left boundary
  116. double length = 20; //the length of the segmment
  117. double step = 0.5;
  118. std::vector<double> res_rec;
  119. double res_expl;
  120. for (int i=0; i <= static_cast<int>(length / step); ++i)
  121. {
  122. for (int j=0; j <= 20; ++j)
  123. {
  124. res_rec = OrthPol(4, j, x);
  125. res_expl = ExplicitExpressionHermite(j, x);
  126. if (res_rec[0] != 0.0)
  127. {
  128. EXPECT_NEAR(1, res_expl / res_rec[0], EPS_HERM);
  129. }
  130. else
  131. {
  132. EXPECT_NEAR(res_rec[0], res_expl, EPS_HERM);
  133. }
  134. }
  135. x += step;
  136. }
  137. }
  138. TEST_F(OrthPolTest, SumFormulaChebCase){
  139. double x = -1; // the left boundary
  140. double length = 2; //the length of the segmment
  141. double step = 0.2;
  142. std::vector<double> U;
  143. std::vector<double> T;
  144. double left_part;
  145. double right_part;
  146. for (int i=0; i <= static_cast<int>(length / step); ++i)
  147. {
  148. for (int j=0; j <= 200; ++j)
  149. {
  150. left_part = SumFormulaCheb1(j, x);
  151. U = OrthPol(2, 2*j, x);
  152. right_part = 0.5 * (1 + U[0]);
  153. if (right_part != 0){
  154. EXPECT_NEAR(left_part / right_part, 1, pow(10, -9));
  155. }
  156. else{
  157. EXPECT_NEAR(left_part, right_part, pow(10, -9));
  158. }
  159. if (2*j > 1){
  160. left_part = SumFormulaCheb2(j, x);
  161. U = OrthPol(2, 2*j - 1, x);
  162. right_part = 0.5 * U[0];
  163. if (right_part != 0){
  164. EXPECT_NEAR(left_part / right_part, 1, pow(10, -9));
  165. }
  166. else{
  167. EXPECT_NEAR(left_part, right_part, pow(10, -9));
  168. }
  169. }
  170. if (x != 1 && x != -1){
  171. left_part = SumFormulaCheb3(j, x);
  172. T = OrthPol(1, 2*j + 2, x);
  173. right_part = 0.5 * (1 - T[0]) / (1 - pow(x, 2));
  174. if (right_part != 0){
  175. EXPECT_NEAR(left_part / right_part, 1, pow(10, -9));
  176. }
  177. else{
  178. EXPECT_NEAR(left_part, right_part, pow(10, -9));
  179. }
  180. }
  181. if (x != 1 && x != -1){
  182. left_part = SumFormulaCheb4(j, x);
  183. T = OrthPol(1, 2*j + 1, x);
  184. right_part = 0.5 * (x - T[0]) / (1 - pow(x, 2));
  185. if (right_part != 0){
  186. EXPECT_NEAR(left_part / right_part, 1, pow(10, -9));
  187. }
  188. else{
  189. EXPECT_NEAR(left_part, right_part, pow(10, -9));
  190. }
  191. }
  192. }
  193. x += step;
  194. }
  195. }
  196. TEST_F(OrthPolTest, SumFormulaLagCase){
  197. double x = -1; // the left boundary
  198. double length = 2; //the length of the segmment
  199. double step = 0.1;
  200. std::vector<double> L;
  201. double left_part;
  202. double right_part;
  203. for (int i=0; i <= static_cast<int>(length / step); ++i)
  204. {
  205. for (int j=0; j <= 25; ++j)
  206. {
  207. for (double mu=0.1; mu <=0.9; mu += 0.1){
  208. left_part = SumFormulaLag1(j, mu, x);
  209. L = OrthPol(3, j, mu * x);
  210. right_part = L[0];
  211. if (right_part != 0){
  212. EXPECT_NEAR(left_part / right_part, 1, pow(10, -9));
  213. }
  214. else{
  215. EXPECT_NEAR(left_part, right_part, pow(10, -9));
  216. } }
  217. }
  218. x += step;
  219. }
  220. }
  221. TEST_F(OrthPolTest, SumFormulaHermiteCase){
  222. double x = -1; // the left boundary
  223. double length = 2; //the length of the segmment
  224. double step = 0.1;
  225. std::vector<double> H;
  226. double left_part;
  227. double right_part;
  228. for (int i=0; i <= static_cast<int>(length / step); ++i)
  229. {
  230. for (int j=0; j <= 25; ++j)
  231. {
  232. right_part = SumFormulaHermite1(j, x);
  233. H = OrthPol(4, j, 2 * x);
  234. left_part = H[0];
  235. if (left_part != 0){
  236. EXPECT_NEAR(right_part / left_part, 1, pow(10, -9));
  237. }
  238. else{
  239. EXPECT_NEAR(left_part, right_part, pow(10, -9));
  240. } }
  241. x += step;
  242. }
  243. }
  244. int main(int argc, char **argv) {
  245. testing::InitGoogleTest(&argc, argv);
  246. return RUN_ALL_TESTS();
  247. }