polydiv.rlg 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. Tue Feb 10 12:26:19 2004 run on Linux
  2. % polydiv.tst -*- REDUCE -*-
  3. % Test and demonstration file for enhanced polynomial division
  4. % file polydiv.red.
  5. % F.J.Wright@Maths.QMW.ac.uk, 7 Nov 1995.
  6. % The example from "Computer Algebra" by Davenport, Siret & Tournier,
  7. % first edition, section 2.3.3.
  8. % First check that remainder still works as before.
  9. % Compute the gcd of the polynomials a and b by Euclid's algorithm:
  10. a := aa := x^8 + x^6 - 3x^4 - 3x^3 + 8x^2 + 2x - 5;
  11. 8 6 4 3 2
  12. a := aa := x + x - 3*x - 3*x + 8*x + 2*x - 5
  13. b := bb := 3x^6 + 5x^4 - 4x^2 - 9x + 21;
  14. 6 4 2
  15. b := bb := 3*x + 5*x - 4*x - 9*x + 21
  16. on rational;
  17. off allfac;
  18. c := remainder(a, b);
  19. 5 4 1 2 1
  20. c := - ---*x + ---*x - ---
  21. 9 9 3
  22. a := b$
  23. b := c$
  24. c := remainder(a, b);
  25. 117 2 441
  26. c := - -----*x - 9*x + -----
  27. 25 25
  28. a := b$
  29. b := c$
  30. c := remainder(a, b);
  31. 233150 102500
  32. c := --------*x - --------
  33. 19773 6591
  34. a := b$
  35. b := c$
  36. c := remainder(a, b);
  37. 1288744821
  38. c := - ------------
  39. 543589225
  40. a := b$
  41. b := c$
  42. c := remainder(a, b);
  43. c := 0
  44. off rational;
  45. % Repeat using pseudo-remainders, to avoid rational arithmetic:
  46. a := aa;
  47. 8 6 4 3 2
  48. a := x + x - 3*x - 3*x + 8*x + 2*x - 5
  49. b := bb;
  50. 6 4 2
  51. b := 3*x + 5*x - 4*x - 9*x + 21
  52. c := pseudo_remainder(a, b);
  53. 4 2
  54. c := - 15*x + 3*x - 9
  55. a := b$
  56. b := c$
  57. c := pseudo_remainder(a, b);
  58. 2
  59. c := 15795*x + 30375*x - 59535
  60. a := b$
  61. b := c$
  62. c := pseudo_remainder(a, b);
  63. c := 1254542875143750*x - 1654608338437500
  64. a := b$
  65. b := c$
  66. c := pseudo_remainder(a, b);
  67. c := 12593338795500743100931141992187500
  68. a := b$
  69. b := c$
  70. c := pseudo_remainder(a, b);
  71. c := 0
  72. % Example from Chris Herssens <herc@sulu.luc.ac.be>
  73. % involving algebraic numbers in the coefficient ring
  74. % (for which naive pseudo-division fails in REDUCE):
  75. factor x;
  76. a:=8*(15*sqrt(2)*x**3 + 18*sqrt(2)*x**2 + 10*sqrt(2)*x + 12*sqrt(2) -
  77. 5*x**4 - 6*x**3 - 30*x**2 - 36*x);
  78. 4 3 2
  79. a := - 40*x + x *(120*sqrt(2) - 48) + x *(144*sqrt(2) - 240)
  80. + x*(80*sqrt(2) - 288) + 96*sqrt(2)
  81. b:= - 16320*sqrt(2)*x**3 - 45801*sqrt(2)*x**2 - 50670*sqrt(2)*x -
  82. 26534*sqrt(2) + 15892*x**3 + 70920*x**2 + 86352*x + 24780;
  83. 3 2
  84. b := x *( - 16320*sqrt(2) + 15892) + x *( - 45801*sqrt(2) + 70920)
  85. + x*( - 50670*sqrt(2) + 86352) - 26534*sqrt(2) + 24780
  86. pseudo_remainder(a, b, x);
  87. 2 3/2
  88. x *( - 51343372800*2 + 72663731640*2 + 106394745600*sqrt(2) - 152808065280) +
  89. 3/2
  90. x*( - 77924736000*2 + 111722451600*2 + 167518488000*sqrt(2) - 236076547200)
  91. 3/2
  92. - 26395315200*2 + 21508247760*2 + 58006274400*sqrt(2) - 51393323520
  93. % Note: We must specify the division variable even though the
  94. % polynomials are apparently univariate:
  95. pseudo_remainder(a, b);
  96. *** Main division variable selected is 2**(1/2)
  97. 7 6 5 4 3 2
  98. 652800*x + 708360*x - 2656800*x - 2660160*x + 4017600*x + 3676320*x
  99. - 2630400*x - 2378880
  100. % Confirm that quotient * b + remainder = constant * a:
  101. pseudo_divide(a, b, x);
  102. {x*(652800*sqrt(2) - 635680) - 1958400*2 + 858360*sqrt(2) + 2073984,
  103. 2 3/2
  104. x *( - 51343372800*2 + 72663731640*2 + 106394745600*sqrt(2) - 152808065280)
  105. + x
  106. 3/2
  107. *( - 77924736000*2 + 111722451600*2 + 167518488000*sqrt(2) - 236076547200)
  108. 3/2
  109. - 26395315200*2 + 21508247760*2 + 58006274400*sqrt(2) - 51393323520}
  110. first ws * b + second ws;
  111. 4
  112. x *(20748595200*sqrt(2) - 31409618560)
  113. 3
  114. + x *(119127169920*sqrt(2) - 162183113472)
  115. 2
  116. + x *(237566198016*sqrt(2) - 337847596800)
  117. + x*(212209122560*sqrt(2) - 309143634432) + 75383084544*sqrt(2) - 99593256960
  118. ws / a;
  119. 4 3
  120. (x *(2593574400*sqrt(2) - 3926202320) + x *(14890896240*sqrt(2) - 20272889184)
  121. 2
  122. + x *(29695774752*sqrt(2) - 42230949600)
  123. + x*(26526140320*sqrt(2) - 38642954304) + 9422885568*sqrt(2) - 12449157120)/(
  124. 4 3 2
  125. - 5*x + x *(15*sqrt(2) - 6) + x *(18*sqrt(2) - 30) + x*(10*sqrt(2) - 36)
  126. + 12*sqrt(2))
  127. % is this constant?
  128. on rationalize;
  129. ws;
  130. - 518714880*sqrt(2) + 785240464
  131. % yes, it is constant
  132. off rationalize;
  133. on allfac;
  134. remfac x;
  135. procedure test_pseudo_division(a, b, x);
  136. begin scalar qr, L;
  137. qr := pseudo_divide(a, b, x);
  138. L := lcof(b,x);
  139. %% For versions of REDUCE prior to 3.6 use:
  140. %% L := if b freeof x then b else lcof(b,x);
  141. if first qr * b + second qr =
  142. L^(deg(a,x)-deg(b,x)+1) * a then
  143. write "Pseudo-division OK"
  144. else
  145. write "Pseudo-division failed"
  146. end;
  147. test_pseudo_division
  148. a := 5x^4 + 4x^3 + 3x^2 + 2x + 1;
  149. 4 3 2
  150. a := 5*x + 4*x + 3*x + 2*x + 1
  151. test_pseudo_division(a, x, x);
  152. Pseudo-division OK
  153. test_pseudo_division(a, x^3, x);
  154. Pseudo-division OK
  155. test_pseudo_division(a, x^5, x);
  156. Pseudo-division OK
  157. test_pseudo_division(a, x^3 + x, x);
  158. Pseudo-division OK
  159. test_pseudo_division(a, 0, x);
  160. ***** Zero divisor
  161. % intentional error!
  162. test_pseudo_division(a, 1, x);
  163. Pseudo-division OK
  164. test_pseudo_division(5x^3 + 7y^2, 2x - y, x);
  165. Pseudo-division OK
  166. test_pseudo_division(5x^3 + 7y^2, 2x - y, y);
  167. Pseudo-division OK
  168. end;
  169. Time for test: 10 ms