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;
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.core.MathArrays;
23
24 /**
25 * This class is used in the junit tests for the ODE integrators.
26
27 * <p>This specific problem is the following differential equation :
28 * <pre>
29 * y' = -y
30 * </pre>
31 * the solution of this equation is a simple exponential function :
32 * <pre>
33 * y (t) = y (t0) exp (t0-t)
34 * </pre>
35 * </p>
36
37 * @param <T> the type of the field elements
38 */
39 public class TestFieldProblem1<T extends RealFieldElement<T>>
40 extends TestFieldProblemAbstract<T> {
41
42 /**
43 * Simple constructor.
44 * @param field field to which elements belong
45 */
46 public TestFieldProblem1(Field<T> field) {
47 super(field);
48 setInitialConditions(convert(0.0), convert(1.0, 0.1));
49 setFinalConditions(convert(4.0));
50 setErrorScale(convert(1.0, 1.0));
51 }
52
53 @Override
54 public T[] doComputeDerivatives(T t, T[] y) {
55
56 final T[] yDot = MathArrays.buildArray(getField(), getDimension());
57
58 // compute the derivatives
59 for (int i = 0; i < getDimension(); ++i) {
60 yDot[i] = y[i].negate();
61 }
62
63 return yDot;
64 }
65
66 @Override
67 public T[] computeTheoreticalState(T t) {
68 final FieldODEState<T> s0 = getInitialState();
69 final T[] y0 = s0.getState();
70 final T[] y = MathArrays.buildArray(getField(), getDimension());
71 T c = s0.getTime().subtract(t).exp();
72 for (int i = 0; i < getDimension(); ++i) {
73 y[i] = c.multiply(y0[i]);
74 }
75 return y;
76 }
77 }