/usr/share/axiom-20170501/src/algebra/IPADIC.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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | )abbrev domain IPADIC InnerPAdicInteger
++ Author: Clifton J. Williamson
++ Date Created: 20 August 1989
++ Date Last Updated: 15 May 1990
++ Description:
++ This domain implements Zp, the p-adic completion of the integers.
++ This is an internal domain.
InnerPAdicInteger(p,unBalanced?) : SIG == CODE where
p : Integer
unBalanced? : Boolean
I ==> Integer
NNI ==> NonNegativeInteger
OUT ==> OutputForm
L ==> List
ST ==> Stream
SUP ==> SparseUnivariatePolynomial
SIG ==> PAdicIntegerCategory p
CODE ==> add
PEXPR := p :: OUT
Rep := ST I
characteristic() == 0
euclideanSize(x) == order(x)
stream(x:%):ST I == x pretend ST(I)
padic(x:ST I):% == x pretend %
digits x == stream x
extend(x,n) == extend(x,n + 1)$Rep
complete x == complete(x)$Rep
modP:I -> I
modP n ==
unBalanced? or (p = 2) => positiveRemainder(n,p)
symmetricRemainder(n,p)
modPInfo:I -> Record(digit:I,carry:I)
modPInfo n ==
dv := divide(n,p)
r0 := dv.remainder; q := dv.quotient
if (r := modP r0) ^= r0 then q := q + ((r0 - r) quo p)
[r,q]
invModP: I -> I
invModP n == invmod(n,p)
modulus() == p
moduloP x == (empty? x => 0; frst x)
quotientByP x == (empty? x => x; rst x)
approximate(x,n) ==
n <= 0 or empty? x => 0
frst x + p * approximate(rst x,n - 1)
x = y ==
st : ST I := stream(x - y)
n : I := _$streamCount$Lisp
for i in 0..n repeat
empty? st => return true
frst st ^= 0 => return false
st := rst st
empty? st
order x ==
st := stream x
for i in 0..1000 repeat
empty? st => return 0
frst st ^= 0 => return i
st := rst st
error "order: series has more than 1000 leading zero coefs"
0 == padic concat(0$I,empty())
1 == padic concat(1$I,empty())
intToPAdic: I -> ST I
intToPAdic n == delay
n = 0 => empty()
modp := modPInfo n
concat(modp.digit,intToPAdic modp.carry)
intPlusPAdic: (I,ST I) -> ST I
intPlusPAdic(n,x) == delay
empty? x => intToPAdic n
modp := modPInfo(n + frst x)
concat(modp.digit,intPlusPAdic(modp.carry,rst x))
intMinusPAdic: (I,ST I) -> ST I
intMinusPAdic(n,x) == delay
empty? x => intToPAdic n
modp := modPInfo(n - frst x)
concat(modp.digit,intMinusPAdic(modp.carry,rst x))
plusAux: (I,ST I,ST I) -> ST I
plusAux(n,x,y) == delay
empty? x and empty? y => intToPAdic n
empty? x => intPlusPAdic(n,y)
empty? y => intPlusPAdic(n,x)
modp := modPInfo(n + frst x + frst y)
concat(modp.digit,plusAux(modp.carry,rst x,rst y))
minusAux: (I,ST I,ST I) -> ST I
minusAux(n,x,y) == delay
empty? x and empty? y => intToPAdic n
empty? x => intMinusPAdic(n,y)
empty? y => intPlusPAdic(n,x)
modp := modPInfo(n + frst x - frst y)
concat(modp.digit,minusAux(modp.carry,rst x,rst y))
x + y == padic plusAux(0,stream x,stream y)
x - y == padic minusAux(0,stream x,stream y)
- y == padic intMinusPAdic(0,stream y)
coerce(n:I) == padic intToPAdic n
intMult:(I,ST I) -> ST I
intMult(n,x) == delay
empty? x => empty()
modp := modPInfo(n * frst x)
concat(modp.digit,intPlusPAdic(modp.carry,intMult(n,rst x)))
(n:I) * (x:%) ==
padic intMult(n,stream x)
timesAux:(ST I,ST I) -> ST I
timesAux(x,y) == delay
empty? x or empty? y => empty()
modp := modPInfo(frst x * frst y)
car := modp.digit
cdr : ST I --!!
cdr := plusAux(modp.carry,intMult(frst x,rst y),timesAux(rst x,y))
concat(car,cdr)
(x:%) * (y:%) == padic timesAux(stream x,stream y)
quotientAux:(ST I,ST I) -> ST I
quotientAux(x,y) == delay
empty? x => error "quotientAux: first argument"
empty? y => empty()
modP frst x = 0 =>
modP frst y = 0 => quotientAux(rst x,rst y)
error "quotient: quotient not integral"
z0 := modP(invModP frst x * frst y)
yy : ST I --!!
yy := rest minusAux(0,y,intMult(z0,x))
concat(z0,quotientAux(x,yy))
recip x ==
empty? x or modP frst x = 0 => "failed"
padic quotientAux(stream x,concat(1,empty()))
iExquo: (%,%,I) -> Union(%,"failed")
iExquo(xx,yy,n) ==
n > 1000 =>
error "exquo: quotient by series with many leading zero coefs"
empty? yy => "failed"
empty? xx => 0
zero? frst yy =>
zero? frst xx => iExquo(rst xx,rst yy,n + 1)
"failed"
(rec := recip yy) case "failed" => "failed"
xx * (rec :: %)
x exquo y == iExquo(stream x,stream y,0)
divide(x,y) ==
(z:=x exquo y) case "failed" => [0,x]
[z, 0]
iSqrt: (I,I,I,%) -> %
iSqrt(pn,an,bn,bSt) == delay
bn1 := (empty? bSt => bn; bn + pn * frst(bSt))
c := (bn1 - an*an) quo pn
aa := modP(c * invmod(2*an,p))
nSt := (empty? bSt => bSt; rst bSt)
concat(aa,iSqrt(pn*p,an + pn*aa,bn1,nSt))
sqrt(b,a) ==
p = 2 =>
error "sqrt: no square roots in Z2 yet"
not zero? modP(a*a - (bb := moduloP b)) =>
error "sqrt: not a square root (mod p)"
b := (empty? b => b; rst b)
a := modP a
concat(a,iSqrt(p,a,bb,b))
iRoot: (SUP I,I,I,I) -> ST I
iRoot(f,alpha,invFpx0,pPow) == delay
num := -((f(alpha) exquo pPow) :: I)
digit := modP(num * invFpx0)
concat(digit,iRoot(f,alpha + digit * pPow,invFpx0,p * pPow))
root(f,x0) ==
x0 := modP x0
not zero? modP f(x0) =>
error "root: not a root (mod p)"
fpx0 := modP (differentiate f)(x0)
zero? fpx0 =>
error "root: approximate root must be a simple root (mod p)"
invFpx0 := modP invModP fpx0
padic concat(x0,iRoot(f,x0,invFpx0,p))
termOutput:(I,I) -> OUT
termOutput(k,c) ==
k = 0 => c :: OUT
mon := (k = 1 => PEXPR; PEXPR ** (k :: OUT))
c = 1 => mon
c = -1 => -mon
(c :: OUT) * mon
showAll?:() -> Boolean
-- check a global Lisp variable
showAll?() == true
coerce(x:%):OUT ==
empty?(st := stream x) => 0 :: OUT
n : NNI ; count : NNI := _$streamCount$Lisp
l : L OUT := empty()
for n in 0..count while not empty? st repeat
if frst(st) ^= 0 then
l := concat(termOutput(n :: I,frst st),l)
st := rst st
if showAll?() then
for n in (count + 1).. while explicitEntries? st and _
not eq?(st,rst st) repeat
if frst(st) ^= 0 then
l := concat(termOutput(n pretend I,frst st),l)
st := rst st
l :=
explicitlyEmpty? st => l
eq?(st,rst st) and frst st = 0 => l
concat(prefix("O" :: OUT,[PEXPR ** (n :: OUT)]),l)
empty? l => 0 :: OUT
reduce("+",reverse_! l)
|