FieldODEState.java

  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. package org.apache.commons.math4.legacy.ode;

  18. import org.apache.commons.math4.legacy.core.Field;
  19. import org.apache.commons.math4.legacy.core.RealFieldElement;
  20. import org.apache.commons.math4.legacy.core.MathArrays;

  21. /** Container for time, main and secondary state vectors.

  22.  * @see FirstOrderFieldDifferentialEquations
  23.  * @see FieldSecondaryEquations
  24.  * @see FirstOrderFieldIntegrator
  25.  * @see FieldODEStateAndDerivative
  26.  * @param <T> the type of the field elements
  27.  * @since 3.6
  28.  */

  29. public class FieldODEState<T extends RealFieldElement<T>> {

  30.     /** Time. */
  31.     private final T time;

  32.     /** Main state at time. */
  33.     private final T[] state;

  34.     /** Secondary state at time. */
  35.     private final T[][] secondaryState;

  36.     /** Simple constructor.
  37.      * <p>Calling this constructor is equivalent to call {@link
  38.      * #FieldODEState(RealFieldElement, RealFieldElement[], RealFieldElement[][])
  39.      * FieldODEState(time, state, null)}.</p>
  40.      * @param time time
  41.      * @param state state at time
  42.      */
  43.     public FieldODEState(T time, T[] state) {
  44.         this(time, state, null);
  45.     }

  46.     /** Simple constructor.
  47.      * @param time time
  48.      * @param state state at time
  49.      * @param secondaryState state at time (may be null)
  50.      */
  51.     public FieldODEState(T time, T[] state, T[][] secondaryState) {
  52.         this.time           = time;
  53.         this.state          = state.clone();
  54.         this.secondaryState = copy(time.getField(), secondaryState);
  55.     }

  56.     /** Copy a two-dimensions array.
  57.      * @param field field to which elements belong
  58.      * @param original original array (may be null)
  59.      * @return copied array or null if original array was null
  60.      */
  61.     protected T[][] copy(final Field<T> field, final T[][] original) {

  62.         // special handling of null arrays
  63.         if (original == null) {
  64.             return null;
  65.         }

  66.         // allocate the array
  67.         final T[][] copied = MathArrays.buildArray(field, original.length, -1);

  68.         // copy content
  69.         for (int i = 0; i < original.length; ++i) {
  70.             copied[i] = original[i].clone();
  71.         }

  72.         return copied;
  73.     }

  74.     /** Get time.
  75.      * @return time
  76.      */
  77.     public T getTime() {
  78.         return time;
  79.     }

  80.     /** Get main state dimension.
  81.      * @return main state dimension
  82.      */
  83.     public int getStateDimension() {
  84.         return state.length;
  85.     }

  86.     /** Get main state at time.
  87.      * @return main state at time
  88.      */
  89.     public T[] getState() {
  90.         return state.clone();
  91.     }

  92.     /** Get the number of secondary states.
  93.      * @return number of secondary states.
  94.      */
  95.     public int getNumberOfSecondaryStates() {
  96.         return secondaryState == null ? 0 : secondaryState.length;
  97.     }

  98.     /** Get secondary state dimension.
  99.      * @param index index of the secondary set as returned
  100.      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)}
  101.      * (beware index 0 corresponds to main state, additional states start at 1)
  102.      * @return secondary state dimension
  103.      */
  104.     public int getSecondaryStateDimension(final int index) {
  105.         return index == 0 ? state.length : secondaryState[index - 1].length;
  106.     }

  107.     /** Get secondary state at time.
  108.      * @param index index of the secondary set as returned
  109.      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)}
  110.      * (beware index 0 corresponds to main state, additional states start at 1)
  111.      * @return secondary state at time
  112.      */
  113.     public T[] getSecondaryState(final int index) {
  114.         return index == 0 ? state.clone() : secondaryState[index - 1].clone();
  115.     }
  116. }