/usr/share/axiom-20170501/src/algebra/CONTFRAC.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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | )abbrev domain CONTFRAC ContinuedFraction
++ Author: Stephen M. Watt
++ Date Created: January 1987
++ Change History: 7 October 1991
++ Description:
++ \spadtype{ContinuedFraction} implements general
++ continued fractions. This version is not restricted to simple,
++ finite fractions and uses the \spadtype{Stream} as a
++ representation. The arithmetic functions assume that the
++ approximants alternate below/above the convergence point.
++ This is enforced by ensuring the partial numerators and partial
++ denominators are greater than 0 in the Euclidean domain view of \spad{R}
++ (\spad{sizeLess?(0, x)}).
ContinuedFraction(R) : SIG == CODE where
R : EuclideanDomain
Q ==> Fraction R
MT ==> MoebiusTransform Q
OUT ==> OutputForm
SIG ==> Join(Algebra R,Algebra Q,Field) with
continuedFraction : Q -> %
++ continuedFraction(r) converts the fraction \spadvar{r} with
++ components of type \spad{R} to a continued fraction over
++ \spad{R}.
continuedFraction : (R, Stream R, Stream R) -> %
++ continuedFraction(b0,a,b) constructs a continued fraction in
++ the following way: if \spad{a = [a1,a2,...]} and \spad{b =
++ [b1,b2,...]} then the result is the continued fraction
++ \spad{b0 + a1/(b1 + a2/(b2 + ...))}.
reducedContinuedFraction : (R, Stream R) -> %
++ reducedContinuedFraction(b0,b) constructs a continued
++ fraction in the following way: if \spad{b = [b1,b2,...]}
++ then the result is the continued fraction \spad{b0 + 1/(b1 +
++ 1/(b2 + ...))}. That is, the result is the same as
++ \spad{continuedFraction(b0,[1,1,1,...],[b1,b2,b3,...])}.
partialNumerators : % -> Stream R
++ partialNumerators(x) extracts the numerators in \spadvar{x}.
++ That is, if \spad{x = continuedFraction(b0, [a1,a2,a3,...],
++ [b1,b2,b3,...])}, then \spad{partialNumerators(x) =
++ [a1,a2,a3,...]}.
partialDenominators : % -> Stream R
++ partialDenominators(x) extracts the denominators in
++ \spadvar{x}. That is, if \spad{x = continuedFraction(b0,
++ [a1,a2,a3,...], [b1,b2,b3,...])}, then
++ \spad{partialDenominators(x) = [b1,b2,b3,...]}.
partialQuotients : % -> Stream R
++ partialQuotients(x) extracts the partial quotients in
++ \spadvar{x}. That is, if \spad{x = continuedFraction(b0,
++ [a1,a2,a3,...], [b1,b2,b3,...])}, then
++ \spad{partialQuotients(x) = [b0,b1,b2,b3,...]}.
wholePart : % -> R
++ wholePart(x) extracts the whole part of \spadvar{x}. That
++ is, if \spad{x = continuedFraction(b0, [a1,a2,a3,...],
++ [b1,b2,b3,...])}, then \spad{wholePart(x) = b0}.
reducedForm : % -> %
++ reducedForm(x) puts the continued fraction \spadvar{x} in
++ reduced form, the function returns an equivalent
++ continued fraction of the form
++ \spad{continuedFraction(b0,[1,1,1,...],[b1,b2,b3,...])}.
approximants : % -> Stream Q
++ approximants(x) returns the stream of approximants of the
++ continued fraction \spadvar{x}. If the continued fraction is
++ finite, then the stream will be infinite and periodic with
++ period 1.
convergents : % -> Stream Q
++ convergents(x) returns the stream of the convergents of the
++ continued fraction \spadvar{x}. If the continued fraction is
++ finite, then the stream will be finite.
numerators : % -> Stream R
++ numerators(x) returns the stream of numerators of the
++ approximants of the continued fraction \spadvar{x}. If the
++ continued fraction is finite, then the stream will be finite.
denominators : % -> Stream R
++ denominators(x) returns the stream of denominators of the
++ approximants of the continued fraction \spadvar{x}. If the
++ continued fraction is finite, then the stream will be finite.
extend : (%,Integer) -> %
++ extend(x,n) causes the first \spadvar{n} entries in the
++ continued fraction \spadvar{x} to be computed. Normally
++ entries are only computed as needed.
complete : % -> %
++ complete(x) causes all entries in \spadvar{x} to be computed.
++ Normally entries are only computed as needed. If \spadvar{x}
++ is an infinite continued fraction, a user-initiated interrupt is
++ necessary to stop the computation.
CODE ==> add
isOrdered ==> R has OrderedRing and R has multiplicativeValuation
canReduce? ==> isOrdered or R has additiveValuation
Rec ==> Record(num: R, den: R)
Str ==> Stream Rec
Rep := Record(value: Record(whole: R, fract: Str), reduced?: Boolean)
import Str
genFromSequence: Stream Q -> %
genReducedForm: (Q, Stream Q, MT) -> Stream Rec
genFractionA: (Stream R,Stream R) -> Stream Rec
genFractionB: (Stream R,Stream R) -> Stream Rec
genNumDen: (R,R, Stream Rec) -> Stream R
genApproximants: (R,R,R,R,Stream Rec) -> Stream Q
genConvergents: (R,R,R,R,Stream Rec) -> Stream Q
iGenApproximants: (R,R,R,R,Stream Rec) -> Stream Q
iGenConvergents: (R,R,R,R,Stream Rec) -> Stream Q
reducedForm c ==
c.reduced? => c
explicitlyFinite? c.value.fract =>
continuedFraction last complete convergents c
canReduce? => genFromSequence approximants c
error "Reduced form not defined for this continued fraction."
eucWhole(a: Q): R == numer a quo denom a
eucWhole0(a: Q): R ==
isOrdered =>
n := numer a
d := denom a
q := n quo d
r := n - q*d
if r < 0 then q := q - 1
q
eucWhole a
x = y ==
x := reducedForm x
y := reducedForm y
x.value.whole ^= y.value.whole => false
xl := x.value.fract; yl := y.value.fract
while not empty? xl and not empty? yl repeat
frst.xl.den ^= frst.yl.den => return false
xl := rst xl; yl := rst yl
empty? xl and empty? yl
continuedFraction q == q :: %
if isOrdered then
continuedFraction(wh,nums,dens) == [[wh,genFractionA(nums,dens)],false]
genFractionA(nums,dens) ==
empty? nums or empty? dens => empty()
n := frst nums
d := frst dens
n < 0 => error "Numerators must be greater than 0."
d < 0 => error "Denominators must be greater than 0."
concat([n,d]$Rec, delay genFractionA(rst nums,rst dens))
else
continuedFraction(wh,nums,dens) == [[wh,genFractionB(nums,dens)],false]
genFractionB(nums,dens) ==
empty? nums or empty? dens => empty()
n := frst nums
d := frst dens
concat([n,d]$Rec, delay genFractionB(rst nums,rst dens))
reducedContinuedFraction(wh,dens) ==
continuedFraction(wh, repeating [1], dens)
coerce(n:Integer):% == [[n::R,empty()], true]
coerce(r:R):% == [[r, empty()], true]
coerce(a: Q): % ==
wh := eucWhole0 a
fr := a - wh::Q
zero? fr => [[wh, empty()], true]
l : List Rec := empty()
n := numer fr
d := denom fr
while not zero? d repeat
qr := divide(n,d)
l := concat([1,qr.quotient],l)
n := d
d := qr.remainder
[[wh, construct rest reverse_! l], true]
characteristic() == characteristic()$Q
genFromSequence apps ==
lo := first apps; apps := rst apps
hi := first apps; apps := rst apps
while eucWhole0 lo ^= eucWhole0 hi repeat
lo := first apps; apps := rst apps
hi := first apps; apps := rst apps
wh := eucWhole0 lo
[[wh, genReducedForm(wh::Q, apps, moebius(1,0,0,1))], canReduce?]
genReducedForm(wh0, apps, mt) ==
lo: Q := first apps - wh0; apps := rst apps
hi: Q := first apps - wh0; apps := rst apps
lo = hi and zero? eval(mt, lo) => empty()
mt := recip mt
wlo := eucWhole eval(mt, lo)
whi := eucWhole eval(mt, hi)
while wlo ^= whi repeat
wlo := eucWhole eval(mt, first apps - wh0); apps := rst apps
whi := eucWhole eval(mt, first apps - wh0); apps := rst apps
concat([1,wlo], delay genReducedForm(wh0, apps, shift(mt, -wlo::Q)))
wholePart c ==
c.value.whole
partialNumerators c ==
map(x1+->x1.num, c.value.fract)$StreamFunctions2(Rec,R)
partialDenominators c ==
map(x1+->x1.den, c.value.fract)$StreamFunctions2(Rec,R)
partialQuotients c ==
concat(c.value.whole, partialDenominators c)
approximants c ==
empty? c.value.fract => repeating [c.value.whole::Q]
genApproximants(1,0,c.value.whole,1,c.value.fract)
convergents c ==
empty? c.value.fract => concat(c.value.whole::Q, empty())
genConvergents (1,0,c.value.whole,1,c.value.fract)
numerators c ==
empty? c.value.fract => concat(c.value.whole, empty())
genNumDen(1,c.value.whole,c.value.fract)
denominators c ==
genNumDen(0,1,c.value.fract)
extend(x,n) == (extend(x.value.fract,n); x)
complete(x) == (complete(x.value.fract); x)
iGenApproximants(pm2,qm2,pm1,qm1,fr) == delay
nd := frst fr
pm := nd.num*pm2 + nd.den*pm1
qm := nd.num*qm2 + nd.den*qm1
genApproximants(pm1,qm1,pm,qm,rst fr)
genApproximants(pm2,qm2,pm1,qm1,fr) ==
empty? fr => repeating [pm1/qm1]
concat(pm1/qm1,iGenApproximants(pm2,qm2,pm1,qm1,fr))
iGenConvergents(pm2,qm2,pm1,qm1,fr) == delay
nd := frst fr
pm := nd.num*pm2 + nd.den*pm1
qm := nd.num*qm2 + nd.den*qm1
genConvergents(pm1,qm1,pm,qm,rst fr)
genConvergents(pm2,qm2,pm1,qm1,fr) ==
empty? fr => concat(pm1/qm1, empty())
concat(pm1/qm1,iGenConvergents(pm2,qm2,pm1,qm1,fr))
genNumDen(m2,m1,fr) ==
empty? fr => concat(m1,empty())
concat(m1,delay genNumDen(m1,m2*frst(fr).num + m1*frst(fr).den,rst fr))
gen ==> genFromSequence
apx ==> approximants
c, d: %
a: R
q: Q
n: Integer
0 == (0$R) :: %
1 == (1$R) :: %
c + d == genFromSequence map((x,y) +-> x + y, apx c, apx d)
c - d == genFromSequence map((x,y) +-> x - y, apx c, rest apx d)
- c == genFromSequence map(x +-> - x, rest apx c)
c * d == genFromSequence map((x,y) +-> x * y, apx c, apx d)
a * d == genFromSequence map(x +-> a * x, apx d)
q * d == genFromSequence map(x +-> q * x, apx d)
n * d == genFromSequence map(x +-> n * x, apx d)
c / d == genFromSequence map((x,y) +-> x / y, apx c, rest apx d)
recip c ==(c = 0 => "failed";
genFromSequence map(x +-> 1/x, rest apx c))
showAll?: () -> Boolean
showAll?() ==
NULL(_$streamsShowAll$Lisp)$Lisp => false
true
zagRec(t:Rec):OUT == zag(t.num :: OUT,t.den :: OUT)
coerce(c:%): OUT ==
wh := c.value.whole
fr := c.value.fract
empty? fr => wh :: OUT
count : NonNegativeInteger := _$streamCount$Lisp
l : List OUT := empty()
for n in 1..count while not empty? fr repeat
l := concat(zagRec frst fr,l)
fr := rst fr
if showAll?() then
for n in (count + 1).. while explicitEntries? fr repeat
l := concat(zagRec frst fr,l)
fr := rst fr
if not explicitlyEmpty? fr then l := concat("..." :: OUT,l)
l := reverse_! l
e := reduce("+",l)
zero? wh => e
(wh :: OUT) + e
|