You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.8 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. // Compute the Legendre polynomials.
  2. #include <cln/number.h>
  3. #include <cln/integer.h>
  4. #include <cln/rational.h>
  5. #include <cln/univpoly.h>
  6. #include <cln/modinteger.h>
  7. #include <cln/univpoly_rational.h>
  8. #include <cln/univpoly_modint.h>
  9. #include <cln/io.h>
  10. #include <stdlib.h>
  11. using namespace cln;
  12. // Computes the n-th Legendre polynomial in R[x], using the formula
  13. // P_n(x) = 1/(2^n n!) * (d/dx)^n (x^2-1)^n. (Assume n >= 0.)
  14. const cl_UP_RA legendre (const cl_rational_ring& R, int n)
  15. {
  16. cl_univpoly_rational_ring PR = find_univpoly_ring(R);
  17. cl_UP_RA b = PR->create(2);
  18. b.set_coeff(2,1);
  19. b.set_coeff(1,0);
  20. b.set_coeff(0,-1);
  21. b.finalize(); // b is now x^2-1
  22. cl_UP_RA p = (n==0 ? PR->one() : expt_pos(b,n));
  23. for (int i = 0; i < n; i++)
  24. p = deriv(p);
  25. cl_RA factor = recip(factorial(n)*ash(1,n));
  26. for (int j = degree(p); j >= 0; j--)
  27. p.set_coeff(j, coeff(p,j) * factor);
  28. p.finalize();
  29. return p;
  30. }
  31. const cl_UP_MI legendre (const cl_modint_ring& R, int n)
  32. {
  33. cl_univpoly_modint_ring PR = find_univpoly_ring(R);
  34. cl_UP_MI b = PR->create(2);
  35. b.set_coeff(2,R->canonhom(1));
  36. b.set_coeff(1,R->canonhom(0));
  37. b.set_coeff(0,R->canonhom(-1));
  38. b.finalize(); // b is now x^2-1
  39. cl_UP_MI p = (n==0 ? PR->one() : expt_pos(b,n));
  40. for (int i = 0; i < n; i++)
  41. p = deriv(p);
  42. cl_MI factor = recip(R->canonhom(factorial(n)*ash(1,n)));
  43. for (int j = degree(p); j >= 0; j--)
  44. p.set_coeff(j, coeff(p,j) * factor);
  45. p.finalize();
  46. return p;
  47. }
  48. int main (int argc, char* argv[])
  49. {
  50. if (!(argc == 2 || argc == 3)) {
  51. fprint(stderr, "Usage: legendre n [m]\n");
  52. exit(1);
  53. }
  54. int n = atoi(argv[1]);
  55. if (!(n >= 0)) {
  56. fprint(stderr, "Usage: legendre n [m] with n >= 0\n");
  57. exit(1);
  58. }
  59. if (argc == 2) {
  60. cl_UP p = legendre(cl_RA_ring,n);
  61. fprint(stdout, p);
  62. } else {
  63. cl_I m = argv[2];
  64. cl_UP p = legendre(find_modint_ring(m),n);
  65. fprint(stdout, p);
  66. }
  67. fprint(stdout, "\n");
  68. }