001/*
002 * $Id$
003 */
004
005package edu.jas.ufd;
006
007
008import java.io.Serializable;
009import java.util.List;
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 squarefree factorization.
018 * @author Heinz Kredel
019 * @param <C> coefficient type
020 */
021
022public class FactorsList<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 List<GenPolynomial<C>> factors;
035
036
037    /**
038     * List of factors with coefficients from AlgebraicNumberRings.
039     */
040    public final List<Factors<C>> afactors;
041
042
043    /**
044     * Constructor.
045     * @param p given GenPolynomial over C.
046     * @param list irreducible factors of p with coefficients from C.
047     */
048    public FactorsList(GenPolynomial<C> p, List<GenPolynomial<C>> list) {
049        this(p, list, null);
050    }
051
052
053    /**
054     * Constructor.
055     * @param p given GenPolynomial over C.
056     * @param list irreducible factors of p with coefficients from C.
057     * @param alist irreducible factors of p with coefficients from an algebraic
058     *            number field.
059     */
060    public FactorsList(GenPolynomial<C> p, List<GenPolynomial<C>> list, List<Factors<C>> alist) {
061        poly = p;
062        factors = list;
063        afactors = alist;
064    }
065
066
067    /**
068     * Get the String representation.
069     * @see java.lang.Object#toString()
070     */
071    @Override
072    public String toString() {
073        StringBuffer sb = new StringBuffer();
074        //sb.append(poly.toString());
075        //sb.append(" =\n");
076        boolean first = true;
077        for (GenPolynomial<C> p : factors) {
078            if (first) {
079                first = false;
080            } else {
081                sb.append(",\n ");
082            }
083            sb.append(p.toString());
084        }
085        if (afactors == null) {
086            return sb.toString();
087        }
088        for (Factors<C> f : afactors) {
089            if (first) {
090                first = false;
091            } else {
092                sb.append(",\n ");
093            }
094            sb.append(f.toString());
095        }
096        return sb.toString();
097    }
098
099
100    /**
101     * Get a scripting compatible string representation.
102     * @return script compatible representation for this container.
103     * @see edu.jas.structure.ElemFactory#toScript()
104     */
105    public String toScript() {
106        // Python case
107        StringBuffer sb = new StringBuffer();
108        sb.append(poly.toScript());
109        sb.append(" =\n");
110        boolean first = true;
111        for (GenPolynomial<C> p : factors) {
112            if (first) {
113                first = false;
114            } else {
115                sb.append("\n * ");
116            }
117            sb.append(p.toScript());
118        }
119        if (afactors == null) {
120            return sb.toString();
121        }
122        for (Factors<C> f : afactors) {
123            if (first) {
124                first = false;
125            } else {
126                sb.append("\n * ");
127            }
128            sb.append(f.toScript());
129        }
130        return sb.toString();
131    }
132
133
134    /**
135     * Find largest extension field.
136     * @return largest extension field or null if no extension field
137     */
138    public AlgebraicNumberRing<C> findExtensionField() {
139        if (afactors == null) {
140            return null;
141        }
142        AlgebraicNumberRing<C> ar = null;
143        int depth = 0;
144        for (Factors<C> f : afactors) {
145            AlgebraicNumberRing<C> aring = f.findExtensionField();
146            if (aring == null) {
147                continue;
148            }
149            int d = aring.depth();
150            if (d > depth) {
151                depth = d;
152                ar = aring;
153            }
154        }
155        return ar;
156    }
157
158}