EquationsMapper.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 java.io.Serializable;

  19. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;

  20. /**
  21.  * Class mapping the part of a complete state or derivative that pertains
  22.  * to a specific differential equation.
  23.  * <p>
  24.  * Instances of this class are guaranteed to be immutable.
  25.  * </p>
  26.  * @see SecondaryEquations
  27.  * @since 3.0
  28.  */
  29. public class EquationsMapper implements Serializable {

  30.     /** Serializable UID. */
  31.     private static final long serialVersionUID = 20110925L;

  32.     /** Index of the first equation element in complete state arrays. */
  33.     private final int firstIndex;

  34.     /** Dimension of the secondary state parameters. */
  35.     private final int dimension;

  36.     /** simple constructor.
  37.      * @param firstIndex index of the first equation element in complete state arrays
  38.      * @param dimension dimension of the secondary state parameters
  39.      */
  40.     public EquationsMapper(final int firstIndex, final int dimension) {
  41.         this.firstIndex = firstIndex;
  42.         this.dimension  = dimension;
  43.     }

  44.     /** Get the index of the first equation element in complete state arrays.
  45.      * @return index of the first equation element in complete state arrays
  46.      */
  47.     public int getFirstIndex() {
  48.         return firstIndex;
  49.     }

  50.     /** Get the dimension of the secondary state parameters.
  51.      * @return dimension of the secondary state parameters
  52.      */
  53.     public int getDimension() {
  54.         return dimension;
  55.     }

  56.     /** Extract equation data from a complete state or derivative array.
  57.      * @param complete complete state or derivative array from which
  58.      * equation data should be retrieved
  59.      * @param equationData placeholder where to put equation data
  60.      * @throws DimensionMismatchException if the dimension of the equation data does not
  61.      * match the mapper dimension
  62.      */
  63.     public void extractEquationData(double[] complete, double[] equationData)
  64.         throws DimensionMismatchException {
  65.         if (equationData.length != dimension) {
  66.             throw new DimensionMismatchException(equationData.length, dimension);
  67.         }
  68.         System.arraycopy(complete, firstIndex, equationData, 0, dimension);
  69.     }

  70.     /** Insert equation data into a complete state or derivative array.
  71.      * @param equationData equation data to be inserted into the complete array
  72.      * @param complete placeholder where to put equation data (only the
  73.      * part corresponding to the equation will be overwritten)
  74.      * @throws DimensionMismatchException if the dimension of the equation data does not
  75.      * match the mapper dimension
  76.      */
  77.     public void insertEquationData(double[] equationData, double[] complete)
  78.         throws DimensionMismatchException {
  79.         if (equationData.length != dimension) {
  80.             throw new DimensionMismatchException(equationData.length, dimension);
  81.         }
  82.         System.arraycopy(equationData, 0, complete, firstIndex, dimension);
  83.     }
  84. }