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 org.apache.commons.math3.Field;
021import org.apache.commons.math3.RealFieldElement;
022import org.apache.commons.math3.ode.FieldEquationsMapper;
023import org.apache.commons.math3.ode.FieldODEStateAndDerivative;
024import org.apache.commons.math3.util.MathArrays;
025
026/**
027 * This class implements the 3/8 fourth order Runge-Kutta
028 * integrator for Ordinary Differential Equations.
029 *
030 * <p>This method is an explicit Runge-Kutta method, its Butcher-array
031 * is the following one :
032 * <pre>
033 *    0  |  0    0    0    0
034 *   1/3 | 1/3   0    0    0
035 *   2/3 |-1/3   1    0    0
036 *    1  |  1   -1    1    0
037 *       |--------------------
038 *       | 1/8  3/8  3/8  1/8
039 * </pre>
040 * </p>
041 *
042 * @see EulerFieldIntegrator
043 * @see ClassicalRungeKuttaFieldIntegrator
044 * @see GillFieldIntegrator
045 * @see MidpointFieldIntegrator
046 * @see LutherFieldIntegrator
047 * @param <T> the type of the field elements
048 * @since 3.6
049 */
050
051public class ThreeEighthesFieldIntegrator<T extends RealFieldElement<T>>
052    extends RungeKuttaFieldIntegrator<T> {
053
054    /** Simple constructor.
055     * Build a 3/8 integrator with the given step.
056     * @param field field to which the time and state vector elements belong
057     * @param step integration step
058     */
059    public ThreeEighthesFieldIntegrator(final Field<T> field, final T step) {
060        super(field, "3/8", step);
061    }
062
063    /** {@inheritDoc} */
064    public T[] getC() {
065        final T[] c = MathArrays.buildArray(getField(), 3);
066        c[0] = fraction(1, 3);
067        c[1] = c[0].add(c[0]);
068        c[2] = getField().getOne();
069        return c;
070    }
071
072    /** {@inheritDoc} */
073    public T[][] getA() {
074        final T[][] a = MathArrays.buildArray(getField(), 3, -1);
075        for (int i = 0; i < a.length; ++i) {
076            a[i] = MathArrays.buildArray(getField(), i + 1);
077        }
078        a[0][0] = fraction(1, 3);
079        a[1][0] = a[0][0].negate();
080        a[1][1] = getField().getOne();
081        a[2][0] = getField().getOne();
082        a[2][1] = getField().getOne().negate();
083        a[2][2] = getField().getOne();
084        return a;
085    }
086
087    /** {@inheritDoc} */
088    public T[] getB() {
089        final T[] b = MathArrays.buildArray(getField(), 4);
090        b[0] = fraction(1, 8);
091        b[1] = fraction(3, 8);
092        b[2] = b[1];
093        b[3] = b[0];
094        return b;
095    }
096
097    /** {@inheritDoc} */
098    @Override
099    protected ThreeEighthesFieldStepInterpolator<T>
100        createInterpolator(final boolean forward, T[][] yDotK,
101                           final FieldODEStateAndDerivative<T> globalPreviousState,
102                           final FieldODEStateAndDerivative<T> globalCurrentState,
103                           final FieldEquationsMapper<T> mapper) {
104        return new ThreeEighthesFieldStepInterpolator<T>(getField(), forward, yDotK,
105                                                         globalPreviousState, globalCurrentState,
106                                                         globalPreviousState, globalCurrentState,
107                                                         mapper);
108    }
109
110}