bigmodp.red 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. MODULE BIGMODP; % Modular polynomial arithmetic where the modulus may
  2. % be a bignum.
  3. % Authors: A. C. Norman and P. M. A. Moore, 1981.
  4. FLUID '(CURRENT!-MODULUS MODULUS!/2);
  5. symbolic SMACRO PROCEDURE COMES!-BEFORE(P1,P2);
  6. % Similar to the REDUCE function ORDPP, but does not cater for
  7. % non-commutative terms and assumes that exponents are small integers.
  8. (CAR P1=CAR P2 AND IGREATERP(CDR P1,CDR P2)) OR
  9. (NOT(CAR P1=CAR P2) AND ORDOP(CAR P1,CAR P2));
  10. SYMBOLIC PROCEDURE GENERAL!-PLUS!-MOD!-P(A,B);
  11. % form the sum of the two polynomials a and b
  12. % working over the ground domain defined by the routines
  13. % general!-modular!-plus, general!-modular!-times etc. the inputs to
  14. % this routine are assumed to have coefficients already
  15. % in the required domain;
  16. IF NULL A THEN B
  17. ELSE IF NULL B THEN A
  18. ELSE IF domainp A THEN
  19. IF domainp B THEN !*n2f GENERAL!-MODULAR!-PLUS(A,B)
  20. ELSE (LT B) .+ GENERAL!-PLUS!-MOD!-P(A,RED B)
  21. ELSE IF domainp B THEN (LT A) .+ GENERAL!-PLUS!-MOD!-P(RED A,B)
  22. ELSE IF LPOW A = LPOW B THEN
  23. ADJOIN!-TERM(LPOW A,
  24. GENERAL!-PLUS!-MOD!-P(LC A,LC B),
  25. GENERAL!-PLUS!-MOD!-P(RED A,RED B))
  26. ELSE IF COMES!-BEFORE(LPOW A,LPOW B) THEN
  27. (LT A) .+ GENERAL!-PLUS!-MOD!-P(RED A,B)
  28. ELSE (LT B) .+ GENERAL!-PLUS!-MOD!-P(A,RED B);
  29. SYMBOLIC PROCEDURE GENERAL!-TIMES!-MOD!-P(A,B);
  30. IF (NULL A) OR (NULL B) THEN NIL
  31. ELSE IF domainp A THEN GEN!-MULT!-BY!-CONST!-MOD!-P(B,A)
  32. ELSE IF domainp B THEN GEN!-MULT!-BY!-CONST!-MOD!-P(A,B)
  33. ELSE IF MVAR A=MVAR B THEN GENERAL!-PLUS!-MOD!-P(
  34. GENERAL!-PLUS!-MOD!-P(GENERAL!-TIMES!-TERM!-MOD!-P(LT A,B),
  35. GENERAL!-TIMES!-TERM!-MOD!-P(LT B,RED A)),
  36. GENERAL!-TIMES!-MOD!-P(RED A,RED B))
  37. ELSE IF ORDOP(MVAR A,MVAR B) THEN
  38. ADJOIN!-TERM(LPOW A,GENERAL!-TIMES!-MOD!-P(LC A,B),
  39. GENERAL!-TIMES!-MOD!-P(RED A,B))
  40. ELSE ADJOIN!-TERM(LPOW B,
  41. GENERAL!-TIMES!-MOD!-P(A,LC B),GENERAL!-TIMES!-MOD!-P(A,RED B));
  42. SYMBOLIC PROCEDURE GENERAL!-TIMES!-TERM!-MOD!-P(TERM,B);
  43. %multiply the given polynomial by the given term;
  44. IF NULL B THEN NIL
  45. ELSE IF domainp B THEN
  46. ADJOIN!-TERM(TPOW TERM,
  47. GEN!-MULT!-BY!-CONST!-MOD!-P(TC TERM,B),NIL)
  48. ELSE IF TVAR TERM=MVAR B THEN
  49. ADJOIN!-TERM(MKSP(TVAR TERM,IPLUS2(TDEG TERM,LDEG B)),
  50. GENERAL!-TIMES!-MOD!-P(TC TERM,LC B),
  51. GENERAL!-TIMES!-TERM!-MOD!-P(TERM,RED B))
  52. ELSE IF ORDOP(TVAR TERM,MVAR B) THEN
  53. ADJOIN!-TERM(TPOW TERM,GENERAL!-TIMES!-MOD!-P(TC TERM,B),NIL)
  54. ELSE ADJOIN!-TERM(LPOW B,
  55. GENERAL!-TIMES!-TERM!-MOD!-P(TERM,LC B),
  56. GENERAL!-TIMES!-TERM!-MOD!-P(TERM,RED B));
  57. SYMBOLIC PROCEDURE GEN!-MULT!-BY!-CONST!-MOD!-P(A,N);
  58. % multiply the polynomial a by the constant n;
  59. IF NULL A THEN NIL
  60. ELSE IF N=1 THEN A
  61. ELSE IF domainp A THEN !*n2f GENERAL!-MODULAR!-TIMES(A,N)
  62. ELSE ADJOIN!-TERM(LPOW A,GEN!-MULT!-BY!-CONST!-MOD!-P(LC A,N),
  63. GEN!-MULT!-BY!-CONST!-MOD!-P(RED A,N));
  64. SYMBOLIC PROCEDURE GENERAL!-DIFFERENCE!-MOD!-P(A,B);
  65. GENERAL!-PLUS!-MOD!-P(A,GENERAL!-MINUS!-MOD!-P B);
  66. SYMBOLIC PROCEDURE GENERAL!-MINUS!-MOD!-P A;
  67. IF NULL A THEN NIL
  68. ELSE IF domainp A THEN GENERAL!-MODULAR!-MINUS A
  69. ELSE (LPOW A .* GENERAL!-MINUS!-MOD!-P LC A) .+
  70. GENERAL!-MINUS!-MOD!-P RED A;
  71. SYMBOLIC PROCEDURE GENERAL!-REDUCE!-MOD!-P A;
  72. %converts a multivariate poly from normal into modular polynomial;
  73. IF NULL A THEN NIL
  74. ELSE IF domainp A THEN !*n2f GENERAL!-MODULAR!-NUMBER A
  75. ELSE ADJOIN!-TERM(LPOW A,
  76. GENERAL!-REDUCE!-MOD!-P LC A,
  77. GENERAL!-REDUCE!-MOD!-P RED A);
  78. SYMBOLIC PROCEDURE GENERAL!-MAKE!-MODULAR!-SYMMETRIC A;
  79. % input is a multivariate MODULAR poly A with nos in the range 0->(p-1).
  80. % This folds it onto the symmetric range (-p/2)->(p/2);
  81. IF NULL A THEN NIL
  82. ELSE IF DOMAINP A THEN
  83. IF A>MODULUS!/2 THEN !*n2f(A - CURRENT!-MODULUS)
  84. ELSE A
  85. ELSE ADJOIN!-TERM(LPOW A,
  86. GENERAL!-MAKE!-MODULAR!-SYMMETRIC LC A,
  87. GENERAL!-MAKE!-MODULAR!-SYMMETRIC RED A);
  88. ENDMODULE;
  89. END;