OrthPol.cpp 1.8 KB

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