vecpoly.red 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. MODULE VECPOLY;
  2. % Authors: A. C. Norman and P. M. A. Moore, 1979;
  3. FLUID '(CURRENT!-MODULUS SAFE!-FLAG);
  4. %**********************************************************************;
  5. % Routines for working with modular univariate polynomials
  6. % stored as vectors. Used to avoid unwarranted storage management
  7. % in the mod-p factorization process;
  8. SAFE!-FLAG:=CARCHECK 0;
  9. SYMBOLIC PROCEDURE COPY!-VECTOR(A,DA,B);
  10. % Copy A into B;
  11. << FOR I:=0:DA DO
  12. PUTV(B,I,GETV(A,I));
  13. DA >>;
  14. SYMBOLIC PROCEDURE TIMES!-IN!-VECTOR(A,DA,B,DB,C);
  15. % Put the product of A and B into C and return its degree.
  16. % C must not overlap with either A or B;
  17. BEGIN
  18. SCALAR DC,IC,W;
  19. IF DA#<0 OR DB#<0 THEN RETURN MINUS!-ONE;
  20. DC:=DA#+DB;
  21. FOR I:=0:DC DO PUTV(C,I,0);
  22. FOR IA:=0:DA DO <<
  23. W:=GETV(A,IA);
  24. FOR IB:=0:DB DO <<
  25. IC:=IA#+IB;
  26. PUTV(C,IC,MODULAR!-PLUS(GETV(C,IC),
  27. MODULAR!-TIMES(W,GETV(B,IB)))) >> >>;
  28. RETURN DC
  29. END;
  30. SYMBOLIC PROCEDURE QUOTFAIL!-IN!-VECTOR(A,DA,B,DB);
  31. % Overwrite A with (A/B) and return degree of result.
  32. % The quotient must be exact;
  33. IF DA#<0 THEN DA
  34. ELSE IF DB#<0 THEN ERRORF "Attempt to divide by zero"
  35. ELSE IF DA#<DB THEN ERRORF "Bad degrees in QUOTFAIL-IN-VECTOR"
  36. ELSE BEGIN
  37. SCALAR DC;
  38. DC:=DA#-DB; % Degree of result;
  39. FOR I:=DC STEP -1 UNTIL 0 DO BEGIN
  40. SCALAR Q;
  41. Q:=MODULAR!-QUOTIENT(GETV(A,DB#+I),GETV(B,DB));
  42. FOR J:=0:DB#-1 DO
  43. PUTV(A,I#+J,MODULAR!-DIFFERENCE(GETV(A,I#+J),
  44. MODULAR!-TIMES(Q,GETV(B,J))));
  45. PUTV(A,DB#+I,Q)
  46. END;
  47. FOR I:=0:DB#-1 DO IF GETV(A,I) NEQ 0 THEN
  48. ERRORF "Quotient not exact in QUOTFAIL!-IN!-VECTOR";
  49. FOR I:=0:DC DO
  50. PUTV(A,I,GETV(A,DB#+I));
  51. RETURN DC
  52. END;
  53. SYMBOLIC PROCEDURE REMAINDER!-IN!-VECTOR(A,DA,B,DB);
  54. % Overwrite the vector A with the remainder when A is
  55. % divided by B, and return the degree of the result;
  56. BEGIN
  57. SCALAR DELTA,DB!-1,RECIP!-LC!-B,W;
  58. IF DB=0 THEN RETURN MINUS!-ONE
  59. ELSE IF DB=MINUS!-ONE THEN ERRORF "ATTEMPT TO DIVIDE BY ZERO";
  60. RECIP!-LC!-B:=MODULAR!-MINUS MODULAR!-RECIPROCAL GETV(B,DB);
  61. DB!-1:=DB#-1; % Leading coeff of B treated specially, hence this;
  62. WHILE NOT((DELTA:=DA#-DB) #< 0) DO <<
  63. W:=MODULAR!-TIMES(RECIP!-LC!-B,GETV(A,DA));
  64. FOR I:=0:DB!-1 DO
  65. PUTV(A,I#+DELTA,MODULAR!-PLUS(GETV(A,I#+DELTA),
  66. MODULAR!-TIMES(GETV(B,I),W)));
  67. DA:=DA#-1;
  68. WHILE NOT(DA#<0) AND GETV(A,DA)=0 DO DA:=DA#-1 >>;
  69. RETURN DA
  70. END;
  71. SYMBOLIC PROCEDURE EVALUATE!-IN!-VECTOR(A,DA,N);
  72. % Evaluate A at N;
  73. BEGIN
  74. SCALAR R;
  75. R:=GETV(A,DA);
  76. FOR I:=DA#-1 STEP -1 UNTIL 0 DO
  77. R:=MODULAR!-PLUS(GETV(A,I),
  78. MODULAR!-TIMES(R,N));
  79. RETURN R
  80. END;
  81. SYMBOLIC PROCEDURE GCD!-IN!-VECTOR(A,DA,B,DB);
  82. % Overwrite A with the gcd of A and B. On input A and B are
  83. % vectors of coefficients, representing polynomials
  84. % of degrees DA and DB. Return DG, the degree of the gcd;
  85. BEGIN
  86. SCALAR W;
  87. IF DA=0 OR DB=0 THEN << PUTV(A,0,1); RETURN 0 >>
  88. ELSE IF DA#<0 OR DB#<0 THEN ERRORF "GCD WITH ZERO NOT ALLOWED";
  89. TOP:
  90. % Reduce the degree of A;
  91. DA:=REMAINDER!-IN!-VECTOR(A,DA,B,DB);
  92. IF DA=0 THEN << PUTV(A,0,1); RETURN 0 >>
  93. ELSE IF DA=MINUS!-ONE THEN <<
  94. W:=MODULAR!-RECIPROCAL GETV(B,DB);
  95. FOR I:=0:DB DO PUTV(A,I,MODULAR!-TIMES(GETV(B,I),W));
  96. RETURN DB >>;
  97. % Now reduce degree of B;
  98. DB:=REMAINDER!-IN!-VECTOR(B,DB,A,DA);
  99. IF DB=0 THEN << PUTV(A,0,1); RETURN 0 >>
  100. ELSE IF DB=MINUS!-ONE THEN <<
  101. W:=MODULAR!-RECIPROCAL GETV(A,DA);
  102. IF NOT (W=1) THEN
  103. FOR I:=0:DA DO PUTV(A,I,MODULAR!-TIMES(GETV(A,I),W));
  104. RETURN DA >>;
  105. GO TO TOP
  106. END;
  107. CARCHECK SAFE!-FLAG;
  108. ENDMODULE;
  109. END;