001    /*
002     * $Id: PolynomialTaylorFunction.java 3596 2011-04-06 20:59:42Z kredel $
003     */
004    
005    package edu.jas.ps;
006    
007    
008    import java.util.List;
009    
010    import edu.jas.poly.ExpVector;
011    import edu.jas.poly.GenPolynomial;
012    import edu.jas.poly.PolyUtil;
013    import 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    
022    public 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        //JAVA6only: @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         * Deriviative.
073         * @return deriviative of this.
074         */
075        //JAVA6only: @Override
076        public TaylorFunction<C> deriviative() {
077            return new PolynomialTaylorFunction<C>(PolyUtil.<C> baseDeriviative(pol));
078        }
079    
080    
081        /*
082         * Partial deriviative.
083         * @param r index of the variable.
084         * @return partial deriviative of this with respect to variable r.
085        public TaylorFunction<C> deriviative(int r) {
086            return new PolynomialTaylorFunction<C>(PolyUtil. <C> baseDeriviative(pol,r)); 
087        }
088         */
089    
090    
091        /**
092         * Multi-partial deriviative.
093         * @param i exponent vector.
094         * @return partial deriviative of this with respect to all variables.
095         */
096        public TaylorFunction<C> deriviative(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> baseDeriviative(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        //JAVA6only: @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.ring, pol, a);
139        }
140    
141    }