factorpoly.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Factor a polynomial
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. extern "C" {
  5. #include "console.h"
  6. }
  7. static void rationalize_coefficients(int);
  8. static int get_factor(void);
  9. static void evalpoly(void);
  10. static void yydivpoly(void);
  11. static int expo;
  12. static U **polycoeff;
  13. #define POLY p1
  14. #define X p2
  15. #define Z p3
  16. #define A p4
  17. #define B p5
  18. #define Q p6
  19. #define RESULT p7
  20. #define FACTOR p8
  21. void
  22. factorpoly(void)
  23. {
  24. save();
  25. p2 = pop();
  26. p1 = pop();
  27. if (!find(p1, p2)) {
  28. push(p1);
  29. restore();
  30. return;
  31. }
  32. if (!ispoly(p1, p2)) {
  33. push(p1);
  34. restore();
  35. return;
  36. }
  37. if (!issymbol(p2)) {
  38. push(p1);
  39. restore();
  40. return;
  41. }
  42. push(p1);
  43. push(p2);
  44. yyfactorpoly();
  45. restore();
  46. }
  47. //-----------------------------------------------------------------------------
  48. //
  49. // Input: tos-2 true polynomial
  50. //
  51. // tos-1 free variable
  52. //
  53. // Output: factored polynomial on stack
  54. //
  55. //-----------------------------------------------------------------------------
  56. void
  57. yyfactorpoly(void)
  58. {
  59. int h, i;
  60. save();
  61. X = pop();
  62. POLY = pop();
  63. h = tos;
  64. if (isfloating(POLY))
  65. stop("floating point numbers in polynomial");
  66. polycoeff = stack + tos;
  67. push(POLY);
  68. push(X);
  69. expo = coeff() - 1;
  70. rationalize_coefficients(h);
  71. // for univariate polynomials we could do expo > 1
  72. while (expo > 0) {
  73. if (iszero(polycoeff[0])) {
  74. push_integer(1);
  75. A = pop();
  76. push_integer(0);
  77. B = pop();
  78. } else if (get_factor() == 0) {
  79. if (verbosing)
  80. printf("no factor found");
  81. break;
  82. }
  83. push(A);
  84. push(X);
  85. multiply();
  86. push(B);
  87. add();
  88. FACTOR = pop();
  89. if (verbosing) {
  90. printf("successFACTOR=");
  91. print(FACTOR);
  92. }
  93. // factor out negative sign (not req'd because A > 1)
  94. #if 0
  95. if (isnegativeterm(A)) {
  96. push(FACTOR);
  97. negate();
  98. FACTOR = pop();
  99. push(RESULT);
  100. negate_noexpand();
  101. RESULT = pop();
  102. }
  103. #endif
  104. push(RESULT);
  105. push(FACTOR);
  106. multiply_noexpand();
  107. RESULT = pop();
  108. yydivpoly();
  109. while (expo && iszero(polycoeff[expo]))
  110. expo--;
  111. }
  112. // unfactored polynomial
  113. push(zero);
  114. for (i = 0; i <= expo; i++) {
  115. push(polycoeff[i]);
  116. push(X);
  117. push_integer(i);
  118. power();
  119. multiply();
  120. add();
  121. }
  122. POLY = pop();
  123. if (verbosing) {
  124. printf("POLY=");
  125. print(POLY);
  126. }
  127. // factor out negative sign
  128. if (expo > 0 && isnegativeterm(polycoeff[expo])) {
  129. push(POLY);
  130. negate();
  131. POLY = pop();
  132. push(RESULT);
  133. negate_noexpand();
  134. RESULT = pop();
  135. }
  136. push(RESULT);
  137. push(POLY);
  138. multiply_noexpand();
  139. RESULT = pop();
  140. if (verbosing) {
  141. printf("RESULT=");
  142. print(RESULT);
  143. }
  144. stack[h] = RESULT;
  145. tos = h + 1;
  146. restore();
  147. }
  148. static void
  149. rationalize_coefficients(int h)
  150. {
  151. int i;
  152. // LCM of all polynomial coefficients
  153. RESULT = one;
  154. for (i = h; i < tos; i++) {
  155. push(stack[i]);
  156. denominator();
  157. push(RESULT);
  158. lcm();
  159. RESULT = pop();
  160. }
  161. // multiply each coefficient by RESULT
  162. for (i = h; i < tos; i++) {
  163. push(RESULT);
  164. push(stack[i]);
  165. multiply();
  166. stack[i] = pop();
  167. }
  168. // reciprocate RESULT
  169. push(RESULT);
  170. reciprocate();
  171. RESULT = pop();
  172. }
  173. static int
  174. get_factor(void)
  175. {
  176. int i, j, h;
  177. int a0, an, na0, nan;
  178. if (verbosing) {
  179. push(zero);
  180. for (i = 0; i <= expo; i++) {
  181. push(polycoeff[i]);
  182. push(X);
  183. push_integer(i);
  184. power();
  185. multiply();
  186. add();
  187. }
  188. POLY = pop();
  189. printf("POLY=");
  190. print(POLY);
  191. }
  192. h = tos;
  193. an = tos;
  194. push(polycoeff[expo]);
  195. divisors_onstack();
  196. nan = tos - an;
  197. a0 = tos;
  198. push(polycoeff[0]);
  199. divisors_onstack();
  200. na0 = tos - a0;
  201. if (verbosing) {
  202. printf("divisors of base term");
  203. for (i = 0; i < na0; i++) {
  204. printf(", ");
  205. print(stack[a0 + i]);
  206. }
  207. printf(" ");
  208. printf("divisors of leading term");
  209. for (i = 0; i < nan; i++) {
  210. printf(", ");
  211. print(stack[an + i]);
  212. }
  213. }
  214. // try roots
  215. for (i = 0; i < nan; i++) {
  216. for (j = 0; j < na0; j++) {
  217. A = stack[an + i];
  218. B = stack[a0 + j];
  219. push(B);
  220. push(A);
  221. divide();
  222. negate();
  223. Z = pop();
  224. evalpoly();
  225. if (verbosing) {
  226. printf("try A=");
  227. print(A);
  228. printf(", B=");
  229. print(B);
  230. printf(", root ");
  231. print(X);
  232. printf("=-B/A=");
  233. print(Z);
  234. printf(", POLY(");
  235. print(Z);
  236. printf(")=");
  237. print(Q);
  238. }
  239. if (iszero(Q)) {
  240. tos = h;
  241. return 1;
  242. }
  243. push(B);
  244. negate();
  245. B = pop();
  246. push(Z);
  247. negate();
  248. Z = pop();
  249. evalpoly();
  250. if (verbosing) {
  251. printf("try A=");
  252. print(A);
  253. printf(", B=");
  254. print(B);
  255. printf(", root ");
  256. print(X);
  257. printf("=-B/A=");
  258. print(Z);
  259. printf(", POLY(");
  260. print(Z);
  261. printf(")=");
  262. print(Q);
  263. }
  264. if (iszero(Q)) {
  265. tos = h;
  266. return 1;
  267. }
  268. }
  269. }
  270. tos = h;
  271. return 0;
  272. }
  273. //-----------------------------------------------------------------------------
  274. //
  275. // Divide a polynomial by Ax+B
  276. //
  277. // Input: polycoeff Dividend coefficients
  278. //
  279. // expo Degree of dividend
  280. //
  281. // A As above
  282. //
  283. // B As above
  284. //
  285. // Output: polycoeff Contains quotient coefficients
  286. //
  287. //-----------------------------------------------------------------------------
  288. static void
  289. yydivpoly(void)
  290. {
  291. int i;
  292. Q = zero;
  293. for (i = expo; i > 0; i--) {
  294. push(polycoeff[i]);
  295. polycoeff[i] = Q;
  296. push(A);
  297. divide();
  298. Q = pop();
  299. push(polycoeff[i - 1]);
  300. push(Q);
  301. push(B);
  302. multiply();
  303. subtract();
  304. polycoeff[i - 1] = pop();
  305. }
  306. polycoeff[0] = Q;
  307. }
  308. static void
  309. evalpoly(void)
  310. {
  311. int i;
  312. push(zero);
  313. for (i = expo; i >= 0; i--) {
  314. push(Z);
  315. multiply();
  316. push(polycoeff[i]);
  317. add();
  318. }
  319. Q = pop();
  320. }