1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.math4.legacy.ode.nonstiff;
19
20 import org.apache.commons.math4.legacy.core.Field;
21 import org.apache.commons.math4.legacy.core.RealFieldElement;
22 import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
23 import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
24 import org.apache.commons.math4.legacy.core.MathArrays;
25
26 /**
27 * This class implements a simple Euler integrator for Ordinary
28 * Differential Equations.
29 *
30 * <p>The Euler algorithm is the simplest one that can be used to
31 * integrate ordinary differential equations. It is a simple inversion
32 * of the forward difference expression :
33 * <code>f'=(f(t+h)-f(t))/h</code> which leads to
34 * <code>f(t+h)=f(t)+hf'</code>. The interpolation scheme used for
35 * dense output is the linear scheme already used for integration.</p>
36 *
37 * <p>This algorithm looks cheap because it needs only one function
38 * evaluation per step. However, as it uses linear estimates, it needs
39 * very small steps to achieve high accuracy, and small steps lead to
40 * numerical errors and instabilities.</p>
41 *
42 * <p>This algorithm is almost never used and has been included in
43 * this package only as a comparison reference for more useful
44 * integrators.</p>
45 *
46 * @see MidpointFieldIntegrator
47 * @see ClassicalRungeKuttaFieldIntegrator
48 * @see GillFieldIntegrator
49 * @see ThreeEighthesFieldIntegrator
50 * @see LutherFieldIntegrator
51 * @param <T> the type of the field elements
52 * @since 3.6
53 */
54
55 public class EulerFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {
56
57 /** Simple constructor.
58 * Build an Euler integrator with the given step.
59 * @param field field to which the time and state vector elements belong
60 * @param step integration step
61 */
62 public EulerFieldIntegrator(final Field<T> field, final T step) {
63 super(field, "Euler", step);
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 public T[] getC() {
69 return MathArrays.buildArray(getField(), 0);
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 public T[][] getA() {
75 return MathArrays.buildArray(getField(), 0, 0);
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public T[] getB() {
81 final T[] b = MathArrays.buildArray(getField(), 1);
82 b[0] = getField().getOne();
83 return b;
84 }
85
86 /** {@inheritDoc} */
87 @Override
88 protected EulerFieldStepInterpolator<T>
89 createInterpolator(final boolean forward, T[][] yDotK,
90 final FieldODEStateAndDerivative<T> globalPreviousState,
91 final FieldODEStateAndDerivative<T> globalCurrentState,
92 final FieldEquationsMapper<T> mapper) {
93 return new EulerFieldStepInterpolator<>(getField(), forward, yDotK,
94 globalPreviousState, globalCurrentState,
95 globalPreviousState, globalCurrentState,
96 mapper);
97 }
98 }