ComplexODEState.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.ode;

  18. import java.io.Serializable;

  19. import org.hipparchus.complex.Complex;

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

  21.  * @see ComplexOrdinaryDifferentialEquation
  22.  * @see SecondaryODE
  23.  * @see ODEIntegrator
  24.  * @see ODEStateAndDerivative
  25.  * @since 1.4
  26.  */

  27. public class ComplexODEState implements Serializable {

  28.     /** Serializable UID. */
  29.     private static final long serialVersionUID = 20180902;

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

  32.     /** Primary state at time. */
  33.     private final Complex[] primaryState;

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

  36.     /** Complete dimension. */
  37.     private final int completeDimension;

  38.     /** Simple constructor.
  39.      * <p>Calling this constructor is equivalent to call {@link
  40.      * #ComplexODEState(double, Complex[], Complex[][])
  41.      * ComplexODEState(time, state, null)}.</p>
  42.      * @param time time
  43.      * @param primaryState primary state at time
  44.      */
  45.     public ComplexODEState(double time, Complex[] primaryState) {
  46.         this(time, primaryState, null);
  47.     }

  48.     /** Simple constructor.
  49.      * @param time time
  50.      * @param primaryState state at time
  51.      * @param secondaryState primary state at time (may be null)
  52.      */
  53.     public ComplexODEState(double time, Complex[] primaryState, Complex[][] secondaryState) {

  54.         this.time           = time;
  55.         this.primaryState   = primaryState.clone();
  56.         this.secondaryState = copy(secondaryState);

  57.         // compute once and for all the complete dimension
  58.         int dimension = primaryState.length;
  59.         if (secondaryState != null) {
  60.             for (final Complex[] secondary : secondaryState) {
  61.                 dimension += secondary.length;
  62.             }
  63.         }
  64.         this.completeDimension = dimension;

  65.     }

  66.     /** Copy a two-dimensions array.
  67.      * @param original original array (may be null)
  68.      * @return copied array or null if original array was null
  69.      */
  70.     protected Complex[][] copy(final Complex[][] original) {

  71.         // special handling of null arrays
  72.         if (original == null) {
  73.             return null; // NOPMD
  74.         }

  75.         // allocate the array
  76.         final Complex[][] copied = new Complex[original.length][];

  77.         // copy content
  78.         for (int i = 0; i < original.length; ++i) {
  79.             copied[i] = original[i].clone();
  80.         }

  81.         return copied;

  82.     }

  83.     /** Get time.
  84.      * @return time
  85.      */
  86.     public double getTime() {
  87.         return time;
  88.     }

  89.     /** Get primary state dimension.
  90.      * @return primary state dimension
  91.      * @see #getSecondaryStateDimension(int)
  92.      * @see #getCompleteStateDimension()
  93.      */
  94.     public int getPrimaryStateDimension() {
  95.         return primaryState.length;
  96.     }

  97.     /** Get primary state at time.
  98.      * @return primary state at time
  99.      * @see #getSecondaryState(int)
  100.      * @see #getCompleteState()
  101.      */
  102.     public Complex[] getPrimaryState() {
  103.         return primaryState.clone();
  104.     }

  105.     /** Get the number of secondary states.
  106.      * @return number of secondary states.
  107.      */
  108.     public int getNumberOfSecondaryStates() {
  109.         return secondaryState == null ? 0 : secondaryState.length;
  110.     }

  111.     /** Get secondary state dimension.
  112.      * @param index index of the secondary set as returned
  113.      * by {@link ExpandableODE#addSecondaryEquations(SecondaryODE)}
  114.      * (beware index 0 corresponds to primary state, secondary states start at 1)
  115.      * @return secondary state dimension
  116.      * @see #getPrimaryStateDimension()
  117.      * @see #getCompleteStateDimension()
  118.      */
  119.     public int getSecondaryStateDimension(final int index) {
  120.         return index == 0 ? primaryState.length : secondaryState[index - 1].length;
  121.     }

  122.     /** Get secondary state at time.
  123.      * @param index index of the secondary set as returned
  124.      * by {@link ExpandableODE#addSecondaryEquations(SecondaryODE)}
  125.      * (beware index 0 corresponds to primary state, secondary states start at 1)
  126.      * @return secondary state at time
  127.      * @see #getPrimaryState()
  128.      * @see #getCompleteState()
  129.      */
  130.     public Complex[] getSecondaryState(final int index) {
  131.         return index == 0 ? primaryState.clone() : secondaryState[index - 1].clone();
  132.     }

  133.     /** Return the dimension of the complete set of equations.
  134.      * <p>
  135.      * The complete set of equations correspond to the primary set plus all secondary sets.
  136.      * </p>
  137.      * @return dimension of the complete set of equations
  138.      * @see #getPrimaryStateDimension()
  139.      * @see #getSecondaryStateDimension(int)
  140.      */
  141.     public int getCompleteStateDimension() {
  142.         return completeDimension;
  143.     }

  144.     /** Get complete state at time.
  145.      * @return complete state at time, starting with
  146.      * {@link #getPrimaryState() primary state}, followed
  147.      * by all {@link #getSecondaryState(int) secondary states} in
  148.      * increasing index order
  149.      * @see #getPrimaryState()
  150.      * @see #getSecondaryState(int)
  151.      */
  152.     public Complex[] getCompleteState() {
  153.         final Complex[] completeState = new Complex[getCompleteStateDimension()];
  154.         System.arraycopy(primaryState, 0, completeState, 0, primaryState.length);
  155.         int offset = primaryState.length;
  156.         if (secondaryState != null) {
  157.             for (int index = 0; index < secondaryState.length; ++index) {
  158.                 System.arraycopy(secondaryState[index], 0,
  159.                                  completeState, offset,
  160.                                  secondaryState[index].length);
  161.                 offset += secondaryState[index].length;
  162.             }
  163.         }
  164.         return completeState;
  165.     }

  166. }