/usr/share/axiom-20170501/src/algebra/INMODGCD.spad is in axiom-source 20170501-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | )abbrev package INMODGCD InnerModularGcd
++ Author: J.H. Davenport and P. Gianni
++ Date Created: July 1990
++ Date Last Updated: November 1991
++ Description:
++ This file contains the functions for modular gcd algorithm
++ for univariate polynomials with coefficients in a
++ non-trivial euclidean domain (not a field).
++ The package parametrised by the coefficient domain,
++ the polynomial domain, a prime, and a function for choosing the next prime
InnerModularGcd(R,BP,pMod,nextMod) : SIG == CODE where
R : EuclideanDomain
BP : UnivariatePolynomialCategory(R)
pMod : R
NNI ==> NonNegativeInteger
nextMod : (R,NNI) -> R
Z ==> Integer
SIG ==> with
modularGcdPrimitive : List BP -> BP
++ modularGcdPrimitive(f1,f2) computes the gcd of the two polynomials
++ f1 and f2 by modular methods.
modularGcd : List BP -> BP
++ modularGcd(listf) computes the gcd of the list of polynomials
++ listf by modular methods.
reduction : (BP,R) -> BP
++ reduction(f,p) reduces the coefficients of the polynomial f
++ modulo the prime p.
CODE ==> add
-- local functions --
height : BP -> NNI
mbound : (BP,BP) -> NNI
modGcdPrimitive : (BP,BP) -> BP
test : (BP,BP,BP) -> Boolean
merge : (R,R) -> Union(R,"failed")
modInverse : (R,R) -> R
exactquo : (BP,BP,R) -> Union(BP,"failed")
constNotZero : BP -> Boolean
constcase : (List NNI ,List BP ) -> BP
lincase : (List NNI ,List BP ) -> BP
if R has IntegerNumberSystem then
reduction(u:BP,p:R):BP ==
p = 0 => u
map((r1:R):R +-> symmetricRemainder(r1,p),u)
else
reduction(u:BP,p:R):BP ==
p = 0 => u
map((r1:R):R +-> r1 rem p,u)
FP:=EuclideanModularRing(R,BP,R,reduction,merge,exactquo)
zeroChar : Boolean := R has CharacteristicZero
-- exported functions --
-- modular Gcd for a list of primitive polynomials
modularGcdPrimitive(listf : List BP) :BP ==
empty? listf => 0$BP
g := first listf
for f in rest listf | ^zero? f while degree g > 0 repeat
g:=modGcdPrimitive(g,f)
g
-- gcd for univariate polynomials
modularGcd(listf : List BP): BP ==
listf:=remove!(0$BP,listf)
empty? listf => 0$BP
# listf = 1 => first listf
minpol:=1$BP
-- extract a monomial gcd
mdeg:= "min"/[minimumDegree f for f in listf]
if mdeg>0 then
minpol1:= monomial(1,mdeg)
listf:= [(f exquo minpol1)::BP for f in listf]
minpol:=minpol*minpol1
listdeg:=[degree f for f in listf ]
-- make the polynomials primitive
listCont := [content f for f in listf]
contgcd:= gcd listCont
-- make the polynomials primitive
listf :=[(f exquo cf)::BP for f in listf for cf in listCont]
minpol:=contgcd*minpol
ans:BP :=
--one polynomial is constant
member?(1,listf) => 1
--one polynomial is linear
member?(1,listdeg) => lincase(listdeg,listf)
modularGcdPrimitive listf
minpol*ans
-- local functions --
--one polynomial is linear, remark that they are primitive
lincase(listdeg:List NNI ,listf:List BP ): BP ==
n:= position(1,listdeg)
g:=listf.n
for f in listf repeat
if (f1:=f exquo g) case "failed" then return 1$BP
g
-- test if d is the gcd
test(f:BP,g:BP,d:BP):Boolean ==
d0:=coefficient(d,0)
coefficient(f,0) exquo d0 case "failed" => false
coefficient(g,0) exquo d0 case "failed" => false
f exquo d case "failed" => false
g exquo d case "failed" => false
true
-- gcd and cofactors for PRIMITIVE univariate polynomials
-- also assumes that constant terms are non zero
modGcdPrimitive(f:BP,g:BP): BP ==
df:=degree f
dg:=degree g
dp:FP
lcf:=leadingCoefficient f
lcg:=leadingCoefficient g
testdeg:NNI
lcd:R:=gcd(lcf,lcg)
prime:=pMod
bound:=mbound(f,g)
while zero? (lcd rem prime) repeat
prime := nextMod(prime,bound)
soFar:=gcd(reduce(f,prime),reduce(g,prime))::BP
testdeg:=degree soFar
zero? testdeg => return 1$BP
ldp:FP:=
((lcdp:=leadingCoefficient(soFar::BP)) = 1) =>
reduce(lcd::BP,prime)
reduce((modInverse(lcdp,prime)*lcd)::BP,prime)
soFar:=reduce(ldp::BP *soFar,prime)::BP
soFarModulus:=prime
-- choose the prime
while true repeat
prime := nextMod(prime,bound)
lcd rem prime =0 => "next prime"
fp:=reduce(f,prime)
gp:=reduce(g,prime)
dp:=gcd(fp,gp)
dgp :=euclideanSize dp
if dgp =0 then return 1$BP
if dgp=dg and ^(f exquo g case "failed") then return g
if dgp=df and ^(g exquo f case "failed") then return f
dgp > testdeg => "next prime"
ldp:FP:=
((lcdp:=leadingCoefficient(dp::BP)) = 1) =>
reduce(lcd::BP,prime)
reduce((modInverse(lcdp,prime)*lcd)::BP,prime)
dp:=ldp *dp
dgp=testdeg =>
correction:=reduce(dp::BP-soFar,prime)::BP
zero? correction =>
ans:=reduce(lcd::BP*soFar,soFarModulus)::BP
cont:=content ans
ans:=(ans exquo cont)::BP
test(f,g,ans) => return ans
soFarModulus:=soFarModulus*prime
correctionFactor:=modInverse(soFarModulus rem prime,prime)
-- the initial rem is just for efficiency
soFar:=soFar+soFarModulus*(correctionFactor*correction)
soFarModulus:=soFarModulus*prime
soFar:=reduce(soFar,soFarModulus)::BP
dgp<testdeg =>
soFarModulus:=prime
soFar:=dp::BP
testdeg:=dgp
if ^zeroChar and euclideanSize(prime)>1 then
result:=dp::BP
test(f,g,result) => return result
-- this is based on the assumption that the caller of this package,
-- in non-zero characteristic, will use primes of the form
-- x-alpha as long as possible, but, if these are exhausted,
-- will switch to a prime of degree larger than the answer
-- so the result can be used directly.
merge(p:R,q:R):Union(R,"failed") ==
p = q => p
p = 0 => q
q = 0 => p
"failed"
modInverse(c:R,p:R):R ==
(extendedEuclidean(c,p,1)::Record(coef1:R,coef2:R)).coef1
exactquo(u:BP,v:BP,p:R):Union(BP,"failed") ==
invlcv:=modInverse(leadingCoefficient v,p)
r:=monicDivide(u,reduction(invlcv*v,p))
reduction(r.remainder,p) ^=0 => "failed"
reduction(invlcv*r.quotient,p)
-- compute the height of a polynomial --
height(f:BP):NNI ==
degf:=degree f
"max"/[euclideanSize cc for cc in coefficients f]
-- compute the bound
mbound(f:BP,g:BP):NNI ==
hf:=height f
hg:=height g
2*min(hf,hg)
|