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.math4.legacy.ode.nonstiff;
019
020import org.apache.commons.math4.legacy.core.Field;
021import org.apache.commons.math4.legacy.core.RealFieldElement;
022import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
023import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
024import org.apache.commons.math4.legacy.core.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 *
041 * @see EulerFieldIntegrator
042 * @see ClassicalRungeKuttaFieldIntegrator
043 * @see GillFieldIntegrator
044 * @see MidpointFieldIntegrator
045 * @see LutherFieldIntegrator
046 * @param <T> the type of the field elements
047 * @since 3.6
048 */
049
050public class ThreeEighthesFieldIntegrator<T extends RealFieldElement<T>>
051    extends RungeKuttaFieldIntegrator<T> {
052
053    /** Simple constructor.
054     * Build a 3/8 integrator with the given step.
055     * @param field field to which the time and state vector elements belong
056     * @param step integration step
057     */
058    public ThreeEighthesFieldIntegrator(final Field<T> field, final T step) {
059        super(field, "3/8", step);
060    }
061
062    /** {@inheritDoc} */
063    @Override
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    @Override
074    public T[][] getA() {
075        final T[][] a = MathArrays.buildArray(getField(), 3, -1);
076        for (int i = 0; i < a.length; ++i) {
077            a[i] = MathArrays.buildArray(getField(), i + 1);
078        }
079        a[0][0] = fraction(1, 3);
080        a[1][0] = a[0][0].negate();
081        a[1][1] = getField().getOne();
082        a[2][0] = getField().getOne();
083        a[2][1] = getField().getOne().negate();
084        a[2][2] = getField().getOne();
085        return a;
086    }
087
088    /** {@inheritDoc} */
089    @Override
090    public T[] getB() {
091        final T[] b = MathArrays.buildArray(getField(), 4);
092        b[0] = fraction(1, 8);
093        b[1] = fraction(3, 8);
094        b[2] = b[1];
095        b[3] = b[0];
096        return b;
097    }
098
099    /** {@inheritDoc} */
100    @Override
101    protected ThreeEighthesFieldStepInterpolator<T>
102        createInterpolator(final boolean forward, T[][] yDotK,
103                           final FieldODEStateAndDerivative<T> globalPreviousState,
104                           final FieldODEStateAndDerivative<T> globalCurrentState,
105                           final FieldEquationsMapper<T> mapper) {
106        return new ThreeEighthesFieldStepInterpolator<>(getField(), forward, yDotK,
107                                                         globalPreviousState, globalCurrentState,
108                                                         globalPreviousState, globalCurrentState,
109                                                         mapper);
110    }
111}