001/*
002 * $Id$
003 */
004
005package edu.jas.ps;
006
007
008import java.util.List;
009
010import edu.jas.poly.ExpVector;
011import edu.jas.poly.GenPolynomial;
012import edu.jas.poly.PolyUtil;
013import edu.jas.structure.RingElem;
014
015
016/**
017 * Polynomial functions capable for Taylor series expansion.
018 * @param <C> ring element type
019 * @author Heinz Kredel
020 */
021
022public class PolynomialTaylorFunction<C extends RingElem<C>> implements TaylorFunction<C> {
023
024
025    final GenPolynomial<C> pol;
026
027
028    final long facul;
029
030
031    public PolynomialTaylorFunction(GenPolynomial<C> p) {
032        this(p, 1L);
033    }
034
035
036    public PolynomialTaylorFunction(GenPolynomial<C> p, long f) {
037        pol = p;
038        facul = f;
039    }
040
041
042    /**
043     * To String.
044     * @return string representation of this.
045     */
046    @Override
047    public String toString() {
048        return pol.toString();
049    }
050
051
052    /**
053     * Get the factorial coefficient.
054     * @return factorial coefficient.
055     */
056    @Override
057    public long getFacul() {
058        return facul;
059    }
060
061
062    /**
063     * Test if this is zero.
064     * @return true if this is 0, else false.
065     */
066    public boolean isZERO() {
067        return pol.isZERO();
068    }
069
070
071    /**
072     * Derivative.
073     * @return derivative of this.
074     */
075    @Override
076    public TaylorFunction<C> derivative() {
077        return new PolynomialTaylorFunction<C>(PolyUtil.<C> baseDerivative(pol));
078    }
079
080
081    /*
082     * Partial derivative.
083     * @param r index of the variable.
084     * @return partial derivative of this with respect to variable r.
085    public TaylorFunction<C> derivative(int r) {
086        return new PolynomialTaylorFunction<C>(PolyUtil. <C> baseDerivative(pol,r)); 
087    }
088     */
089
090
091    /**
092     * Multi-partial derivative.
093     * @param i exponent vector.
094     * @return partial derivative of this with respect to all variables.
095     */
096    public TaylorFunction<C> derivative(ExpVector i) {
097        GenPolynomial<C> p = pol;
098        long f = 1L;
099        if (i.signum() == 0 || pol.isZERO()) {
100            return new PolynomialTaylorFunction<C>(p, f);
101        }
102        for (int j = 0; j < i.length(); j++) {
103            long e = i.getVal(j);
104            if (e == 0) {
105                continue;
106            }
107            int jl = i.length() - 1 - j;
108            for (long k = 0; k < e; k++) {
109                p = PolyUtil.<C> baseDerivative(p, jl);
110                f *= (k + 1);
111                if (p.isZERO()) {
112                    return new PolynomialTaylorFunction<C>(p, f);
113                }
114            }
115        }
116        //System.out.println("i = " + i + ", f = " + f + ", der = " + p);
117        return new PolynomialTaylorFunction<C>(p, f);
118    }
119
120
121    /**
122     * Evaluate.
123     * @param a element.
124     * @return this(a).
125     */
126    @Override
127    public C evaluate(C a) {
128        return PolyUtil.<C> evaluateMain(pol.ring.coFac, pol, a);
129    }
130
131
132    /**
133     * Evaluate at a tuple of elements.
134     * @param a tuple of elements.
135     * @return this(a).
136     */
137    public C evaluate(List<C> a) {
138        return PolyUtil.<C> evaluateAll(pol.ring.coFac, pol, a);
139    }
140
141}