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.math3.ode;
19 
20 import org.apache.commons.math3.Field;
21 import org.apache.commons.math3.RealFieldElement;
22 import org.apache.commons.math3.util.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      */
FieldODEState(T time, T[] state)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      */
FieldODEState(T time, T[] state, T[][] secondaryState)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      */
copy(final Field<T> field, final T[][] original)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 
91     /** Get time.
92      * @return time
93      */
getTime()94     public T getTime() {
95         return time;
96     }
97 
98     /** Get main state dimension.
99      * @return main state dimension
100      */
getStateDimension()101     public int getStateDimension() {
102         return state.length;
103     }
104 
105     /** Get main state at time.
106      * @return main state at time
107      */
getState()108     public T[] getState() {
109         return state.clone();
110     }
111 
112     /** Get the number of secondary states.
113      * @return number of secondary states.
114      */
getNumberOfSecondaryStates()115     public int getNumberOfSecondaryStates() {
116         return secondaryState == null ? 0 : secondaryState.length;
117     }
118 
119     /** Get secondary state dimension.
120      * @param index index of the secondary set as returned
121      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)}
122      * (beware index 0 corresponds to main state, additional states start at 1)
123      * @return secondary state dimension
124      */
getSecondaryStateDimension(final int index)125     public int getSecondaryStateDimension(final int index) {
126         return index == 0 ? state.length : secondaryState[index - 1].length;
127     }
128 
129     /** Get secondary state at time.
130      * @param index index of the secondary set as returned
131      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)}
132      * (beware index 0 corresponds to main state, additional states start at 1)
133      * @return secondary state at time
134      */
getSecondaryState(final int index)135     public T[] getSecondaryState(final int index) {
136         return index == 0 ? state.clone() : secondaryState[index - 1].clone();
137     }
138 
139 }
140