001/*
002 * $Id: FactorsMap.java 3992 2012-07-14 21:32:18Z kredel $
003 */
004
005package edu.jas.ufd;
006
007
008import java.io.Serializable;
009import java.util.SortedMap;
010
011import edu.jas.poly.AlgebraicNumberRing;
012import edu.jas.poly.GenPolynomial;
013import edu.jas.structure.GcdRingElem;
014
015
016/**
017 * Container for the factors of a eventually non-squarefree factorization.
018 * @author Heinz Kredel
019 * @param <C> coefficient type
020 */
021
022public class FactorsMap<C extends GcdRingElem<C>> implements Serializable {
023
024
025    /**
026     * Original polynomial to be factored with coefficients from C.
027     */
028    public final GenPolynomial<C> poly;
029
030
031    /**
032     * List of factors with coefficients from C.
033     */
034    public final SortedMap<GenPolynomial<C>, Long> factors;
035
036
037    /**
038     * List of factors with coefficients from AlgebraicNumberRings.
039     */
040    public final SortedMap<Factors<C>, Long> afactors;
041
042
043    /**
044     * Constructor.
045     * @param p given GenPolynomial over C.
046     * @param map irreducible factors of p with coefficients from C.
047     */
048    public FactorsMap(GenPolynomial<C> p, SortedMap<GenPolynomial<C>, Long> map) {
049        this(p, map, null);
050    }
051
052
053    /**
054     * Constructor.
055     * @param p given GenPolynomial over C.
056     * @param map irreducible factors of p with coefficients from C.
057     * @param amap irreducible factors of p with coefficients from an algebraic
058     *            number field.
059     */
060    public FactorsMap(GenPolynomial<C> p, SortedMap<GenPolynomial<C>, Long> map,
061            SortedMap<Factors<C>, Long> amap) {
062        poly = p;
063        factors = map;
064        afactors = amap;
065    }
066
067
068    /**
069     * Get the String representation.
070     * @see java.lang.Object#toString()
071     */
072    @Override
073    public String toString() {
074        StringBuffer sb = new StringBuffer();
075        sb.append(poly.toString());
076        sb.append(" =\n");
077        boolean first = true;
078        for (GenPolynomial<C> p : factors.keySet()) {
079            if (first) {
080                first = false;
081            } else {
082                sb.append(",\n ");
083            }
084            sb.append(p.toString());
085            long e = factors.get(p);
086            if (e > 1) {
087                sb.append("**" + e);
088            }
089        }
090        if (afactors == null) {
091            return sb.toString();
092        }
093        for (Factors<C> f : afactors.keySet()) {
094            if (first) {
095                first = false;
096            } else {
097                sb.append(",\n ");
098            }
099            sb.append(f.toString());
100            Long e = afactors.get(f);
101            if ( e == null ) {
102                continue;
103            }
104            if (e > 1) {
105                sb.append("**" + e);
106            }
107        }
108        return sb.toString();
109    }
110
111
112    /**
113     * Get a scripting compatible string representation.
114     * @return script compatible representation for this container.
115     * @see edu.jas.structure.ElemFactory#toScript()
116     */
117    public String toScript() {
118        // Python case
119        StringBuffer sb = new StringBuffer();
120        //sb.append(poly.toScript());
121        //sb.append(" =\n");
122        boolean first = true;
123        for (GenPolynomial<C> p : factors.keySet()) {
124            if (first) {
125                first = false;
126            } else {
127                sb.append("\n * ");
128            }
129            sb.append(p.toScript());
130            long e = factors.get(p);
131            if (e > 1) {
132                sb.append("**" + e);
133            }
134        }
135        if (afactors == null) {
136            return sb.toString();
137        }
138        for (Factors<C> f : afactors.keySet()) {
139            if (first) {
140                first = false;
141            } else {
142                sb.append("\n * ");
143            }
144            Long e = afactors.get(f);
145            if ( e == null ) { // should not happen
146                System.out.println("f = " + f);
147                System.out.println("afactors = " + afactors);
148                throw new RuntimeException("this should not happen");
149            }
150            if (e == 1) {
151                sb.append(f.toScript());
152            } else {
153                sb.append("(\n");
154                sb.append(f.toScript());
155                sb.append("\n)**" + e);
156            }
157        }
158        return sb.toString();
159    }
160
161
162    /**
163     * Find largest extension field.
164     * @return largest extension field or null if no extension field
165     */
166    public AlgebraicNumberRing<C> findExtensionField() {
167        if (afactors == null) {
168            return null;
169        }
170        AlgebraicNumberRing<C> ar = null;
171        int depth = 0;
172        for (Factors<C> f : afactors.keySet()) {
173            AlgebraicNumberRing<C> aring = f.findExtensionField();
174            if (aring == null) {
175                continue;
176            }
177            int d = aring.depth();
178            if (d > depth) {
179                depth = d;
180                ar = aring;
181            }
182        }
183        return ar;
184    }
185
186}