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