View Javadoc
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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.ode;
24  
25  import org.hipparchus.Field;
26  import org.hipparchus.CalculusFieldElement;
27  import org.hipparchus.util.MathArrays;
28  
29  /**
30   * This class is used in the junit tests for the ODE integrators.
31  
32   * <p>This specific problem is the following differential equation :
33   * <pre>
34   *    y' = -y
35   * </pre>
36   * the solution of this equation is a simple exponential function :
37   * <pre>
38   *   y (t) = y (t0) exp (t0-t)
39   * </pre>
40   * </p>
41  
42   * @param <T> the type of the field elements
43   */
44  public class TestFieldProblem1<T extends CalculusFieldElement<T>>
45      extends TestFieldProblemAbstract<T> {
46  
47      /**
48       * Simple constructor.
49       * @param field field to which elements belong
50       */
51      public TestFieldProblem1(Field<T> field) {
52          super(convert(field, 0.0),
53                convert(field, 1.0, 0.1),
54                convert(field, 4.0),
55                convert(field, 1.0, 1.0));
56      }
57  
58      @Override
59      public T[] doComputeDerivatives(T t, T[] y) {
60  
61          final T[] yDot = MathArrays.buildArray(getField(), getDimension());
62  
63          // compute the derivatives
64          for (int i = 0; i < getDimension(); ++i) {
65              yDot[i] = y[i].negate();
66          }
67  
68          return yDot;
69  
70      }
71  
72      @Override
73      public T[] computeTheoreticalState(T t) {
74          final FieldODEState<T> s0 = getInitialState();
75          final T[] y0 = s0.getPrimaryState();
76          final T[] y = MathArrays.buildArray(getField(), getDimension());
77          T c = s0.getTime().subtract(t).exp();
78          for (int i = 0; i < getDimension(); ++i) {
79              y[i] = c.multiply(y0[i]);
80          }
81          return y;
82      }
83  
84  }