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 simple Euler integrator for Ordinary
028 * Differential Equations.
029 *
030 * <p>The Euler algorithm is the simplest one that can be used to
031 * integrate ordinary differential equations. It is a simple inversion
032 * of the forward difference expression :
033 * <code>f'=(f(t+h)-f(t))/h</code> which leads to
034 * <code>f(t+h)=f(t)+hf'</code>. The interpolation scheme used for
035 * dense output is the linear scheme already used for integration.</p>
036 *
037 * <p>This algorithm looks cheap because it needs only one function
038 * evaluation per step. However, as it uses linear estimates, it needs
039 * very small steps to achieve high accuracy, and small steps lead to
040 * numerical errors and instabilities.</p>
041 *
042 * <p>This algorithm is almost never used and has been included in
043 * this package only as a comparison reference for more useful
044 * integrators.</p>
045 *
046 * @see MidpointFieldIntegrator
047 * @see ClassicalRungeKuttaFieldIntegrator
048 * @see GillFieldIntegrator
049 * @see ThreeEighthesFieldIntegrator
050 * @see LutherFieldIntegrator
051 * @param <T> the type of the field elements
052 * @since 3.6
053 */
054
055public class EulerFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {
056
057    /** Simple constructor.
058     * Build an Euler integrator with the given step.
059     * @param field field to which the time and state vector elements belong
060     * @param step integration step
061     */
062    public EulerFieldIntegrator(final Field<T> field, final T step) {
063        super(field, "Euler", step);
064    }
065
066    /** {@inheritDoc} */
067    @Override
068    public T[] getC() {
069        return MathArrays.buildArray(getField(), 0);
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public T[][] getA() {
075        return MathArrays.buildArray(getField(), 0, 0);
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public T[] getB() {
081        final T[] b = MathArrays.buildArray(getField(), 1);
082        b[0] = getField().getOne();
083        return b;
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    protected EulerFieldStepInterpolator<T>
089        createInterpolator(final boolean forward, T[][] yDotK,
090                           final FieldODEStateAndDerivative<T> globalPreviousState,
091                           final FieldODEStateAndDerivative<T> globalCurrentState,
092                           final FieldEquationsMapper<T> mapper) {
093        return new EulerFieldStepInterpolator<>(getField(), forward, yDotK,
094                                                 globalPreviousState, globalCurrentState,
095                                                 globalPreviousState, globalCurrentState,
096                                                 mapper);
097    }
098}