001/*
002 * $Id$
003 */
004
005package edu.jas.integrate;
006
007
008import java.io.Serializable;
009import java.util.ArrayList;
010import java.util.List;
011
012import edu.jas.poly.GenPolynomial;
013import edu.jas.structure.GcdRingElem;
014import edu.jas.ufd.Quotient;
015import edu.jas.ufd.QuotientRing;
016
017
018/**
019 * Container for a rational function integral, quotient version .
020 * integral(num/den) = pol + sum_rat( rat_i/rat_{i+1} ) + sum_log( a_i log ( d_i
021 * ) )
022 * @author Heinz Kredel
023 * @param <C> coefficient type
024 */
025
026public class QuotIntegral<C extends GcdRingElem<C>> implements Serializable {
027
028
029    /**
030     * Original rational function with coefficients from C.
031     */
032    public final Quotient<C> quot;
033
034
035    /**
036     * Integral of the polynomial and rational part.
037     */
038    public final List<Quotient<C>> rational;
039
040
041    /**
042     * Integral of the logarithmic part.
043     */
044    public final List<LogIntegral<C>> logarithm;
045
046
047    /**
048     * Constructor.
049     * @param ri integral.
050     */
051    public QuotIntegral(Integral<C> ri) {
052        this(new QuotientRing<C>(ri.den.ring), ri);
053    }
054
055
056    /**
057     * Constructor.
058     * @param r rational function QuotientRing over C.
059     * @param ri integral.
060     */
061    public QuotIntegral(QuotientRing<C> r, Integral<C> ri) {
062        this(new Quotient<C>(r, ri.num, ri.den), ri.pol, ri.rational, ri.logarithm);
063    }
064
065
066    /**
067     * Constructor.
068     * @param r rational function Quotient over C.
069     * @param p integral of polynomial part.
070     * @param rat list of rational integrals.
071     */
072    public QuotIntegral(Quotient<C> r, GenPolynomial<C> p, List<GenPolynomial<C>> rat) {
073        this(r, p, rat, new ArrayList<LogIntegral<C>>());
074    }
075
076
077    /**
078     * Constructor.
079     * @param r rational function Quotient over C.
080     * @param p integral of polynomial part.
081     * @param rat list of rational integrals.
082     * @param log list of logarithmic part.
083     */
084    public QuotIntegral(Quotient<C> r, GenPolynomial<C> p, List<GenPolynomial<C>> rat,
085                    List<LogIntegral<C>> log) {
086        quot = r;
087        QuotientRing<C> qr = r.ring;
088        rational = new ArrayList<Quotient<C>>();
089        if (!p.isZERO()) {
090            rational.add(new Quotient<C>(qr, p));
091        }
092        for (int i = 0; i < rat.size(); i++) {
093            GenPolynomial<C> rn = rat.get(i++);
094            GenPolynomial<C> rd = rat.get(i);
095            rational.add(new Quotient<C>(qr, rn, rd));
096        }
097        logarithm = log;
098    }
099
100
101    /**
102     * Get the String representation.
103     * @see java.lang.Object#toString()
104     */
105    @Override
106    public String toString() {
107        StringBuffer sb = new StringBuffer();
108        sb.append("integral( " + quot.toString() + " )");
109        sb.append(" =\n");
110        boolean first = true;
111        if (rational.size() != 0) {
112            for (int i = 0; i < rational.size(); i++) {
113                if (first) {
114                    first = false;
115                } else {
116                    sb.append(" + ");
117                }
118                sb.append("(" + rational.get(i) + ")");
119            }
120        }
121        if (logarithm.size() != 0) {
122            if (rational.size() != 0) {
123                sb.append(" + ");
124            }
125            first = true;
126            for (LogIntegral<C> pf : logarithm) {
127                if (first) {
128                    first = false;
129                } else {
130                    sb.append(" + ");
131                }
132                sb.append(pf);
133            }
134            sb.append("\n");
135        }
136        return sb.toString();
137    }
138
139
140    /**
141     * Hash code for QuotIntegral.
142     * @see java.lang.Object#hashCode()
143     */
144    @Override
145    public int hashCode() {
146        int h = quot.hashCode();
147        h = h * 37 + rational.hashCode();
148        h = h * 37 + logarithm.hashCode();
149        return h;
150    }
151
152
153    /**
154     * Comparison with any other object.
155     * @see java.lang.Object#equals(java.lang.Object)
156     */
157    @Override
158    @SuppressWarnings("unchecked")
159    public boolean equals(Object B) {
160        QuotIntegral<C> b = null;
161        try {
162            b = (QuotIntegral<C>) B;
163        } catch (ClassCastException ignored) {
164        }
165        if (b == null) {
166            return false;
167        }
168        return quot.equals(b.quot) && rational.equals(b.rational) && logarithm.equals(b.logarithm);
169    }
170
171}