OrthPol.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "OrthPol.h"
  2. // The implementation of the function OrthPol() are from the book "Computation of special functions" by Shanjie Zhang and Jianmming Jin, printing 1996
  3. // chapter 2, page 12
  4. /**
  5. * Purpose: Compute orthogonal polynomials: Chebyshev, Laguerre, Hermite polynomials, and their derivatives
  6. *
  7. * @param KF
  8. * KF = 1 for Chebyshev polynomial of the first kind
  9. * KF = 2 for Chebyshev polynomial of the second kind
  10. * KF = 3 for Laguerre polynomial
  11. * KF = 4 for Hermite polynomial
  12. * @param n - Integer order of orthogonal polynomial.
  13. * @param x - Argument of orthogonal polynomial.
  14. * @return Vector with values of the orthogonal polynomial (the first place) of order n with argument x, and its first derivatives (the second place).
  15. * @throws None
  16. */
  17. std::vector<double> OrthPol(const int& KF, const int& n, const double& x)
  18. {
  19. std::vector<double> result;
  20. double* PL = new double[n + 1];
  21. double* DPL = new double[n + 1];
  22. double A = 2.0;
  23. double B = 0.0;
  24. double C = 1.0;
  25. double Y0 = 1.0;
  26. double Y1 = 2.0 * x;
  27. double DY0 = 0.0;
  28. double DY1 = 2.0;
  29. PL[0] = 1.0;
  30. PL[1] = 2.0 * x;
  31. DPL[0] = 0.0;
  32. DPL[1] = 2.0;
  33. if (KF == 1)
  34. {
  35. Y1 = x;
  36. DY1 = 1.0;
  37. PL[1] = x;
  38. DPL[1] = 1.0;
  39. }
  40. else if (KF == 3)
  41. {
  42. Y1 = 1.0 - x;
  43. DY1 = -1.0;
  44. PL[1] = 1.0 - x;
  45. DPL[1] = -1.0;
  46. }
  47. for (int k = 2; k <= n; k++)
  48. {
  49. if (KF == 3)
  50. {
  51. A = -1.0 / k;
  52. B = 2.0 + A;
  53. C = 1.0 + A;
  54. }
  55. else if (KF == 4)
  56. {
  57. C = 2.0 * (k - 1.0);
  58. }
  59. double YN = (A * x + B) * Y1 - C * Y0;
  60. double DYN = A * Y1 + (A * x + B) * DY1 - C * DY0;
  61. PL[k] = YN;
  62. DPL[k] = DYN;
  63. Y0 = Y1;
  64. Y1 = YN;
  65. DY0 = DY1;
  66. DY1 = DYN;
  67. }
  68. result.push_back(PL[n]);
  69. result.push_back(DPL[n]);
  70. return result;
  71. }