001 /*
002 * $Id: FactorsMap.java 2754 2009-07-16 19:40:51Z kredel $
003 */
004
005 package edu.jas.ufd;
006
007
008 import java.io.Serializable;
009 import java.util.SortedMap;
010
011 import edu.jas.poly.AlgebraicNumberRing;
012 import edu.jas.poly.GenPolynomial;
013 import 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
022 public 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 }
149 if (e == 1) {
150 sb.append(f.toScript());
151 } else {
152 sb.append("(\n");
153 sb.append(f.toScript());
154 sb.append("\n)**" + e);
155 }
156 }
157 return sb.toString();
158 }
159
160
161 /**
162 * Find largest extension field.
163 * @return largest extension field or null if no extension field
164 */
165 public AlgebraicNumberRing<C> findExtensionField() {
166 if (afactors == null) {
167 return null;
168 }
169 AlgebraicNumberRing<C> ar = null;
170 int depth = 0;
171 for (Factors<C> f : afactors.keySet()) {
172 AlgebraicNumberRing<C> aring = f.findExtensionField();
173 if (aring == null) {
174 continue;
175 }
176 int d = aring.depth();
177 if (d > depth) {
178 depth = d;
179 ar = aring;
180 }
181 }
182 return ar;
183 }
184
185 }