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 java.io.Serializable;
21  
22  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
23  
24  /**
25   * Class mapping the part of a complete state or derivative that pertains
26   * to a specific differential equation.
27   * <p>
28   * Instances of this class are guaranteed to be immutable.
29   * </p>
30   * @see SecondaryEquations
31   * @since 3.0
32   */
33  public class EquationsMapper implements Serializable {
34  
35      /** Serializable UID. */
36      private static final long serialVersionUID = 20110925L;
37  
38      /** Index of the first equation element in complete state arrays. */
39      private final int firstIndex;
40  
41      /** Dimension of the secondary state parameters. */
42      private final int dimension;
43  
44      /** simple constructor.
45       * @param firstIndex index of the first equation element in complete state arrays
46       * @param dimension dimension of the secondary state parameters
47       */
48      public EquationsMapper(final int firstIndex, final int dimension) {
49          this.firstIndex = firstIndex;
50          this.dimension  = dimension;
51      }
52  
53      /** Get the index of the first equation element in complete state arrays.
54       * @return index of the first equation element in complete state arrays
55       */
56      public int getFirstIndex() {
57          return firstIndex;
58      }
59  
60      /** Get the dimension of the secondary state parameters.
61       * @return dimension of the secondary state parameters
62       */
63      public int getDimension() {
64          return dimension;
65      }
66  
67      /** Extract equation data from a complete state or derivative array.
68       * @param complete complete state or derivative array from which
69       * equation data should be retrieved
70       * @param equationData placeholder where to put equation data
71       * @throws DimensionMismatchException if the dimension of the equation data does not
72       * match the mapper dimension
73       */
74      public void extractEquationData(double[] complete, double[] equationData)
75          throws DimensionMismatchException {
76          if (equationData.length != dimension) {
77              throw new DimensionMismatchException(equationData.length, dimension);
78          }
79          System.arraycopy(complete, firstIndex, equationData, 0, dimension);
80      }
81  
82      /** Insert equation data into a complete state or derivative array.
83       * @param equationData equation data to be inserted into the complete array
84       * @param complete placeholder where to put equation data (only the
85       * part corresponding to the equation will be overwritten)
86       * @throws DimensionMismatchException if the dimension of the equation data does not
87       * match the mapper dimension
88       */
89      public void insertEquationData(double[] equationData, double[] complete)
90          throws DimensionMismatchException {
91          if (equationData.length != dimension) {
92              throw new DimensionMismatchException(equationData.length, dimension);
93          }
94          System.arraycopy(equationData, 0, complete, firstIndex, dimension);
95      }
96  }