polydiv.tst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. % polydiv.tst -*- REDUCE -*-
  2. % Test and demonstration file for enhanced polynomial division
  3. % file polydiv.red.
  4. % F.J.Wright@Maths.QMW.ac.uk, 7 Nov 1995.
  5. % The example from "Computer Algebra" by Davenport, Siret & Tournier,
  6. % first edition, section 2.3.3.
  7. % First check that remainder still works as before.
  8. % Compute the gcd of the polynomials a and b by Euclid's algorithm:
  9. a := aa := x^8 + x^6 - 3x^4 - 3x^3 + 8x^2 + 2x - 5;
  10. b := bb := 3x^6 + 5x^4 - 4x^2 - 9x + 21;
  11. on rational; off allfac;
  12. c := remainder(a, b); a := b$ b := c$
  13. c := remainder(a, b); a := b$ b := c$
  14. c := remainder(a, b); a := b$ b := c$
  15. c := remainder(a, b); a := b$ b := c$
  16. c := remainder(a, b);
  17. off rational;
  18. % Repeat using pseudo-remainders, to avoid rational arithmetic:
  19. a := aa;
  20. b := bb;
  21. c := pseudo_remainder(a, b); a := b$ b := c$
  22. c := pseudo_remainder(a, b); a := b$ b := c$
  23. c := pseudo_remainder(a, b); a := b$ b := c$
  24. c := pseudo_remainder(a, b); a := b$ b := c$
  25. c := pseudo_remainder(a, b);
  26. % Example from Chris Herssens <herc@sulu.luc.ac.be>
  27. % involving algebraic numbers in the coefficient ring
  28. % (for which naive pseudo-division fails in REDUCE):
  29. factor x;
  30. a:=8*(15*sqrt(2)*x**3 + 18*sqrt(2)*x**2 + 10*sqrt(2)*x + 12*sqrt(2) -
  31. 5*x**4 - 6*x**3 - 30*x**2 - 36*x);
  32. b:= - 16320*sqrt(2)*x**3 - 45801*sqrt(2)*x**2 - 50670*sqrt(2)*x -
  33. 26534*sqrt(2) + 15892*x**3 + 70920*x**2 + 86352*x + 24780;
  34. pseudo_remainder(a, b, x);
  35. % Note: We must specify the division variable even though the
  36. % polynomials are apparently univariate:
  37. pseudo_remainder(a, b);
  38. % Confirm that quotient * b + remainder = constant * a:
  39. pseudo_divide(a, b, x);
  40. first ws * b + second ws;
  41. ws / a; % is this constant?
  42. on rationalize;
  43. ws; % yes, it is constant
  44. off rationalize;
  45. on allfac; remfac x;
  46. procedure test_pseudo_division(a, b, x);
  47. begin scalar qr, L;
  48. qr := pseudo_divide(a, b, x);
  49. L := lcof(b,x);
  50. %% For versions of REDUCE prior to 3.6 use:
  51. %% L := if b freeof x then b else lcof(b,x);
  52. if first qr * b + second qr =
  53. L^(deg(a,x)-deg(b,x)+1) * a then
  54. write "Pseudo-division OK"
  55. else
  56. write "Pseudo-division failed"
  57. end;
  58. a := 5x^4 + 4x^3 + 3x^2 + 2x + 1;
  59. test_pseudo_division(a, x, x);
  60. test_pseudo_division(a, x^3, x);
  61. test_pseudo_division(a, x^5, x);
  62. test_pseudo_division(a, x^3 + x, x);
  63. test_pseudo_division(a, 0, x); % intentional error!
  64. test_pseudo_division(a, 1, x);
  65. test_pseudo_division(5x^3 + 7y^2, 2x - y, x);
  66. test_pseudo_division(5x^3 + 7y^2, 2x - y, y);
  67. end;