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
018package org.apache.commons.math3.ode.nonstiff;
019
020import java.util.Arrays;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.commons.math3.fraction.BigFraction;
025import org.apache.commons.math3.linear.Array2DRowFieldMatrix;
026import org.apache.commons.math3.linear.Array2DRowRealMatrix;
027import org.apache.commons.math3.linear.ArrayFieldVector;
028import org.apache.commons.math3.linear.FieldDecompositionSolver;
029import org.apache.commons.math3.linear.FieldLUDecomposition;
030import org.apache.commons.math3.linear.FieldMatrix;
031import org.apache.commons.math3.linear.MatrixUtils;
032import org.apache.commons.math3.linear.QRDecomposition;
033import org.apache.commons.math3.linear.RealMatrix;
034
035/** Transformer to Nordsieck vectors for Adams integrators.
036 * <p>This class is used by {@link AdamsBashforthIntegrator Adams-Bashforth} and
037 * {@link AdamsMoultonIntegrator Adams-Moulton} integrators to convert between
038 * classical representation with several previous first derivatives and Nordsieck
039 * representation with higher order scaled derivatives.</p>
040 *
041 * <p>We define scaled derivatives s<sub>i</sub>(n) at step n as:
042 * <pre>
043 * s<sub>1</sub>(n) = h y'<sub>n</sub> for first derivative
044 * s<sub>2</sub>(n) = h<sup>2</sup>/2 y''<sub>n</sub> for second derivative
045 * s<sub>3</sub>(n) = h<sup>3</sup>/6 y'''<sub>n</sub> for third derivative
046 * ...
047 * s<sub>k</sub>(n) = h<sup>k</sup>/k! y<sup>(k)</sup><sub>n</sub> for k<sup>th</sup> derivative
048 * </pre></p>
049 *
050 * <p>With the previous definition, the classical representation of multistep methods
051 * uses first derivatives only, i.e. it handles y<sub>n</sub>, s<sub>1</sub>(n) and
052 * q<sub>n</sub> where q<sub>n</sub> is defined as:
053 * <pre>
054 *   q<sub>n</sub> = [ s<sub>1</sub>(n-1) s<sub>1</sub>(n-2) ... s<sub>1</sub>(n-(k-1)) ]<sup>T</sup>
055 * </pre>
056 * (we omit the k index in the notation for clarity).</p>
057 *
058 * <p>Another possible representation uses the Nordsieck vector with
059 * higher degrees scaled derivatives all taken at the same step, i.e it handles y<sub>n</sub>,
060 * s<sub>1</sub>(n) and r<sub>n</sub>) where r<sub>n</sub> is defined as:
061 * <pre>
062 * r<sub>n</sub> = [ s<sub>2</sub>(n), s<sub>3</sub>(n) ... s<sub>k</sub>(n) ]<sup>T</sup>
063 * </pre>
064 * (here again we omit the k index in the notation for clarity)
065 * </p>
066 *
067 * <p>Taylor series formulas show that for any index offset i, s<sub>1</sub>(n-i) can be
068 * computed from s<sub>1</sub>(n), s<sub>2</sub>(n) ... s<sub>k</sub>(n), the formula being exact
069 * for degree k polynomials.
070 * <pre>
071 * s<sub>1</sub>(n-i) = s<sub>1</sub>(n) + &sum;<sub>j&gt;0</sub> (j+1) (-i)<sup>j</sup> s<sub>j+1</sub>(n)
072 * </pre>
073 * The previous formula can be used with several values for i to compute the transform between
074 * classical representation and Nordsieck vector at step end. The transform between r<sub>n</sub>
075 * and q<sub>n</sub> resulting from the Taylor series formulas above is:
076 * <pre>
077 * q<sub>n</sub> = s<sub>1</sub>(n) u + P r<sub>n</sub>
078 * </pre>
079 * where u is the [ 1 1 ... 1 ]<sup>T</sup> vector and P is the (k-1)&times;(k-1) matrix built
080 * with the (j+1) (-i)<sup>j</sup> terms with i being the row number starting from 1 and j being
081 * the column number starting from 1:
082 * <pre>
083 *        [  -2   3   -4    5  ... ]
084 *        [  -4  12  -32   80  ... ]
085 *   P =  [  -6  27 -108  405  ... ]
086 *        [  -8  48 -256 1280  ... ]
087 *        [          ...           ]
088 * </pre></p>
089 *
090 * <p>Changing -i into +i in the formula above can be used to compute a similar transform between
091 * classical representation and Nordsieck vector at step start. The resulting matrix is simply
092 * the absolute value of matrix P.</p>
093 *
094 * <p>For {@link AdamsBashforthIntegrator Adams-Bashforth} method, the Nordsieck vector
095 * at step n+1 is computed from the Nordsieck vector at step n as follows:
096 * <ul>
097 *   <li>y<sub>n+1</sub> = y<sub>n</sub> + s<sub>1</sub>(n) + u<sup>T</sup> r<sub>n</sub></li>
098 *   <li>s<sub>1</sub>(n+1) = h f(t<sub>n+1</sub>, y<sub>n+1</sub>)</li>
099 *   <li>r<sub>n+1</sub> = (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u + P<sup>-1</sup> A P r<sub>n</sub></li>
100 * </ul>
101 * where A is a rows shifting matrix (the lower left part is an identity matrix):
102 * <pre>
103 *        [ 0 0   ...  0 0 | 0 ]
104 *        [ ---------------+---]
105 *        [ 1 0   ...  0 0 | 0 ]
106 *    A = [ 0 1   ...  0 0 | 0 ]
107 *        [       ...      | 0 ]
108 *        [ 0 0   ...  1 0 | 0 ]
109 *        [ 0 0   ...  0 1 | 0 ]
110 * </pre></p>
111 *
112 * <p>For {@link AdamsMoultonIntegrator Adams-Moulton} method, the predicted Nordsieck vector
113 * at step n+1 is computed from the Nordsieck vector at step n as follows:
114 * <ul>
115 *   <li>Y<sub>n+1</sub> = y<sub>n</sub> + s<sub>1</sub>(n) + u<sup>T</sup> r<sub>n</sub></li>
116 *   <li>S<sub>1</sub>(n+1) = h f(t<sub>n+1</sub>, Y<sub>n+1</sub>)</li>
117 *   <li>R<sub>n+1</sub> = (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u + P<sup>-1</sup> A P r<sub>n</sub></li>
118 * </ul>
119 * From this predicted vector, the corrected vector is computed as follows:
120 * <ul>
121 *   <li>y<sub>n+1</sub> = y<sub>n</sub> + S<sub>1</sub>(n+1) + [ -1 +1 -1 +1 ... &plusmn;1 ] r<sub>n+1</sub></li>
122 *   <li>s<sub>1</sub>(n+1) = h f(t<sub>n+1</sub>, y<sub>n+1</sub>)</li>
123 *   <li>r<sub>n+1</sub> = R<sub>n+1</sub> + (s<sub>1</sub>(n+1) - S<sub>1</sub>(n+1)) P<sup>-1</sup> u</li>
124 * </ul>
125 * where the upper case Y<sub>n+1</sub>, S<sub>1</sub>(n+1) and R<sub>n+1</sub> represent the
126 * predicted states whereas the lower case y<sub>n+1</sub>, s<sub>n+1</sub> and r<sub>n+1</sub>
127 * represent the corrected states.</p>
128 *
129 * <p>We observe that both methods use similar update formulas. In both cases a P<sup>-1</sup>u
130 * vector and a P<sup>-1</sup> A P matrix are used that do not depend on the state,
131 * they only depend on k. This class handles these transformations.</p>
132 *
133 * @since 2.0
134 */
135public class AdamsNordsieckTransformer {
136
137    /** Cache for already computed coefficients. */
138    private static final Map<Integer, AdamsNordsieckTransformer> CACHE =
139        new HashMap<Integer, AdamsNordsieckTransformer>();
140
141    /** Update matrix for the higher order derivatives h<sup>2</sup>/2 y'', h<sup>3</sup>/6 y''' ... */
142    private final Array2DRowRealMatrix update;
143
144    /** Update coefficients of the higher order derivatives wrt y'. */
145    private final double[] c1;
146
147    /** Simple constructor.
148     * @param n number of steps of the multistep method
149     * (excluding the one being computed)
150     */
151    private AdamsNordsieckTransformer(final int n) {
152
153        final int rows = n - 1;
154
155        // compute exact coefficients
156        FieldMatrix<BigFraction> bigP = buildP(rows);
157        FieldDecompositionSolver<BigFraction> pSolver =
158            new FieldLUDecomposition<BigFraction>(bigP).getSolver();
159
160        BigFraction[] u = new BigFraction[rows];
161        Arrays.fill(u, BigFraction.ONE);
162        BigFraction[] bigC1 = pSolver.solve(new ArrayFieldVector<BigFraction>(u, false)).toArray();
163
164        // update coefficients are computed by combining transform from
165        // Nordsieck to multistep, then shifting rows to represent step advance
166        // then applying inverse transform
167        BigFraction[][] shiftedP = bigP.getData();
168        for (int i = shiftedP.length - 1; i > 0; --i) {
169            // shift rows
170            shiftedP[i] = shiftedP[i - 1];
171        }
172        shiftedP[0] = new BigFraction[rows];
173        Arrays.fill(shiftedP[0], BigFraction.ZERO);
174        FieldMatrix<BigFraction> bigMSupdate =
175            pSolver.solve(new Array2DRowFieldMatrix<BigFraction>(shiftedP, false));
176
177        // convert coefficients to double
178        update         = MatrixUtils.bigFractionMatrixToRealMatrix(bigMSupdate);
179        c1             = new double[rows];
180        for (int i = 0; i < rows; ++i) {
181            c1[i] = bigC1[i].doubleValue();
182        }
183
184    }
185
186    /** Get the Nordsieck transformer for a given number of steps.
187     * @param nSteps number of steps of the multistep method
188     * (excluding the one being computed)
189     * @return Nordsieck transformer for the specified number of steps
190     */
191    public static AdamsNordsieckTransformer getInstance(final int nSteps) {
192        synchronized(CACHE) {
193            AdamsNordsieckTransformer t = CACHE.get(nSteps);
194            if (t == null) {
195                t = new AdamsNordsieckTransformer(nSteps);
196                CACHE.put(nSteps, t);
197            }
198            return t;
199        }
200    }
201
202    /** Get the number of steps of the method
203     * (excluding the one being computed).
204     * @return number of steps of the method
205     * (excluding the one being computed)
206     * @deprecated as of 3.6, this method is not used anymore
207     */
208    @Deprecated
209    public int getNSteps() {
210        return c1.length;
211    }
212
213    /** Build the P matrix.
214     * <p>The P matrix general terms are shifted (j+1) (-i)<sup>j</sup> terms
215     * with i being the row number starting from 1 and j being the column
216     * number starting from 1:
217     * <pre>
218     *        [  -2   3   -4    5  ... ]
219     *        [  -4  12  -32   80  ... ]
220     *   P =  [  -6  27 -108  405  ... ]
221     *        [  -8  48 -256 1280  ... ]
222     *        [          ...           ]
223     * </pre></p>
224     * @param rows number of rows of the matrix
225     * @return P matrix
226     */
227    private FieldMatrix<BigFraction> buildP(final int rows) {
228
229        final BigFraction[][] pData = new BigFraction[rows][rows];
230
231        for (int i = 1; i <= pData.length; ++i) {
232            // build the P matrix elements from Taylor series formulas
233            final BigFraction[] pI = pData[i - 1];
234            final int factor = -i;
235            int aj = factor;
236            for (int j = 1; j <= pI.length; ++j) {
237                pI[j - 1] = new BigFraction(aj * (j + 1));
238                aj *= factor;
239            }
240        }
241
242        return new Array2DRowFieldMatrix<BigFraction>(pData, false);
243
244    }
245
246    /** Initialize the high order scaled derivatives at step start.
247     * @param h step size to use for scaling
248     * @param t first steps times
249     * @param y first steps states
250     * @param yDot first steps derivatives
251     * @return Nordieck vector at start of first step (h<sup>2</sup>/2 y''<sub>n</sub>,
252     * h<sup>3</sup>/6 y'''<sub>n</sub> ... h<sup>k</sup>/k! y<sup>(k)</sup><sub>n</sub>)
253     */
254
255    public Array2DRowRealMatrix initializeHighOrderDerivatives(final double h, final double[] t,
256                                                               final double[][] y,
257                                                               final double[][] yDot) {
258
259        // using Taylor series with di = ti - t0, we get:
260        //  y(ti)  - y(t0)  - di y'(t0) =   di^2 / h^2 s2 + ... +   di^k     / h^k sk + O(h^k)
261        //  y'(ti) - y'(t0)             = 2 di   / h^2 s2 + ... + k di^(k-1) / h^k sk + O(h^(k-1))
262        // we write these relations for i = 1 to i= 1+n/2 as a set of n + 2 linear
263        // equations depending on the Nordsieck vector [s2 ... sk rk], so s2 to sk correspond
264        // to the appropriately truncated Taylor expansion, and rk is the Taylor remainder.
265        // The goal is to have s2 to sk as accurate as possible considering the fact the sum is
266        // truncated and we don't want the error terms to be included in s2 ... sk, so we need
267        // to solve also for the remainder
268        final double[][] a     = new double[c1.length + 1][c1.length + 1];
269        final double[][] b     = new double[c1.length + 1][y[0].length];
270        final double[]   y0    = y[0];
271        final double[]   yDot0 = yDot[0];
272        for (int i = 1; i < y.length; ++i) {
273
274            final double di    = t[i] - t[0];
275            final double ratio = di / h;
276            double dikM1Ohk    =  1 / h;
277
278            // linear coefficients of equations
279            // y(ti) - y(t0) - di y'(t0) and y'(ti) - y'(t0)
280            final double[] aI    = a[2 * i - 2];
281            final double[] aDotI = (2 * i - 1) < a.length ? a[2 * i - 1] : null;
282            for (int j = 0; j < aI.length; ++j) {
283                dikM1Ohk *= ratio;
284                aI[j]     = di      * dikM1Ohk;
285                if (aDotI != null) {
286                    aDotI[j]  = (j + 2) * dikM1Ohk;
287                }
288            }
289
290            // expected value of the previous equations
291            final double[] yI    = y[i];
292            final double[] yDotI = yDot[i];
293            final double[] bI    = b[2 * i - 2];
294            final double[] bDotI = (2 * i - 1) < b.length ? b[2 * i - 1] : null;
295            for (int j = 0; j < yI.length; ++j) {
296                bI[j]    = yI[j] - y0[j] - di * yDot0[j];
297                if (bDotI != null) {
298                    bDotI[j] = yDotI[j] - yDot0[j];
299                }
300            }
301
302        }
303
304        // solve the linear system to get the best estimate of the Nordsieck vector [s2 ... sk],
305        // with the additional terms s(k+1) and c grabbing the parts after the truncated Taylor expansion
306        final QRDecomposition decomposition = new QRDecomposition(new Array2DRowRealMatrix(a, false));
307        final RealMatrix x = decomposition.getSolver().solve(new Array2DRowRealMatrix(b, false));
308
309        // extract just the Nordsieck vector [s2 ... sk]
310        final Array2DRowRealMatrix truncatedX = new Array2DRowRealMatrix(x.getRowDimension() - 1, x.getColumnDimension());
311        for (int i = 0; i < truncatedX.getRowDimension(); ++i) {
312            for (int j = 0; j < truncatedX.getColumnDimension(); ++j) {
313                truncatedX.setEntry(i, j, x.getEntry(i, j));
314            }
315        }
316        return truncatedX;
317
318    }
319
320    /** Update the high order scaled derivatives for Adams integrators (phase 1).
321     * <p>The complete update of high order derivatives has a form similar to:
322     * <pre>
323     * r<sub>n+1</sub> = (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u + P<sup>-1</sup> A P r<sub>n</sub>
324     * </pre>
325     * this method computes the P<sup>-1</sup> A P r<sub>n</sub> part.</p>
326     * @param highOrder high order scaled derivatives
327     * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
328     * @return updated high order derivatives
329     * @see #updateHighOrderDerivativesPhase2(double[], double[], Array2DRowRealMatrix)
330     */
331    public Array2DRowRealMatrix updateHighOrderDerivativesPhase1(final Array2DRowRealMatrix highOrder) {
332        return update.multiply(highOrder);
333    }
334
335    /** Update the high order scaled derivatives Adams integrators (phase 2).
336     * <p>The complete update of high order derivatives has a form similar to:
337     * <pre>
338     * r<sub>n+1</sub> = (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u + P<sup>-1</sup> A P r<sub>n</sub>
339     * </pre>
340     * this method computes the (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u part.</p>
341     * <p>Phase 1 of the update must already have been performed.</p>
342     * @param start first order scaled derivatives at step start
343     * @param end first order scaled derivatives at step end
344     * @param highOrder high order scaled derivatives, will be modified
345     * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
346     * @see #updateHighOrderDerivativesPhase1(Array2DRowRealMatrix)
347     */
348    public void updateHighOrderDerivativesPhase2(final double[] start,
349                                                 final double[] end,
350                                                 final Array2DRowRealMatrix highOrder) {
351        final double[][] data = highOrder.getDataRef();
352        for (int i = 0; i < data.length; ++i) {
353            final double[] dataI = data[i];
354            final double c1I = c1[i];
355            for (int j = 0; j < dataI.length; ++j) {
356                dataI[j] += c1I * (start[j] - end[j]);
357            }
358        }
359    }
360
361}