001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.analysis.polynomials;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.commons.math.fraction.BigFraction;
025    import org.apache.commons.math.util.ArithmeticUtils;
026    import org.apache.commons.math.util.FastMath;
027    
028    /**
029     * A collection of static methods that operate on or return polynomials.
030     *
031     * @version $Id: PolynomialsUtils.java 1182787 2011-10-13 11:20:48Z celestin $
032     * @since 2.0
033     */
034    public class PolynomialsUtils {
035    
036        /** Coefficients for Chebyshev polynomials. */
037        private static final List<BigFraction> CHEBYSHEV_COEFFICIENTS;
038    
039        /** Coefficients for Hermite polynomials. */
040        private static final List<BigFraction> HERMITE_COEFFICIENTS;
041    
042        /** Coefficients for Laguerre polynomials. */
043        private static final List<BigFraction> LAGUERRE_COEFFICIENTS;
044    
045        /** Coefficients for Legendre polynomials. */
046        private static final List<BigFraction> LEGENDRE_COEFFICIENTS;
047    
048        /** Coefficients for Jacobi polynomials. */
049        private static final Map<JacobiKey, List<BigFraction>> JACOBI_COEFFICIENTS;
050    
051        static {
052    
053            // initialize recurrence for Chebyshev polynomials
054            // T0(X) = 1, T1(X) = 0 + 1 * X
055            CHEBYSHEV_COEFFICIENTS = new ArrayList<BigFraction>();
056            CHEBYSHEV_COEFFICIENTS.add(BigFraction.ONE);
057            CHEBYSHEV_COEFFICIENTS.add(BigFraction.ZERO);
058            CHEBYSHEV_COEFFICIENTS.add(BigFraction.ONE);
059    
060            // initialize recurrence for Hermite polynomials
061            // H0(X) = 1, H1(X) = 0 + 2 * X
062            HERMITE_COEFFICIENTS = new ArrayList<BigFraction>();
063            HERMITE_COEFFICIENTS.add(BigFraction.ONE);
064            HERMITE_COEFFICIENTS.add(BigFraction.ZERO);
065            HERMITE_COEFFICIENTS.add(BigFraction.TWO);
066    
067            // initialize recurrence for Laguerre polynomials
068            // L0(X) = 1, L1(X) = 1 - 1 * X
069            LAGUERRE_COEFFICIENTS = new ArrayList<BigFraction>();
070            LAGUERRE_COEFFICIENTS.add(BigFraction.ONE);
071            LAGUERRE_COEFFICIENTS.add(BigFraction.ONE);
072            LAGUERRE_COEFFICIENTS.add(BigFraction.MINUS_ONE);
073    
074            // initialize recurrence for Legendre polynomials
075            // P0(X) = 1, P1(X) = 0 + 1 * X
076            LEGENDRE_COEFFICIENTS = new ArrayList<BigFraction>();
077            LEGENDRE_COEFFICIENTS.add(BigFraction.ONE);
078            LEGENDRE_COEFFICIENTS.add(BigFraction.ZERO);
079            LEGENDRE_COEFFICIENTS.add(BigFraction.ONE);
080    
081            // initialize map for Jacobi polynomials
082            JACOBI_COEFFICIENTS = new HashMap<JacobiKey, List<BigFraction>>();
083    
084        }
085    
086        /**
087         * Private constructor, to prevent instantiation.
088         */
089        private PolynomialsUtils() {
090        }
091    
092        /**
093         * Create a Chebyshev polynomial of the first kind.
094         * <p><a href="http://mathworld.wolfram.com/ChebyshevPolynomialoftheFirstKind.html">Chebyshev
095         * polynomials of the first kind</a> are orthogonal polynomials.
096         * They can be defined by the following recurrence relations:
097         * <pre>
098         *  T<sub>0</sub>(X)   = 1
099         *  T<sub>1</sub>(X)   = X
100         *  T<sub>k+1</sub>(X) = 2X T<sub>k</sub>(X) - T<sub>k-1</sub>(X)
101         * </pre></p>
102         * @param degree degree of the polynomial
103         * @return Chebyshev polynomial of specified degree
104         */
105        public static PolynomialFunction createChebyshevPolynomial(final int degree) {
106            return buildPolynomial(degree, CHEBYSHEV_COEFFICIENTS,
107                    new RecurrenceCoefficientsGenerator() {
108                private final BigFraction[] coeffs = { BigFraction.ZERO, BigFraction.TWO, BigFraction.ONE };
109                /** {@inheritDoc} */
110                public BigFraction[] generate(int k) {
111                    return coeffs;
112                }
113            });
114        }
115    
116        /**
117         * Create a Hermite polynomial.
118         * <p><a href="http://mathworld.wolfram.com/HermitePolynomial.html">Hermite
119         * polynomials</a> are orthogonal polynomials.
120         * They can be defined by the following recurrence relations:
121         * <pre>
122         *  H<sub>0</sub>(X)   = 1
123         *  H<sub>1</sub>(X)   = 2X
124         *  H<sub>k+1</sub>(X) = 2X H<sub>k</sub>(X) - 2k H<sub>k-1</sub>(X)
125         * </pre></p>
126    
127         * @param degree degree of the polynomial
128         * @return Hermite polynomial of specified degree
129         */
130        public static PolynomialFunction createHermitePolynomial(final int degree) {
131            return buildPolynomial(degree, HERMITE_COEFFICIENTS,
132                    new RecurrenceCoefficientsGenerator() {
133                /** {@inheritDoc} */
134                public BigFraction[] generate(int k) {
135                    return new BigFraction[] {
136                            BigFraction.ZERO,
137                            BigFraction.TWO,
138                            new BigFraction(2 * k)};
139                }
140            });
141        }
142    
143        /**
144         * Create a Laguerre polynomial.
145         * <p><a href="http://mathworld.wolfram.com/LaguerrePolynomial.html">Laguerre
146         * polynomials</a> are orthogonal polynomials.
147         * They can be defined by the following recurrence relations:
148         * <pre>
149         *        L<sub>0</sub>(X)   = 1
150         *        L<sub>1</sub>(X)   = 1 - X
151         *  (k+1) L<sub>k+1</sub>(X) = (2k + 1 - X) L<sub>k</sub>(X) - k L<sub>k-1</sub>(X)
152         * </pre></p>
153         * @param degree degree of the polynomial
154         * @return Laguerre polynomial of specified degree
155         */
156        public static PolynomialFunction createLaguerrePolynomial(final int degree) {
157            return buildPolynomial(degree, LAGUERRE_COEFFICIENTS,
158                    new RecurrenceCoefficientsGenerator() {
159                /** {@inheritDoc} */
160                public BigFraction[] generate(int k) {
161                    final int kP1 = k + 1;
162                    return new BigFraction[] {
163                            new BigFraction(2 * k + 1, kP1),
164                            new BigFraction(-1, kP1),
165                            new BigFraction(k, kP1)};
166                }
167            });
168        }
169    
170        /**
171         * Create a Legendre polynomial.
172         * <p><a href="http://mathworld.wolfram.com/LegendrePolynomial.html">Legendre
173         * polynomials</a> are orthogonal polynomials.
174         * They can be defined by the following recurrence relations:
175         * <pre>
176         *        P<sub>0</sub>(X)   = 1
177         *        P<sub>1</sub>(X)   = X
178         *  (k+1) P<sub>k+1</sub>(X) = (2k+1) X P<sub>k</sub>(X) - k P<sub>k-1</sub>(X)
179         * </pre></p>
180         * @param degree degree of the polynomial
181         * @return Legendre polynomial of specified degree
182         */
183        public static PolynomialFunction createLegendrePolynomial(final int degree) {
184            return buildPolynomial(degree, LEGENDRE_COEFFICIENTS,
185                                   new RecurrenceCoefficientsGenerator() {
186                /** {@inheritDoc} */
187                public BigFraction[] generate(int k) {
188                    final int kP1 = k + 1;
189                    return new BigFraction[] {
190                            BigFraction.ZERO,
191                            new BigFraction(k + kP1, kP1),
192                            new BigFraction(k, kP1)};
193                }
194            });
195        }
196    
197        /**
198         * Create a Jacobi polynomial.
199         * <p><a href="http://mathworld.wolfram.com/JacobiPolynomial.html">Jacobi
200         * polynomials</a> are orthogonal polynomials.
201         * They can be defined by the following recurrence relations:
202         * <pre>
203         *        P<sub>0</sub><sup>vw</sup>(X)   = 1
204         *        P<sub>-1</sub><sup>vw</sup>(X)  = 0
205         *  2k(k + v + w)(2k + v + w - 2) P<sub>k</sub><sup>vw</sup>(X) =
206         *  (2k + v + w - 1)[(2k + v + w)(2k + v + w - 2) X + v<sup>2</sup> - w<sup>2</sup>] P<sub>k-1</sub><sup>vw</sup>(X)
207         *  - 2(k + v - 1)(k + w - 1)(2k + v + w) P<sub>k-2</sub><sup>vw</sup>(X)
208         * </pre></p>
209         * @param degree degree of the polynomial
210         * @param v first exponent
211         * @param w second exponent
212         * @return Jacobi polynomial of specified degree
213         */
214        public static PolynomialFunction createJacobiPolynomial(final int degree, final int v, final int w) {
215    
216            // select the appropriate list
217            final JacobiKey key = new JacobiKey(v, w);
218    
219            if (!JACOBI_COEFFICIENTS.containsKey(key)) {
220    
221                // allocate a new list for v, w
222                final List<BigFraction> list = new ArrayList<BigFraction>();
223                JACOBI_COEFFICIENTS.put(key, list);
224    
225                // Pv,w,0(x) = 1;
226                list.add(BigFraction.ONE);
227    
228                // P1(x) = (v - w) / 2 + (2 + v + w) * X / 2
229                list.add(new BigFraction(v - w, 2));
230                list.add(new BigFraction(2 + v + w, 2));
231    
232            }
233    
234            return buildPolynomial(degree, JACOBI_COEFFICIENTS.get(key),
235                                   new RecurrenceCoefficientsGenerator() {
236                /** {@inheritDoc} */
237                public BigFraction[] generate(int k) {
238                    k++;
239                    final int kvw      = k + v + w;
240                    final int twoKvw   = kvw + k;
241                    final int twoKvwM1 = twoKvw - 1;
242                    final int twoKvwM2 = twoKvw - 2;
243                    final int den      = 2 * k *  kvw * twoKvwM2;
244    
245                    return new BigFraction[] {
246                        new BigFraction(twoKvwM1 * (v * v - w * w), den),
247                        new BigFraction(twoKvwM1 * twoKvw * twoKvwM2, den),
248                        new BigFraction(2 * (k + v - 1) * (k + w - 1) * twoKvw, den)
249                    };
250                }
251            });
252    
253        }
254    
255        /** Inner class for Jacobi polynomials keys. */
256        private static class JacobiKey {
257    
258            /** First exponent. */
259            private final int v;
260    
261            /** Second exponent. */
262            private final int w;
263    
264            /** Simple constructor.
265             * @param v first exponent
266             * @param w second exponent
267             */
268            public JacobiKey(final int v, final int w) {
269                this.v = v;
270                this.w = w;
271            }
272    
273            /** Get hash code.
274             * @return hash code
275             */
276            public int hashCode() {
277                return (v << 16) ^ w;
278            }
279    
280            /** Check if the instance represent the same key as another instance.
281             * @param key other key
282             * @return true if the instance and the other key refer to the same polynomial
283             */
284            public boolean equals(final Object key) {
285    
286                if ((key == null) || !(key instanceof JacobiKey)) {
287                    return false;
288                }
289    
290                final JacobiKey otherK = (JacobiKey) key;
291                return (v == otherK.v) && (w == otherK.w);
292    
293            }
294        }
295    
296        /**
297         * Compute the coefficients of the polynomial <code>P<sub>s</sub>(x)</code>
298         * whose values at point {@code x} will be the same as the those from the
299         * original polynomial <code>P(x)</code> when computed at {@code x + shift}.
300         * Thus, if <code>P(x) = &Sigma;<sub>i</sub> a<sub>i</sub> x<sup>i</sup></code>,
301         * then
302         * <pre>
303         *  <table>
304         *   <tr>
305         *    <td><code>P<sub>s</sub>(x)</td>
306         *    <td>= &Sigma;<sub>i</sub> b<sub>i</sub> x<sup>i</sup></code></td>
307         *   </tr>
308         *   <tr>
309         *    <td></td>
310         *    <td>= &Sigma;<sub>i</sub> a<sub>i</sub> (x + shift)<sup>i</sup></code></td>
311         *   </tr>
312         *  </table>
313         * </pre>
314         *
315         * @param coefficients Coefficients of the original polynomial.
316         * @param shift Shift value.
317         * @return the coefficients <code>b<sub>i</sub></code> of the shifted
318         * polynomial.
319         */
320        public static double[] shift(final double[] coefficients,
321                                     final double shift) {
322            final int dp1 = coefficients.length;
323            final double[] newCoefficients = new double[dp1];
324    
325            // Pascal triangle.
326            final int[][] coeff = new int[dp1][dp1];
327            for (int i = 0; i < dp1; i++){
328                for(int j = 0; j <= i; j++){
329                    coeff[i][j] = (int) ArithmeticUtils.binomialCoefficient(i, j);
330                }
331            }
332    
333            // First polynomial coefficient.
334            for (int i = 0; i < dp1; i++){
335                newCoefficients[0] += coefficients[i] * FastMath.pow(shift, i);
336            }
337    
338            // Superior order.
339            final int d = dp1 - 1;
340            for (int i = 0; i < d; i++) {
341                for (int j = i; j < d; j++){
342                    newCoefficients[i + 1] += coeff[j + 1][j - i] *
343                        coefficients[j + 1] * FastMath.pow(shift, j - i);
344                }
345            }
346    
347            return newCoefficients;
348        }
349    
350    
351        /** Get the coefficients array for a given degree.
352         * @param degree degree of the polynomial
353         * @param coefficients list where the computed coefficients are stored
354         * @param generator recurrence coefficients generator
355         * @return coefficients array
356         */
357        private static PolynomialFunction buildPolynomial(final int degree,
358                                                          final List<BigFraction> coefficients,
359                                                          final RecurrenceCoefficientsGenerator generator) {
360    
361            final int maxDegree = (int) FastMath.floor(FastMath.sqrt(2 * coefficients.size())) - 1;
362            synchronized (PolynomialsUtils.class) {
363                if (degree > maxDegree) {
364                    computeUpToDegree(degree, maxDegree, generator, coefficients);
365                }
366            }
367    
368            // coefficient  for polynomial 0 is  l [0]
369            // coefficients for polynomial 1 are l [1] ... l [2] (degrees 0 ... 1)
370            // coefficients for polynomial 2 are l [3] ... l [5] (degrees 0 ... 2)
371            // coefficients for polynomial 3 are l [6] ... l [9] (degrees 0 ... 3)
372            // coefficients for polynomial 4 are l[10] ... l[14] (degrees 0 ... 4)
373            // coefficients for polynomial 5 are l[15] ... l[20] (degrees 0 ... 5)
374            // coefficients for polynomial 6 are l[21] ... l[27] (degrees 0 ... 6)
375            // ...
376            final int start = degree * (degree + 1) / 2;
377    
378            final double[] a = new double[degree + 1];
379            for (int i = 0; i <= degree; ++i) {
380                a[i] = coefficients.get(start + i).doubleValue();
381            }
382    
383            // build the polynomial
384            return new PolynomialFunction(a);
385    
386        }
387    
388        /** Compute polynomial coefficients up to a given degree.
389         * @param degree maximal degree
390         * @param maxDegree current maximal degree
391         * @param generator recurrence coefficients generator
392         * @param coefficients list where the computed coefficients should be appended
393         */
394        private static void computeUpToDegree(final int degree, final int maxDegree,
395                                              final RecurrenceCoefficientsGenerator generator,
396                                              final List<BigFraction> coefficients) {
397    
398            int startK = (maxDegree - 1) * maxDegree / 2;
399            for (int k = maxDegree; k < degree; ++k) {
400    
401                // start indices of two previous polynomials Pk(X) and Pk-1(X)
402                int startKm1 = startK;
403                startK += k;
404    
405                // Pk+1(X) = (a[0] + a[1] X) Pk(X) - a[2] Pk-1(X)
406                BigFraction[] ai = generator.generate(k);
407    
408                BigFraction ck     = coefficients.get(startK);
409                BigFraction ckm1   = coefficients.get(startKm1);
410    
411                // degree 0 coefficient
412                coefficients.add(ck.multiply(ai[0]).subtract(ckm1.multiply(ai[2])));
413    
414                // degree 1 to degree k-1 coefficients
415                for (int i = 1; i < k; ++i) {
416                    final BigFraction ckPrev = ck;
417                    ck     = coefficients.get(startK + i);
418                    ckm1   = coefficients.get(startKm1 + i);
419                    coefficients.add(ck.multiply(ai[0]).add(ckPrev.multiply(ai[1])).subtract(ckm1.multiply(ai[2])));
420                }
421    
422                // degree k coefficient
423                final BigFraction ckPrev = ck;
424                ck = coefficients.get(startK + k);
425                coefficients.add(ck.multiply(ai[0]).add(ckPrev.multiply(ai[1])));
426    
427                // degree k+1 coefficient
428                coefficients.add(ck.multiply(ai[1]));
429    
430            }
431    
432        }
433    
434        /** Interface for recurrence coefficients generation. */
435        private interface RecurrenceCoefficientsGenerator {
436            /**
437             * Generate recurrence coefficients.
438             * @param k highest degree of the polynomials used in the recurrence
439             * @return an array of three coefficients such that
440             * P<sub>k+1</sub>(X) = (a[0] + a[1] X) P<sub>k</sub>(X) - a[2] P<sub>k-1</sub>(X)
441             */
442            BigFraction[] generate(int k);
443        }
444    
445    }