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    *      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  /** Container for time, main and secondary state vectors.
25  
26   * @see FirstOrderFieldDifferentialEquations
27   * @see FieldSecondaryEquations
28   * @see FirstOrderFieldIntegrator
29   * @see FieldODEStateAndDerivative
30   * @param <T> the type of the field elements
31   * @since 3.6
32   */
33  
34  public class FieldODEState<T extends RealFieldElement<T>> {
35  
36      /** Time. */
37      private final T time;
38  
39      /** Main state at time. */
40      private final T[] state;
41  
42      /** Secondary state at time. */
43      private final T[][] secondaryState;
44  
45      /** Simple constructor.
46       * <p>Calling this constructor is equivalent to call {@link
47       * #FieldODEState(RealFieldElement, RealFieldElement[], RealFieldElement[][])
48       * FieldODEState(time, state, null)}.</p>
49       * @param time time
50       * @param state state at time
51       */
52      public FieldODEState(T time, T[] state) {
53          this(time, state, null);
54      }
55  
56      /** Simple constructor.
57       * @param time time
58       * @param state state at time
59       * @param secondaryState state at time (may be null)
60       */
61      public FieldODEState(T time, T[] state, T[][] secondaryState) {
62          this.time           = time;
63          this.state          = state.clone();
64          this.secondaryState = copy(time.getField(), secondaryState);
65      }
66  
67      /** Copy a two-dimensions array.
68       * @param field field to which elements belong
69       * @param original original array (may be null)
70       * @return copied array or null if original array was null
71       */
72      protected T[][] copy(final Field<T> field, final T[][] original) {
73  
74          // special handling of null arrays
75          if (original == null) {
76              return null;
77          }
78  
79          // allocate the array
80          final T[][] copied = MathArrays.buildArray(field, original.length, -1);
81  
82          // copy content
83          for (int i = 0; i < original.length; ++i) {
84              copied[i] = original[i].clone();
85          }
86  
87          return copied;
88      }
89  
90      /** Get time.
91       * @return time
92       */
93      public T getTime() {
94          return time;
95      }
96  
97      /** Get main state dimension.
98       * @return main state dimension
99       */
100     public int getStateDimension() {
101         return state.length;
102     }
103 
104     /** Get main state at time.
105      * @return main state at time
106      */
107     public T[] getState() {
108         return state.clone();
109     }
110 
111     /** Get the number of secondary states.
112      * @return number of secondary states.
113      */
114     public int getNumberOfSecondaryStates() {
115         return secondaryState == null ? 0 : secondaryState.length;
116     }
117 
118     /** Get secondary state dimension.
119      * @param index index of the secondary set as returned
120      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)}
121      * (beware index 0 corresponds to main state, additional states start at 1)
122      * @return secondary state dimension
123      */
124     public int getSecondaryStateDimension(final int index) {
125         return index == 0 ? state.length : secondaryState[index - 1].length;
126     }
127 
128     /** Get secondary state at time.
129      * @param index index of the secondary set as returned
130      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)}
131      * (beware index 0 corresponds to main state, additional states start at 1)
132      * @return secondary state at time
133      */
134     public T[] getSecondaryState(final int index) {
135         return index == 0 ? state.clone() : secondaryState[index - 1].clone();
136     }
137 }