001/*
002 * $Id$
003 */
004
005package edu.jas.application;
006
007
008import java.io.Reader;
009import java.util.ArrayList;
010import java.util.List;
011import java.util.Random;
012import java.util.SortedSet;
013import java.util.TreeSet;
014
015import org.apache.logging.log4j.LogManager;
016import org.apache.logging.log4j.Logger;
017
018import edu.jas.poly.GenPolynomial;
019import edu.jas.poly.GenPolynomialRing;
020import edu.jas.structure.GcdRingElem;
021import edu.jas.structure.RingFactory;
022import edu.jas.ufd.GCDFactory;
023import edu.jas.ufd.GreatestCommonDivisor;
024
025
026/**
027 * Residue ring factory based on GenPolynomial with RingFactory interface.
028 * Objects of this class are immutable.
029 * @author Heinz Kredel
030 */
031public class ResidueRing<C extends GcdRingElem<C>> implements RingFactory<Residue<C>> {
032
033
034    private static final Logger logger = LogManager.getLogger(ResidueRing.class);
035
036
037    //private static final boolean debug = logger.isDebugEnabled(); 
038
039
040    /**
041     * Greatest common divisor engine for coefficient content and primitive
042     * parts.
043     */
044    protected final GreatestCommonDivisor<C> engine;
045
046
047    /**
048     * Polynomial ideal for the reduction.
049     */
050    public final Ideal<C> ideal;
051
052
053    /**
054     * Polynomial ring of the factory. Shortcut to ideal.list.ring.
055     */
056    public final GenPolynomialRing<C> ring;
057
058
059    /**
060     * Indicator if this ring is a field.
061     */
062    protected int isField = -1; // initially unknown
063
064
065    /**
066     * The constructor creates a ResidueRing object from an Ideal.
067     * @param i polynomial ideal.
068     */
069    public ResidueRing(Ideal<C> i) {
070        this(i, false);
071    }
072
073
074    /**
075     * The constructor creates a ResidueRing object from an Ideal.
076     * @param i polynomial ideal.
077     * @param isMaximal true, if ideal is maxmal.
078     */
079    public ResidueRing(Ideal<C> i, boolean isMaximal) {
080        ideal = i.GB(); // cheap if isGB
081        ring = ideal.list.ring;
082        //engine = GCDFactory.<C>getImplementation( ring.coFac );
083        engine = GCDFactory.<C> getProxy(ring.coFac);
084        if (isMaximal) {
085            isField = 1;
086            return;
087        }
088        //System.out.println("rr engine = " + engine.getClass().getName());
089        //System.out.println("rr ring   = " + ring.getClass().getName());
090        //System.out.println("rr cofac  = " + ring.coFac.getClass().getName());
091        if (ideal.isONE()) {
092            logger.warn("ideal is one, so all residues are 0");
093        }
094    }
095
096
097    /**
098     * Is this structure finite or infinite.
099     * @return true if this structure is finite, else false.
100     * @see edu.jas.structure.ElemFactory#isFinite()
101     */
102    public boolean isFinite() {
103        return ideal.commonZeroTest() <= 0 && ring.coFac.isFinite();
104    }
105
106
107    /**
108     * Copy Residue element c.
109     * @param c
110     * @return a copy of c.
111     */
112    public Residue<C> copy(Residue<C> c) {
113        //System.out.println("rr copy in    = " + c.val);
114        if (c == null) { // where does this happen?
115            return getZERO(); // or null?
116        }
117        Residue<C> r = new Residue<C>(this, c.val);
118        //System.out.println("rr copy out   = " + r.val);
119        //System.out.println("rr copy ideal = " + ideal.list.list);
120        return r; //new Residue<C>( c.ring, c.val );
121    }
122
123
124    /**
125     * Get the zero element.
126     * @return 0 as Residue.
127     */
128    public Residue<C> getZERO() {
129        return new Residue<C>(this, ring.getZERO());
130    }
131
132
133    /**
134     * Get the one element.
135     * @return 1 as Residue.
136     */
137    public Residue<C> getONE() {
138        Residue<C> one = new Residue<C>(this, ring.getONE());
139        if (one.isZERO()) {
140            logger.warn("ideal is one, so all residues are 0");
141        }
142        return one;
143    }
144
145
146    /**
147     * Get a list of the generating elements.
148     * @return list of generators for the algebraic structure.
149     * @see edu.jas.structure.ElemFactory#generators()
150     */
151    public List<Residue<C>> generators() {
152        List<GenPolynomial<C>> pgens = ring.generators();
153        List<Residue<C>> gens = new ArrayList<Residue<C>>(pgens.size());
154        SortedSet<Residue<C>> sgens = new TreeSet<Residue<C>>();
155        List<GenPolynomial<C>> rgens = new ArrayList<GenPolynomial<C>>(pgens.size());
156        Ideal<C> gi = new Ideal<C>(ring, rgens);
157        ResidueRing<C> gr = new ResidueRing<C>(gi);
158        for (GenPolynomial<C> p : pgens) {
159            Residue<C> r = new Residue<C>(this, p);
160            if (r.isZERO()) {
161                continue;
162            }
163            if (!r.isONE() && r.val.isConstant()) {
164                continue;
165            }
166            // avoid duplicate generators
167            Residue<C> x = new Residue<C>(gr, r.val);
168            if (x.isZERO()) {
169                continue;
170            }
171            if (!x.isONE() && x.val.isConstant()) {
172                continue;
173            }
174            r = new Residue<C>(this, x.val);
175            if (r.isZERO()) {
176                continue;
177            }
178            r = r.monic();
179            if (!r.isONE() && !r.val.isConstant()) {
180                rgens.add(r.val);
181                //System.out.println("rgens = " + rgens);
182                gi = new Ideal<C>(ring, rgens);
183                gr = new ResidueRing<C>(gi);
184            }
185            sgens.add(r);
186        }
187        gens.addAll(sgens);
188        return gens;
189    }
190
191
192    /**
193     * Query if this ring is commutative.
194     * @return true if this ring is commutative, else false.
195     */
196    public boolean isCommutative() {
197        return ring.isCommutative();
198    }
199
200
201    /**
202     * Query if this ring is associative.
203     * @return true if this ring is associative, else false.
204     */
205    public boolean isAssociative() {
206        return ring.isAssociative();
207    }
208
209
210    /**
211     * Query if this ring is a field.
212     * @return false.
213     */
214    public boolean isField() {
215        if (isField > 0) {
216            return true;
217        }
218        if (isField == 0) {
219            return false;
220        }
221        if (ideal.isMaximal()) {
222            isField = 1;
223            return true;
224        }
225        isField = 0;
226        return false;
227    }
228
229
230    /**
231     * Characteristic of this ring.
232     * @return characteristic of this ring.
233     */
234    public java.math.BigInteger characteristic() {
235        return ring.characteristic();
236    }
237
238
239    /**
240     * Get a Residue element from a BigInteger value.
241     * @param a BigInteger.
242     * @return a Residue.
243     */
244    public Residue<C> fromInteger(java.math.BigInteger a) {
245        return new Residue<C>(this, ring.fromInteger(a));
246    }
247
248
249    /**
250     * Get a Residue element from a long value.
251     * @param a long.
252     * @return a Residue.
253     */
254    public Residue<C> fromInteger(long a) {
255        return new Residue<C>(this, ring.fromInteger(a));
256    }
257
258
259    /**
260     * Get the String representation as RingFactory.
261     * @see java.lang.Object#toString()
262     */
263    @Override
264    public String toString() {
265        return "ResidueRing[ " + ideal.toString() + " ]";
266    }
267
268
269    /**
270     * Get a scripting compatible string representation.
271     * @return script compatible representation for this ElemFactory.
272     * @see edu.jas.structure.ElemFactory#toScript()
273     */
274    @Override
275    public String toScript() {
276        // Python case
277        return "RC(" + ideal.list.toScript() + ")";
278        //return "RC(" + ideal.toScript() + "," + ring.toScript()  + ")";
279    }
280
281
282    /**
283     * Comparison with any other object.
284     * @see java.lang.Object#equals(java.lang.Object)
285     */
286    @Override
287    @SuppressWarnings("unchecked")
288    public boolean equals(Object b) {
289        if (!(b instanceof ResidueRing)) {
290            return false;
291        }
292        ResidueRing<C> a = null;
293        try {
294            a = (ResidueRing<C>) b;
295        } catch (ClassCastException e) {
296        }
297        if (a == null) {
298            return false;
299        }
300        if (!ring.equals(a.ring)) {
301            return false;
302        }
303        return ideal.equals(a.ideal);
304    }
305
306
307    /**
308     * Hash code for this residue ring.
309     * @see java.lang.Object#hashCode()
310     */
311    @Override
312    public int hashCode() {
313        int h;
314        h = ideal.hashCode();
315        return h;
316    }
317
318
319    /**
320     * Residue random.
321     * @param n such that 0 &le; v &le; (2<sup>n</sup>-1).
322     * @return a random residue element.
323     */
324    public Residue<C> random(int n) {
325        GenPolynomial<C> x = ring.random(n).monic();
326        return new Residue<C>(this, x);
327    }
328
329
330    /**
331     * Generate a random residum polynomial.
332     * @param k bitsize of random coefficients.
333     * @param l number of terms.
334     * @param d maximal degree in each variable.
335     * @param q density of nozero exponents.
336     * @return a random residue polynomial.
337     */
338    public Residue<C> random(int k, int l, int d, float q) {
339        GenPolynomial<C> x = ring.random(k, l, d, q).monic();
340        return new Residue<C>(this, x);
341    }
342
343
344    /**
345     * Residue random.
346     * @param n such that 0 &le; v &le; (2<sup>n</sup>-1).
347     * @param rnd is a source for random bits.
348     * @return a random residue element.
349     */
350    public Residue<C> random(int n, Random rnd) {
351        GenPolynomial<C> x = ring.random(n, rnd).monic();
352        return new Residue<C>(this, x);
353    }
354
355
356    /**
357     * Parse Residue from String.
358     * @param s String.
359     * @return Residue from s.
360     */
361    public Residue<C> parse(String s) {
362        GenPolynomial<C> x = ring.parse(s);
363        return new Residue<C>(this, x);
364    }
365
366
367    /**
368     * Parse Residue from Reader.
369     * @param r Reader.
370     * @return next Residue from r.
371     */
372    public Residue<C> parse(Reader r) {
373        GenPolynomial<C> x = ring.parse(r);
374        return new Residue<C>(this, x);
375    }
376
377}