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 a second order Runge-Kutta integrator for
028 * 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
034 *   1/2 | 1/2   0
035 *       |----------
036 *       |  0    1
037 * </pre>
038 *
039 * @see EulerFieldIntegrator
040 * @see ClassicalRungeKuttaFieldIntegrator
041 * @see GillFieldIntegrator
042 * @see ThreeEighthesFieldIntegrator
043 * @see LutherFieldIntegrator
044 *
045 * @param <T> the type of the field elements
046 * @since 3.6
047 */
048
049public class MidpointFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {
050
051    /** Simple constructor.
052     * Build a midpoint integrator with the given step.
053     * @param field field to which the time and state vector elements belong
054     * @param step integration step
055     */
056    public MidpointFieldIntegrator(final Field<T> field, final T step) {
057        super(field, "midpoint", step);
058    }
059
060    /** {@inheritDoc} */
061    @Override
062    public T[] getC() {
063        final T[] c = MathArrays.buildArray(getField(), 1);
064        c[0] = getField().getOne().multiply(0.5);
065        return c;
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public T[][] getA() {
071        final T[][] a = MathArrays.buildArray(getField(), 1, 1);
072        a[0][0] = fraction(1, 2);
073        return a;
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    public T[] getB() {
079        final T[] b = MathArrays.buildArray(getField(), 2);
080        b[0] = getField().getZero();
081        b[1] = getField().getOne();
082        return b;
083    }
084
085    /** {@inheritDoc} */
086    @Override
087    protected MidpointFieldStepInterpolator<T>
088        createInterpolator(final boolean forward, T[][] yDotK,
089                           final FieldODEStateAndDerivative<T> globalPreviousState,
090                           final FieldODEStateAndDerivative<T> globalCurrentState,
091                           final FieldEquationsMapper<T> mapper) {
092        return new MidpointFieldStepInterpolator<>(getField(), forward, yDotK,
093                                                    globalPreviousState, globalCurrentState,
094                                                    globalPreviousState, globalCurrentState,
095                                                    mapper);
096    }
097}