/usr/share/axiom-20170501/src/algebra/FRAC.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 | )abbrev domain FRAC Fraction
++ Author: Mark Botch
++ Date Last Updated: 12 February 1992
++ Basic Functions: Field, numer, denom
++ References:
++ Corl00 According to Abramowitz and Stegun or arccoth needn't be Uncouth
++ Fate01a A Critique of OpenMath and Thoughts on Encoding Mathematics
++ Description:
++ Fraction takes an IntegralDomain S and produces
++ the domain of Fractions with numerators and denominators from S.
++ If S is also a GcdDomain, then gcd's between numerator and
++ denominator will be cancelled during all operations.
Fraction(S) : SIG == CODE where
S : IntegralDomain
SIG ==> QuotientFieldCategory S with
if S has IntegerNumberSystem and S has OpenMath then OpenMath
if S has canonical and S has GcdDomain and S has canonicalUnitNormal
then canonical
++ \spad{canonical} means that equal elements are in fact identical.
CODE ==> LocalAlgebra(S, S, S) add
Rep:= Record(num:S, den:S)
coerce(d:S):% == [d,1]
zero?(x:%) == zero? x.num
if S has GcdDomain and S has canonicalUnitNormal then
retract(x:%):S ==
((x.den) = 1) => x.num
error "Denominator not equal to 1"
retractIfCan(x:%):Union(S, "failed") ==
((x.den) = 1) => x.num
"failed"
else
retract(x:%):S ==
(a:= x.num exquo x.den) case "failed" =>
error "Denominator not equal to 1"
a
retractIfCan(x:%):Union(S,"failed") == x.num exquo x.den
if S has EuclideanDomain then
wholePart x ==
((x.den) = 1) => x.num
x.num quo x.den
if S has IntegerNumberSystem then
floor x ==
((x.den) = 1) => x.num
x < 0 => -ceiling(-x)
wholePart x
ceiling x ==
((x.den) = 1) => x.num
x < 0 => -floor(-x)
1 + wholePart x
if S has OpenMath then
-- TODO: somwhere this file does something which redefines the division
-- operator. Doh!
writeOMFrac(dev: OpenMathDevice, x: %): Void ==
OMputApp(dev)
OMputSymbol(dev, "nums1", "rational")
OMwrite(dev, x.num, false)
OMwrite(dev, x.den, false)
OMputEndApp(dev)
OMwrite(x: %): String ==
s: String := ""
sp := OM_-STRINGTOSTRINGPTR(s)$Lisp
dev: OpenMathDevice := OMopenString(sp pretend String, OMencodingXML)
OMputObject(dev)
writeOMFrac(dev, x)
OMputEndObject(dev)
OMclose(dev)
s := OM_-STRINGPTRTOSTRING(sp)$Lisp pretend String
s
OMwrite(x: %, wholeObj: Boolean): String ==
s: String := ""
sp := OM_-STRINGTOSTRINGPTR(s)$Lisp
dev: OpenMathDevice := OMopenString(sp pretend String, OMencodingXML)
if wholeObj then
OMputObject(dev)
writeOMFrac(dev, x)
if wholeObj then
OMputEndObject(dev)
OMclose(dev)
s := OM_-STRINGPTRTOSTRING(sp)$Lisp pretend String
s
OMwrite(dev: OpenMathDevice, x: %): Void ==
OMputObject(dev)
writeOMFrac(dev, x)
OMputEndObject(dev)
OMwrite(dev: OpenMathDevice, x: %, wholeObj: Boolean): Void ==
if wholeObj then
OMputObject(dev)
writeOMFrac(dev, x)
if wholeObj then
OMputEndObject(dev)
if S has GcdDomain then
cancelGcd: % -> S
normalize: % -> %
normalize x ==
zero?(x.num) => 0
((x.den) = 1) => x
uca := unitNormal(x.den)
zero?(x.den := uca.canonical) => error "division by zero"
x.num := x.num * uca.associate
x
recip x ==
zero?(x.num) => "failed"
normalize [x.den, x.num]
cancelGcd x ==
((x.den) = 1) => x.den
d := gcd(x.num, x.den)
xn := x.num exquo d
xn case "failed" =>
error "gcd not gcd in QF cancelGcd (numerator)"
xd := x.den exquo d
xd case "failed" =>
error "gcd not gcd in QF cancelGcd (denominator)"
x.num := xn :: S
x.den := xd :: S
d
nn:S / dd:S ==
zero? dd => error "division by zero"
cancelGcd(z := [nn, dd])
normalize z
x + y ==
zero? y => x
zero? x => y
z := [x.den,y.den]
d := cancelGcd z
g := [z.den * x.num + z.num * y.num, d]
cancelGcd g
g.den := g.den * z.num * z.den
normalize g
-- We can not rely on the defaulting mechanism
-- to supply a definition for -, even though this
-- definition would do, for thefollowing reasons:
-- 1) The user could have defined a subtraction
-- in Localize, which would not work for
-- QuotientField;
-- 2) even if he doesn't, the system currently
-- places a default definition in Localize,
-- which uses Localize's +, which does not
-- cancel gcds
x - y ==
zero? y => x
z := [x.den, y.den]
d := cancelGcd z
g := [z.den * x.num - z.num * y.num, d]
cancelGcd g
g.den := g.den * z.num * z.den
normalize g
x:% * y:% ==
zero? x or zero? y => 0
(x = 1) => y
(y = 1) => x
(x, y) := ([x.num, y.den], [y.num, x.den])
cancelGcd x; cancelGcd y;
normalize [x.num * y.num, x.den * y.den]
n:Integer * x:% ==
y := [n::S, x.den]
cancelGcd y
normalize [x.num * y.num, y.den]
nn:S * x:% ==
y := [nn, x.den]
cancelGcd y
normalize [x.num * y.num, y.den]
differentiate(x:%, deriv:S -> S) ==
y := [deriv(x.den), x.den]
d := cancelGcd(y)
y.num := deriv(x.num) * y.den - x.num * y.num
(d, y.den) := (y.den, d)
cancelGcd y
y.den := y.den * d * d
normalize y
if S has canonicalUnitNormal then
x = y == (x.num = y.num) and (x.den = y.den)
one? x == ((x.num) = 1) and ((x.den) = 1)
-- again assuming canonical nature of representation
else
nn:S/dd:S == if zero? dd then error "division by zero" else [nn,dd]
recip x ==
zero?(x.num) => "failed"
[x.den, x.num]
if (S has RetractableTo Fraction Integer) then
retract(x:%):Fraction(Integer) == retract(retract(x)@S)
retractIfCan(x:%):Union(Fraction Integer, "failed") ==
(u := retractIfCan(x)@Union(S, "failed")) case "failed" => "failed"
retractIfCan(u::S)
else if (S has RetractableTo Integer) then
retract(x:%):Fraction(Integer) ==
retract(numer x) / retract(denom x)
retractIfCan(x:%):Union(Fraction Integer, "failed") ==
(n := retractIfCan numer x) case "failed" => "failed"
(d := retractIfCan denom x) case "failed" => "failed"
(n::Integer) / (d::Integer)
QFP ==> SparseUnivariatePolynomial %
DP ==> SparseUnivariatePolynomial S
import UnivariatePolynomialCategoryFunctions2(%,QFP,S,DP)
import UnivariatePolynomialCategoryFunctions2(S,DP,%,QFP)
if S has GcdDomain then
gcdPolynomial(pp,qq) ==
zero? pp => qq
zero? qq => pp
zero? degree pp or zero? degree qq => 1
denpp:="lcm"/[denom u for u in coefficients pp]
ppD:DP:=map(x+->retract(x*denpp),pp)
denqq:="lcm"/[denom u for u in coefficients qq]
qqD:DP:=map(x+->retract(x*denqq),qq)
g:=gcdPolynomial(ppD,qqD)
zero? degree g => 1
((lc:=leadingCoefficient g) = 1) => map(x+->x::%,g)
map(x+->x/lc,g)
if (S has PolynomialFactorizationExplicit) then
-- we'll let the solveLinearPolynomialEquations operator
-- default from Field
pp,qq: QFP
lpp: List QFP
import Factored SparseUnivariatePolynomial %
if S has CharacteristicNonZero then
if S has canonicalUnitNormal and S has GcdDomain then
charthRoot x ==
n:= charthRoot x.num
n case "failed" => "failed"
d:=charthRoot x.den
d case "failed" => "failed"
n/d
else
charthRoot x ==
-- to find x = p-th root of n/d
-- observe that xd is p-th root of n*d**(p-1)
ans:=charthRoot(x.num *
(x.den)**(characteristic()$%-1)::NonNegativeInteger)
ans case "failed" => "failed"
ans / x.den
clear: List % -> List S
clear l ==
d:="lcm"/[x.den for x in l]
[ x.num * (d exquo x.den)::S for x in l]
mat: Matrix %
conditionP mat ==
matD: Matrix S
matD:= matrix [ clear l for l in listOfLists mat ]
ansD := conditionP matD
ansD case "failed" => "failed"
ansDD:=ansD :: Vector(S)
[ ansDD(i)::% for i in 1..#ansDD]$Vector(%)
factorPolynomial(pp) ==
zero? pp => 0
denpp:="lcm"/[denom u for u in coefficients pp]
ppD:DP:=map(x+->retract(x*denpp),pp)
ff:=factorPolynomial ppD
den1:%:=denpp::%
lfact:List Record(flg:Union("nil", "sqfr", "irred", "prime"),
fctr:QFP, xpnt:Integer)
lfact:= [[w.flg,
if leadingCoefficient w.fctr =1 then
map(x+->x::%,w.fctr)
else (lc:=(leadingCoefficient w.fctr)::%;
den1:=den1/lc**w.xpnt;
map(x+->x::%/lc,w.fctr)),
w.xpnt] for w in factorList ff]
makeFR(map(x+->x::%/den1,unit(ff)),lfact)
factorSquareFreePolynomial(pp) ==
zero? pp => 0
degree pp = 0 => makeFR(pp,empty())
lcpp:=leadingCoefficient pp
pp:=pp/lcpp
denpp:="lcm"/[denom u for u in coefficients pp]
ppD:DP:=map(x+->retract(x*denpp),pp)
ff:=factorSquareFreePolynomial ppD
den1:%:=denpp::%/lcpp
lfact:List Record(flg:Union("nil", "sqfr", "irred", "prime"),
fctr:QFP, xpnt:Integer)
lfact:= [[w.flg,
if leadingCoefficient w.fctr =1 then
map(x+->x::%,w.fctr)
else (lc:=(leadingCoefficient w.fctr)::%;
den1:=den1/lc**w.xpnt;
map(x+->x::%/lc,w.fctr)),
w.xpnt] for w in factorList ff]
makeFR(map(x+->x::%/den1,unit(ff)),lfact)
|