001/* 002 * $Id$ 003 */ 004 005package edu.jas.ufd; 006 007 008import java.util.Map; 009import java.util.SortedMap; 010import java.util.TreeMap; 011 012import org.apache.logging.log4j.LogManager; 013import org.apache.logging.log4j.Logger; 014 015import edu.jas.poly.ExpVector; 016import edu.jas.poly.GenPolynomial; 017import edu.jas.poly.GenPolynomialRing; 018import edu.jas.poly.PolyUtil; 019import edu.jas.structure.GcdRingElem; 020import edu.jas.structure.RingFactory; 021 022 023/** 024 * Squarefree decomposition for coefficient rings of characteristic 0. 025 * @author Heinz Kredel 026 */ 027 028public class SquarefreeRingChar0<C extends GcdRingElem<C>> 029 extends SquarefreeAbstract<C> /*implements Squarefree<C>*/ { 030 031 032 private static final Logger logger = LogManager.getLogger(SquarefreeRingChar0.class); 033 034 035 private static final boolean debug = logger.isDebugEnabled(); 036 037 038 /** 039 * Factory for ring of characteristic 0 coefficients. 040 */ 041 protected final RingFactory<C> coFac; 042 043 044 /** 045 * Constructor. 046 */ 047 public SquarefreeRingChar0(RingFactory<C> fac) { 048 super(GCDFactory.<C> getProxy(fac)); 049 if (fac.isField()) { 050 throw new IllegalArgumentException("fac is a field: use SquarefreeFieldChar0"); 051 } 052 if (fac.characteristic().signum() != 0) { 053 throw new IllegalArgumentException("characterisic(fac) must be zero"); 054 } 055 coFac = fac; 056 } 057 058 059 /** 060 * Get the String representation. 061 * @see java.lang.Object#toString() 062 */ 063 @Override 064 public String toString() { 065 return getClass().getName() + " with " + engine + " over " + coFac; 066 } 067 068 069 /** 070 * GenPolynomial polynomial greatest squarefree divisor. 071 * @param P GenPolynomial. 072 * @return squarefree(pp(P)). 073 */ 074 @Override 075 public GenPolynomial<C> baseSquarefreePart(GenPolynomial<C> P) { 076 if (P == null || P.isZERO()) { 077 return P; 078 } 079 GenPolynomialRing<C> pfac = P.ring; 080 if (pfac.nvar > 1) { 081 throw new IllegalArgumentException( 082 this.getClass().getName() + " only for univariate polynomials"); 083 } 084 GenPolynomial<C> pp = engine.basePrimitivePart(P); 085 if (pp.isConstant()) { 086 return pp; 087 } 088 GenPolynomial<C> d = PolyUtil.<C> baseDeriviative(pp); 089 d = engine.basePrimitivePart(d); 090 GenPolynomial<C> g = engine.baseGcd(pp, d); 091 g = engine.basePrimitivePart(g); 092 GenPolynomial<C> q = PolyUtil.<C> basePseudoDivide(pp, g); 093 q = engine.basePrimitivePart(q); 094 return q; 095 } 096 097 098 /** 099 * GenPolynomial polynomial squarefree factorization. 100 * @param A GenPolynomial. 101 * @return [p_1 -> e_1, ..., p_k -> e_k] with A = prod_{i=1,...,k} 102 * p_i^{e_i} and p_i squarefree and gcd(p_i, p_j) = 1, for i != j. 103 */ 104 @Override 105 public SortedMap<GenPolynomial<C>, Long> baseSquarefreeFactors(GenPolynomial<C> A) { 106 SortedMap<GenPolynomial<C>, Long> sfactors = new TreeMap<GenPolynomial<C>, Long>(); 107 if (A == null || A.isZERO()) { 108 return sfactors; 109 } 110 if (A.isConstant()) { 111 sfactors.put(A, 1L); 112 return sfactors; 113 } 114 GenPolynomialRing<C> pfac = A.ring; 115 if (pfac.nvar > 1) { 116 throw new IllegalArgumentException( 117 this.getClass().getName() + " only for univariate polynomials"); 118 } 119 C ldbcf = A.leadingBaseCoefficient(); 120 if (!ldbcf.isONE()) { 121 C cc = engine.baseContent(A); 122 A = A.divide(cc); 123 GenPolynomial<C> f1 = pfac.getONE().multiply(cc); 124 //System.out.println("gcda sqf f1 = " + f1); 125 sfactors.put(f1, 1L); 126 } 127 // divide by trailing term 128 ExpVector et = A.trailingExpVector(); 129 if (!et.isZERO()) { 130 GenPolynomial<C> tr = pfac.valueOf(et); 131 logger.info("trailing term = {}", tr); 132 A = PolyUtil.<C> basePseudoDivide(A, tr); 133 long ep = et.getVal(0); // univariate 134 et = et.subst(0, 1); 135 tr = pfac.valueOf(et); 136 logger.info("tr, ep = {}, {}", tr, ep); 137 sfactors.put(tr, ep); 138 if (A.length() == 1) { 139 return sfactors; 140 } 141 } 142 GenPolynomial<C> T0 = A; 143 GenPolynomial<C> Tp; 144 GenPolynomial<C> T = null; 145 GenPolynomial<C> V = null; 146 long k = 0L; 147 boolean init = true; 148 while (true) { 149 if (init) { 150 if (T0.isConstant() || T0.isZERO()) { 151 break; 152 } 153 Tp = PolyUtil.<C> baseDeriviative(T0); 154 T = engine.baseGcd(T0, Tp); 155 T = engine.basePrimitivePart(T); 156 V = PolyUtil.<C> basePseudoDivide(T0, T); 157 //System.out.println("iT0 = " + T0); 158 //System.out.println("iTp = " + Tp); 159 //System.out.println("iT = " + T); 160 //System.out.println("iV = " + V); 161 k = 0L; 162 init = false; 163 } 164 if (V.isConstant()) { 165 break; 166 } 167 k++; 168 GenPolynomial<C> W = engine.baseGcd(T, V); 169 W = engine.basePrimitivePart(W); 170 GenPolynomial<C> z = PolyUtil.<C> basePseudoDivide(V, W); 171 //System.out.println("W = " + W); 172 //System.out.println("z = " + z); 173 V = W; 174 T = PolyUtil.<C> basePseudoDivide(T, V); 175 //System.out.println("V = " + V); 176 //System.out.println("T = " + T); 177 if (z.degree(0) > 0) { 178 if (ldbcf.isONE() && !z.leadingBaseCoefficient().isONE()) { 179 z = engine.basePrimitivePart(z); 180 //logger.info("z,pp = {}", z); 181 } 182 logger.info("z, k = {}, {}", z, k); 183 sfactors.put(z, k); 184 } 185 } 186 return normalizeFactorization(sfactors); 187 } 188 189 190 /** 191 * GenPolynomial recursive univariate polynomial greatest squarefree 192 * divisor. 193 * @param P recursive univariate GenPolynomial. 194 * @return squarefree(pp(P)). 195 */ 196 @Override 197 public GenPolynomial<GenPolynomial<C>> recursiveUnivariateSquarefreePart( 198 GenPolynomial<GenPolynomial<C>> P) { 199 if (P == null || P.isZERO()) { 200 return P; 201 } 202 GenPolynomialRing<GenPolynomial<C>> pfac = P.ring; 203 if (pfac.nvar > 1) { 204 throw new IllegalArgumentException( 205 this.getClass().getName() + " only for multivariate polynomials"); 206 } 207 // squarefree content 208 GenPolynomial<GenPolynomial<C>> pp = P; 209 GenPolynomial<C> Pc = engine.recursiveContent(P); 210 Pc = engine.basePrimitivePart(Pc); 211 //System.out.println("Pc,bPP = " + Pc); 212 if (!Pc.isONE()) { 213 pp = PolyUtil.<C> coefficientPseudoDivide(pp, Pc); 214 //System.out.println("pp,sqp = " + pp); 215 //GenPolynomial<C> Pr = squarefreePart(Pc); 216 //Pr = engine.basePrimitivePart(Pr); 217 //System.out.println("Pr,bPP = " + Pr); 218 } 219 if (pp.leadingExpVector().getVal(0) < 1) { 220 //System.out.println("pp = " + pp); 221 //System.out.println("Pc = " + Pc); 222 return pp.multiply(Pc); 223 } 224 GenPolynomial<GenPolynomial<C>> d = PolyUtil.<C> recursiveDeriviative(pp); 225 //System.out.println("d = " + d); 226 GenPolynomial<GenPolynomial<C>> g = engine.recursiveUnivariateGcd(pp, d); 227 //System.out.println("g,rec = " + g); 228 g = engine.baseRecursivePrimitivePart(g); 229 //System.out.println("g,bPP = " + g); 230 GenPolynomial<GenPolynomial<C>> q = PolyUtil.<C> recursivePseudoDivide(pp, g); 231 q = engine.baseRecursivePrimitivePart(q); 232 //System.out.println("q,bPP = " + q); 233 return q.multiply(Pc); 234 } 235 236 237 /** 238 * GenPolynomial recursive univariate polynomial squarefree factorization. 239 * @param P recursive univariate GenPolynomial. 240 * @return [p_1 -> e_1, ..., p_k -> e_k] with P = prod_{i=1,...,k} 241 * p_i^{e_i} and p_i squarefree and gcd(p_i, p_j) = 1, for i != j. 242 */ 243 @Override 244 public SortedMap<GenPolynomial<GenPolynomial<C>>, Long> recursiveUnivariateSquarefreeFactors( 245 GenPolynomial<GenPolynomial<C>> P) { 246 SortedMap<GenPolynomial<GenPolynomial<C>>, Long> sfactors = new TreeMap<GenPolynomial<GenPolynomial<C>>, Long>(); 247 if (P == null || P.isZERO()) { 248 return sfactors; 249 } 250 GenPolynomialRing<GenPolynomial<C>> pfac = P.ring; 251 if (pfac.nvar > 1) { 252 // recursiveContent not possible by return type 253 throw new IllegalArgumentException( 254 this.getClass().getName() + " only for univariate polynomials"); 255 } 256 // if base coefficient ring is a field, make monic 257 GenPolynomialRing<C> cfac = (GenPolynomialRing<C>) pfac.coFac; 258 C bcc = engine.baseRecursiveContent(P); 259 if (!bcc.isONE()) { 260 GenPolynomial<C> lc = cfac.getONE().multiply(bcc); 261 GenPolynomial<GenPolynomial<C>> pl = pfac.getONE().multiply(lc); 262 sfactors.put(pl, 1L); 263 P = PolyUtil.<C> baseRecursiveDivide(P, bcc); 264 } 265 // factors of content 266 GenPolynomial<C> Pc = engine.recursiveContent(P); 267 logger.info("Pc = {}", Pc); 268 Pc = engine.basePrimitivePart(Pc); 269 //System.out.println("Pc,PP = " + Pc); 270 if (!Pc.isONE()) { 271 P = PolyUtil.<C> coefficientPseudoDivide(P, Pc); 272 } 273 SortedMap<GenPolynomial<C>, Long> rsf = squarefreeFactors(Pc); 274 logger.info("rsf = {}", rsf); 275 // add factors of content 276 for (Map.Entry<GenPolynomial<C>, Long> me : rsf.entrySet()) { 277 GenPolynomial<C> c = me.getKey(); 278 if (!c.isONE()) { 279 GenPolynomial<GenPolynomial<C>> cr = pfac.getONE().multiply(c); 280 Long rk = me.getValue(); //rsf.get(c); 281 sfactors.put(cr, rk); 282 } 283 } 284 // divide by trailing term 285 ExpVector et = P.trailingExpVector(); 286 if (!et.isZERO()) { 287 GenPolynomial<GenPolynomial<C>> tr = pfac.valueOf(et); 288 logger.info("trailing term = {}", tr); 289 P = PolyUtil.<C> recursivePseudoDivide(P, tr); 290 long ep = et.getVal(0); // univariate 291 et = et.subst(0, 1); 292 tr = pfac.valueOf(et); 293 sfactors.put(tr, ep); 294 } 295 296 // factors of recursive polynomial 297 GenPolynomial<GenPolynomial<C>> T0 = P; 298 GenPolynomial<GenPolynomial<C>> Tp; 299 GenPolynomial<GenPolynomial<C>> T = null; 300 GenPolynomial<GenPolynomial<C>> V = null; 301 long k = 0L; 302 boolean init = true; 303 while (true) { 304 if (init) { 305 if (T0.isConstant() || T0.isZERO()) { 306 break; 307 } 308 Tp = PolyUtil.<C> recursiveDeriviative(T0); 309 //System.out.println("iTp = " + Tp); 310 T = engine.recursiveUnivariateGcd(T0, Tp); 311 //System.out.println("iT = " + T); 312 if (debug) { 313 logger.info("T0 = {}, Tp = {}, T = {}", T0, Tp, T); 314 } 315 T = engine.baseRecursivePrimitivePart(T); 316 //System.out.println("iT = " + T); 317 V = PolyUtil.<C> recursivePseudoDivide(T0, T); 318 //System.out.println("iT0 = " + T0); 319 //System.out.println("iV = " + V); 320 k = 0L; 321 init = false; 322 } 323 if (V.isConstant()) { 324 break; 325 } 326 k++; 327 GenPolynomial<GenPolynomial<C>> W = engine.recursiveUnivariateGcd(T, V); 328 if (debug) { 329 logger.info("T = {}, V = {}, W = {}", T, V, W); 330 } 331 W = engine.baseRecursivePrimitivePart(W); 332 GenPolynomial<GenPolynomial<C>> z = PolyUtil.<C> recursivePseudoDivide(V, W); 333 //System.out.println("W = " + W); 334 //System.out.println("z = " + z); 335 V = W; 336 T = PolyUtil.<C> recursivePseudoDivide(T, V); 337 //System.out.println("V = " + V); 338 //System.out.println("T = " + T); 339 //was: if ( z.degree(0) > 0 ) { 340 if (!z.isONE() && !z.isZERO()) { 341 z = engine.baseRecursivePrimitivePart(z); 342 logger.info("z = {}, k = {}", z, k); 343 sfactors.put(z, k); 344 } 345 } 346 return sfactors; 347 } 348 349 350 /** 351 * GenPolynomial greatest squarefree divisor. 352 * @param P GenPolynomial. 353 * @return squarefree(pp(P)). 354 */ 355 @Override 356 public GenPolynomial<C> squarefreePart(GenPolynomial<C> P) { 357 if (P == null) { 358 throw new IllegalArgumentException(this.getClass().getName() + " P != null"); 359 } 360 if (P.isZERO()) { 361 return P; 362 } 363 GenPolynomialRing<C> pfac = P.ring; 364 if (pfac.nvar <= 1) { 365 return baseSquarefreePart(P); 366 } 367 GenPolynomialRing<GenPolynomial<C>> rfac = pfac.recursive(1); 368 GenPolynomial<GenPolynomial<C>> Pr = PolyUtil.<C> recursive(rfac, P); 369 GenPolynomial<C> Pc = engine.recursiveContent(Pr); 370 Pr = PolyUtil.<C> coefficientPseudoDivide(Pr, Pc); 371 GenPolynomial<C> Ps = squarefreePart(Pc); 372 GenPolynomial<GenPolynomial<C>> PP = recursiveUnivariateSquarefreePart(Pr); 373 GenPolynomial<GenPolynomial<C>> PS = PP.multiply(Ps); 374 GenPolynomial<C> D = PolyUtil.<C> distribute(pfac, PS); 375 return D; 376 } 377 378 379 /** 380 * GenPolynomial squarefree factorization. 381 * @param P GenPolynomial. 382 * @return [p_1 -> e_1, ..., p_k -> e_k] with P = prod_{i=1,...,k} 383 * p_i^{e_i} and p_i squarefree and gcd(p_i, p_j) = 1, for i != j. 384 */ 385 @Override 386 public SortedMap<GenPolynomial<C>, Long> squarefreeFactors(GenPolynomial<C> P) { 387 if (P == null) { 388 throw new IllegalArgumentException(this.getClass().getName() + " P != null"); 389 } 390 GenPolynomialRing<C> pfac = P.ring; 391 if (pfac.nvar <= 1) { 392 return baseSquarefreeFactors(P); 393 } 394 SortedMap<GenPolynomial<C>, Long> sfactors = new TreeMap<GenPolynomial<C>, Long>(); 395 if (P.isZERO()) { 396 return sfactors; 397 } 398 if (P.isONE()) { 399 sfactors.put(P, 1L); 400 return sfactors; 401 } 402 GenPolynomialRing<GenPolynomial<C>> rfac = pfac.recursive(1); 403 404 GenPolynomial<GenPolynomial<C>> Pr = PolyUtil.<C> recursive(rfac, P); 405 SortedMap<GenPolynomial<GenPolynomial<C>>, Long> PP = recursiveUnivariateSquarefreeFactors(Pr); 406 407 for (Map.Entry<GenPolynomial<GenPolynomial<C>>, Long> m : PP.entrySet()) { 408 Long i = m.getValue(); 409 GenPolynomial<GenPolynomial<C>> Dr = m.getKey(); 410 GenPolynomial<C> D = PolyUtil.<C> distribute(pfac, Dr); 411 sfactors.put(D, i); 412 } 413 return normalizeFactorization(sfactors); 414 } 415 416 417 /** 418 * Coefficients squarefree factorization. 419 * @param P coefficient. 420 * @return [p_1 -> e_1, ..., p_k -> e_k] with P = prod_{i=1,...,k} 421 * p_i^{e_i} and p_i squarefree and gcd(p_i, p_j) = 1, for i != j. 422 */ 423 @Override 424 public SortedMap<C, Long> squarefreeFactors(C P) { 425 throw new UnsupportedOperationException("method not implemented"); 426 } 427 428}