001/*
002 * $Id: IdealWithRealAlgebraicRoots.java 4050 2012-07-25 17:14:32Z kredel $
003 */
004
005package edu.jas.application;
006
007
008import java.io.Serializable;
009import java.util.ArrayList;
010import java.util.List;
011
012import edu.jas.arith.BigDecimal;
013import edu.jas.arith.Rational;
014import edu.jas.poly.GenPolynomial;
015import edu.jas.root.RealAlgebraicNumber;
016import edu.jas.structure.GcdRingElem;
017import edu.jas.structure.RingElem;
018
019
020/**
021 * Container for Ideals together with univariate polynomials and real algebraic
022 * roots.
023 * @author Heinz Kredel
024 */
025public class IdealWithRealAlgebraicRoots<D extends GcdRingElem<D> & Rational>
026        extends IdealWithUniv<D> {
027
028
029    /**
030     * The list of real algebraic roots.
031     */
032    public final List<List<RealAlgebraicNumber<D>>> ran;
033
034
035    /**
036     * The list of decimal approximations of the real algebraic roots.
037     */
038    protected List<List<BigDecimal>> droots = null;
039
040
041    /**
042     * Constructor not for use.
043     */
044    protected IdealWithRealAlgebraicRoots() {
045        throw new IllegalArgumentException("do not use this constructor");
046    }
047
048
049    /**
050     * Constructor.
051     * @param id the ideal
052     * @param up the list of univaraite polynomials
053     * @param rr the list of real algebraic roots
054     */
055    public IdealWithRealAlgebraicRoots(Ideal<D> id, List<GenPolynomial<D>> up,
056            List<List<RealAlgebraicNumber<D>>> rr) {
057        super(id, up);
058        ran = rr;
059    }
060
061
062    /**
063     * Constructor.
064     * @param iu the ideal with univariate polynomials
065     * @param rr the list of real algebraic roots
066     */
067    public IdealWithRealAlgebraicRoots(IdealWithUniv<D> iu, List<List<RealAlgebraicNumber<D>>> rr) {
068        super(iu.ideal, iu.upolys);
069        ran = rr;
070    }
071
072
073    /**
074     * String representation of the ideal.
075     * @see java.lang.Object#toString()
076     */
077    @Override
078    public String toString() {
079        StringBuffer sb = new StringBuffer(super.toString() + "\nreal roots:\n");
080        sb.append("[");
081        boolean f1 = true;
082        for (List<RealAlgebraicNumber<D>> lr : ran) {
083            if (!f1) {
084                sb.append(", ");
085            } else {
086                f1 = false;
087            }
088            sb.append("[");
089            boolean f2 = true;
090            for (RealAlgebraicNumber<D> rr : lr) {
091                if (!f2) {
092                    sb.append(", ");
093                } else {
094                    f2 = false;
095                }
096                sb.append(rr.ring.toScript());
097            }
098            sb.append("]");
099        }
100        sb.append("]");
101        if (droots != null) {
102            sb.append("\ndecimal real root approximation:\n");
103            for (List<BigDecimal> d : droots) {
104                sb.append(d.toString());
105                sb.append("\n");
106            }
107        }
108        return sb.toString();
109    }
110
111
112    /**
113     * Get a scripting compatible string representation.
114     * @return script compatible representation for this Element.
115     * @see edu.jas.structure.Element#toScript()
116     */
117    @Override
118    public String toScript() {
119        // Python case
120        return super.toScript() + ",  " + ran.toString();
121    }
122
123
124    /**
125     * Get decimal approximation of the real root tuples.
126     */
127    public synchronized List<List<BigDecimal>> decimalApproximation() {
128        if (this.droots != null) {
129            return droots;
130        }
131        List<List<BigDecimal>> rroots = new ArrayList<List<BigDecimal>>();
132        for (List<RealAlgebraicNumber<D>> rri : this.ran) {
133            List<BigDecimal> r = new ArrayList<BigDecimal>();
134            for (RealAlgebraicNumber<D> rr : rri) {
135                BigDecimal d = new BigDecimal(rr.magnitude());
136                r.add(d);
137            }
138            rroots.add(r);
139        }
140        droots = rroots;
141        return rroots;
142    }
143
144
145    /**
146     * compute decimal approximation of the real root tuples.
147     */
148    public void doDecimalApproximation() {
149        List<List<BigDecimal>> unused = decimalApproximation();
150        if ( unused.isEmpty() ) { // use for findbugs
151            System.out.println("unused is empty");
152        }
153        return;
154    }
155
156}