polydiv.rlg 6.0 KB

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