aboutsummaryrefslogtreecommitdiff
path: root/src/algebra/unifact.spad.pamphlet
blob: 119ee5d47c5962ae69c693ae3bd059791ae2b014 (plain)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
\documentclass{article}
\usepackage{open-axiom}
\begin{document}
\title{\$SPAD/src/algebra unifact.spad}
\author{Patrizia Gianni}
\maketitle
\begin{abstract}
\end{abstract}
\eject
\tableofcontents
\eject
\section{package UNIFACT UnivariateFactorize}
<<package UNIFACT UnivariateFactorize>>=
)abbrev package UNIFACT UnivariateFactorize
++ Factorisation of univariate polynomials  with integer coefficients
++ Author: Patrizia Gianni
++ Date Created: ???
++ Date Last Updated: December 1993
++ Description:
++ Package for the factorization of univariate polynomials with integer
++ coefficients. The factorization is done by "lifting" (HENSEL) the
++ factorization over a finite field.
UnivariateFactorize(ZP) : public == private where
  Z    ==>  Integer
  PI   ==>  PositiveInteger
  NNI  ==>  NonNegativeInteger
  SUPZ ==>  SparseUnivariatePolynomial Z

  ZP : UnivariatePolynomialCategory Z

  FR        ==>  Factored ZP
  fUnion    ==>  Union("nil", "sqfr", "irred", "prime")
  FFE       ==>  Record(flg:fUnion, fctr:ZP, xpnt:Z)
  ParFact   ==>  Record(irr: ZP,pow: Z)
  FinalFact ==>  Record(contp: Z,factors:List(ParFact))


  public == with
     factor            :           ZP              -> FR
       ++ factor(m) returns the factorization of m
     factorSquareFree  :           ZP              -> FR
       ++ factorSquareFree(m) returns the factorization of m square free
       ++ polynomial
     henselFact        :      (ZP,Boolean)         -> FinalFact
       ++ henselFact(m,flag) returns the factorization of m,
       ++ FinalFact is a Record s.t. FinalFact.contp=content m,
       ++ FinalFact.factors=List of irreducible factors
       ++ of m with exponent , if flag =true the polynomial is
       ++ assumed square free.

  private == add
                 --- local functions ---

     henselfact  :           ZP      -> List(ZP)
     quadratic   :           ZP      -> List(ZP)
     remp        :        (Z, PI)    -> Z
     negShiftz   :        (Z, PI)    -> Z
     negShiftp   :        (ZP,PI)    -> ZP
     bound       :           ZP      -> PI
     choose      :           ZP      -> FirstStep
     eisenstein  :           ZP      -> Boolean
     isPowerOf2  :           Z       -> Boolean
     subMinusX   :          SUPZ     -> ZP
     sqroot      :           Z       -> Z

                 ---   declarations  ---
     CYC       ==> CyclotomicPolynomialPackage()
     DDRecord  ==> Record(factor: ZP,degree: Z)
     DDList    ==> List DDRecord
     FirstStep ==> Record(prime:PI,factors:DDList)
     ContPrim  ==> Record(cont: Z,prim: ZP)

     import GeneralHenselPackage(Z,ZP)
     import ModularDistinctDegreeFactorizer ZP


     factor(m: ZP) ==
       flist := henselFact(m,false)
       ctp:=unitNormal flist.contp
       makeFR((ctp.unit)::ZP,cons(["nil",ctp.canonical::ZP,1$Z]$FFE,
                      [["prime",u.irr,u.pow]$FFE for u in flist.factors]))

     factorSquareFree(m: ZP) ==
       flist := henselFact(m,true)
       ctp:=unitNormal flist.contp
       makeFR((ctp.unit)::ZP,cons(["nil",ctp.canonical::ZP,1$Z]$FFE,
                     [["prime",u.irr,u.pow]$FFE for u in flist.factors]))


     -- Integer square root: returns 0 if t is non-positive
     sqroot(t: Z): Z  ==
      t <= 0 => 0
      s:Integer:=t::Integer
      s:=approxSqrt(s)$IntegerRoots(Integer)
      t:=s::Z
      t

     -- Eisenstein criterion: returns true if polynomial is
     -- irreducible. Result of false in inconclusive.
     eisenstein(m : ZP): Boolean ==
       -- calculate the content of the terms after the first
       c := content reductum m
       c = 0 => false
       c = 1 => false
       -- factor the content
       -- if there is a prime in the factorization that does not divide
       -- the leading term and appears to multiplicity 1, and the square
       -- of this does not divide the last coef, return true.
       -- Otherwise reurn false.
       lead := leadingCoefficient m
       trail := lead
       m := reductum m
       while m ~= 0 repeat
         trail := leadingCoefficient m
         m:= reductum m
       fc := factor(c) :: Factored(Z)
       for r in factors fc repeat
         if (r.exponent = 1) and (0 ~= (lead rem r.factor)) and
           (0 ~= (trail rem (r.factor ** 2))) then return true
       false

     negShiftz(n: Z,Modulus:PI): Z ==
       if negative? n then n := n+Modulus
       n > (Modulus quo 2) => n-Modulus
       n

     negShiftp(pp: ZP,Modulus:PI): ZP ==
       map(negShiftz(#1,Modulus),pp)

     -- Choose the bound for the coefficients of factors
     bound(m: ZP):PI ==
       nm,nmq2,lcm,bin0,bin1:NNI
       cbound,j : PI
       k:NNI
       lcm := abs(leadingCoefficient m)::NNI
       nm := (degree m)::NNI
       nmq2:NNI := nm quo 2
       norm: Z := sqroot(+/[coefficient(m,k)**2 for k in 0..nm])
       if not one? nmq2 then nm := (nmq2-1):NNI
       else nm := nmq2
       bin0 := nm
       cbound := (bin0*norm+lcm)::PI
       for i in 2..(nm-1)::NNI repeat
         bin1 := bin0
         bin0 := (bin0*(nm+1-i):NNI) quo i
         j := (bin0*norm+bin1*lcm)::PI
         if cbound<j then cbound := j
       (2*cbound*lcm)::PI -- adjusted by lcm to prepare for exquo in ghensel

     remp(t: Z,q:PI): Z == (negative?(t := t rem q) => t+q ;t)

     numFactors(ddlist:DDList): Z ==
       ans: Z := 0
       for dd in ddlist repeat
         (d := degree(dd.factor)) = 0 => nil
         ans := ans + ((d pretend Z) exquo dd.degree):: Z
       ans

     -- select the prime,try up to 4 primes,
     -- choose the one yielding the fewest factors, but stopping if
     -- fewer than 9 factors
     choose(m: ZP):FirstStep ==
       qSave:PI := 1
       ddSave:DDList := []
       numberOfFactors: Z := 0
       lcm := leadingCoefficient m
       k: Z := 1
       ddRep := 5
       disc:ZP:=0
       q:PI:=2
       while k<ddRep repeat
         -- q must be a new prime number at each iteration
         q:=nextPrime(q)$IntegerPrimesPackage(Z) pretend PI
         (rr:=lcm rem q) = 0$Z => "next prime"
         disc:=gcd(m,differentiate m,q)
         not zero?(degree disc) => "next prime"
         k := k+1
         newdd := ddFact(m,q)
         ((n := numFactors(newdd)) < 9) =>
           ddSave := newdd
           qSave := q
           k := 5
         (numberOfFactors = 0) or (n < numberOfFactors) =>
           ddSave := newdd
           qSave := q
           numberOfFactors := n
       [qSave,ddSave]$FirstStep

     -- Find the factors of m,primitive, square-free, with lc positive
     -- and mindeg m = 0
     henselfact1(m: ZP):List(ZP) ==
      zero? degree m =>
          one? m => []
          [m]
      selected := choose(m)
      (numFactors(selected.factors) = 1$Z) => [m]
      q := selected.prime
      fl := separateFactors(selected.factors,q)
      --choose the bound
      cbound := bound(m)
      completeHensel(m,fl,q,cbound)

     -- check for possible degree reduction
     -- could use polynomial decomposition ?
     henselfact(m: ZP):List ZP ==
      deggcd:=degree m
      mm:= m
      while not zero? mm repeat (deggcd:=gcd(deggcd, degree mm); mm:=reductum mm)
      deggcd>1 and deggcd<degree m =>
         faclist := henselfact1(divideExponents(m, deggcd)::ZP)
         "append"/[henselfact1 multiplyExponents(mm, deggcd) for mm in faclist]
      henselfact1 m

     quadratic(m: ZP):List(ZP) ==
       d,d2: Z
       d := coefficient(m,1)**2-4*coefficient(m,0)*coefficient(m,2)
       d2 := sqroot(d)
       not zero?(d-d2**2) => [m]
       alpha: Z := coefficient(m,1)+d2
       beta: Z := 2*coefficient(m,2)
       d := gcd(alpha,beta)
       if not one? d then
         alpha := alpha quo d
         beta := beta quo d
       m0: ZP := monomial(beta,1)+monomial(alpha,0)
       cons(m0,[(m exquo m0):: ZP])

     isPowerOf2(n : Z): Boolean ==
       n = 1 => true
       qr : Record(quotient: Z, remainder: Z) := divide(n,2)
       qr.remainder = 1 => false
       isPowerOf2 qr.quotient

     subMinusX(supPol : SUPZ): ZP ==
       minusX : SUPZ := monomial(-1,1)$SUPZ
       (elt(supPol,minusX)$SUPZ) : ZP

--   Factorize the polynomial m, test=true if m is known to be
--   square-free, false otherwise.
--   FinalFact.contp=content m, FinalFact.factors=List of irreducible
--   factors with exponent .
     henselFact(m: ZP,test:Boolean):FinalFact ==
      factorlist : List(ParFact) := []
      c : Z

      -- make m primitive
      c := content m
      m := (m exquo c)::ZP

      -- make the lc m positive
      if negative? leadingCoefficient m then
        c := -c
        m := -m

      -- is x**d factor of m?
      if positive?(d := minimumDegree m) then
        m := (monicDivide(m,monomial(1,d))).quotient
        factorlist := [[monomial(1,1),d]$ParFact]

      d := degree m

      -- is m constant?
      d=0 => [c,factorlist]$FinalFact

      -- is m linear?
      d=1 => [c,cons([m,1]$ParFact,factorlist)]$FinalFact

      -- does m satisfy Eisenstein's criterion?
      eisenstein m => [c,cons([m,1]$ParFact,factorlist)]$FinalFact

      lcPol : ZP := leadingCoefficient(m) :: ZP

      -- is m cyclotomic (x**n - 1)?
      -lcPol = reductum(m) =>    -- if true, both will = 1
        for fac in
          (cyclotomicDecomposition(degree m)$CYC : List ZP) repeat
            factorlist := cons([fac,1]$ParFact,factorlist)
        [c,factorlist]$FinalFact

      -- is m odd cyclotomic (x**(2*n+1) + 1)?
      odd?(d) and (lcPol = reductum(m)) =>
        for sfac in cyclotomicDecomposition(degree m)$CYC repeat
           fac:=subMinusX sfac
           if negative? leadingCoefficient fac then fac := -fac
           factorlist := cons([fac,1]$ParFact,factorlist)
        [c,factorlist]$FinalFact

      -- is the poly of the form x**n + 1 with n a power of 2?
      -- if so, then irreducible
      isPowerOf2(d) and (lcPol = reductum(m)) =>
        factorlist := cons([m,1]$ParFact,factorlist)
        [c,factorlist]$FinalFact

      -- is m quadratic?
      d=2 =>
       lfq:List(ZP) := quadratic m
       #lfq=1 => [c,cons([lfq.first,1]$ParFact,factorlist)]$FinalFact
       (lf0,lf1) := (lfq.first,second lfq)
       if lf0=lf1 then factorlist := cons([lf0,2]$ParFact,factorlist)
       else factorlist := append([[v,1]$ParFact for v in lfq],factorlist)
       [c,factorlist]$FinalFact

      -- m is square-free
      test =>
        fln := henselfact(m)
        [c,append(factorlist,[[pf,1]$ParFact for pf in fln])]$FinalFact

      -- find the square-free decomposition of m
      irrFact := squareFree(m)
      llf := factors irrFact

      -- factorize the square-free primitive terms
      for l1 in llf repeat
        d1 := l1.exponent
        pol := l1.factor
        degree pol=1 => factorlist := cons([pol,d1]$ParFact,factorlist)
        degree pol=2 =>
          fln := quadratic(pol)
          factorlist := append([[pf,d1]$ParFact for pf in fln],factorlist)
        fln := henselfact(pol)
        factorlist := append([[pf,d1]$ParFact for pf in fln],factorlist)
      [c,factorlist]$FinalFact

@
\section{License}
<<license>>=
--Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
--All rights reserved.
--
--Redistribution and use in source and binary forms, with or without
--modification, are permitted provided that the following conditions are
--met:
--
--    - Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--
--    - Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in
--      the documentation and/or other materials provided with the
--      distribution.
--
--    - Neither the name of The Numerical ALgorithms Group Ltd. nor the
--      names of its contributors may be used to endorse or promote products
--      derived from this software without specific prior written permission.
--
--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
--IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
--TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
--PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
--OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
--EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
--PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@
<<*>>=
<<license>>

<<package UNIFACT UnivariateFactorize>>
@
\eject
\begin{thebibliography}{99}
\bibitem{1} nothing
\end{thebibliography}
\end{document}